Skip to content

Commit

Permalink
Bug 1374233 - Part 11: Implement ToAnimatedValue for background-size.
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: DMcvpaqHdy9
  • Loading branch information
BorisChiou committed Aug 4, 2017
1 parent 1e79e5f commit ebedea5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components/style/properties/data.py
Expand Up @@ -157,7 +157,7 @@ def __init__(self, style_struct, name, spec=None, animation_value_type=None, der
allowed_in_keyframe_block=True, cast_type='u8',
has_uncacheable_values=False, logical=False, alias=None, extra_prefixes=None, boxed=False,
flags=None, allowed_in_page_rule=False, allow_quirks=False, ignored_when_colors_disabled=False,
gecko_pref_ident=None, vector=False):
gecko_pref_ident=None, vector=False, need_animatable=False):
self.name = name
if not spec:
raise TypeError("Spec should be specified for %s" % name)
Expand Down
8 changes: 5 additions & 3 deletions components/style/properties/helpers.mako.rs
Expand Up @@ -76,8 +76,10 @@
We assume that the default/initial value is an empty vector for these.
`initial_value` need not be defined for these.
</%doc>
<%def name="vector_longhand(name, animation_value_type=None, allow_empty=False, separator='Comma', **kwargs)">
<%call expr="longhand(name, animation_value_type=animation_value_type, vector=True, **kwargs)">
<%def name="vector_longhand(name, animation_value_type=None, allow_empty=False, separator='Comma',
need_animatable=False, **kwargs)">
<%call expr="longhand(name, animation_value_type=animation_value_type, vector=True,
need_animatable=need_animatable, **kwargs)">
#[allow(unused_imports)]
use smallvec::SmallVec;
use std::fmt;
Expand Down Expand Up @@ -127,7 +129,7 @@
% endif
);

% if animation_value_type == "ComputedValue":
% if need_animatable or animation_value_type == "ComputedValue":
use properties::animated_properties::Animatable;
use values::animated::ToAnimatedZero;

Expand Down
Expand Up @@ -16,6 +16,7 @@ use euclid::{Point2D, Size2D};
#[cfg(feature = "gecko")] use gecko_string_cache::Atom;
use properties::{CSSWideKeyword, PropertyDeclaration};
use properties::longhands;
use properties::longhands::background_size::computed_value::T as BackgroundSizeList;
use properties::longhands::border_spacing::computed_value::T as BorderSpacing;
use properties::longhands::font_weight::computed_value::T as FontWeight;
use properties::longhands::font_stretch::computed_value::T as FontStretch;
Expand Down
3 changes: 2 additions & 1 deletion components/style/properties/longhand/background.mako.rs
Expand Up @@ -167,7 +167,8 @@ ${helpers.predefined_type("background-size", "BackgroundSize",
initial_specified_value="specified::LengthOrPercentageOrAuto::Auto.into()",
spec="https://drafts.csswg.org/css-backgrounds/#the-background-size",
vector=True,
animation_value_type="ComputedValue",
animation_value_type="BackgroundSizeList",
need_animatable=True,
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER",
extra_prefixes="webkit")}

Expand Down
18 changes: 18 additions & 0 deletions components/style/values/animated/mod.rs
Expand Up @@ -9,6 +9,7 @@
//! module's raison d'être is to ultimately contain all these types.

use app_units::Au;
use smallvec::SmallVec;
use std::cmp::max;
use values::computed::Angle as ComputedAngle;
use values::computed::BorderCornerRadius as ComputedBorderCornerRadius;
Expand Down Expand Up @@ -71,6 +72,23 @@ where
}
}

impl<T> ToAnimatedValue for SmallVec<[T; 1]>
where
T: ToAnimatedValue,
{
type AnimatedValue = SmallVec<[T::AnimatedValue; 1]>;

#[inline]
fn to_animated_value(self) -> Self::AnimatedValue {
self.into_iter().map(T::to_animated_value).collect()
}

#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
animated.into_iter().map(T::from_animated_value).collect()
}
}

/// Marker trait for computed values with the same representation during animations.
pub trait AnimatedValueAsComputed {}

Expand Down
52 changes: 51 additions & 1 deletion components/style/values/computed/background.rs
Expand Up @@ -5,7 +5,8 @@
//! Computed types for CSS values related to backgrounds.

use properties::animated_properties::{Animatable, RepeatableListAnimatable};
use values::animated::ToAnimatedZero;
use properties::longhands::background_size::computed_value::T as BackgroundSizeList;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::computed::length::LengthOrPercentageOrAuto;
use values::generics::background::BackgroundSize as GenericBackgroundSize;

Expand Down Expand Up @@ -56,3 +57,52 @@ impl ToAnimatedZero for BackgroundSize {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Err(()) }
}

impl ToAnimatedValue for BackgroundSize {
type AnimatedValue = Self;

#[inline]
fn to_animated_value(self) -> Self {
self
}

#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
use app_units::Au;
use values::computed::Percentage;
let clamp_animated_value = |value: LengthOrPercentageOrAuto| -> LengthOrPercentageOrAuto {
match value {
LengthOrPercentageOrAuto::Length(len) => {
LengthOrPercentageOrAuto::Length(Au(::std::cmp::max(len.0, 0)))
},
LengthOrPercentageOrAuto::Percentage(percent) => {
LengthOrPercentageOrAuto::Percentage(Percentage(percent.0.max(0.)))
},
_ => value
}
};
match animated {
GenericBackgroundSize::Explicit { width, height } => {
GenericBackgroundSize::Explicit {
width: clamp_animated_value(width),
height: clamp_animated_value(height)
}
},
_ => animated
}
}
}

impl ToAnimatedValue for BackgroundSizeList {
type AnimatedValue = Self;

#[inline]
fn to_animated_value(self) -> Self {
self
}

#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
BackgroundSizeList(ToAnimatedValue::from_animated_value(animated.0))
}
}

0 comments on commit ebedea5

Please sign in to comment.