style/values/computed/
animation.rs1use 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
22pub type AnimationDuration = generics::GenericAnimationDuration<Time>;
24
25impl AnimationDuration {
26 #[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#[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 #[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
97pub type AnimationTimeline = generics::GenericAnimationTimeline<LengthPercentage>;
99
100pub type ViewTimelineInset = generics::GenericViewTimelineInset<LengthPercentage>;
102
103pub type AnimationRangeStart = generics::GenericAnimationRangeStart<LengthPercentage>;
105impl AnimationRangeStart {
106 pub fn normal() -> Self {
108 Self(generics::GenericAnimationRangeValue::normal(
109 LengthPercentage::zero_percent(),
110 ))
111 }
112}
113
114pub type AnimationRangeEnd = generics::GenericAnimationRangeEnd<LengthPercentage>;
116impl AnimationRangeEnd {
117 pub fn normal() -> Self {
119 Self(generics::GenericAnimationRangeValue::normal(
120 LengthPercentage::hundred_percent(),
121 ))
122 }
123}