mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Introduce #[animation(constant)] for the Animate trait
This allows us to handle fields that should be the same during animations.
This commit is contained in:
parent
e49dbc4dfa
commit
eaf2f1ec33
5 changed files with 59 additions and 52 deletions
|
@ -139,21 +139,6 @@ impl ToAnimatedValue for ComputedTextShadowList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME(nox): This could be derived if we implement Animate for bool.
|
|
||||||
impl Animate for BoxShadow {
|
|
||||||
#[inline]
|
|
||||||
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
|
|
||||||
if self.inset != other.inset {
|
|
||||||
return Err(());
|
|
||||||
}
|
|
||||||
Ok(BoxShadow {
|
|
||||||
base: self.base.animate(&other.base, procedure)?,
|
|
||||||
spread: self.spread.animate(&other.spread, procedure)?,
|
|
||||||
inset: self.inset,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ComputeSquaredDistance for BoxShadow {
|
impl ComputeSquaredDistance for BoxShadow {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
||||||
|
|
|
@ -44,11 +44,18 @@ add_impls_for_keyword_enum!(ShapeBox);
|
||||||
/// A shape source, for some reference box.
|
/// A shape source, for some reference box.
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
#[derive(Clone, Debug, PartialEq, ToComputedValue, ToCss)]
|
#[derive(Animate, Clone, Debug, PartialEq, ToComputedValue, ToCss)]
|
||||||
pub enum ShapeSource<BasicShape, ReferenceBox, Url> {
|
pub enum ShapeSource<BasicShape, ReferenceBox, Url> {
|
||||||
|
#[animation(error)]
|
||||||
Url(Url),
|
Url(Url),
|
||||||
Shape(BasicShape, Option<ReferenceBox>),
|
Shape(
|
||||||
|
BasicShape,
|
||||||
|
#[animation(constant)]
|
||||||
|
Option<ReferenceBox>,
|
||||||
|
),
|
||||||
|
#[animation(error)]
|
||||||
Box(ReferenceBox),
|
Box(ReferenceBox),
|
||||||
|
#[animation(error)]
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,29 +133,6 @@ define_css_keyword_enum!(FillRule:
|
||||||
);
|
);
|
||||||
add_impls_for_keyword_enum!(FillRule);
|
add_impls_for_keyword_enum!(FillRule);
|
||||||
|
|
||||||
// FIXME(nox): This should be derivable, but we need to implement Animate
|
|
||||||
// on the T types.
|
|
||||||
impl<B, T, U> Animate for ShapeSource<B, T, U>
|
|
||||||
where
|
|
||||||
B: Animate,
|
|
||||||
T: Clone + PartialEq,
|
|
||||||
{
|
|
||||||
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
|
|
||||||
match (self, other) {
|
|
||||||
(
|
|
||||||
&ShapeSource::Shape(ref this, ref this_box),
|
|
||||||
&ShapeSource::Shape(ref other, ref other_box),
|
|
||||||
) if this_box == other_box => {
|
|
||||||
Ok(ShapeSource::Shape(
|
|
||||||
this.animate(other, procedure)?,
|
|
||||||
this_box.clone(),
|
|
||||||
))
|
|
||||||
},
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME(nox): Implement ComputeSquaredDistance for T types and stop
|
// FIXME(nox): Implement ComputeSquaredDistance for T types and stop
|
||||||
// using PartialEq here, this will let us derive this impl.
|
// using PartialEq here, this will let us derive this impl.
|
||||||
impl<B, T, U> ComputeSquaredDistance for ShapeSource<B, T, U>
|
impl<B, T, U> ComputeSquaredDistance for ShapeSource<B, T, U>
|
||||||
|
|
|
@ -11,13 +11,14 @@ use values::specified::url::SpecifiedUrl;
|
||||||
|
|
||||||
/// A generic value for a single `box-shadow`.
|
/// A generic value for a single `box-shadow`.
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue)]
|
#[derive(Animate, Clone, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue)]
|
||||||
pub struct BoxShadow<Color, SizeLength, BlurShapeLength, ShapeLength> {
|
pub struct BoxShadow<Color, SizeLength, BlurShapeLength, ShapeLength> {
|
||||||
/// The base shadow.
|
/// The base shadow.
|
||||||
pub base: SimpleShadow<Color, SizeLength, BlurShapeLength>,
|
pub base: SimpleShadow<Color, SizeLength, BlurShapeLength>,
|
||||||
/// The spread radius.
|
/// The spread radius.
|
||||||
pub spread: ShapeLength,
|
pub spread: ShapeLength,
|
||||||
/// Whether this is an inset box shadow.
|
/// Whether this is an inset box shadow.
|
||||||
|
#[animation(constant)]
|
||||||
pub inset: bool,
|
pub inset: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,9 +28,30 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
|
||||||
let mut computations = quote!();
|
let mut computations = quote!();
|
||||||
let iter = result_info.iter().zip(this_info.iter().zip(&other_info));
|
let iter = result_info.iter().zip(this_info.iter().zip(&other_info));
|
||||||
computations.append_all(iter.map(|(result, (this, other))| {
|
computations.append_all(iter.map(|(result, (this, other))| {
|
||||||
where_clause.add_trait_bound(this.field.ty.clone());
|
let field_attrs = cg::parse_field_attrs::<AnimateFieldAttrs>(&result.field);
|
||||||
|
if field_attrs.constant {
|
||||||
|
if cg::is_parameterized(&result.field.ty, where_clause.params) {
|
||||||
|
where_clause.inner.predicates.push(cg::where_predicate(
|
||||||
|
result.field.ty.clone(),
|
||||||
|
&["std", "cmp", "PartialEq"],
|
||||||
|
));
|
||||||
|
where_clause.inner.predicates.push(cg::where_predicate(
|
||||||
|
result.field.ty.clone(),
|
||||||
|
&["std", "clone", "Clone"],
|
||||||
|
));
|
||||||
|
}
|
||||||
quote! {
|
quote! {
|
||||||
let #result = ::values::animated::Animate::animate(#this, #other, procedure)?;
|
if #this != #other {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
let #result = ::std::clone::Clone::clone(#this);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
where_clause.add_trait_bound(result.field.ty.clone());
|
||||||
|
quote! {
|
||||||
|
let #result =
|
||||||
|
::values::animated::Animate::animate(#this, #other, procedure)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
@ -67,3 +88,9 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
|
||||||
pub struct AnimateAttrs {
|
pub struct AnimateAttrs {
|
||||||
pub error: bool,
|
pub error: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, FromField)]
|
||||||
|
#[darling(attributes(animation), default)]
|
||||||
|
pub struct AnimateFieldAttrs {
|
||||||
|
pub constant: bool,
|
||||||
|
}
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use darling::FromVariant;
|
use darling::{FromField, FromVariant};
|
||||||
use quote::{ToTokens, Tokens};
|
use quote::{ToTokens, Tokens};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
use syn::{self, AngleBracketedParameterData, Body, DeriveInput, Ident};
|
use syn::{self, AngleBracketedParameterData, Body, DeriveInput, Field, Ident};
|
||||||
use syn::{ImplGenerics, Path, PathParameters, PathSegment, PolyTraitRef};
|
use syn::{ImplGenerics, Path, PathParameters, PathSegment, PolyTraitRef};
|
||||||
use syn::{QSelf, TraitBoundModifier, Ty, TyGenerics, TyParam, TyParamBound};
|
use syn::{QSelf, TraitBoundModifier, Ty, TyGenerics, TyParam, TyParamBound};
|
||||||
use syn::{Variant, WhereBoundPredicate, WherePredicate};
|
use syn::{Variant, WhereBoundPredicate, WherePredicate};
|
||||||
|
@ -15,15 +15,15 @@ use syn::visit::{self, Visitor};
|
||||||
use synstructure::{self, BindOpts, BindStyle, BindingInfo};
|
use synstructure::{self, BindOpts, BindStyle, BindingInfo};
|
||||||
|
|
||||||
pub struct WhereClause<'input, 'path> {
|
pub struct WhereClause<'input, 'path> {
|
||||||
clause: syn::WhereClause,
|
pub inner: syn::WhereClause,
|
||||||
params: &'input [TyParam],
|
pub params: &'input [TyParam],
|
||||||
trait_path: &'path [&'path str],
|
trait_path: &'path [&'path str],
|
||||||
bounded_types: HashSet<Ty>,
|
bounded_types: HashSet<Ty>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'input, 'path> ToTokens for WhereClause<'input, 'path> {
|
impl<'input, 'path> ToTokens for WhereClause<'input, 'path> {
|
||||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||||
self.clause.to_tokens(tokens);
|
self.inner.to_tokens(tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ impl<'input, 'path> WhereClause<'input, 'path> {
|
||||||
pub fn add_trait_bound(&mut self, ty: Ty) {
|
pub fn add_trait_bound(&mut self, ty: Ty) {
|
||||||
if is_parameterized(&ty, self.params) && !self.bounded_types.contains(&ty) {
|
if is_parameterized(&ty, self.params) && !self.bounded_types.contains(&ty) {
|
||||||
self.bounded_types.insert(ty.clone());
|
self.bounded_types.insert(ty.clone());
|
||||||
self.clause.predicates.push(where_predicate(ty, self.trait_path));
|
self.inner.predicates.push(where_predicate(ty, self.trait_path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,13 +116,23 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_field_attrs<A>(field: &Field) -> A
|
||||||
|
where
|
||||||
|
A: FromField,
|
||||||
|
{
|
||||||
|
match A::from_field(field) {
|
||||||
|
Ok(attrs) => attrs,
|
||||||
|
Err(e) => panic!("failed to parse field attributes: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_variant_attrs<A>(variant: &Variant) -> A
|
pub fn parse_variant_attrs<A>(variant: &Variant) -> A
|
||||||
where
|
where
|
||||||
A: FromVariant,
|
A: FromVariant,
|
||||||
{
|
{
|
||||||
match A::from_variant(variant) {
|
match A::from_variant(variant) {
|
||||||
Ok(attrs) => attrs,
|
Ok(attrs) => attrs,
|
||||||
Err(e) => panic!("failed to parse attributes: {}", e),
|
Err(e) => panic!("failed to parse variant attributes: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +154,7 @@ pub fn trait_parts<'input, 'path>(
|
||||||
) -> (ImplGenerics<'input>, TyGenerics<'input>, WhereClause<'input, 'path>) {
|
) -> (ImplGenerics<'input>, TyGenerics<'input>, WhereClause<'input, 'path>) {
|
||||||
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
||||||
let where_clause = WhereClause {
|
let where_clause = WhereClause {
|
||||||
clause: where_clause.clone(),
|
inner: where_clause.clone(),
|
||||||
params: &input.generics.ty_params,
|
params: &input.generics.ty_params,
|
||||||
trait_path,
|
trait_path,
|
||||||
bounded_types: HashSet::new()
|
bounded_types: HashSet::new()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue