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