Introduce values::animated::Animate

This replaces the Animatable trait and merges its three former methods into a single one.
This commit is contained in:
Anthony Ramine 2017-08-17 16:38:08 +02:00
parent 0cceeb9d5c
commit aea0cd7ec7
23 changed files with 876 additions and 937 deletions

View file

@ -4,12 +4,11 @@
//! Computed angles.
use properties::animated_properties::Animatable;
use std::{f32, f64, fmt};
use std::f64::consts::PI;
use style_traits::ToCss;
use values::CSSFloat;
use values::animated::ToAnimatedZero;
use values::animated::{Animate, Procedure, ToAnimatedZero};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
/// A computed angle.
@ -66,36 +65,34 @@ impl Angle {
}
/// https://drafts.csswg.org/css-transitions/#animtype-number
impl Animatable for Angle {
impl Animate for Angle {
#[inline]
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
match (self, other) {
(&Angle::Degree(ref this), &Angle::Degree(ref other)) => {
Ok(Angle::Degree(this.add_weighted(other, self_portion, other_portion)?))
Ok(Angle::Degree(this.animate(other, procedure)?))
},
(&Angle::Gradian(ref this), &Angle::Gradian(ref other)) => {
Ok(Angle::Gradian(this.add_weighted(other, self_portion, other_portion)?))
Ok(Angle::Gradian(this.animate(other, procedure)?))
},
(&Angle::Turn(ref this), &Angle::Turn(ref other)) => {
Ok(Angle::Turn(this.add_weighted(other, self_portion, other_portion)?))
Ok(Angle::Turn(this.animate(other, procedure)?))
},
_ => {
self.radians()
.add_weighted(&other.radians(), self_portion, other_portion)
.map(Angle::from_radians)
}
Ok(Angle::from_radians(self.radians().animate(&other.radians(), procedure)?))
},
}
}
}
impl ToAnimatedZero for Angle {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
fn to_animated_zero(&self) -> Result<Angle, ()> {
match *self {
Angle::Degree(value) => Ok(Angle::Degree(value.to_animated_zero()?)),
Angle::Gradian(value) => Ok(Angle::Gradian(value.to_animated_zero()?)),
Angle::Radian(value) => Ok(Angle::Radian(value.to_animated_zero()?)),
Angle::Turn(value) => Ok(Angle::Turn(value.to_animated_zero()?)),
Angle::Degree(ref this) => Ok(Angle::Degree(this.to_animated_zero()?)),
Angle::Gradian(ref this) => Ok(Angle::Gradian(this.to_animated_zero()?)),
Angle::Radian(ref this) => Ok(Angle::Radian(this.to_animated_zero()?)),
Angle::Turn(ref this) => Ok(Angle::Turn(this.to_animated_zero()?)),
}
}
}

View file

