mirror of
https://github.com/servo/servo.git
synced 2025-08-08 23:15:33 +01:00
Introduce ToAnimatedValue 🎥
This commit is contained in:
parent
522d24d126
commit
9ab0b9b4ac
10 changed files with 354 additions and 219 deletions
|
@ -8,4 +8,83 @@
|
|||
//! computed values and need yet another intermediate representation. This
|
||||
//! module's raison d'être is to ultimately contain all these types.
|
||||
|
||||
use app_units::Au;
|
||||
use values::computed::Angle as ComputedAngle;
|
||||
use values::specified::url::SpecifiedUrl;
|
||||
|
||||
pub mod effects;
|
||||
|
||||
/// Conversion between computed values and intermediate values for animations.
|
||||
///
|
||||
/// Notably, colors are represented as four floats during animations.
|
||||
pub trait ToAnimatedValue {
|
||||
/// The type of the animated value.
|
||||
type AnimatedValue;
|
||||
|
||||
/// Converts this value to an animated value.
|
||||
fn to_animated_value(self) -> Self::AnimatedValue;
|
||||
|
||||
/// Converts back an animated value into a computed value.
|
||||
fn from_animated_value(animated: Self::AnimatedValue) -> Self;
|
||||
}
|
||||
|
||||
impl<T> ToAnimatedValue for Option<T>
|
||||
where
|
||||
T: ToAnimatedValue,
|
||||
{
|
||||
type AnimatedValue = Option<<T as ToAnimatedValue>::AnimatedValue>;
|
||||
|
||||
#[inline]
|
||||
fn to_animated_value(self) -> Self::AnimatedValue {
|
||||
self.map(T::to_animated_value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
|
||||
animated.map(T::from_animated_value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ToAnimatedValue for Vec<T>
|
||||
where
|
||||
T: ToAnimatedValue,
|
||||
{
|
||||
type AnimatedValue = Vec<<T as ToAnimatedValue>::AnimatedValue>;
|
||||
|
||||
#[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 {}
|
||||
|
||||
impl AnimatedValueAsComputed for Au {}
|
||||
impl AnimatedValueAsComputed for ComputedAngle {}
|
||||
impl AnimatedValueAsComputed for SpecifiedUrl {}
|
||||
impl AnimatedValueAsComputed for bool {}
|
||||
impl AnimatedValueAsComputed for f32 {}
|
||||
|
||||
impl<T> ToAnimatedValue for T
|
||||
where
|
||||
T: AnimatedValueAsComputed,
|
||||
{
|
||||
type AnimatedValue = Self;
|
||||
|
||||
#[inline]
|
||||
fn to_animated_value(self) -> Self {
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
|
||||
animated
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue