Skip to main content

style/values/computed/
animation.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Computed values for properties related to animations and transitions
6
7use crate::derives::*;
8use crate::typed_om::{KeywordValue, ToTyped, TypedValue};
9use crate::values::computed::{Context, LengthPercentage, Time, ToComputedValue};
10use crate::values::generics::animation as generics;
11use crate::values::specified::animation as specified;
12use crate::values::CSSFloat;
13use std::fmt::{self, Write};
14use style_traits::{CssString, CssWriter, ToCss};
15use thin_vec::ThinVec;
16
17pub use crate::values::specified::animation::{
18    AnimationComposition, AnimationDirection, AnimationFillMode, AnimationName, AnimationPlayState,
19    ScrollAxis, TimelineName, TransitionBehavior, TransitionProperty, ViewTransitionClass,
20    ViewTransitionName,
21};
22
23/// A computed value for the `animation-duration` property.
24pub type AnimationDuration = generics::GenericAnimationDuration<Time>;
25
26impl AnimationDuration {
27    /// Returns the amount of seconds this time represents.
28    #[inline]
29    pub fn seconds(&self) -> CSSFloat {
30        match *self {
31            Self::Auto => 0.0,
32            Self::Time(ref t) => t.seconds(),
33        }
34    }
35}
36
37/// A computed value for the `animation-iteration-count` property.
38#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToResolvedValue, ToShmem)]
39#[repr(C)]
40pub struct AnimationIterationCount(pub f32);
41
42impl ToComputedValue for specified::AnimationIterationCount {
43    type ComputedValue = AnimationIterationCount;
44
45    #[inline]
46    fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
47        AnimationIterationCount(match self {
48            specified::AnimationIterationCount::Number(n) => n.to_computed_value(context).0,
49            specified::AnimationIterationCount::Infinite => f32::INFINITY,
50        })
51    }
52
53    #[inline]
54    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
55        use crate::values::specified::NonNegativeNumber;
56        if computed.0.is_infinite() {
57            specified::AnimationIterationCount::Infinite
58        } else {
59            specified::AnimationIterationCount::Number(NonNegativeNumber::new(computed.0))
60        }
61    }
62}
63
64impl AnimationIterationCount {
65    /// Returns the value `1.0`.
66    #[inline]
67    pub fn one() -> Self {
68        Self(1.0)
69    }
70}
71
72impl ToCss for AnimationIterationCount {
73    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
74    where
75        W: Write,
76    {
77        if self.0.is_infinite() {
78            dest.write_str("infinite")
79        } else {
80            self.0.to_css(dest)
81        }
82    }
83}
84
85impl ToTyped for AnimationIterationCount {
86    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
87        if self.0.is_infinite() {
88            dest.push(TypedValue::Keyword(KeywordValue(CssString::from(
89                "infinite",
90            ))));
91            Ok(())
92        } else {
93            self.0.to_typed(dest)
94        }
95    }
96}
97
98/// A computed value for the `animation-timeline` property.
99pub type AnimationTimeline = generics::GenericAnimationTimeline<LengthPercentage>;
100
101/// A computed value for the `view-timeline-inset` property.
102pub type ViewTimelineInset = generics::GenericViewTimelineInset<LengthPercentage>;
103
104/// A computed value for the `animation-range-start` property.
105pub type AnimationRangeStart = generics::GenericAnimationRangeStart<LengthPercentage>;
106impl AnimationRangeStart {
107    /// The `normal` value.
108    pub fn normal() -> Self {
109        Self(generics::GenericAnimationRangeValue::normal(
110            LengthPercentage::zero_percent(),
111        ))
112    }
113}
114
115/// A computed value for the `animation-range-end` property.
116pub type AnimationRangeEnd = generics::GenericAnimationRangeEnd<LengthPercentage>;
117impl AnimationRangeEnd {
118    /// The `normal` value.
119    pub fn normal() -> Self {
120        Self(generics::GenericAnimationRangeValue::normal(
121            LengthPercentage::hundred_percent(),
122        ))
123    }
124}