Bug 1207734 - Part 10. Implement Animate trait for individual transform. draft
authorcku <cku@mozilla.com>
Thu, 07 Dec 2017 14:46:37 +0800
changeset 708797 072441e227b8f4b90bbd98954f591fdfbcf0dacb
parent 708796 071dd0a3ea3939f3d355c6a841a77bdd58507aed
child 743251 a12c8144e639078a3d64526cb058a4d1e7842912
push id92453
push userbmo:cku@mozilla.com
push dateThu, 07 Dec 2017 06:47:04 +0000
bugs1207734
milestone59.0a1
Bug 1207734 - Part 10. Implement Animate trait for individual transform. MozReview-Commit-ID: HlDD9xXh8jv
servo/components/style/properties/helpers/animated_properties.mako.rs
servo/components/style/values/generics/transform.rs
--- a/servo/components/style/properties/helpers/animated_properties.mako.rs
+++ b/servo/components/style/properties/helpers/animated_properties.mako.rs
@@ -40,17 +40,20 @@ use values::computed::{ClipRect, Context
 use values::computed::{Length, LengthOrPercentage, LengthOrPercentageOrAuto};
 use values::computed::{LengthOrPercentageOrNone, MaxLength};
 use values::computed::{NonNegativeNumber, Number, NumberOrPercentage, Percentage};
 use values::computed::length::NonNegativeLengthOrPercentage;
 use values::computed::ToComputedValue;
 use values::computed::transform::{DirectionVector, Matrix, Matrix3D};
 use values::computed::transform::TransformOperation as ComputedTransformOperation;
 use values::computed::transform::Transform as ComputedTransform;
-use values::generics::transform::{self, Transform, TransformOperation};
+use values::computed::transform::Rotate as ComputedRotate;
+use values::computed::transform::Translate as ComputedTranslate;
+use values::computed::transform::Scale as ComputedScale;
+use values::generics::transform::{self, Transform, TransformOperation, Translate, Scale, Rotate};
 use values::distance::{ComputeSquaredDistance, SquaredDistance};
 #[cfg(feature = "gecko")] use values::generics::FontSettings as GenericFontSettings;
 #[cfg(feature = "gecko")] use values::generics::FontSettingTag as GenericFontSettingTag;
 #[cfg(feature = "gecko")] use values::generics::FontSettingTagFloat;
 use values::generics::NonNegative;
 use values::generics::effects::Filter;
 use values::generics::position as generic_position;
 use values::generics::svg::{SVGLength,  SvgLengthOrPercentageOrNumber, SVGPaint};
@@ -2240,16 +2243,125 @@ impl Matrix3D {
              self.m12*self.m21*self.m33 + self.m11*self.m22*self.m33),
         };
 
         Some(x)
     }
 }
 
 /// <https://drafts.csswg.org/css-transforms/#interpolation-of-transforms>
