diff --git a/components/style/cbindgen.toml b/components/style/cbindgen.toml index 53108f0d273..1a736eb1be9 100644 --- a/components/style/cbindgen.toml +++ b/components/style/cbindgen.toml @@ -83,6 +83,7 @@ include = [ "MaxSize", "FlexBasis", "Position", + "BackgroundSize", ] item_types = ["enums", "structs", "typedefs"] @@ -157,6 +158,10 @@ item_types = ["enums", "structs", "typedefs"] static inline StyleGenericPosition FromPercentage(float); """ +"GenericBackgroundSize" = """ + bool IsInitialValue() const; +""" + "Rect" = """ // Defined in nsStyleCoord.h template inline bool All(Predicate) const; diff --git a/components/style/gecko/conversions.rs b/components/style/gecko/conversions.rs index 91dc77c3ceb..2a2ae268395 100644 --- a/components/style/gecko/conversions.rs +++ b/components/style/gecko/conversions.rs @@ -23,13 +23,10 @@ use crate::values::computed::url::ComputedImageUrl; use crate::values::computed::{Angle, Gradient, Image}; use crate::values::computed::{Integer, LengthPercentage}; use crate::values::computed::{Length, Percentage, TextAlign}; -use crate::values::computed::{LengthPercentageOrAuto, NonNegativeLengthPercentageOrAuto}; use crate::values::generics::box_::VerticalAlign; use crate::values::generics::grid::{TrackListValue, TrackSize}; use crate::values::generics::image::{CompatMode, GradientItem, Image as GenericImage}; -use crate::values::generics::length::LengthPercentageOrAuto as GenericLengthPercentageOrAuto; use crate::values::generics::rect::Rect; -use crate::values::generics::NonNegative; use app_units::Au; use std::f32::consts::PI; use style_traits::values::specified::AllowedNumericType; @@ -62,42 +59,6 @@ impl From for LengthPercentage { ) } } - -impl NonNegativeLengthPercentageOrAuto { - /// Convert this value in an appropriate `nsStyleCoord::CalcValue`. - pub fn to_calc_value(&self) -> Option { - match *self { - GenericLengthPercentageOrAuto::LengthPercentage(ref len) => Some(From::from(len.0)), - GenericLengthPercentageOrAuto::Auto => None, - } - } -} - -impl From for LengthPercentageOrAuto { - fn from(other: nsStyleCoord_CalcValue) -> LengthPercentageOrAuto { - GenericLengthPercentageOrAuto::LengthPercentage(LengthPercentage::from(other)) - } -} - -// FIXME(emilio): A lot of these impl From should probably become explicit or -// disappear as we move more stuff to cbindgen. -impl From for NonNegativeLengthPercentageOrAuto { - fn from(other: nsStyleCoord_CalcValue) -> Self { - GenericLengthPercentageOrAuto::LengthPercentage(NonNegative( - LengthPercentage::with_clamping_mode( - Au(other.mLength).into(), - if other.mHasPercent { - Some(Percentage(other.mPercent)) - } else { - None - }, - AllowedNumericType::NonNegative, - /* was_calc = */ true, - ), - )) - } -} - impl From for CoordDataValue { fn from(reference: Angle) -> Self { CoordDataValue::Degree(reference.degrees()) diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 11c0c8906f7..cb64a3d1ed0 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -3797,80 +3797,12 @@ fn static_assert() { % endfor <%self:simple_image_array_property name="size" shorthand="${shorthand}" field_name="mSize"> - use crate::gecko_bindings::structs::nsStyleImageLayers_Size_Dimension; - use crate::gecko_bindings::structs::nsStyleImageLayers_Size_DimensionType; - use crate::gecko_bindings::structs::{nsStyleCoord_CalcValue, nsStyleImageLayers_Size}; - use crate::values::generics::background::BackgroundSize; - - let mut width = nsStyleCoord_CalcValue::new(); - let mut height = nsStyleCoord_CalcValue::new(); - - let (w_type, h_type) = match servo { - BackgroundSize::Explicit { width: explicit_width, height: explicit_height } => { - let mut w_type = nsStyleImageLayers_Size_DimensionType::eAuto; - let mut h_type = nsStyleImageLayers_Size_DimensionType::eAuto; - if let Some(w) = explicit_width.to_calc_value() { - width = w; - w_type = nsStyleImageLayers_Size_DimensionType::eLengthPercentage; - } - if let Some(h) = explicit_height.to_calc_value() { - height = h; - h_type = nsStyleImageLayers_Size_DimensionType::eLengthPercentage; - } - (w_type, h_type) - } - BackgroundSize::Cover => { - ( - nsStyleImageLayers_Size_DimensionType::eCover, - nsStyleImageLayers_Size_DimensionType::eCover, - ) - }, - BackgroundSize::Contain => { - ( - nsStyleImageLayers_Size_DimensionType::eContain, - nsStyleImageLayers_Size_DimensionType::eContain, - ) - }, - }; - - nsStyleImageLayers_Size { - mWidth: nsStyleImageLayers_Size_Dimension { _base: width }, - mHeight: nsStyleImageLayers_Size_Dimension { _base: height }, - mWidthType: w_type as u8, - mHeightType: h_type as u8, - } + servo pub fn clone_${shorthand}_size(&self) -> longhands::${shorthand}_size::computed_value::T { - use crate::gecko_bindings::structs::nsStyleCoord_CalcValue as CalcValue; - use crate::gecko_bindings::structs::nsStyleImageLayers_Size_DimensionType as DimensionType; - use crate::values::computed::NonNegativeLengthPercentageOrAuto; - use crate::values::generics::background::BackgroundSize; - - fn to_servo(value: CalcValue, ty: u8) -> NonNegativeLengthPercentageOrAuto { - if ty == DimensionType::eAuto as u8 { - NonNegativeLengthPercentageOrAuto::auto() - } else { - debug_assert_eq!(ty, DimensionType::eLengthPercentage as u8); - value.into() - } - } - longhands::${shorthand}_size::computed_value::List( - self.gecko.${image_layers_field}.mLayers.iter().map(|ref layer| { - if DimensionType::eCover as u8 == layer.mSize.mWidthType { - debug_assert_eq!(layer.mSize.mHeightType, DimensionType::eCover as u8); - return BackgroundSize::Cover - } - if DimensionType::eContain as u8 == layer.mSize.mWidthType { - debug_assert_eq!(layer.mSize.mHeightType, DimensionType::eContain as u8); - return BackgroundSize::Contain - } - BackgroundSize::Explicit { - width: to_servo(layer.mSize.mWidth._base, layer.mSize.mWidthType), - height: to_servo(layer.mSize.mHeight._base, layer.mSize.mHeightType), - } - }).collect() + self.gecko.${image_layers_field}.mLayers.iter().map(|layer| layer.mSize).collect() ) } diff --git a/components/style/values/computed/background.rs b/components/style/values/computed/background.rs index dc23915a74b..e2a58f8b74e 100644 --- a/components/style/values/computed/background.rs +++ b/components/style/values/computed/background.rs @@ -6,19 +6,8 @@ use crate::values::computed::length::NonNegativeLengthPercentage; use crate::values::generics::background::BackgroundSize as GenericBackgroundSize; -use crate::values::generics::length::LengthPercentageOrAuto; pub use crate::values::specified::background::BackgroundRepeat; /// A computed value for the `background-size` property. pub type BackgroundSize = GenericBackgroundSize; - -impl BackgroundSize { - /// Returns `auto auto`. - pub fn auto() -> Self { - GenericBackgroundSize::Explicit { - width: LengthPercentageOrAuto::auto(), - height: LengthPercentageOrAuto::auto(), - } - } -} diff --git a/components/style/values/generics/background.rs b/components/style/values/generics/background.rs index d739329e4fc..c133f0c8b85 100644 --- a/components/style/values/generics/background.rs +++ b/components/style/values/generics/background.rs @@ -4,7 +4,7 @@ //! Generic types for CSS values related to backgrounds. -use crate::values::generics::length::LengthPercentageOrAuto; +use crate::values::generics::length::{LengthPercentageOrAuto, GenericLengthPercentageOrAuto}; use std::fmt::{self, Write}; use style_traits::{CssWriter, ToCss}; @@ -22,13 +22,14 @@ use style_traits::{CssWriter, ToCss}; ToAnimatedZero, ToComputedValue, )] -pub enum BackgroundSize { +#[repr(C, u8)] +pub enum GenericBackgroundSize { /// ` ` - Explicit { + ExplicitSize { /// Explicit width. - width: LengthPercentageOrAuto, + width: GenericLengthPercentageOrAuto, /// Explicit height. - height: LengthPercentageOrAuto, + height: GenericLengthPercentageOrAuto, }, /// `cover` #[animation(error)] @@ -38,6 +39,8 @@ pub enum BackgroundSize { Contain, } +pub use self::GenericBackgroundSize as BackgroundSize; + impl ToCss for BackgroundSize where LengthPercentage: ToCss, @@ -47,7 +50,7 @@ where W: Write, { match self { - BackgroundSize::Explicit { width, height } => { + BackgroundSize::ExplicitSize { width, height } => { width.to_css(dest)?; // NOTE(emilio): We should probably simplify all these in case // `width == `height`, but all other browsers agree on only @@ -63,3 +66,13 @@ where } } } + +impl BackgroundSize { + /// Returns `auto auto`. + pub fn auto() -> Self { + GenericBackgroundSize::ExplicitSize { + width: LengthPercentageOrAuto::Auto, + height: LengthPercentageOrAuto::Auto, + } + } +} diff --git a/components/style/values/specified/background.rs b/components/style/values/specified/background.rs index b53669abfa8..a1605e977eb 100644 --- a/components/style/values/specified/background.rs +++ b/components/style/values/specified/background.rs @@ -26,7 +26,7 @@ impl Parse for BackgroundSize { let height = input .try(|i| NonNegativeLengthPercentageOrAuto::parse(context, i)) .unwrap_or(NonNegativeLengthPercentageOrAuto::auto()); - return Ok(GenericBackgroundSize::Explicit { width, height }); + return Ok(GenericBackgroundSize::ExplicitSize { width, height }); } Ok(try_match_ident_ignore_ascii_case! { input, "cover" => GenericBackgroundSize::Cover, @@ -35,16 +35,6 @@ impl Parse for BackgroundSize { } } -impl BackgroundSize { - /// Returns `auto auto`. - pub fn auto() -> Self { - GenericBackgroundSize::Explicit { - width: NonNegativeLengthPercentageOrAuto::auto(), - height: NonNegativeLengthPercentageOrAuto::auto(), - } - } -} - /// One of the keywords for `background-repeat`. #[derive( Clone,