dioxus-motion 0.3.5

Animations library for Dioxus.
Documentation
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! `AnimationSequence<T>` - Optimized animation step sequences

use crate::animations::core::Animatable;
use crate::prelude::AnimationConfig;

use std::sync::Mutex;
use std::sync::{Arc, MutexGuard};

#[derive(Clone)]
pub struct AnimationStep<T: Animatable> {
    pub target: T,
    pub config: Arc<AnimationConfig>,
    pub predicted_next: Option<T>,
}

struct SequenceState {
    current_step: u8,
    #[allow(clippy::type_complexity)]
    on_complete: Option<Box<dyn FnOnce() + Send>>,
}

/// Animation sequence that keeps step data simple and stores only the mutable
/// execution state behind a mutex for shared access.
pub struct AnimationSequence<T: Animatable> {
    steps: Vec<AnimationStep<T>>,
    state: Mutex<SequenceState>,
}

impl<T: Animatable> AnimationSequence<T> {
    fn lock_state(&self) -> MutexGuard<'_, SequenceState> {
        match self.state.lock() {
            Ok(state) => state,
            Err(poisoned) => poisoned.into_inner(),
        }
    }

    /// Creates a new empty animation sequence
    pub fn new() -> Self {
        Self {
            steps: Vec::new(),
            state: Mutex::new(SequenceState {
                current_step: 0,
                on_complete: None,
            }),
        }
    }

    /// Creates a new animation sequence with specified capacity hint.
    pub fn with_capacity(capacity: u8) -> Self {
        Self {
            steps: Vec::with_capacity(capacity as usize),
            state: Mutex::new(SequenceState {
                current_step: 0,
                on_complete: None,
            }),
        }
    }

    /// Creates a new animation sequence from a vector of steps
    pub fn from_steps(steps: Vec<AnimationStep<T>>) -> Self {
        Self {
            steps,
            state: Mutex::new(SequenceState {
                current_step: 0,
                on_complete: None,
            }),
        }
    }

    /// Creates a new animation sequence with a completion callback
    pub fn with_on_complete<F>(steps: Vec<AnimationStep<T>>, on_complete: F) -> Self
    where
        F: FnOnce() + Send + 'static,
    {
        Self {
            steps,
            state: Mutex::new(SequenceState {
                current_step: 0,
                on_complete: Some(Box::new(on_complete)),
            }),
        }
    }

    /// Reserve additional capacity for future steps.
    pub fn reserve(&mut self, additional: u8) {
        self.steps.reserve(additional as usize);
    }

    /// Adds a new step to the sequence and returns a new sequence
    pub fn then(mut self, target: T, config: AnimationConfig) -> Self {
        let predicted_next = if self.steps.is_empty() {
            None
        } else {
            self.steps
                .last()
                .map(|last_step| last_step.target.interpolate(&target, 0.5))
        };

        let new_step = AnimationStep {
            target,
            config: Arc::new(config),
            predicted_next,
        };

        self.steps.push(new_step);
        self
    }

    /// Sets a completion callback
    pub fn on_complete<F: FnOnce() + Send + 'static>(self, f: F) -> Self {
        let mut state = self.lock_state();
        state.on_complete = Some(Box::new(f));
        drop(state);
        self
    }

    /// Advances to the next step in the sequence
    /// Returns true if advanced, false if already at the end
    pub fn advance_step(&self) -> bool {
        let mut state = self.lock_state();
        let current = state.current_step;
        let total_steps = self.steps.len() as u8;

        if current < total_steps.saturating_sub(1) {
            state.current_step += 1;
            true
        } else {
            false
        }
    }

    /// Gets the current step index
    pub fn current_step_index(&self) -> u8 {
        self.lock_state().current_step
    }

    /// Gets the current step index (kept for backward compatibility)
    pub fn current_step(&self) -> u8 {
        self.current_step_index()
    }

    /// Gets the configuration for the current step
    pub fn current_config(&self) -> Option<&AnimationConfig> {
        let current = self.current_step_index() as usize;
        self.steps.get(current).map(|step| step.config.as_ref())
    }

    /// Gets the target value for the current step
    pub fn current_target(&self) -> Option<T> {
        let current = self.current_step_index() as usize;
        self.steps.get(current).map(|step| step.target)
    }

    /// Gets the current step data
    pub fn current_step_data(&self) -> Option<&AnimationStep<T>> {
        let current = self.current_step_index() as usize;
        self.steps.get(current)
    }

    /// Gets all steps (for backward compatibility)
    pub fn steps(&self) -> &[AnimationStep<T>] {
        &self.steps
    }

    /// Checks if the sequence is complete (at the last step)
    pub fn is_complete(&self) -> bool {
        let current = self.current_step_index();
        let total_steps = self.steps.len() as u8;
        current >= total_steps.saturating_sub(1)
    }

    /// Gets the total number of steps
    pub fn total_steps(&self) -> usize {
        self.steps.len()
    }

    /// Resets the sequence to the first step
    pub fn reset(&self) {
        self.lock_state().current_step = 0;
    }

    /// Executes the completion callback if present
    pub fn execute_completion(&self) {
        if let Some(callback) = self.lock_state().on_complete.take() {
            callback();
        }
    }
}