@ -4,9 +4,9 @@
//! Computed types for CSS values related to backgrounds.
use properties::animated_properties::{Animatable, RepeatableListAnimatable};
use properties::animated_properties::RepeatableListAnimatable;
use properties::longhands::background_size::computed_value::T as BackgroundSizeList;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::animated::{Animate, Procedure, ToAnimatedValue, ToAnimatedZero};
use values::computed::length::LengthOrPercentageOrAuto;
use values::generics::background::BackgroundSize as GenericBackgroundSize;
@ -15,16 +15,16 @@ pub type BackgroundSize = GenericBackgroundSize<LengthOrPercentageOrAuto>;
impl RepeatableListAnimatable for BackgroundSize {}
impl Animatable for BackgroundSize {
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
impl Animate for BackgroundSize {
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
match (self, other) {
(
&GenericBackgroundSize::Explicit { width: self_width, height: self_height },
&GenericBackgroundSize::Explicit { width: other_width, height: other_height },
) => {
Ok(GenericBackgroundSize::Explicit {
width: self_width.add_weighted(&other_width, self_portion, other_portion)?,
height: self_height.add_weighted(&other_height, self_portion, other_portion)?,
width: self_width.animate(&other_width, procedure)?,
height: self_height.animate(&other_height, procedure)?,
})
}
_ => Err(()),

View file

@ -11,6 +11,7 @@ use style_traits::ToCss;
use style_traits::values::specified::AllowedLengthType;
use super::{Number, ToComputedValue, Context, Percentage};
use values::{Auto, CSSFloat, Either, ExtremumLength, None_, Normal, specified};
use values::animated::ToAnimatedZero;
use values::computed::{NonNegativeAu, NonNegativeNumber};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::generics::NonNegative;
@ -72,6 +73,17 @@ pub struct CalcLengthOrPercentage {
pub percentage: Option<Percentage>,
}
impl ToAnimatedZero for CalcLengthOrPercentage {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
Ok(CalcLengthOrPercentage {
clamping_mode: self.clamping_mode,
length: self.length.to_animated_zero()?,
percentage: self.percentage.to_animated_zero()?,
})
}
}
impl ComputeSquaredDistance for CalcLengthOrPercentage {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {

View file

@ -4,11 +4,10 @@
//! Computed percentages.
use properties::animated_properties::Animatable;
use std::fmt;
use style_traits::ToCss;
use values::{CSSFloat, serialize_percentage};
use values::animated::ToAnimatedZero;
use values::animated::{Animate, Procedure, ToAnimatedZero};
/// A computed percentage.
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Default, HasViewportPercentage, PartialEq, PartialOrd)]
@ -36,10 +35,10 @@ impl Percentage {
}
/// https://drafts.csswg.org/css-transitions/#animtype-percentage
impl Animatable for Percentage {
impl Animate for Percentage {
#[inline]
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
Ok(Percentage((self.0 as f64 * self_portion + other.0 as f64 * other_portion) as f32))
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
Ok(Percentage(self.0.animate(&other.0, procedure)?))
}
}

View file

@ -4,9 +4,8 @@
//! Computed types for text properties.
use properties::animated_properties::Animatable;
use values::{CSSInteger, CSSFloat};
use values::animated::ToAnimatedZero;
use values::animated::{Animate, Procedure, ToAnimatedZero};
use values::computed::{NonNegativeAu, NonNegativeNumber};
use values::computed::length::{Length, LengthOrPercentage};
use values::generics::text::InitialLetter as GenericInitialLetter;
@ -25,21 +24,21 @@ pub type WordSpacing = Spacing<LengthOrPercentage>;
/// A computed value for the `line-height` property.
pub type LineHeight = GenericLineHeight<NonNegativeNumber, NonNegativeAu>;
impl Animatable for LineHeight {
impl Animate for LineHeight {
#[inline]
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
match (*self, *other) {
(GenericLineHeight::Length(ref this), GenericLineHeight::Length(ref other)) => {
this.add_weighted(other, self_portion, other_portion).map(GenericLineHeight::Length)
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
match (self, other) {
(&GenericLineHeight::Length(ref this), &GenericLineHeight::Length(ref other)) => {
Ok(GenericLineHeight::Length(this.animate(other, procedure)?))
},
(GenericLineHeight::Number(ref this), GenericLineHeight::Number(ref other)) => {
this.add_weighted(other, self_portion, other_portion).map(GenericLineHeight::Number)
(&GenericLineHeight::Number(ref this), &GenericLineHeight::Number(ref other)) => {
Ok(GenericLineHeight::Number(this.animate(other, procedure)?))
},
(GenericLineHeight::Normal, GenericLineHeight::Normal) => {
(&GenericLineHeight::Normal, &GenericLineHeight::Normal) => {
Ok(GenericLineHeight::Normal)
},
#[cfg(feature = "gecko")]
(GenericLineHeight::MozBlockHeight, GenericLineHeight::MozBlockHeight) => {
(&GenericLineHeight::MozBlockHeight, &GenericLineHeight::MozBlockHeight) => {
Ok(GenericLineHeight::MozBlockHeight)
},
_ => Err(()),

View file

@ -4,8 +4,7 @@
//! Computed types for CSS values that are related to transformations.
use properties::animated_properties::Animatable;
use values::animated::ToAnimatedZero;
use values::animated::{Animate, Procedure, ToAnimatedZero};
use values::computed::{Length, LengthOrPercentage, Number, Percentage};
use values::generics::transform::TimingFunction as GenericTimingFunction;
use values::generics::transform::TransformOrigin as GenericTransformOrigin;
@ -28,13 +27,13 @@ impl TransformOrigin {
}
}
impl Animatable for TransformOrigin {
impl Animate for TransformOrigin {
#[inline]
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
Ok(Self::new(
self.horizontal.add_weighted(&other.horizontal, self_portion, other_portion)?,
self.vertical.add_weighted(&other.vertical, self_portion, other_portion)?,
self.depth.add_weighted(&other.depth, self_portion, other_portion)?,
self.horizontal.animate(&other.horizontal, procedure)?,
self.vertical.animate(&other.vertical, procedure)?,
self.depth.animate(&other.depth, procedure)?,
))
}
}