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