diff options
author | Emilio Cobos Álvarez <me@emiliocobos.me> | 2016-06-30 19:23:07 -0700 |
---|---|---|
committer | Emilio Cobos Álvarez <me@emiliocobos.me> | 2016-07-01 14:22:53 -0700 |
commit | 30963dd74da5e9a0e2fd7c8d56ec23aa474e8dd3 (patch) | |
tree | 7751ed70f30f0131238bc0755e007d3a01545771 | |
parent | ba53c4ea8db7f9d2028f73db3873b7565f55567a (diff) | |
download | servo-30963dd74da5e9a0e2fd7c8d56ec23aa474e8dd3.tar.gz servo-30963dd74da5e9a0e2fd7c8d56ec23aa474e8dd3.zip |
style: remove is_servo(), as_servo() and as_servo_mut()
This commit adds a need_index prop to the style system, and autogenerates
iterators, and a get_xxx_mod(i) function from a get_xxx_prop() and
get_xxx_at(index) functions.
This allows us to (finally!) take rid of the as_servo() hack. There are a few
unimplemented clones, but I'm just too lazy for that right now.
-rw-r--r-- | components/style/animation.rs | 5 | ||||
-rw-r--r-- | components/style/properties/data.py | 7 | ||||
-rw-r--r-- | components/style/properties/helpers/animated_properties.mako.rs | 9 | ||||
-rw-r--r-- | components/style/properties/properties.mako.rs | 15 | ||||
-rw-r--r-- | components/style/values.rs | 5 | ||||
-rw-r--r-- | ports/geckolib/properties.mako.rs | 178 | ||||
-rw-r--r-- | ports/geckolib/values.rs | 158 |
7 files changed, 296 insertions, 81 deletions
diff --git a/components/style/animation.rs b/components/style/animation.rs index 594623de06c..9b85a689c58 100644 --- a/components/style/animation.rs +++ b/components/style/animation.rs @@ -405,11 +405,6 @@ pub fn maybe_start_animations<Impl: SelectorImplExt>(context: &SharedStyleContex { let mut had_animations = false; - // FIXME(emilio): Implement animations for geckolib. - if !new_style.is_servo() { - return false; - } - let box_style = new_style.get_box(); for (i, name) in box_style.animation_name_iter().enumerate() { debug!("maybe_start_animations: name={}", name); diff --git a/components/style/properties/data.py b/components/style/properties/data.py index 3baa5780823..f7fd3eec1b7 100644 --- a/components/style/properties/data.py +++ b/components/style/properties/data.py @@ -57,7 +57,6 @@ class Longhand(object): self.experimental = ("layout.%s.enabled" % name) if experimental else None self.custom_cascade = custom_cascade self.internal = internal - self.need_clone = need_clone self.need_index = need_index self.gecko_ffi_name = gecko_ffi_name or "m" + self.camel_case self.derived_from = (derived_from or "").split() @@ -72,6 +71,12 @@ class Longhand(object): assert animatable == "True" or animatable == "False" self.animatable = animatable == "True" + # NB: Animatable implies clone because a property animation requires a + # copy of the computed value. + # + # See components/style/helpers/animated_properties.mako.rs. + self.need_clone = need_clone or self.animatable + class Shorthand(object): def __init__(self, name, sub_properties, experimental=False, internal=False): diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 552f5b303c3..52b2e181f6a 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -127,22 +127,17 @@ impl AnimatedProperty { } } - // NB: Transition properties need clone pub fn from_transition_property<C: ComputedValues>(transition_property: &TransitionProperty, old_style: &C, new_style: &C) -> AnimatedProperty { - // TODO: Generalise this for GeckoLib, adding clone_xxx to the - // appropiate longhands. - let old_style = old_style.as_servo(); - let new_style = new_style.as_servo(); match *transition_property { TransitionProperty::All => panic!("Can't use TransitionProperty::All here."), % for prop in data.longhands: % if prop.animatable: TransitionProperty::${prop.camel_case} => { AnimatedProperty::${prop.camel_case}( - old_style.get_${prop.style_struct.ident.strip("_")}().${prop.ident}.clone(), - new_style.get_${prop.style_struct.ident.strip("_")}().${prop.ident}.clone()) + old_style.get_${prop.style_struct.ident.strip("_")}().clone_${prop.ident}(), + new_style.get_${prop.style_struct.ident.strip("_")}().clone_${prop.ident}()) } % endif % endfor diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 3269b89a46b..1ba7dd932ea 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -1246,14 +1246,6 @@ pub trait ComputedValues : Debug + Clone + Send + Sync + 'static { type Concrete${style_struct.trait_name}: style_struct_traits::${style_struct.trait_name}; % endfor - // Temporary bailout case for stuff we haven't made work with the trait - // yet - panics for non-Servo implementations. - // - // Used only for animations. Don't use it in other places. - fn is_servo(&self) -> bool; - fn as_servo<'a>(&'a self) -> &'a ServoComputedValues; - fn as_servo_mut<'a>(&'a mut self) -> &'a mut ServoComputedValues; - fn new(custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>, shareable: bool, writing_mode: WritingMode, @@ -1302,13 +1294,6 @@ impl ComputedValues for ServoComputedValues { type Concrete${style_struct.trait_name} = style_structs::${style_struct.servo_struct_name}; % endfor - #[inline] - fn is_servo(&self) -> bool { true } - #[inline] - fn as_servo<'a>(&'a self) -> &'a ServoComputedValues { self } - #[inline] - fn as_servo_mut<'a>(&'a mut self) -> &'a mut ServoComputedValues { self } - fn new(custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>, shareable: bool, writing_mode: WritingMode, diff --git a/components/style/values.rs b/components/style/values.rs index 5dea6f3cb7a..007426be88f 100644 --- a/components/style/values.rs +++ b/components/style/values.rs @@ -1194,6 +1194,11 @@ pub mod specified { pub fn radians(self) -> f32 { self.0 } + + #[inline] + pub fn from_radians(r: f32) -> Self { + Angle(r) + } } const RAD_PER_DEG: CSSFloat = PI / 180.0; diff --git a/ports/geckolib/properties.mako.rs b/ports/geckolib/properties.mako.rs index 40c10ea160e..9c458602c7d 100644 --- a/ports/geckolib/properties.mako.rs +++ b/ports/geckolib/properties.mako.rs @@ -34,7 +34,7 @@ use style::logical_geometry::WritingMode; use style::properties::{CascadePropertyFn, ServoComputedValues, ComputedValues}; use style::properties::longhands; use style::properties::style_struct_traits::*; -use values::{StyleCoordHelpers, ToGeckoStyleCoord, convert_nscolor_to_rgba}; +use values::{StyleCoordHelpers, GeckoStyleCoordConvertible, convert_nscolor_to_rgba}; use values::{convert_rgba_to_nscolor, debug_assert_unit_is_safe_to_copy}; use values::round_border_to_device_pixels; @@ -73,11 +73,6 @@ impl ComputedValues for GeckoComputedValues { type Concrete${style_struct.trait_name} = ${style_struct.gecko_struct_name}; % endfor - // These will go away, and we will never implement them. - fn is_servo(&self) -> bool { false } - fn as_servo<'a>(&'a self) -> &'a ServoComputedValues { unimplemented!() } - fn as_servo_mut<'a>(&'a mut self) -> &'a mut ServoComputedValues { unimplemented!() } - fn new(custom_properties: Option<Arc<ComputedValuesMap>>, shareable: bool, writing_mode: WritingMode, @@ -148,7 +143,13 @@ pub struct ${style_struct.gecko_struct_name} { } </%def> -<%def name="impl_simple_copy(ident, gecko_ffi_name)"> +<%def name="impl_simple_clone(ident, gecko_ffi_name)"> + fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { + self.gecko.${gecko_ffi_name} + } +</%def> + +<%def name="impl_simple_copy(ident, gecko_ffi_name, *kwargs)"> fn copy_${ident}_from(&mut self, other: &Self) { self.gecko.${gecko_ffi_name} = other.gecko.${gecko_ffi_name}; } @@ -248,12 +249,24 @@ def set_gecko_property(ffi_name, expr): <%def name="impl_color_copy(ident, gecko_ffi_name, color_flags_ffi_name=None)"> fn copy_${ident}_from(&mut self, other: &Self) { % if color_flags_ffi_name: - ${clear_color_flags(color_flags_ffi_name)} - if ${get_current_color_flag_from("other.gecko." + color_flags_ffi_name)} { - ${set_current_color_flag(color_flags_ffi_name)} - } + ${clear_color_flags(color_flags_ffi_name)} + if ${get_current_color_flag_from("other.gecko." + color_flags_ffi_name)} { + ${set_current_color_flag(color_flags_ffi_name)} + } % endif - self.gecko.${gecko_ffi_name} = other.gecko.${gecko_ffi_name}; + self.gecko.${gecko_ffi_name} = other.gecko.${gecko_ffi_name} + } +</%def> + +<%def name="impl_color_clone(ident, gecko_ffi_name, color_flags_ffi_name=None)"> + fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { + use cssparser::Color; + % if color_flags_ffi_name: + if ${get_current_color_flag_from("self.gecko." + color_flags_ffi_name)} { + return Color::CurrentColor + } + % endif + Color::RGBA(convert_nscolor_to_rgba(${get_gecko_property(gecko_ffi_name)})) } </%def> @@ -265,14 +278,20 @@ def set_gecko_property(ffi_name, expr): % endif </%def> -<%def name="impl_simple(ident, gecko_ffi_name)"> +<%def name="impl_simple(ident, gecko_ffi_name, need_clone=False)"> <%call expr="impl_simple_setter(ident, gecko_ffi_name)"></%call> <%call expr="impl_simple_copy(ident, gecko_ffi_name)"></%call> +% if need_clone: + <%call expr="impl_simple_clone(ident, gecko_ffi_name)"></%call> +% endif </%def> -<%def name="impl_color(ident, gecko_ffi_name, color_flags_ffi_name=None)"> +<%def name="impl_color(ident, gecko_ffi_name, color_flags_ffi_name=None, need_clone=False)"> <%call expr="impl_color_setter(ident, gecko_ffi_name, color_flags_ffi_name)"></%call> <%call expr="impl_color_copy(ident, gecko_ffi_name, color_flags_ffi_name)"></%call> +% if need_clone: + <%call expr="impl_color_clone(ident, gecko_ffi_name, color_flags_ffi_name)"></%call> +% endif </%def> <%def name="impl_app_units(ident, gecko_ffi_name, need_clone, round_to_pixels=False)"> @@ -292,7 +311,7 @@ def set_gecko_property(ffi_name, expr): % endif </%def> -<%def name="impl_split_style_coord(ident, unit_ffi_name, union_ffi_name)"> +<%def name="impl_split_style_coord(ident, unit_ffi_name, union_ffi_name, need_clone=False)"> fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) { v.to_gecko_style_coord(&mut self.gecko.${unit_ffi_name}, &mut self.gecko.${union_ffi_name}); @@ -302,13 +321,25 @@ def set_gecko_property(ffi_name, expr): self.gecko.${unit_ffi_name} = other.gecko.${unit_ffi_name}; self.gecko.${union_ffi_name} = other.gecko.${union_ffi_name}; } + % if need_clone: + fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { + use style::properties::longhands::${ident}::computed_value::T; + T::from_gecko_style_coord(&self.gecko.${unit_ffi_name}, + &self.gecko.${union_ffi_name}) + .expect("clone for ${ident} failed") + } + % endif </%def> -<%def name="impl_style_coord(ident, gecko_ffi_name)"> -<%call expr="impl_split_style_coord(ident, '%s.mUnit' % gecko_ffi_name, '%s.mValue' % gecko_ffi_name)"></%call> +<%def name="impl_style_coord(ident, gecko_ffi_name, need_clone=False)"> +${impl_split_style_coord(ident, + "%s.mUnit" % gecko_ffi_name, + "%s.mValue" % gecko_ffi_name, + need_clone=need_clone)} </%def> -<%def name="impl_corner_style_coord(ident, x_unit_ffi_name, x_union_ffi_name, y_unit_ffi_name, y_union_ffi_name)"> +<%def name="impl_corner_style_coord(ident, x_unit_ffi_name, x_union_ffi_name, \ + y_unit_ffi_name, y_union_ffi_name, need_clone=False)"> fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) { v.0.width.to_gecko_style_coord(&mut self.gecko.${x_unit_ffi_name}, &mut self.gecko.${x_union_ffi_name}); @@ -323,6 +354,19 @@ def set_gecko_property(ffi_name, expr): self.gecko.${y_unit_ffi_name} = other.gecko.${y_unit_ffi_name}; self.gecko.${y_union_ffi_name} = other.gecko.${y_union_ffi_name}; } + % if need_clone: + fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { + use style::properties::longhands::${ident}::computed_value::T; + use euclid::Size2D; + let width = GeckoStyleCoordConvertible::from_gecko_style_coord(&self.gecko.${x_unit_ffi_name}, + &self.gecko.${x_union_ffi_name}) + .expect("Failed to clone ${ident}"); + let height = GeckoStyleCoordConvertible::from_gecko_style_coord(&self.gecko.${y_unit_ffi_name}, + &self.gecko.${y_union_ffi_name}) + .expect("Failed to clone ${ident}"); + T(Size2D::new(width, height)) + } + % endif </%def> <%def name="impl_style_struct(style_struct)"> @@ -425,7 +469,7 @@ impl ${style_struct.trait_name} for ${style_struct.gecko_struct_name} { impl_keyword(longhand.ident, longhand.gecko_ffi_name, longhand.keyword, longhand.need_clone) for longhand in predefined_longhands: impl_fn = predefined_types[longhand.predefined_type] - impl_fn(longhand.ident, longhand.gecko_ffi_name) + impl_fn(longhand.ident, longhand.gecko_ffi_name, need_clone=longhand.need_clone) %> /* @@ -522,9 +566,9 @@ fn static_assert() { need_clone=True) %> <% impl_color("border_%s_color" % side.ident, "mBorderColor[%s]" % side.index, - color_flags_ffi_name="mBorderStyle[%s]" % side.index) %> + color_flags_ffi_name="mBorderStyle[%s]" % side.index, need_clone=True) %> - <% impl_app_units("border_%s_width" % side.ident, "mComputedBorder.%s" % side.ident, need_clone=False, + <% impl_app_units("border_%s_width" % side.ident, "mComputedBorder.%s" % side.ident, need_clone=True, round_to_pixels=True) %> fn border_${side.ident}_has_nonzero_width(&self) -> bool { @@ -537,7 +581,8 @@ fn static_assert() { "mBorderRadius.mUnits[%s]" % corner.x_index, "mBorderRadius.mValues[%s]" % corner.x_index, "mBorderRadius.mUnits[%s]" % corner.y_index, - "mBorderRadius.mValues[%s]" % corner.y_index) %> + "mBorderRadius.mValues[%s]" % corner.y_index, + need_clone=True) %> % endfor </%self:impl_trait> @@ -548,7 +593,8 @@ fn static_assert() { % for side in SIDES: <% impl_split_style_coord("margin_%s" % side.ident, "mMargin.mUnits[%s]" % side.index, - "mMargin.mValues[%s]" % side.index) %> + "mMargin.mValues[%s]" % side.index, + need_clone=True) %> % endfor </%self:impl_trait> @@ -559,7 +605,8 @@ fn static_assert() { % for side in SIDES: <% impl_split_style_coord("padding_%s" % side.ident, "mPadding.mUnits[%s]" % side.index, - "mPadding.mValues[%s]" % side.index) %> + "mPadding.mValues[%s]" % side.index, + need_clone=True) %> % endfor </%self:impl_trait> @@ -570,7 +617,8 @@ fn static_assert() { % for side in SIDES: <% impl_split_style_coord("%s" % side.ident, "mOffset.mUnits[%s]" % side.index, - "mOffset.mValues[%s]" % side.index) %> + "mOffset.mValues[%s]" % side.index, + need_clone=True) %> % endfor fn set_z_index(&mut self, v: longhands::z_index::computed_value::T) { @@ -587,6 +635,17 @@ fn static_assert() { self.gecko.mZIndex.mValue = other.gecko.mZIndex.mValue; } + fn clone_z_index(&self) -> longhands::z_index::computed_value::T { + use style::properties::longhands::z_index::computed_value::T; + + if self.gecko.mZIndex.is_auto() { + return T::Auto; + } + + debug_assert!(self.gecko.mZIndex.is_int()); + T::Number(self.gecko.mZIndex.get_int()) + } + fn set_box_sizing(&mut self, v: longhands::box_sizing::computed_value::T) { use style::computed_values::box_sizing::T; use gecko_bindings::structs::StyleBoxSizing; @@ -609,9 +668,9 @@ fn static_assert() { <% impl_keyword("outline_style", "mOutlineStyle", border_style_keyword, need_clone=True) %> - <% impl_color("outline_color", "mOutlineColor", color_flags_ffi_name="mOutlineStyle") %> + <% impl_color("outline_color", "mOutlineColor", color_flags_ffi_name="mOutlineStyle", need_clone=True) %> - <% impl_app_units("outline_width", "mActualOutlineWidth", need_clone=False, + <% impl_app_units("outline_width", "mActualOutlineWidth", need_clone=True, round_to_pixels=True) %> % for corner in CORNERS: @@ -682,7 +741,7 @@ fn static_assert() { fn set_font_weight(&mut self, v: longhands::font_weight::computed_value::T) { self.gecko.mFont.weight = v as u16; } - <%call expr="impl_simple_copy('font_weight', 'mFont.weight')"></%call> + ${impl_simple_copy('font_weight', 'mFont.weight')} fn clone_font_weight(&self) -> longhands::font_weight::computed_value::T { debug_assert!(self.gecko.mFont.weight >= 100); @@ -704,7 +763,7 @@ fn static_assert() { "table-header-group table-footer-group table-row table-column-group " + "table-column table-cell table-caption list-item flex none " + "-moz-box -moz-inline-box") %> - <%call expr="impl_keyword('display', 'mDisplay', display_keyword, True)"></%call> + ${impl_keyword('display', 'mDisplay', display_keyword, True)} // overflow-y is implemented as a newtype of overflow-x, so we need special handling. // We could generalize this if we run into other newtype keywords. @@ -718,7 +777,7 @@ fn static_assert() { % endfor }; } - <%call expr="impl_simple_copy('overflow_y', 'mOverflowY')"></%call> + ${impl_simple_copy('overflow_y', 'mOverflowY')} fn clone_overflow_y(&self) -> longhands::overflow_y::computed_value::T { use style::properties::longhands::overflow_x::computed_value::T as BaseType; use style::properties::longhands::overflow_y::computed_value::T as NewType; @@ -744,6 +803,26 @@ fn static_assert() { } } + fn clone_vertical_align(&self) -> longhands::vertical_align::computed_value::T { + use style::properties::longhands::vertical_align::computed_value::T; + use style::values::computed::LengthOrPercentage; + + if self.gecko.mVerticalAlign.is_enum() { + match self.gecko.mVerticalAlign.get_enum() as u32 { + % for value in keyword.values_for('gecko'): + structs::${keyword.gecko_constant(value)} + => T::${to_rust_ident(value)}, + % endfor + _ => panic!("Unexpected enum variant for vertical-align"), + } + } else { + let v = LengthOrPercentage::from_gecko_style_coord(&self.gecko.mVerticalAlign.mUnit, + &self.gecko.mVerticalAlign.mValue) + .expect("Expected length or percentage for vertical-align"); + T::LengthOrPercentage(v) + } + } + <%call expr="impl_coord_copy('vertical_align', 'mVerticalAlign')"></%call> fn set__moz_binding(&mut self, v: longhands::_moz_binding::computed_value::T) { @@ -777,7 +856,7 @@ fn static_assert() { skip_longhands="${skip_background_longhands}" skip_additionals="*"> - <% impl_color("background_color", "mBackgroundColor") %> + <% impl_color("background_color", "mBackgroundColor", need_clone=True) %> fn copy_background_repeat_from(&mut self, other: &Self) { self.gecko.mImage.mRepeatCount = cmp::min(1, other.gecko.mImage.mRepeatCount); @@ -952,8 +1031,8 @@ fn static_assert() { <%self:impl_trait style_struct_name="List" skip_longhands="list-style-type" skip_additionals="*"> - <% impl_keyword_setter("list_style_type", "__LIST_STYLE_TYPE__", - data.longhands_by_name["list-style-type"].keyword) %> + ${impl_keyword_setter("list_style_type", "__LIST_STYLE_TYPE__", + data.longhands_by_name["list-style-type"].keyword)} fn copy_list_style_type_from(&mut self, other: &Self) { unsafe { Gecko_CopyListStyleTypeFrom(&mut self.gecko, &other.gecko); @@ -967,7 +1046,7 @@ fn static_assert() { <% text_align_keyword = Keyword("text-align", "start end left right center justify -moz-center -moz-left " + "-moz-right match-parent") %> - <%call expr="impl_keyword('text_align', 'mTextAlign', text_align_keyword, need_clone=False)"></%call> + ${impl_keyword('text_align', 'mTextAlign', text_align_keyword, need_clone=False)} fn set_line_height(&mut self, v: longhands::line_height::computed_value::T) { use style::properties::longhands::line_height::computed_value::T; @@ -981,6 +1060,22 @@ fn static_assert() { } } + fn clone_line_height(&self) -> longhands::line_height::computed_value::T { + use style::properties::longhands::line_height::computed_value::T; + if self.gecko.mLineHeight.is_normal() { + return T::Normal; + } + if self.gecko.mLineHeight.is_coord() { + return T::Length(self.gecko.mLineHeight.get_coord()); + } + if self.gecko.mLineHeight.is_factor() { + return T::Number(self.gecko.mLineHeight.get_factor()); + } + + debug_assert!(self.gecko.mLineHeight.get_enum() == structs::NS_STYLE_LINE_HEIGHT_BLOCK_HEIGHT as i32); + T::MozBlockHeight + } + <%call expr="impl_coord_copy('line_height', 'mLineHeight')"></%call> </%self:impl_trait> @@ -989,8 +1084,8 @@ fn static_assert() { skip_longhands="text-decoration-color text-decoration-line" skip_additionals="*"> - <% impl_color("text_decoration_color", "mTextDecorationColor", - color_flags_ffi_name="mTextDecorationStyle") %> + ${impl_color("text_decoration_color", "mTextDecorationColor", + color_flags_ffi_name="mTextDecorationStyle", need_clone=True)} fn set_text_decoration_line(&mut self, v: longhands::text_decoration_line::computed_value::T) { let mut bits: u8 = 0; @@ -1006,14 +1101,19 @@ fn static_assert() { self.gecko.mTextDecorationLine = bits; } - <%call expr="impl_simple_copy('text_decoration_line', 'mTextDecorationLine')"></%call> + ${impl_simple_copy('text_decoration_line', 'mTextDecorationLine')} + #[inline] fn has_underline(&self) -> bool { (self.gecko.mTextDecorationLine & (structs::NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE as u8)) != 0 } + + #[inline] fn has_overline(&self) -> bool { (self.gecko.mTextDecorationLine & (structs::NS_STYLE_TEXT_DECORATION_LINE_OVERLINE as u8)) != 0 } + + #[inline] fn has_line_through(&self) -> bool { (self.gecko.mTextDecorationLine & (structs::NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH as u8)) != 0 } @@ -1033,7 +1133,6 @@ fn static_assert() { <%self:impl_trait style_struct_name="Color" skip_longhands="*"> - fn set_color(&mut self, v: longhands::color::computed_value::T) { let result = convert_rgba_to_nscolor(&v); ${set_gecko_property("mColor", "result")} @@ -1049,7 +1148,6 @@ fn static_assert() { <%self:impl_trait style_struct_name="Pointing" skip_longhands="cursor"> - fn set_cursor(&mut self, v: longhands::cursor::computed_value::T) { use style::properties::longhands::cursor::computed_value::T; use style_traits::cursor::Cursor; @@ -1097,7 +1195,6 @@ fn static_assert() { } ${impl_simple_copy('cursor', 'mCursor')} - </%self:impl_trait> <%self:impl_trait style_struct_name="Column" @@ -1111,7 +1208,6 @@ fn static_assert() { } ${impl_coord_copy('column_width', 'mColumnWidth')} - </%self:impl_trait> <%def name="define_ffi_struct_accessor(style_struct)"> diff --git a/ports/geckolib/values.rs b/ports/geckolib/values.rs index 978453aa707..ee24be7565c 100644 --- a/ports/geckolib/values.rs +++ b/ports/geckolib/values.rs @@ -11,17 +11,37 @@ use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto, Leng pub trait StyleCoordHelpers { fn copy_from(&mut self, other: &Self); - fn set<T: ToGeckoStyleCoord>(&mut self, val: T); + fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T); + fn set_auto(&mut self); + fn is_auto(&self) -> bool; + fn set_normal(&mut self); + fn is_normal(&self) -> bool; + fn set_coord(&mut self, val: Au); + fn is_coord(&self) -> bool; + fn get_coord(&self) -> Au; + fn set_int(&mut self, val: i32); + fn is_int(&self) -> bool; + fn get_int(&self) -> i32; + fn set_enum(&mut self, val: i32); + fn is_enum(&self) -> bool; + fn get_enum(&self) -> i32; + fn set_percent(&mut self, val: f32); + fn is_percent(&self) -> bool; + fn get_percent(&self) -> f32; + fn set_factor(&mut self, val: f32); + fn is_factor(&self) -> bool; + fn get_factor(&self) -> f32; } impl StyleCoordHelpers for nsStyleCoord { + #[inline] fn copy_from(&mut self, other: &Self) { debug_assert_unit_is_safe_to_copy(self.mUnit); debug_assert_unit_is_safe_to_copy(other.mUnit); @@ -29,51 +49,113 @@ impl StyleCoordHelpers for nsStyleCoord { self.mValue = other.mValue; } - fn set<T: ToGeckoStyleCoord>(&mut self, val: T) { + #[inline] + fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T) { val.to_gecko_style_coord(&mut self.mUnit, &mut self.mValue); } + #[inline] fn set_auto(&mut self) { self.mUnit = nsStyleUnit::eStyleUnit_Auto; unsafe { *self.mValue.mInt.as_mut() = 0; } } + #[inline] + fn is_auto(&self) -> bool { + self.mUnit == nsStyleUnit::eStyleUnit_Auto + } + #[inline] fn set_normal(&mut self) { self.mUnit = nsStyleUnit::eStyleUnit_Normal; unsafe { *self.mValue.mInt.as_mut() = 0; } } + #[inline] + fn is_normal(&self) -> bool { + self.mUnit == nsStyleUnit::eStyleUnit_Normal + } + #[inline] fn set_coord(&mut self, val: Au) { self.mUnit = nsStyleUnit::eStyleUnit_Coord; unsafe { *self.mValue.mInt.as_mut() = val.0; } } - - fn set_percent(&mut self, val: f32) { - self.mUnit = nsStyleUnit::eStyleUnit_Percent; - unsafe { *self.mValue.mFloat.as_mut() = val; } + #[inline] + fn is_coord(&self) -> bool { + self.mUnit == nsStyleUnit::eStyleUnit_Coord + } + #[inline] + fn get_coord(&self) -> Au { + debug_assert!(self.is_coord()); + Au(unsafe { *self.mValue.mInt.as_ref() }) } + #[inline] fn set_int(&mut self, val: i32) { self.mUnit = nsStyleUnit::eStyleUnit_Integer; unsafe { *self.mValue.mInt.as_mut() = val; } } + #[inline] + fn is_int(&self) -> bool { + self.mUnit == nsStyleUnit::eStyleUnit_Integer + } + #[inline] + fn get_int(&self) -> i32 { + debug_assert!(self.is_int()); + unsafe { *self.mValue.mInt.as_ref() } + } + #[inline] fn set_enum(&mut self, val: i32) { self.mUnit = nsStyleUnit::eStyleUnit_Enumerated; unsafe { *self.mValue.mInt.as_mut() = val; } } + #[inline] + fn is_enum(&self) -> bool { + self.mUnit == nsStyleUnit::eStyleUnit_Enumerated + } + #[inline] + fn get_enum(&self) -> i32 { + debug_assert!(self.is_enum()); + unsafe { *self.mValue.mInt.as_ref() } + } + + #[inline] + fn set_percent(&mut self, val: f32) { + self.mUnit = nsStyleUnit::eStyleUnit_Percent; + unsafe { *self.mValue.mFloat.as_mut() = val; } + } + #[inline] + fn is_percent(&self) -> bool { + self.mUnit == nsStyleUnit::eStyleUnit_Percent + } + #[inline] + fn get_percent(&self) -> f32 { + debug_assert!(self.is_percent()); + unsafe { *self.mValue.mFloat.as_ref() } + } + #[inline] fn set_factor(&mut self, val: f32) { self.mUnit = nsStyleUnit::eStyleUnit_Factor; unsafe { *self.mValue.mFloat.as_mut() = val; } } + #[inline] + fn is_factor(&self) -> bool { + self.mUnit == nsStyleUnit::eStyleUnit_Factor + } + #[inline] + fn get_factor(&self) -> f32 { + debug_assert!(self.is_factor()); + unsafe { *self.mValue.mFloat.as_ref() } + } } -pub trait ToGeckoStyleCoord { +pub trait GeckoStyleCoordConvertible : Sized { fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion); + fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self>; } -impl ToGeckoStyleCoord for LengthOrPercentage { +impl GeckoStyleCoordConvertible for LengthOrPercentage { fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { match *self { LengthOrPercentage::Length(au) => { @@ -87,9 +169,21 @@ impl ToGeckoStyleCoord for LengthOrPercentage { LengthOrPercentage::Calc(_) => unimplemented!(), }; } + + fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { + match *unit { + nsStyleUnit::eStyleUnit_Coord + => Some(LengthOrPercentage::Length(Au(unsafe { *union.mInt.as_ref() }))), + nsStyleUnit::eStyleUnit_Percent + => Some(LengthOrPercentage::Percentage(unsafe { *union.mFloat.as_ref() })), + nsStyleUnit::eStyleUnit_Calc + => unimplemented!(), + _ => None, + } + } } -impl ToGeckoStyleCoord for LengthOrPercentageOrAuto { +impl GeckoStyleCoordConvertible for LengthOrPercentageOrAuto { fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { match *self { LengthOrPercentageOrAuto::Length(au) => { @@ -107,9 +201,23 @@ impl ToGeckoStyleCoord for LengthOrPercentageOrAuto { LengthOrPercentageOrAuto::Calc(_) => unimplemented!(), }; } + + fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { + match *unit { + nsStyleUnit::eStyleUnit_Auto + => Some(LengthOrPercentageOrAuto::Auto), + nsStyleUnit::eStyleUnit_Coord + => Some(LengthOrPercentageOrAuto::Length(Au(unsafe { *union.mInt.as_ref() }))), + nsStyleUnit::eStyleUnit_Percent + => Some(LengthOrPercentageOrAuto::Percentage(unsafe { *union.mFloat.as_ref() })), + nsStyleUnit::eStyleUnit_Calc + => unimplemented!(), + _ => None, + } + } } -impl ToGeckoStyleCoord for LengthOrPercentageOrNone { +impl GeckoStyleCoordConvertible for LengthOrPercentageOrNone { fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { match *self { LengthOrPercentageOrNone::Length(au) => { @@ -127,9 +235,23 @@ impl ToGeckoStyleCoord for LengthOrPercentageOrNone { LengthOrPercentageOrNone::Calc(_) => unimplemented!(), }; } + + fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { + match *unit { + nsStyleUnit::eStyleUnit_None + => Some(LengthOrPercentageOrNone::None), + nsStyleUnit::eStyleUnit_Coord + => Some(LengthOrPercentageOrNone::Length(Au(unsafe { *union.mInt.as_ref() }))), + nsStyleUnit::eStyleUnit_Percent + => Some(LengthOrPercentageOrNone::Percentage(unsafe { *union.mFloat.as_ref() })), + nsStyleUnit::eStyleUnit_Calc + => unimplemented!(), + _ => None, + } + } } -impl<T: ToGeckoStyleCoord> ToGeckoStyleCoord for Option<T> { +impl<T: GeckoStyleCoordConvertible> GeckoStyleCoordConvertible for Option<T> { fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { if let Some(ref me) = *self { me.to_gecko_style_coord(unit, union); @@ -138,15 +260,27 @@ impl<T: ToGeckoStyleCoord> ToGeckoStyleCoord for Option<T> { unsafe { *union.mInt.as_mut() = 0; } } } + + fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { + Some(T::from_gecko_style_coord(unit, union)) + } } -impl ToGeckoStyleCoord for Angle { +impl GeckoStyleCoordConvertible for Angle { fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { *unit = nsStyleUnit::eStyleUnit_Radian; unsafe { *union.mFloat.as_mut() = self.radians() }; } + + fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { + if *unit == nsStyleUnit::eStyleUnit_Radian { + Some(Angle::from_radians(unsafe { *union.mFloat.as_ref() })) + } else { + None + } + } } pub fn convert_rgba_to_nscolor(rgba: &RGBA) -> u32 { |