Skip to content

Commit

Permalink
Cleanup position and make use of generic Position for its users
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Apr 25, 2017
1 parent 3f53fb1 commit 61a1799
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 676 deletions.
13 changes: 7 additions & 6 deletions components/style/gecko/conversions.rs
Expand Up @@ -273,8 +273,8 @@ impl nsStyleImage {
},
}
unsafe {
(*gecko_gradient).mBgPosX.set(position.horizontal);
(*gecko_gradient).mBgPosY.set(position.vertical);
(*gecko_gradient).mBgPosX.set(position.horizontal.0);
(*gecko_gradient).mBgPosY.set(position.vertical.0);
}

gecko_gradient
Expand Down Expand Up @@ -330,6 +330,7 @@ pub mod basic_shape {
use values::computed::position;
use values::generics::BorderRadiusSize as GenericBorderRadiusSize;
use values::generics::basic_shape::FillRule;
use values::generics::position::{HorizontalPosition, VerticalPosition};

// using Borrow so that we can have a non-moving .into()
impl<T: Borrow<StyleBasicShape>> From<T> for BasicShape {
Expand Down Expand Up @@ -440,8 +441,8 @@ pub mod basic_shape {
impl From<position::Position> for structs::Position {
fn from(other: position::Position) -> Self {
structs::Position {
mXPosition: other.horizontal.into(),
mYPosition: other.vertical.into()
mXPosition: other.horizontal.0.into(),
mYPosition: other.vertical.0.into()
}
}
}
Expand All @@ -458,8 +459,8 @@ pub mod basic_shape {
fn from(other: T) -> Self {
let other = other.borrow();
position::Position {
horizontal: other.mXPosition.into(),
vertical: other.mYPosition.into(),
horizontal: HorizontalPosition(other.mXPosition.into()),
vertical: VerticalPosition(other.mYPosition.into()),
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -376,17 +376,17 @@ fn color_to_nscolor_zero_currentcolor(color: Color) -> structs::nscolor {
<%def name="impl_position(ident, gecko_ffi_name, need_clone=False)">
#[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
${set_gecko_property("%s.mXPosition" % gecko_ffi_name, "v.horizontal.into()")}
${set_gecko_property("%s.mYPosition" % gecko_ffi_name, "v.vertical.into()")}
${set_gecko_property("%s.mXPosition" % gecko_ffi_name, "v.horizontal.0.into()")}
${set_gecko_property("%s.mYPosition" % gecko_ffi_name, "v.vertical.0.into()")}
}
<%call expr="impl_simple_copy(ident, gecko_ffi_name)"></%call>
% if need_clone:
#[allow(non_snake_case)]
pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T {
use values::computed::Position;
use values::generics::position::{HorizontalPosition, Position, VerticalPosition};
Position {
horizontal: self.gecko.${gecko_ffi_name}.mXPosition.into(),
vertical: self.gecko.${gecko_ffi_name}.mYPosition.into(),
horizontal: HorizontalPosition(self.gecko.${gecko_ffi_name}.mXPosition.into()),
vertical: VerticalPosition(self.gecko.${gecko_ffi_name}.mYPosition.into()),
}
}
% endif
Expand Down Expand Up @@ -1947,8 +1947,8 @@ fn static_assert() {
for (gecko, servo) in self.gecko.mScrollSnapCoordinate
.iter_mut()
.zip(v.0.iter()) {
gecko.mXPosition = servo.horizontal.into();
gecko.mYPosition = servo.vertical.into();
gecko.mXPosition = servo.horizontal.0.into();
gecko.mYPosition = servo.vertical.0.into();
}
}

Expand Down Expand Up @@ -2600,7 +2600,7 @@ fn static_assert() {

pub fn clone_${shorthand}_position_${orientation[0]}(&self)
-> longhands::${shorthand}_position_${orientation[0]}::computed_value::T {
use values::computed::position::${orientation[1]}Position;
use values::generics::position::${orientation[1]}Position;
longhands::${shorthand}_position_${orientation[0]}::computed_value::T(
self.gecko.${image_layers_field}.mLayers.iter()
.take(self.gecko.${image_layers_field}.mPosition${orientation[0].upper()}Count as usize)
Expand Down
19 changes: 11 additions & 8 deletions components/style/properties/helpers/animated_properties.mako.rs
Expand Up @@ -36,9 +36,9 @@ use values::computed::{Angle, LengthOrPercentageOrAuto, LengthOrPercentageOrNone
use values::computed::{BorderRadiusSize, ClipRect, LengthOrNone};
use values::computed::{CalcLengthOrPercentage, Context, LengthOrPercentage};
use values::computed::{MaxLength, MinLength};
use values::computed::position::{HorizontalPosition, Position, VerticalPosition};
use values::computed::position::{HorizontalPosition, VerticalPosition};
use values::computed::ToComputedValue;

use values::generics::position as generic_position;


/// A given transition property, that is either `All`, or an animatable
Expand Down Expand Up @@ -957,23 +957,24 @@ impl Interpolate for FontWeight {
}

/// https://drafts.csswg.org/css-transitions/#animtype-simple-list
impl Interpolate for Position {
impl<H: Interpolate, V: Interpolate> Interpolate for generic_position::Position<H, V> {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(Position {
Ok(generic_position::Position {
horizontal: try!(self.horizontal.interpolate(&other.horizontal, progress)),
vertical: try!(self.vertical.interpolate(&other.vertical, progress)),
})
}
}

impl RepeatableListInterpolate for Position {}
impl<H, V> RepeatableListInterpolate for generic_position::Position<H, V>
where H: RepeatableListInterpolate, V: RepeatableListInterpolate {}

/// https://drafts.csswg.org/css-transitions/#animtype-simple-list
impl Interpolate for HorizontalPosition {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(HorizontalPosition(try!(self.0.interpolate(&other.0, progress))))
self.0.interpolate(&other.0, progress).map(generic_position::HorizontalPosition)
}
}

Expand All @@ -983,7 +984,7 @@ impl RepeatableListInterpolate for HorizontalPosition {}
impl Interpolate for VerticalPosition {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(VerticalPosition(try!(self.0.interpolate(&other.0, progress))))
self.0.interpolate(&other.0, progress).map(generic_position::VerticalPosition)
}
}

Expand Down Expand Up @@ -2607,7 +2608,9 @@ impl ComputeDistance for FontWeight {
}
}

impl ComputeDistance for Position {
impl<H, V> ComputeDistance for generic_position::Position<H, V>
where H: ComputeDistance, V: ComputeDistance
{
#[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
self.compute_squared_distance(other).map(|sd| sd.sqrt())
Expand Down
26 changes: 14 additions & 12 deletions components/style/properties/longhand/background.mako.rs
Expand Up @@ -111,26 +111,27 @@ ${helpers.predefined_type("background-color", "CSSColor",
#[inline]
#[allow(missing_docs)]
pub fn get_initial_value() -> computed_value::T {
use values::computed::position::HorizontalPosition;
use values::generics::position::HorizontalPosition;
HorizontalPosition(computed::LengthOrPercentage::Percentage(0.0))
}
#[inline]
#[allow(missing_docs)]
pub fn get_initial_specified_value() -> SpecifiedValue {
use values::specified::position::Keyword;
HorizontalPosition {
use values::generics::position::{HorizontalPosition, Keyword, PositionValue};
HorizontalPosition(PositionValue {
keyword: Some(Keyword::Left),
position: None,
}
})
}
#[inline]
#[allow(missing_docs)]
pub fn get_initial_position_value() -> SpecifiedValue {
use values::generics::position::{HorizontalPosition, PositionValue};
use values::specified::{LengthOrPercentage, Percentage};
HorizontalPosition {
HorizontalPosition(PositionValue {
keyword: None,
position: Some(LengthOrPercentage::Percentage(Percentage(0.0))),
}
})
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -162,26 +163,27 @@ ${helpers.predefined_type("background-color", "CSSColor",
#[inline]
#[allow(missing_docs)]
pub fn get_initial_value() -> computed_value::T {
use values::computed::position::VerticalPosition;
use values::generics::position::VerticalPosition;
VerticalPosition(computed::LengthOrPercentage::Percentage(0.0))
}
#[inline]
#[allow(missing_docs)]
pub fn get_initial_specified_value() -> SpecifiedValue {
use values::specified::position::Keyword;
VerticalPosition {
use values::generics::position::{Keyword, PositionValue, VerticalPosition};
VerticalPosition(PositionValue {
keyword: Some(Keyword::Top),
position: None,
}
})
}
#[inline]
#[allow(missing_docs)]
pub fn get_initial_position_value() -> SpecifiedValue {
use values::generics::position::{PositionValue, VerticalPosition};
use values::specified::{LengthOrPercentage, Percentage};
VerticalPosition {
VerticalPosition(PositionValue {
keyword: None,
position: Some(LengthOrPercentage::Percentage(Percentage(0.0))),
}
})
}

#[inline]
Expand Down
85 changes: 7 additions & 78 deletions components/style/properties/longhand/box.mako.rs
Expand Up @@ -1113,7 +1113,6 @@ ${helpers.predefined_type("scroll-snap-coordinate",
delegate_animate=True)}



<%helpers:longhand name="transform" extra_prefixes="webkit"
animation_value_type="ComputedValue"
flags="CREATES_STACKING_CONTEXT FIXPOS_CB"
Expand Down Expand Up @@ -2096,83 +2095,13 @@ ${helpers.predefined_type("perspective",
flags="CREATES_STACKING_CONTEXT FIXPOS_CB",
animation_value_type="ComputedValue")}

<%helpers:longhand name="perspective-origin" boxed="True"
animation_value_type="ComputedValue"
extra_prefixes="moz webkit"
spec="https://drafts.csswg.org/css-transforms/#perspective-origin-property">
use std::fmt;
use style_traits::ToCss;
use values::HasViewportPercentage;
use values::specified::{LengthOrPercentage, Percentage};

pub mod computed_value {
use properties::animated_properties::Interpolate;
use values::computed::LengthOrPercentage;
use values::computed::Position;

pub type T = Position;
}

impl HasViewportPercentage for SpecifiedValue {
fn has_viewport_percentage(&self) -> bool {
self.horizontal.has_viewport_percentage() || self.vertical.has_viewport_percentage()
}
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct SpecifiedValue {
horizontal: LengthOrPercentage,
vertical: LengthOrPercentage,
}

impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.horizontal.to_css(dest));
try!(dest.write_str(" "));
self.vertical.to_css(dest)
}
}

#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T {
horizontal: computed::LengthOrPercentage::Percentage(0.5),
vertical: computed::LengthOrPercentage::Percentage(0.5),
}
}

pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
let result = try!(super::parse_origin(context, input));
match result.depth {
Some(_) => Err(()),
None => Ok(SpecifiedValue {
horizontal: result.horizontal.unwrap_or(LengthOrPercentage::Percentage(Percentage(0.5))),
vertical: result.vertical.unwrap_or(LengthOrPercentage::Percentage(Percentage(0.5))),
})
}
}

impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;

#[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T {
computed_value::T {
horizontal: self.horizontal.to_computed_value(context),
vertical: self.vertical.to_computed_value(context),
}
}

#[inline]
fn from_computed_value(computed: &computed_value::T) -> Self {
SpecifiedValue {
horizontal: ToComputedValue::from_computed_value(&computed.horizontal),
vertical: ToComputedValue::from_computed_value(&computed.vertical),
}
}
}
</%helpers:longhand>
${helpers.predefined_type("perspective-origin",
"position::OriginPosition",
"computed::position::OriginPosition::center()",
boxed="True",
extra_prefixes="moz webkit",
spec="https://drafts.csswg.org/css-transforms/#perspective-origin-property",
animation_value_type="ComputedValue")}

${helpers.single_keyword("backface-visibility",
"visible hidden",
Expand Down

0 comments on commit 61a1799

Please sign in to comment.