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