/// Cloning `AnimationSequence` preserves the queued steps and current_step_index,
/// but resets the inner `SequenceState::on_complete` callback to `None`.
/// Callers that clone an `AnimationSequence` must re-register `on_complete`
/// on the cloned instance when they need completion behavior there too.
impl<T: Animatable> Clone for AnimationSequence<T> {
    fn clone(&self) -> Self {
        let current_step = self.current_step_index();
        Self {
            steps: self.steps.clone(),
            state: Mutex::new(SequenceState {
                current_step,
                on_complete: None,
            }),
        }
    }
}

impl<T: Animatable> Default for AnimationSequence<T> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;
    use crate::animations::core::AnimationMode;
    use crate::animations::spring::Spring;
    use std::sync::{Arc, Mutex};

    #[test]
    fn test_animation_sequence_basic() {
        let steps = vec![
            AnimationStep {
                target: 10.0f32,
                config: Arc::new(AnimationConfig::new(AnimationMode::Spring(
                    Spring::default(),
                ))),
                predicted_next: None,
            },
            AnimationStep {
                target: 20.0f32,
                config: Arc::new(AnimationConfig::new(AnimationMode::Spring(
                    Spring::default(),
                ))),
                predicted_next: None,
            },
            AnimationStep {
                target: 30.0f32,
                config: Arc::new(AnimationConfig::new(AnimationMode::Spring(
                    Spring::default(),
                ))),
                predicted_next: None,
            },
        ];

        let sequence = AnimationSequence::from_steps(steps);

        // Test initial state
        assert_eq!(sequence.current_step_index(), 0);
        assert_eq!(sequence.current_target().unwrap(), 10.0f32);
        assert!(!sequence.is_complete());
        assert_eq!(sequence.total_steps(), 3);

        // Test advancing steps
        assert!(sequence.advance_step());
        assert_eq!(sequence.current_step_index(), 1);
        assert_eq!(sequence.current_target().unwrap(), 20.0f32);
        assert!(!sequence.is_complete());

        assert!(sequence.advance_step());
        assert_eq!(sequence.current_step_index(), 2);
        assert_eq!(sequence.current_target().unwrap(), 30.0f32);
        assert!(sequence.is_complete());

        // Test can't advance past end
        assert!(!sequence.advance_step());
        assert_eq!(sequence.current_step_index(), 2);

        // Test reset
        sequence.reset();
        assert_eq!(sequence.current_step_index(), 0);
        assert!(!sequence.is_complete());
    }

    #[test]
    fn test_animation_sequence_builder_pattern() {
        let sequence = AnimationSequence::new()
            .then(
                10.0f32,
                AnimationConfig::new(AnimationMode::Spring(Spring::default())),
            )
            .then(
                20.0f32,
                AnimationConfig::new(AnimationMode::Spring(Spring::default())),
            )
            .then(
                30.0f32,
                AnimationConfig::new(AnimationMode::Spring(Spring::default())),
            );

        assert_eq!(sequence.total_steps(), 3);
        assert_eq!(sequence.current_target().unwrap(), 10.0f32);
        assert!(!sequence.is_complete());

        assert!(sequence.advance_step());
        assert_eq!(sequence.current_target().unwrap(), 20.0f32);

        assert!(sequence.advance_step());
        assert_eq!(sequence.current_target().unwrap(), 30.0f32);
        assert!(sequence.is_complete());
    }

    #[test]
    fn test_animation_sequence_with_callback() {
        let callback_executed = Arc::new(Mutex::new(false));
        let callback_executed_clone = callback_executed.clone();

        let steps = vec![AnimationStep {
            target: 10.0f32,
            config: Arc::new(AnimationConfig::new(AnimationMode::Spring(
                Spring::default(),
            ))),
            predicted_next: None,
        }];

        let sequence = AnimationSequence::with_on_complete(steps, move || {
            *callback_executed_clone.lock().unwrap() = true;
        });

        // Execute completion callback
        sequence.execute_completion();

        assert!(*callback_executed.lock().unwrap());
    }

    #[test]
    fn test_animation_sequence_callback_with_shared_references() {
        let callback_executed = Arc::new(Mutex::new(false));
        let callback_executed_clone = callback_executed.clone();

        let steps = vec![AnimationStep {
            target: 10.0f32,
            config: Arc::new(AnimationConfig::new(AnimationMode::Spring(
                Spring::default(),
            ))),
            predicted_next: None,
        }];

        let sequence = AnimationSequence::with_on_complete(steps, move || {
            *callback_executed_clone.lock().unwrap() = true;
        });

        // Create multiple Arc references to the sequence
        let sequence_arc1 = Arc::new(sequence);
        let sequence_arc2 = sequence_arc1.clone();
        let sequence_arc3 = sequence_arc1.clone();

        // Verify that Arc::try_unwrap would fail (multiple references exist)
        assert!(Arc::try_unwrap(sequence_arc1.clone()).is_err());

        // Execute completion callback through one of the references
        // This should work even though we can't get ownership
        sequence_arc1.execute_completion();

        // Verify the callback was executed
        assert!(*callback_executed.lock().unwrap());

        // Verify that other references still exist and are valid
        assert_eq!(sequence_arc2.current_step_index(), 0);
        assert_eq!(sequence_arc3.current_step_index(), 0);
    }

    #[test]
    fn test_animation_sequence_clone() {
        let steps = vec![AnimationStep {
            target: 10.0f32,
            config: Arc::new(AnimationConfig::new(AnimationMode::Spring(
                Spring::default(),
            ))),
            predicted_next: None,
        }];

        let sequence1 = AnimationSequence::from_steps(steps);
        sequence1.advance_step(); // This won't work since there's only one step, but let's test the clone

        let sequence2 = sequence1.clone();

        // Both sequences should have the same step data but independent counters
        assert_eq!(
            sequence1.current_step_index(),
            sequence2.current_step_index()
        );
        assert_eq!(sequence1.total_steps(), sequence2.total_steps());
        assert_eq!(sequence1.current_target(), sequence2.current_target());
    }

    #[test]
    fn test_animation_sequence_backward_compatibility() {
        // Test that the old API still works
        let sequence = AnimationSequence::new();
        let sequence = sequence.then(
            10.0f32,
            AnimationConfig::new(AnimationMode::Spring(Spring::default())),
        );
        let sequence = sequence.then(
            20.0f32,
            AnimationConfig::new(AnimationMode::Spring(Spring::default())),
        );

        // Test old method names
        assert_eq!(sequence.current_step(), 0);
        assert_eq!(sequence.steps().len(), 2);

        // Test with_capacity (should work but be a no-op)
        let _sequence_with_capacity = AnimationSequence::<f32>::with_capacity(10);

        // Test reserve (should work but be a no-op)
        let mut sequence_mut = sequence.clone();
        sequence_mut.reserve(5);
    }
}