Skip to content

Commit

Permalink
Make Position a gecko-only vector longhand
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Aug 22, 2016
1 parent 65a8a8d commit 979a279
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 26 deletions.
1 change: 1 addition & 0 deletions components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/style/Cargo.toml
Expand Up @@ -34,6 +34,7 @@ heapsize_plugin = {version = "0.1.2", optional = true}
lazy_static = "0.2"
log = "0.3.5"
matches = "0.1"
num-integer = "0.1.32"
num-traits = "0.1.32"
ordered-float = "0.2.2"
quickersort = "2.0.0"
Expand Down
1 change: 1 addition & 0 deletions components/style/lib.rs
Expand Up @@ -56,6 +56,7 @@ extern crate log;
#[allow(unused_extern_crates)]
#[macro_use]
extern crate matches;
extern crate num_integer;
extern crate num_traits;
extern crate ordered_float;
extern crate quickersort;
Expand Down
47 changes: 36 additions & 11 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -944,7 +944,7 @@ fn static_assert() {
}
for (layer, other) in self.gecko.mImage.mLayers.iter_mut()
.zip(other.gecko.mImage.mLayers.iter())
.take(other.gecko.mImage.${field_name}Count) {
.take(other.gecko.mImage.${field_name}Count as usize) {
layer.${field_name} = other.${field_name};
}
self.gecko.mImage.${field_name}Count = other.gecko.mImage.${field_name}Count;
Expand Down Expand Up @@ -1033,23 +1033,48 @@ fn static_assert() {
self.gecko.mImage.mPositionYCount = cmp::min(1, other.gecko.mImage.mPositionYCount);
self.gecko.mImage.mLayers.mFirstElement.mPosition =
other.gecko.mImage.mLayers.mFirstElement.mPosition;
unsafe {
Gecko_EnsureImageLayersLength(&mut self.gecko.mImage, other.gecko.mImage.mLayers.len());
}
for (layer, other) in self.gecko.mImage.mLayers.iter_mut()
.zip(other.gecko.mImage.mLayers.iter())
.take(other.gecko.mImage.mPositionXCount as usize) {
layer.mPosition.mXPosition = other.mPosition.mXPosition;
}
for (layer, other) in self.gecko.mImage.mLayers.iter_mut()
.zip(other.gecko.mImage.mLayers.iter())
.take(other.gecko.mImage.mPositionYCount as usize) {
layer.mPosition.mYPosition = other.mPosition.mYPosition;
}
self.gecko.mImage.mPositionXCount = other.gecko.mImage.mPositionXCount;
self.gecko.mImage.mPositionYCount = other.gecko.mImage.mPositionYCount;
}

pub fn clone_background_position(&self) -> longhands::background_position::computed_value::T {
use values::computed::position::Position;
let position = &self.gecko.mImage.mLayers.mFirstElement.mPosition;
Position {
horizontal: position.mXPosition.into(),
vertical: position.mYPosition.into(),
}
longhands::background_position::computed_value::T(
self.gecko.mImage.mLayers.iter()
.take(self.gecko.mImage.mPositionXCount as usize)
.take(self.gecko.mImage.mPositionYCount as usize)
.map(|position| Position {
horizontal: position.mPosition.mXPosition.into(),
vertical: position.mPosition.mYPosition.into(),
})
.collect()
)
}

pub fn set_background_position(&mut self, v: longhands::background_position::computed_value::T) {
let position = &mut self.gecko.mImage.mLayers.mFirstElement.mPosition;
position.mXPosition = v.horizontal.into();
position.mYPosition = v.vertical.into();
self.gecko.mImage.mPositionXCount = 1;
self.gecko.mImage.mPositionYCount = 1;
unsafe {
Gecko_EnsureImageLayersLength(&mut self.gecko.mImage, v.0.len());
}

self.gecko.mImage.mPositionXCount = v.0.len() as u32;
self.gecko.mImage.mPositionYCount = v.0.len() as u32;
for (servo, geckolayer) in v.0.into_iter().zip(self.gecko.mImage.mLayers.iter_mut()) {
geckolayer.mPosition.mXPosition = servo.horizontal.into();
geckolayer.mPosition.mYPosition = servo.vertical.into();
}
}

pub fn copy_background_image_from(&mut self, other: &Self) {
Expand Down
43 changes: 32 additions & 11 deletions components/style/properties/helpers/animated_properties.mako.rs
Expand Up @@ -155,6 +155,25 @@ pub trait Interpolate: Sized {
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()>;
}

/// https://drafts.csswg.org/css-transitions/#animtype-repeatable-list
pub trait RepeatableListInterpolate: Interpolate {}

impl<T: RepeatableListInterpolate> Interpolate for Vec<T> {
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()> {
use num_integer::lcm;
let len = lcm(self.len(), other.len());
let ret = self.iter().cycle().zip(other.iter().cycle())
.take(len)
.filter_map(|(ref me, ref you)| {
me.interpolate(you, time).ok()
}).collect::<Self>();
if ret.len() == len {
Ok(ret)
} else {
Err(())
}
}
}
/// https://drafts.csswg.org/css-transitions/#animtype-number
impl Interpolate for Au {
#[inline]
Expand Down Expand Up @@ -296,6 +315,14 @@ impl Interpolate for BorderSpacing {
}
}

impl Interpolate for BackgroundSize {
#[inline]
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()> {
self.0.interpolate(&other.0, time).map(BackgroundSize)
}
}


/// https://drafts.csswg.org/css-transitions/#animtype-color
impl Interpolate for RGBA {
#[inline]
Expand Down Expand Up @@ -496,18 +523,12 @@ impl Interpolate for Position {
}
}

impl Interpolate for BackgroundSize {
impl RepeatableListInterpolate for Position {}

impl Interpolate for BackgroundPosition {
#[inline]
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()> {
use properties::longhands::background_size::computed_value::ExplicitSize;
match (self, other) {
(&BackgroundSize::Explicit(ref me), &BackgroundSize::Explicit(ref other)) => {
Ok(BackgroundSize::Explicit(ExplicitSize {
width: try!(me.width.interpolate(&other.width, time)),
height: try!(me.height.interpolate(&other.height, time)),
}))
}
_ => Err(()),
}
Ok(BackgroundPosition(try!(self.0.interpolate(&other.0, time))))
}
}

Expand Down
46 changes: 42 additions & 4 deletions components/style/properties/longhand/background.mako.rs
Expand Up @@ -54,6 +54,10 @@ ${helpers.predefined_type("background-color", "CSSColor",
pub fn get_initial_value() -> computed_value::T {
computed_value::T(None)
}
#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
SpecifiedValue(None)
}
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
Ok(SpecifiedValue(None))
Expand All @@ -75,7 +79,7 @@ ${helpers.predefined_type("background-color", "CSSColor",
}
</%helpers:vector_longhand>

<%helpers:longhand name="background-position" animatable="True">
<%helpers:vector_longhand name="background-position" gecko_only="True" animatable="True">
use cssparser::ToCss;
use std::fmt;
use values::LocalToCss;
Expand All @@ -84,6 +88,7 @@ ${helpers.predefined_type("background-color", "CSSColor",

pub mod computed_value {
use values::computed::position::Position;
use properties::animated_properties::{Interpolate, RepeatableListInterpolate};

pub type T = Position;
}
Expand All @@ -98,12 +103,20 @@ ${helpers.predefined_type("background-color", "CSSColor",
vertical: computed::LengthOrPercentage::Percentage(0.0),
}
}
#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
use values::specified::Percentage;
Position {
horizontal: specified::LengthOrPercentage::Percentage(Percentage(0.0)),
vertical: specified::LengthOrPercentage::Percentage(Percentage(0.0)),
}
}

pub fn parse(_context: &ParserContext, input: &mut Parser)
-> Result<SpecifiedValue, ()> {
Ok(try!(Position::parse(input)))
}
</%helpers:longhand>
</%helpers:vector_longhand>

${helpers.single_keyword("background-repeat",
"repeat repeat-x repeat-y no-repeat",
Expand All @@ -129,14 +142,15 @@ ${helpers.single_keyword("background-origin",
gecko_only=True,
animatable=False)}

<%helpers:longhand name="background-size" animatable="True">
<%helpers:vector_longhand name="background-size" animatable="True">
use cssparser::{ToCss, Token};
use std::ascii::AsciiExt;
use std::fmt;
use values::HasViewportPercentage;

pub mod computed_value {
use values::computed::LengthOrPercentageOrAuto;
use properties::animated_properties::{Interpolate, RepeatableListInterpolate};

#[derive(PartialEq, Clone, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
Expand All @@ -152,6 +166,23 @@ ${helpers.single_keyword("background-origin",
Cover,
Contain,
}

impl RepeatableListInterpolate for T {}

impl Interpolate for T {
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()> {
use properties::longhands::background_size::single_value::computed_value::ExplicitSize;
match (self, other) {
(&T::Explicit(ref me), &T::Explicit(ref other)) => {
Ok(T::Explicit(ExplicitSize {
width: try!(me.width.interpolate(&other.width, time)),
height: try!(me.height.interpolate(&other.height, time)),
}))
}
_ => Err(()),
}
}
}
}

impl ToCss for computed_value::T {
Expand Down Expand Up @@ -245,6 +276,13 @@ ${helpers.single_keyword("background-origin",
height: computed::LengthOrPercentageOrAuto::Auto,
})
}
#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
SpecifiedValue::Explicit(SpecifiedExplicitSize {
width: specified::LengthOrPercentageOrAuto::Auto,
height: specified::LengthOrPercentageOrAuto::Auto,
})
}

pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
let width;
Expand Down Expand Up @@ -282,4 +320,4 @@ ${helpers.single_keyword("background-origin",
height: height,
}))
}
</%helpers:longhand>
</%helpers:vector_longhand>
1 change: 1 addition & 0 deletions ports/cef/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions ports/geckolib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 979a279

Please sign in to comment.