1use std::cell::Cell;
12
13use sui_animation::{AnimationSpec, Interpolate, MotionValue};
14use sui_core::{InvalidationKind, MotionPolicy, MotionPreference};
15
16#[derive(Debug, Clone, Copy, PartialEq)]
17struct MotionSettings {
18 system: MotionPreference,
19 app: Option<MotionPreference>,
20 time_scale: f32,
21}
22
23impl MotionSettings {
24 const DEFAULT: Self = Self {
25 system: MotionPreference::Full,
26 app: None,
27 time_scale: 1.0,
28 };
29}
30
31thread_local! {
32 static MOTION_SETTINGS: Cell<MotionSettings> = const { Cell::new(MotionSettings::DEFAULT) };
33}
34
35fn update(change: impl FnOnce(&mut MotionSettings)) {
36 MOTION_SETTINGS.with(|settings| {
37 let mut next = settings.get();
38 change(&mut next);
39 settings.set(next);
40 });
41}
42
43fn settings() -> MotionSettings {
44 MOTION_SETTINGS.with(Cell::get)
45}
46
47pub fn motion_policy() -> MotionPolicy {
50 let settings = settings();
51 MotionPolicy::new(settings.app.unwrap_or(settings.system)).with_time_scale(settings.time_scale)
52}
53
54pub fn system_motion_preference() -> MotionPreference {
56 settings().system
57}
58
59pub fn set_system_motion_preference(preference: MotionPreference) {
62 update(|settings| settings.system = preference);
63}
64
65pub fn app_motion_preference() -> Option<MotionPreference> {
67 settings().app
68}
69
70pub fn set_app_motion_preference(preference: Option<MotionPreference>) {
73 update(|settings| settings.app = preference);
74}
75
76pub fn motion_time_scale() -> f32 {
78 settings().time_scale
79}
80
81pub fn set_motion_time_scale(time_scale: f32) {
84 let time_scale = MotionPolicy::FULL.with_time_scale(time_scale).time_scale();
85 update(|settings| settings.time_scale = time_scale);
86}
87
88pub fn reset_motion_settings() {
91 MOTION_SETTINGS.with(|settings| settings.set(MotionSettings::DEFAULT));
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
96pub enum FramePacing {
97 #[default]
100 Timer,
101 Display,
105}
106
107pub trait FrameClock {
110 fn frame_time(&self) -> f64;
111}
112
113impl FrameClock for f64 {
114 fn frame_time(&self) -> f64 {
115 *self
116 }
117}
118
119pub trait AnimateCtx: FrameClock {
122 fn animate_after<T>(
124 &mut self,
125 motion: &mut Motion<T>,
126 target: T,
127 delay: f64,
128 spec: AnimationSpec,
129 ) -> bool
130 where
131 T: Interpolate + Copy + PartialEq;
132
133 fn animate<T>(&mut self, motion: &mut Motion<T>, target: T, spec: AnimationSpec) -> bool
135 where
136 T: Interpolate + Copy + PartialEq,
137 {
138 self.animate_after(motion, target, 0.0, spec)
139 }
140}
141
142#[derive(Debug, Clone, Copy, PartialEq)]
153pub struct Motion<T> {
154 value: MotionValue<T>,
155 invalidation: InvalidationKind,
156 movement: bool,
157}
158
159impl<T: Copy> Motion<T> {
160 pub const fn new(value: T) -> Self {
161 Self {
162 value: MotionValue::new(value),
163 invalidation: InvalidationKind::Paint,
164 movement: false,
165 }
166 }
167
168 pub const fn invalidating(mut self, kind: InvalidationKind) -> Self {
172 self.invalidation = kind;
173 self
174 }
175
176 pub const fn movement(mut self) -> Self {
179 self.movement = true;
180 self
181 }
182
183 pub const fn invalidation(&self) -> InvalidationKind {
184 self.invalidation
185 }
186
187 pub const fn is_movement(&self) -> bool {
188 self.movement
189 }
190
191 pub fn target(&self) -> T {
193 self.value.target()
194 }
195
196 pub fn jump_to(&mut self, value: T) {
198 self.value.jump_to(value);
199 }
200}
201
202impl<T> Motion<T>
203where
204 T: Interpolate + Copy + PartialEq,
205{
206 pub fn get(&self, clock: &impl FrameClock) -> T {
208 self.at(clock.frame_time())
209 }
210
211 pub fn at(&self, time: f64) -> T {
213 self.value.value(time)
214 }
215
216 pub fn is_animating(&self, clock: &impl FrameClock) -> bool {
218 self.value.is_animating_at(clock.frame_time())
219 }
220
221 pub fn end_time(&self) -> Option<f64> {
223 self.value.end_time()
224 }
225
226 pub fn start(&mut self, target: T, time: f64, spec: AnimationSpec) -> Option<f64> {
230 self.start_after(target, time, 0.0, spec)
231 }
232
233 pub fn start_after(
237 &mut self,
238 target: T,
239 time: f64,
240 delay: f64,
241 spec: AnimationSpec,
242 ) -> Option<f64> {
243 let policy = motion_policy();
244 let allowed = if self.movement {
245 policy.allows_movement()
246 } else {
247 policy.allows_motion()
248 };
249 let spec = if self.movement {
250 spec.with_movement_policy(policy)
251 } else {
252 spec.with_policy(policy)
253 };
254 let delay = if allowed {
255 delay / f64::from(policy.time_scale())
256 } else {
257 0.0
258 };
259 self.value.advance(time);
261 if self.value.animate_to_after(target, time, delay, spec) {
262 self.value.end_time()
263 } else {
264 None
265 }
266 }
267}
268
269#[cfg(test)]
270mod tests {
271 use super::*;
272
273 #[test]
274 fn app_preference_overrides_the_system_until_cleared() {
275 reset_motion_settings();
276 set_system_motion_preference(MotionPreference::Reduced);
277 assert_eq!(motion_policy().preference, MotionPreference::Reduced);
278
279 set_app_motion_preference(Some(MotionPreference::Full));
280 assert_eq!(motion_policy().preference, MotionPreference::Full);
281 assert_eq!(system_motion_preference(), MotionPreference::Reduced);
282
283 set_app_motion_preference(None);
284 assert_eq!(motion_policy().preference, MotionPreference::Reduced);
285 reset_motion_settings();
286 }
287
288 #[test]
289 fn motion_reads_its_value_at_the_frame_time() {
290 reset_motion_settings();
291 let mut motion = Motion::new(0.0_f32);
292 let until = motion
293 .start(
294 1.0,
295 10.0,
296 AnimationSpec::tween(0.2, sui_animation::Easing::Linear),
297 )
298 .expect("a transition starts");
299
300 assert!((until - 10.2).abs() < 1e-9);
301 assert!((motion.get(&10.1) - 0.5).abs() < 1e-5);
302 assert!(motion.is_animating(&10.1));
303 assert_eq!(motion.get(&10.3), 1.0);
304 assert!(!motion.is_animating(&10.3));
305 assert_eq!(motion.target(), 1.0);
306 }
307
308 #[test]
309 fn motion_follows_the_policy_for_movement() {
310 reset_motion_settings();
311 set_app_motion_preference(Some(MotionPreference::Reduced));
312 let spec = AnimationSpec::tween(0.2, sui_animation::Easing::Linear);
313
314 let mut fade = Motion::new(0.0_f32);
315 assert!(fade.start(1.0, 0.0, spec).is_some());
316 let mut slide = Motion::new(0.0_f32).movement();
317 assert!(slide.start(1.0, 0.0, spec).is_none());
318 assert_eq!(slide.get(&0.0), 1.0);
319 reset_motion_settings();
320 }
321
322 #[test]
323 fn delays_follow_the_time_scale_and_vanish_without_motion() {
324 reset_motion_settings();
325 let spec = AnimationSpec::tween(0.2, sui_animation::Easing::Linear);
326 set_motion_time_scale(0.5);
327 let mut slow = Motion::new(0.0_f32);
328 let until = slow
329 .start_after(1.0, 0.0, 0.1, spec)
330 .expect("a transition starts");
331 assert!(
332 (until - 0.6).abs() < 1e-9,
333 "delay and duration both double: {until}"
334 );
335 assert_eq!(slow.get(&0.15), 0.0);
336
337 reset_motion_settings();
338 set_app_motion_preference(Some(MotionPreference::Off));
339 let mut off = Motion::new(0.0_f32);
340 assert!(off.start_after(1.0, 0.0, 0.1, spec).is_none());
341 assert_eq!(off.get(&0.0), 1.0);
342 reset_motion_settings();
343 }
344
345 #[test]
346 fn time_scale_is_kept_positive() {
347 reset_motion_settings();
348 set_motion_time_scale(0.25);
349 assert_eq!(motion_policy().time_scale, 0.25);
350
351 set_motion_time_scale(-1.0);
352 assert_eq!(motion_time_scale(), MotionPolicy::MIN_TIME_SCALE);
353 reset_motion_settings();
354 assert_eq!(motion_policy(), MotionPolicy::FULL);
355 }
356}