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::values::computed::{Context, LengthPercentage, Time, ToComputedValue};
8use crate::values::generics::animation as generics;
9use crate::values::specified::animation as specified;
10use crate::values::CSSFloat;
11use std::fmt::{self, Write};
12use style_traits::{CssWriter, ToCss};
13
14pub use crate::values::specified::animation::{
15    AnimationComposition, AnimationDirection, AnimationFillMode, AnimationName, AnimationPlayState,
16    ScrollAxis, TimelineName, TransitionBehavior, TransitionProperty, ViewTransitionClass,
17    ViewTransitionName,
18};
19
20/// A computed value for the `animation-duration` property.
21pub type AnimationDuration = generics::GenericAnimationDuration<Time>;
22
23impl AnimationDuration {
24    /// Returns the amount of seconds this time represents.
25    #[inline]
26    pub fn seconds(&self) -> CSSFloat {
27        match *self {
28            Self::Auto => 0.0,
29            Self::Time(ref t) => t.seconds(),
30        }
31    }
32}
33
34/// A computed value for the `animation-iteration-count` property.
35#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToResolvedValue, ToShmem)]
36#[repr(C)]
37pub struct AnimationIterationCount(pub f32);
38
39impl ToComputedValue for specified::AnimationIterationCount {
40    type ComputedValue = AnimationIterationCount;
41
42    #[inline]
43    fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
44        AnimationIterationCount(match *self {
45            specified::AnimationIterationCount::Number(n) => n.to_computed_value(context).0,
46            specified::AnimationIterationCount::Infinite => f32::INFINITY,
47        })
48    }
49
50    #[inline]
51    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
52        use crate::values::specified::NonNegativeNumber;
53        if computed.0.is_infinite() {
54            specified::AnimationIterationCount::Infinite
55        } else {
56            specified::AnimationIterationCount::Number(NonNegativeNumber::new(computed.0))
57        }
58    }
59}
60
61impl AnimationIterationCount {
62    /// Returns the value `1.0`.
63    #[inline]
64    pub fn one() -> Self {
65        Self(1.0)
66    }
67}
68
69impl ToCss for AnimationIterationCount {
70    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
71    where
72        W: Write,
73    {
74        if self.0.is_infinite() {
75            dest.write_str("infinite")
76        } else {
77            self.0.to_css(dest)
78        }
79    }
80}
81
82/// A computed value for the `animation-timeline` property.
83pub type AnimationTimeline = generics::GenericAnimationTimeline<LengthPercentage>;
84
85/// A computed value for the `view-timeline-inset` property.
86pub type ViewTimelineInset = generics::GenericViewTimelineInset<LengthPercentage>;