dioxus_motion/
manager.rs

1use crate::Duration;
2use crate::animations::core::Animatable;
3use crate::keyframes::KeyframeAnimation;
4use crate::motion::Motion;
5use crate::prelude::AnimationConfig;
6use crate::sequence::AnimationSequence;
7
8use dioxus::{
9    prelude::{Readable, Signal, Writable},
10    signals::{ReadableExt, WritableExt},
11};
12
13pub trait AnimationManager<T: Animatable + Send + 'static>: Clone + Copy {
14    fn new(initial: T) -> Self;
15    fn animate_to(&mut self, target: T, config: AnimationConfig);
16    fn animate_sequence(&mut self, sequence: AnimationSequence<T>);
17    fn animate_keyframes(&mut self, animation: KeyframeAnimation<T>);
18    fn update(&mut self, dt: f32) -> bool;
19    fn get_value(&self) -> T;
20    fn is_running(&self) -> bool;
21    fn reset(&mut self);
22    fn stop(&mut self);
23    fn delay(&mut self, duration: Duration);
24}
25
26impl<T: Animatable + Send + 'static> AnimationManager<T> for Signal<Motion<T>> {
27    fn new(initial: T) -> Self {
28        Signal::new(Motion::new(initial))
29    }
30
31    fn animate_to(&mut self, target: T, config: AnimationConfig) {
32        (*self.write()).animate_to(target, config);
33    }
34
35    fn animate_sequence(&mut self, sequence: AnimationSequence<T>) {
36        if let Some(first_step) = sequence.steps().first() {
37            let mut state = self.write();
38            (*state).animate_to(first_step.target, (*first_step.config).clone());
39            state.sequence = Some(sequence.into());
40        }
41    }
42
43    fn animate_keyframes(&mut self, animation: KeyframeAnimation<T>) {
44        (*self.write()).animate_keyframes(animation);
45    }
46
47    fn update(&mut self, dt: f32) -> bool {
48        (*self.write()).update(dt)
49    }
50
51    fn get_value(&self) -> T {
52        (*self.read()).get_value()
53    }
54
55    fn is_running(&self) -> bool {
56        (*self.read()).is_running()
57    }
58
59    fn reset(&mut self) {
60        (*self.write()).reset();
61    }
62
63    #[track_caller]
64    fn stop(&mut self) {
65        (*self.write()).stop();
66    }
67
68    fn delay(&mut self, duration: Duration) {
69        (*self.write()).delay(duration);
70    }
71}