From 54dc3e1670852510f0a7913a85f890297623ce8e Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 21 Aug 2017 11:10:44 +0200 Subject: [PATCH] Clean up filter animations --- .../helpers/animated_properties.mako.rs | 181 ++++++++---------- components/style/values/animated/effects.rs | 6 +- 2 files changed, 86 insertions(+), 101 deletions(-) diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 6901142b31a..6e2cc93440c 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -2959,125 +2959,107 @@ impl ToAnimatedZero for SVGOpacity %> /// https://drafts.fxtf.org/filters/#animation-of-filters -fn add_weighted_filter_function_impl(from: &AnimatedFilter, - to: &AnimatedFilter, - self_portion: f64, - other_portion: f64) - -> Result { - match (from, to) { - % for func in [ 'Blur', 'HueRotate' ]: - (&Filter::${func}(from_value), &Filter::${func}(to_value)) => { - Ok(Filter::${func}(from_value.add_weighted( - &to_value, +impl Animatable for AnimatedFilter { + fn add_weighted( + &self, + other: &Self, + self_portion: f64, + other_portion: f64, + ) -> Result { + match (self, other) { + % for func in ['Blur', 'Grayscale', 'HueRotate', 'Invert', 'Sepia']: + (&Filter::${func}(ref this), &Filter::${func}(ref other)) => { + Ok(Filter::${func}(this.add_weighted( + other, self_portion, other_portion, )?)) - }, - % endfor - % for func in [ 'Grayscale', 'Invert', 'Sepia' ]: - (&Filter::${func}(from_value), &Filter::${func}(to_value)) => { - Ok(Filter::${func}(add_weighted_with_initial_val( - &from_value, - &to_value, - self_portion, - other_portion, - &NonNegative::(0.0), - )?)) }, - % endfor - % for func in [ 'Brightness', 'Contrast', 'Opacity', 'Saturate' ]: - (&Filter::${func}(from_value), &Filter::${func}(to_value)) => { + % endfor + % for func in ['Brightness', 'Contrast', 'Opacity', 'Saturate']: + (&Filter::${func}(ref this), &Filter::${func}(ref other)) => { Ok(Filter::${func}(add_weighted_with_initial_val( - &from_value, - &to_value, + this, + other, self_portion, other_portion, &NonNegative::(1.0), )?)) - }, - % endfor - % if product == "gecko": - (&Filter::DropShadow(ref from_value), &Filter::DropShadow(ref to_value)) => { - Ok(Filter::DropShadow(from_value.add_weighted( - &to_value, - self_portion, - other_portion, - )?)) - }, - (&Filter::Url(_), &Filter::Url(_)) => { - Err(()) - }, - % endif - _ => { - // If specified the different filter functions, - // we will need to interpolate as discreate. - Err(()) - }, + }, + % endfor + % if product == "gecko": + (&Filter::DropShadow(ref this), &Filter::DropShadow(ref other)) => { + Ok(Filter::DropShadow(this.add_weighted( + other, + self_portion, + other_portion, + )?)) + }, + % endif + _ => Err(()), + } } } -/// https://drafts.fxtf.org/filters/#animation-of-filters -fn add_weighted_filter_function(from: Option<<&AnimatedFilter>, - to: Option<<&AnimatedFilter>, - self_portion: f64, - other_portion: f64) -> Result { - match (from, to) { - (Some(f), Some(t)) => { - add_weighted_filter_function_impl(f, t, self_portion, other_portion) - }, - (Some(f), None) => { - add_weighted_filter_function_impl(f, f, self_portion, 0.0) - }, - (None, Some(t)) => { - add_weighted_filter_function_impl(t, t, other_portion, 0.0) - }, - _ => { Err(()) } + +impl ToAnimatedZero for AnimatedFilter { + fn to_animated_zero(&self) -> Result { + match *self { + % for func in ['Blur', 'Grayscale', 'HueRotate', 'Invert', 'Sepia']: + Filter::${func}(ref this) => Ok(Filter::${func}(this.to_animated_zero()?)), + % endfor + % for func in ['Brightness', 'Contrast', 'Opacity', 'Saturate']: + Filter::${func}(_) => Ok(Filter::${func}(NonNegative(1.))), + % endfor + % if product == "gecko": + Filter::DropShadow(ref this) => Ok(Filter::DropShadow(this.to_animated_zero()?)), + % endif + _ => Err(()), + } } } -fn compute_filter_square_distance(from: &AnimatedFilter, to: &AnimatedFilter) -> Result { - match (from, to) { - % for func in FILTER_FUNCTIONS : - (&Filter::${func}(f), - &Filter::${func}(t)) => { - Ok(try!(f.compute_squared_distance(&t))) + +// FIXME(nox): This should be derived. +impl ComputeSquaredDistance for AnimatedFilter { + fn compute_squared_distance(&self, other: &Self) -> Result { + match (self, other) { + % for func in FILTER_FUNCTIONS: + (&Filter::${func}(ref this), &Filter::${func}(ref other)) => { + this.compute_squared_distance(other) }, - % endfor - % if product == "gecko": - (&Filter::DropShadow(ref f), &Filter::DropShadow(ref t)) => { - Ok(try!(f.compute_squared_distance(&t))) + % endfor + % if product == "gecko": + (&Filter::DropShadow(ref this), &Filter::DropShadow(ref other)) => { + this.compute_squared_distance(other) }, - % endif - _ => { - Err(()) + % endif + _ => Err(()), } } } impl Animatable for AnimatedFilterList { #[inline] - fn add_weighted(&self, other: &Self, - self_portion: f64, other_portion: f64) -> Result { - let mut filters = vec![]; - let mut from_iter = self.0.iter(); - let mut to_iter = other.0.iter(); - - let mut from = from_iter.next(); - let mut to = to_iter.next(); - while from.is_some() || to.is_some() { - filters.push(try!(add_weighted_filter_function(from, - to, - self_portion, - other_portion))); - if from.is_some() { - from = from_iter.next(); + fn add_weighted( + &self, + other: &Self, + self_portion: f64, + other_portion: f64, + ) -> Result { + Ok(AnimatedFilterList(self.0.iter().zip_longest(other.0.iter()).map(|it| { + match it { + EitherOrBoth::Both(this, other) => { + this.add_weighted(other, self_portion, other_portion) + }, + EitherOrBoth::Left(this) => { + this.add_weighted(&this.to_animated_zero()?, self_portion, other_portion) + }, + EitherOrBoth::Right(other) => { + other.to_animated_zero()?.add_weighted(other, self_portion, other_portion) + }, } - if to.is_some() { - to = to_iter.next(); - } - } - - Ok(AnimatedFilterList(filters)) + }).collect::, _>>()?)) } fn add(&self, other: &Self) -> Result { @@ -3090,12 +3072,11 @@ impl ComputeSquaredDistance for AnimatedFilterList { fn compute_squared_distance(&self, other: &Self) -> Result { self.0.iter().zip_longest(other.0.iter()).map(|it| { match it { - EitherOrBoth::Both(from, to) => { - compute_filter_square_distance(&from, &to) + EitherOrBoth::Both(this, other) => { + this.compute_squared_distance(other) }, - EitherOrBoth::Left(list) | EitherOrBoth::Right(list)=> { - let none = add_weighted_filter_function(Some(list), Some(list), 0.0, 0.0)?; - compute_filter_square_distance(&none, &list) + EitherOrBoth::Left(list) | EitherOrBoth::Right(list) => { + list.to_animated_zero()?.compute_squared_distance(list) }, } }).sum() diff --git a/components/style/values/animated/effects.rs b/components/style/values/animated/effects.rs index 3806af7dc1f..b94a336e16a 100644 --- a/components/style/values/animated/effects.rs +++ b/components/style/values/animated/effects.rs @@ -245,7 +245,11 @@ impl ToAnimatedZero for SimpleShadow { #[inline] fn to_animated_zero(&self) -> Result { Ok(SimpleShadow { - color: Some(RGBA::transparent()), + color: if let Some(color) = self.color.as_ref() { + Some(color.to_animated_zero()?) + } else { + None + }, horizontal: self.horizontal.to_animated_zero()?, vertical: self.vertical.to_animated_zero()?, blur: self.blur.to_animated_zero()?,