1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use dioxus_core::{prelude::spawn, use_hook};
use dioxus_signals::{Readable, Signal, Writable};
use tokio::time::Instant;
use uuid::Uuid;

use crate::{use_platform, Animation, UsePlatform};

/// Manage the lifecyle of an [Animation].
#[derive(Clone)]
pub struct AnimationManager {
    init_value: f64,
    current_animation_id: Signal<Option<Uuid>>,
    value: Signal<f64>,
    platform: UsePlatform,
}

impl AnimationManager {
    /// Start the given [Animation].
    pub fn start(&mut self, mut anim: Animation) {
        let new_id = Uuid::new_v4();

        // Set as current this new animation
        self.current_animation_id.set(Some(new_id));

        let platform = self.platform.clone();
        let mut ticker = platform.new_ticker();
        let mut value = self.value;
        let mut current_animation_id = self.current_animation_id;

        // Spawn the animation that will run at 1ms speed
        spawn(async move {
            platform.request_animation_frame();

            let mut index = 0;
            let mut prev_frame = Instant::now();

            loop {
                // Wait for the event loop to tick
                ticker.tick().await;
                platform.request_animation_frame();

                // Stop running the animation if it was removed
                if *current_animation_id.peek() == Some(new_id) {
                    // Remove the current animation if it has finished
                    if anim.is_finished() {
                        current_animation_id.set(None);
                        break;
                    }

                    index += prev_frame.elapsed().as_millis() as i32;
                    value.set(anim.move_value(index));

                    prev_frame = Instant::now();
                } else {
                    break;
                }
            }
        });
    }

    /// Clear the currently running [Animation].
    pub fn clear(&mut self) {
        self.current_animation_id.set(None);
        self.set_value(self.init_value);
    }

    /// Check whether there is an [Animation] running or not.
    pub fn is_animating(&self) -> bool {
        self.current_animation_id.read().is_some()
    }

    /// Get the current value of the [Animation].
    pub fn value(&self) -> f64 {
        *self.value.read()
    }

    /// Get the current value of the [Animation], silently.
    pub fn peek_value(&self) -> f64 {
        *self.value.peek()
    }

    /// Set a new value for the [Animation].
    pub fn set_value(&mut self, new_value: f64) {
        self.value.set(new_value);
    }
}

/// Run animations.
///
/// ## Usage
/// ```rust,no_run
/// # use freya::prelude::*;
/// fn app() -> Element {
///     let mut animation = use_animation(|| 0.0);
///
///     let progress = animation.value();
///
///     use_hook(move || {
///         animation.start(Animation::new_linear(0.0..=100.0, 50));
///     });
///
///     rsx!(
///         rect {
///             width: "{progress}",
///         }
///     )
/// }
/// ```
///
pub fn use_animation(init_value: impl FnOnce() -> f64) -> AnimationManager {
    use_hook(|| {
        let value = init_value();
        AnimationManager {
            current_animation_id: Signal::new(None),
            value: Signal::new(value),
            init_value: value,
            platform: use_platform(),
        }
    })
}

#[cfg(test)]
mod test {
    use std::time::Duration;

    use crate::{use_animation, Animation};
    use dioxus_core::use_hook;
    use dioxus_hooks::to_owned;
    use freya::prelude::*;
    use freya_testing::{events::pointer::MouseButton, launch_test, FreyaEvent};
    use tokio::time::sleep;

    #[tokio::test]
    pub async fn track_progress() {
        fn use_animation_app() -> Element {
            let mut animation = use_animation(|| 0.0);

            let progress = animation.value();

            let _ = use_memo(move || {
                animation.start(Animation::new_linear(0.0..=100.0, 50));
            });

            rsx!(rect {
                width: "{progress}",
            })
        }

        let mut utils = launch_test(use_animation_app);

        // Disable event loop ticker
        utils.config().enable_ticker(false);

        // Initial state
        utils.wait_for_update().await;

        assert_eq!(utils.root().get(0).layout().unwrap().width(), 0.0);

        // State somewhere in the middle
        sleep(Duration::from_millis(15)).await;
        utils.wait_for_update().await;

        let width = utils.root().get(0).layout().unwrap().width();
        assert!(width > 0.0);

        // Enable event loop ticker
        utils.config().enable_ticker(true);

        // Already finished
        sleep(Duration::from_millis(50)).await;

        // State in the end
        utils.wait_for_update().await;

        let width = utils.root().get(0).layout().unwrap().width();
        assert_eq!(width, 100.0);
    }

    #[tokio::test]
    pub async fn restart_progress() {
        fn use_animation_app() -> Element {
            let mut animation = use_animation(|| 10.0);

            let progress = animation.value();

            let mut restart = {
                to_owned![animation];
                move || {
                    animation.clear();
                }
            };

            use_hook(|| {
                animation.start(Animation::new_linear(10.0..=100.0, 50));
            });

            rsx!(rect {
                background: "white",
                height: "100%",
                onclick: move |_| restart(),
                width: "{progress}",
            })
        }

        let mut utils = launch_test(use_animation_app);

        // Disable event loop ticker
        utils.config().enable_ticker(false);

        // Initial state
        utils.wait_for_update().await;

        assert_eq!(utils.root().get(0).layout().unwrap().width(), 10.0);

        // State somewhere in the middle
        sleep(Duration::from_millis(32)).await;
        utils.wait_for_update().await;

        let width = utils.root().get(0).layout().unwrap().width();
        assert!(width > 10.0);

        // Trigger the click event to restart the animation
        utils.push_event(FreyaEvent::Mouse {
            name: "click".to_string(),
            cursor: (5.0, 5.0).into(),
            button: Some(MouseButton::Left),
        });

        // Enable event loop ticker
        utils.config().enable_ticker(true);

        // Already finished
        sleep(Duration::from_millis(50)).await;

        // State has been restarted
        utils.wait_for_update().await;
        utils.wait_for_update().await;

        let width = utils.root().get(0).layout().unwrap().width();
        assert_eq!(width, 10.0);
    }

    #[test]
    pub fn animation_mode_settings() {
        let anim = Animation::new_sine_in_out(7.0..=99.0, 500);
        assert_eq!(anim.duration(), 500);
        assert_eq!(anim.initial_value(), 7.0);
        assert_eq!(anim.final_value(), 99.0);
    }
}