style: Use rust types for gradient stops.

This doesn't clean up all that much, yet, but it's a step in the right
direction.

Differential Revision: https://phabricator.services.mozilla.com/D29168
This commit is contained in:
Emilio Cobos Álvarez 2019-04-30 20:37:54 +00:00
parent e40500622e
commit 8123007717
4 changed files with 52 additions and 63 deletions

View file

@ -55,7 +55,7 @@ pub enum LineDirection {
pub type EndingShape = generic::EndingShape<Length, LengthPercentage>;
/// A computed gradient item.
pub type GradientItem = generic::GradientItem<Color, LengthPercentage>;
pub type GradientItem = generic::GenericGradientItem<Color, LengthPercentage>;
/// A computed color stop.
pub type ColorStop = generic::ColorStop<Color, LengthPercentage>;

View file

@ -135,13 +135,23 @@ pub enum ShapeExtent {
#[derive(
Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss, ToResolvedValue, ToShmem,
)]
pub enum GradientItem<Color, LengthPercentage> {
/// A color stop.
ColorStop(ColorStop<Color, LengthPercentage>),
#[repr(C, u8)]
pub enum GenericGradientItem<Color, LengthPercentage> {
/// A simple color stop, without position.
SimpleColorStop(Color),
/// A complex color stop, with a position.
ComplexColorStop {
/// The color for the stop.
color: Color,
/// The position for the stop.
position: LengthPercentage,
},
/// An interpolation hint.
InterpolationHint(LengthPercentage),
}
pub use self::GenericGradientItem as GradientItem;
/// A color stop.
/// <https://drafts.csswg.org/css-images/#typedef-color-stop-list>
#[derive(
@ -154,6 +164,20 @@ pub struct ColorStop<Color, LengthPercentage> {
pub position: Option<LengthPercentage>,
}
impl<Color, LengthPercentage> ColorStop<Color, LengthPercentage> {
/// Convert the color stop into an appropriate `GradientItem`.
#[inline]
pub fn into_item(self) -> GradientItem<Color, LengthPercentage> {
match self.position {
Some(position) => GradientItem::ComplexColorStop {
color: self.color,
position,
},
None => GradientItem::SimpleColorStop(self.color),
}
}
}
/// Specified values for a paint worklet.
/// <https://drafts.css-houdini.org/css-paint-api/>
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]

View file

@ -484,24 +484,24 @@ impl Gradient {
if reverse_stops {
p.reverse();
}
Ok(generic::GradientItem::ColorStop(generic::ColorStop {
color: color,
position: Some(p.into()),
}))
Ok(generic::GradientItem::ComplexColorStop {
color,
position: p.into(),
})
})
})
.unwrap_or(vec![]);
if items.is_empty() {
items = vec![
generic::GradientItem::ColorStop(generic::ColorStop {
generic::GradientItem::ComplexColorStop {
color: Color::transparent().into(),
position: Some(Percentage::zero().into()),
}),
generic::GradientItem::ColorStop(generic::ColorStop {
position: Percentage::zero().into(),
},
generic::GradientItem::ComplexColorStop {
color: Color::transparent().into(),
position: Some(Percentage::hundred().into()),
}),
position: Percentage::hundred().into(),
},
];
} else if items.len() == 1 {
let first = items[0].clone();
@ -510,12 +510,12 @@ impl Gradient {
items.sort_by(|a, b| {
match (a, b) {
(
&generic::GradientItem::ColorStop(ref a),
&generic::GradientItem::ColorStop(ref b),
) => match (&a.position, &b.position) {
&generic::GradientItem::ComplexColorStop { position: ref a_position, .. },
&generic::GradientItem::ComplexColorStop { position: ref b_position, .. },
) => match (a_position, b_position) {
(
&Some(LengthPercentage::Percentage(a)),
&Some(LengthPercentage::Percentage(b)),
&LengthPercentage::Percentage(a),
&LengthPercentage::Percentage(b),
) => {
return a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal);
},
@ -960,13 +960,13 @@ impl GradientItem {
if let Ok(multi_position) = input.try(|i| LengthPercentage::parse(context, i)) {
let stop_color = stop.color.clone();
items.push(generic::GradientItem::ColorStop(stop));
items.push(generic::GradientItem::ColorStop(ColorStop {
items.push(stop.into_item());
items.push(ColorStop {
color: stop_color,
position: Some(multi_position),
}));
}.into_item());
} else {
items.push(generic::GradientItem::ColorStop(stop));
items.push(stop.into_item());
}
seen_stop = true;