style/values/generics/
animation.rs1use crate::values::generics::length::GenericLengthPercentageOrAuto;
8use crate::values::specified::animation::{ScrollAxis, ScrollFunction, TimelineName};
9use crate::Zero;
10use std::fmt::{self, Write};
11use style_traits::{CssWriter, ToCss};
12
13#[derive(
17 Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToComputedValue, ToShmem,
18)]
19#[repr(C, u8)]
20pub enum GenericAnimationDuration<T> {
21 Auto,
23 Time(T),
25}
26
27pub use self::GenericAnimationDuration as AnimationDuration;
28
29impl<T> AnimationDuration<T> {
30 pub fn auto() -> Self {
32 Self::Auto
33 }
34
35 pub fn is_auto(&self) -> bool {
37 matches!(*self, Self::Auto)
38 }
39}
40
41impl<T: Zero> Zero for AnimationDuration<T> {
42 fn zero() -> Self {
43 Self::Time(T::zero())
44 }
45
46 fn is_zero(&self) -> bool {
47 match *self {
48 Self::Time(ref t) => t.is_zero(),
49 _ => false,
50 }
51 }
52}
53
54impl<T: ToCss + Zero> ToCss for AnimationDuration<T> {
55 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
56 where
57 W: Write,
58 {
59 match *self {
60 Self::Auto => {
61 if static_prefs::pref!("layout.css.scroll-driven-animations.enabled") {
62 dest.write_str("auto")
63 } else {
64 Self::Time(T::zero()).to_css(dest)
65 }
66 },
67 Self::Time(ref t) => t.to_css(dest),
68 }
69 }
70}
71
72#[derive(
75 Clone,
76 Debug,
77 MallocSizeOf,
78 PartialEq,
79 SpecifiedValueInfo,
80 ToComputedValue,
81 ToCss,
82 ToResolvedValue,
83 ToShmem,
84)]
85#[css(function = "view")]
86#[repr(C)]
87pub struct GenericViewFunction<LengthPercent> {
88 #[css(skip_if = "ScrollAxis::is_default")]
90 pub axis: ScrollAxis,
91 #[css(skip_if = "GenericViewTimelineInset::is_auto")]
93 #[css(field_bound)]
94 pub inset: GenericViewTimelineInset<LengthPercent>,
95}
96
97pub use self::GenericViewFunction as ViewFunction;
98
99#[derive(
103 Clone,
104 Debug,
105 MallocSizeOf,
106 PartialEq,
107 SpecifiedValueInfo,
108 ToComputedValue,
109 ToCss,
110 ToResolvedValue,
111 ToShmem,
112)]
113#[repr(C, u8)]
114pub enum GenericAnimationTimeline<LengthPercent> {
115 Auto,
117 Timeline(TimelineName),
122 Scroll(ScrollFunction),
125 View(#[css(field_bound)] GenericViewFunction<LengthPercent>),
128}
129
130pub use self::GenericAnimationTimeline as AnimationTimeline;
131
132impl<LengthPercent> AnimationTimeline<LengthPercent> {
133 pub fn auto() -> Self {
135 Self::Auto
136 }
137
138 pub fn is_auto(&self) -> bool {
140 matches!(self, Self::Auto)
141 }
142}
143
144#[derive(
148 Clone,
149 Copy,
150 Debug,
151 MallocSizeOf,
152 PartialEq,
153 SpecifiedValueInfo,
154 ToComputedValue,
155 ToResolvedValue,
156 ToShmem,
157)]
158#[repr(C)]
159pub struct GenericViewTimelineInset<LengthPercent> {
160 pub start: GenericLengthPercentageOrAuto<LengthPercent>,
162 pub end: GenericLengthPercentageOrAuto<LengthPercent>,
164}
165
166pub use self::GenericViewTimelineInset as ViewTimelineInset;
167
168impl<LengthPercent> ViewTimelineInset<LengthPercent> {
169 #[inline]
171 fn is_auto(&self) -> bool {
172 self.start.is_auto() && self.end.is_auto()
173 }
174}
175
176impl<LengthPercent> ToCss for ViewTimelineInset<LengthPercent>
177where
178 LengthPercent: PartialEq + ToCss,
179{
180 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
181 where
182 W: Write,
183 {
184 self.start.to_css(dest)?;
185 if self.end != self.start {
186 dest.write_char(' ')?;
187 self.end.to_css(dest)?;
188 }
189 Ok(())
190 }
191}
192
193impl<LengthPercent> Default for ViewTimelineInset<LengthPercent> {
194 fn default() -> Self {
195 Self {
196 start: GenericLengthPercentageOrAuto::auto(),
197 end: GenericLengthPercentageOrAuto::auto(),
198 }
199 }
200}