+impl ComputedRotate {
+    fn fill_unspecified(rotate_: &ComputedRotate)
+        -> Result<(Number, Number, Number, Angle), ()>{
+        // If the axis is unspecified, it defaults to "0 0 1"
+        match rotate_ {
+            &Rotate::None =>
+                Ok((0., 0., 1., Angle::Deg(0.))),
+            &Rotate::Specified(None, None, None, angle) =>
+                Ok((0., 0., 1., angle)),
+            &Rotate::Specified(Some(rx), Some(ry), Some(rz), angle) =>
+                Ok((rx, ry, rz, angle)),
+            &_ =>
+                panic!("Found unexpected value for rotate property: {:?}", rotate_),
+        }
+    }
+}
+
+impl Animate for ComputedRotate {
+    #[inline]
+    fn animate(
+        &self,
+        other_: &Self,
+        procedure: Procedure,
+    ) -> Result<Self, ()> {
+        let from = ComputedRotate::fill_unspecified(self)?;
+        let to = ComputedRotate::fill_unspecified(other_)?;
+
+        let rx = from.0.animate(&to.0, procedure)?;
+        let ry = from.1.animate(&to.1, procedure)?;
+        let rz = from.2.animate(&to.2, procedure)?;
+        let angle = from.3.animate(&to.3, procedure)?;
+
+        Ok(Rotate::Specified(Some(rx), Some(ry), Some(rz), angle))
+    }
+}
+
+impl ComputedTranslate {
+    fn fill_unspecified(translate_: &ComputedTranslate)
+        -> Result<(LengthOrPercentage, LengthOrPercentage, Length), ()>{
+        // Unspecified translations default to 0px
+        match translate_ {
+            &Translate::None =>
+                Ok((LengthOrPercentage::Length(Length::new(0.)),
+                    LengthOrPercentage::Length(Length::new(0.)),
+                    Length::new(0.))),
+            &Translate::Specified(tx, Some(ty), None) =>
+                Ok((tx, ty, Length::new(0.))),
+            &Translate::Specified(tx, Some(ty), Some(tz)) =>
+                Ok((tx, ty, tz)),
+            &_ =>
+                panic!("Found unexpected value for translate property: {:?}", translate_),
+        }
+    }
+}
+
+impl Animate for ComputedTranslate {
+    #[inline]
+    fn animate(
+        &self,
+        other_: &Self,
+        procedure: Procedure,
+    ) -> Result<Self, ()> {
+        let from = ComputedTranslate::fill_unspecified(self)?;
+        let to = ComputedTranslate::fill_unspecified(other_)?;
+
+        let sx = from.0.animate(&to.0, procedure)?;
+        let sy = from.1.animate(&to.1, procedure)?;
+        let sz = from.2.animate(&to.2, procedure)?;
+
+        Ok(Translate::Specified(sx, Some(sy), Some(sz)))
+    }
+}
+
+impl ComputedScale {
+    fn fill_unspecified(scale_: &ComputedScale)
+        -> Result<(Number, Number, Number), ()>{
+        // Unspecified scales default to 1
+        match scale_ {
+            &Scale::None =>
+                Ok((1.0, 1.0, 1.0)),
+            &Scale::Specified(sx, Some(sy), None) =>
+                Ok((sx, sy, 1.0)),
+            &Scale::Specified(sx, Some(sy), Some(sz)) =>
+                Ok((sx, sy, sz)),
+            &_ =>
+                panic!("Found unexpected value for scale property: {:?}", scale_),
+        }
+    }
+}
+
+impl Animate for ComputedScale {
+    #[inline]
+    fn animate(
+        &self,
+        other_: &Self,
+        procedure: Procedure,
+    ) -> Result<Self, ()> {
+        let from = ComputedScale::fill_unspecified(self)?;
+        let to = ComputedScale::fill_unspecified(other_)?;
+
+        let sx = from.0.animate(&to.0, procedure)?;
+        let sy = from.1.animate(&to.1, procedure)?;
+        let sz = from.2.animate(&to.2, procedure)?;
+
+        Ok(Scale::Specified(sx, Some(sy), Some(sz)))
+    }
+}
+
+/// <https://drafts.csswg.org/css-transforms/#interpolation-of-transforms>
 impl Animate for ComputedTransform {
     #[inline]
     fn animate(
         &self,
         other_: &Self,
         procedure: Procedure,
     ) -> Result<Self, ()> {
 
--- a/servo/components/style/values/generics/transform.rs
+++ b/servo/components/style/values/generics/transform.rs
@@ -749,17 +749,17 @@ pub fn get_normalized_vector_and_angle<T
         // rotation to not be applied, so we use identity matrix (i.e. rotate3d(0, 0, 1, 0)).
         (0., 0., 1., T::zero())
     } else {
         let vector = vector.normalize();
         (vector.x, vector.y, vector.z, angle)
     }
 }
 
-#[derive(ToComputedValue, Animate, ComputeSquaredDistance, ToAnimatedZero)]
+#[derive(ToComputedValue, ComputeSquaredDistance, ToAnimatedZero)]
 #[derive(Clone, Debug, MallocSizeOf, PartialEq)]
 /// A value of the `Rotate` property
 pub enum Rotate<Number, Angle> {
     /// 'none'
     None,
     /// '<number>{3}? <angle>'
     Specified(Option<Number>, Option<Number>, Option<Number>, Angle)
 }
@@ -793,17 +793,17 @@ impl<Number: ToCss + Copy + fmt::Debug, 
 
 impl<LengthOrPercentage, Length> Rotate<LengthOrPercentage, Length> {
     /// `none`
     pub fn none() -> Self {
         Rotate::None
     }
 }
 
-#[derive(ToComputedValue, Animate, ComputeSquaredDistance, ToAnimatedZero)]
+#[derive(ToComputedValue, ComputeSquaredDistance, ToAnimatedZero)]
 #[derive(Clone, Debug, MallocSizeOf, PartialEq)]
 /// A value of the `Translate` property
 pub enum Translate<LengthOrPercentage, Length> {
     /// 'none'
     None,
     /// '<number>{1,3}'
     Specified(LengthOrPercentage, Option<LengthOrPercentage>, Option<Length>)
 }
@@ -838,17 +838,17 @@ impl<LengthOrPercentage: ToCss  + fmt::D
 
 impl<LengthOrPercentage, Length> Translate<LengthOrPercentage, Length> {
     /// `none`
     pub fn none() -> Self {
         Translate::None
     }
 }
 
-#[derive(ToComputedValue, Animate, ComputeSquaredDistance, ToAnimatedZero)]
+#[derive(ToComputedValue, ComputeSquaredDistance, ToAnimatedZero)]
 #[derive(Clone, Debug, MallocSizeOf, PartialEq)]
 /// A value of the `Scale` property
 pub enum Scale<Number> {
     /// 'none'
     None,
     /// '<length-percentage> [ <length-percentage> <length>? ]?'
     Specified(Number, Option<Number>, Option<Number>)
 }