leptos-motion-layout 0.8.2

Layout animations and FLIP transitions for Leptos Motion
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Comprehensive tests for FLIP layout animations
//!
//! This module tests FLIP (First, Last, Invert, Play) animations,
//! layout transitions, and element positioning using a TDD approach.

use leptos_motion_core::*;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

/// Test FLIP animation initialization
#[wasm_bindgen_test]
fn test_flip_animation_initialization() {
    // Test that FLIP system can be initialized
    let flip_system = FlipSystem::new();
    assert!(flip_system.is_ok());
}

/// Test element position recording
#[wasm_bindgen_test]
fn test_element_position_recording() {
    // Test position recording
    let position = ElementPosition {
        x: 100.0,
        y: 200.0,
        width: 300.0,
        height: 150.0,
        rotation: 0.0,
        scale_x: 1.0,
        scale_y: 1.0,
    };

    assert_eq!(position.x, 100.0);
    assert_eq!(position.y, 200.0);
    assert_eq!(position.width, 300.0);
    assert_eq!(position.height, 150.0);
    assert_eq!(position.rotation, 0.0);
    assert_eq!(position.scale_x, 1.0);
    assert_eq!(position.scale_y, 1.0);
}

/// Test FLIP animation calculation
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_flip_animation_calculation() {
    // Test FLIP calculation
    let first_position = ElementPosition {
        x: 0.0,
        y: 0.0,
        width: 100.0,
        height: 100.0,
        rotation: 0.0,
        scale_x: 1.0,
        scale_y: 1.0,
    };

    let last_position = ElementPosition {
        x: 200.0,
        y: 300.0,
        width: 150.0,
        height: 150.0,
        rotation: 45.0,
        scale_x: 1.5,
        scale_y: 1.5,
    };

    let flip_animation = calculate_flip_animation(&first_position, &last_position);

    assert_eq!(flip_animation.translate_x, 200.0);
    assert_eq!(flip_animation.translate_y, 300.0);
    assert_eq!(flip_animation.scale_x, 1.5);
    assert_eq!(flip_animation.scale_y, 1.5);
    assert_eq!(flip_animation.rotate, 45.0);
}

/// Test layout transition detection
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_transition_detection() {
    // Test layout transition detection
    let old_layout = LayoutState {
        elements: vec![
            ElementLayout {
                id: "item1".to_string(),
                position: ElementPosition {
                    x: 0.0,
                    y: 0.0,
                    width: 100.0,
                    height: 100.0,
                    rotation: 0.0,
                    scale_x: 1.0,
                    scale_y: 1.0,
                },
            },
            ElementLayout {
                id: "item2".to_string(),
                position: ElementPosition {
                    x: 100.0,
                    y: 0.0,
                    width: 100.0,
                    height: 100.0,
                    rotation: 0.0,
                    scale_x: 1.0,
                    scale_y: 1.0,
                },
            },
        ],
    };

    let new_layout = LayoutState {
        elements: vec![
            ElementLayout {
                id: "item1".to_string(),
                position: ElementPosition {
                    x: 0.0,
                    y: 100.0,
                    width: 100.0,
                    height: 100.0,
                    rotation: 0.0,
                    scale_x: 1.0,
                    scale_y: 1.0,
                },
            },
            ElementLayout {
                id: "item2".to_string(),
                position: ElementPosition {
                    x: 0.0,
                    y: 0.0,
                    width: 100.0,
                    height: 100.0,
                    rotation: 0.0,
                    scale_x: 1.0,
                    scale_y: 1.0,
                },
            },
        ],
    };

    let transitions = detect_layout_transitions(&old_layout, &new_layout);
    assert_eq!(transitions.len(), 2);

    // Check item1 transition
    let item1_transition = transitions
        .iter()
        .find(|t| t.element_id == "item1")
        .unwrap();
    assert_eq!(item1_transition.translate_y, 100.0);

    // Check item2 transition
    let item2_transition = transitions
        .iter()
        .find(|t| t.element_id == "item2")
        .unwrap();
    assert_eq!(item2_transition.translate_x, -100.0);
}

/// Test staggered animation timing
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_staggered_animation_timing() {
    // Test staggered timing calculation
    let transitions = vec![
        LayoutTransition {
            element_id: "item1".to_string(),
            translate_x: 0.0,
            translate_y: 100.0,
            scale_x: 1.0,
            scale_y: 1.0,
            rotate: 0.0,
            duration: 300.0,
            delay: 0.0,
        },
        LayoutTransition {
            element_id: "item2".to_string(),
            translate_x: -100.0,
            translate_y: 0.0,
            scale_x: 1.0,
            scale_y: 1.0,
            rotate: 0.0,
            duration: 300.0,
            delay: 0.0,
        },
        LayoutTransition {
            element_id: "item3".to_string(),
            translate_x: 0.0,
            translate_y: -100.0,
            scale_x: 1.0,
            scale_y: 1.0,
            rotate: 0.0,
            duration: 300.0,
            delay: 0.0,
        },
    ];

    let staggered_transitions = apply_stagger(transitions, 50.0);

    assert_eq!(staggered_transitions[0].delay, 0.0);
    assert_eq!(staggered_transitions[1].delay, 50.0);
    assert_eq!(staggered_transitions[2].delay, 100.0);
}

/// Test layout animation performance
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_animation_performance() {
    // Test performance with many elements
    let start_time = std::time::Instant::now();

    let mut layout = LayoutState {
        elements: Vec::new(),
    };

    // Create 100 elements
    for i in 0..100 {
        layout.elements.push(ElementLayout {
            id: format!("item{}", i),
            position: ElementPosition {
                x: (i % 10) as f64 * 100.0,
                y: (i / 10) as f64 * 100.0,
                width: 100.0,
                height: 100.0,
                rotation: 0.0,
                scale_x: 1.0,
                scale_y: 1.0,
            },
        });
    }

    // Simulate layout change
    let mut new_layout = layout.clone();
    for element in &mut new_layout.elements {
        element.position.x += 50.0;
        element.position.y += 50.0;
    }

    let transitions = detect_layout_transitions(&layout, &new_layout);

    let duration = start_time.elapsed();
    assert!(duration.as_millis() < 50); // Should process 100 elements in under 50ms
    assert_eq!(transitions.len(), 100);
}

/// Test layout animation easing
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_animation_easing() {
    // Test different easing functions for layout animations
    let easings = vec![
        Easing::Linear,
        Easing::EaseIn,
        Easing::EaseOut,
        Easing::EaseInOut,
        Easing::CircIn,
        Easing::CircOut,
        Easing::CircInOut,
    ];

    for easing in easings {
        let transition = LayoutTransition {
            element_id: "test".to_string(),
            translate_x: 100.0,
            translate_y: 0.0,
            scale_x: 1.0,
            scale_y: 1.0,
            rotate: 0.0,
            duration: 300.0,
            delay: 0.0,
        };

        let eased_transition = apply_easing(transition, easing);
        assert_eq!(eased_transition.element_id, "test");
    }
}

/// Test layout animation cancellation
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_animation_cancellation() {
    // Test animation cancellation
    let mut flip_system = FlipSystem::new().unwrap();

    // Start an animation
    let transition = LayoutTransition {
        element_id: "test".to_string(),
        translate_x: 100.0,
        translate_y: 0.0,
        scale_x: 1.0,
        scale_y: 1.0,
        rotate: 0.0,
        duration: 1000.0,
        delay: 0.0,
    };

    let animation_id = flip_system.start_animation(transition);
    assert!(animation_id.is_ok());

    // Cancel the animation
    let result = flip_system.cancel_animation(animation_id.unwrap());
    assert!(result.is_ok());
}

/// Test layout animation completion
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_animation_completion() {
    // Test animation completion detection
    let mut flip_system = FlipSystem::new().unwrap();

    let transition = LayoutTransition {
        element_id: "test".to_string(),
        translate_x: 100.0,
        translate_y: 0.0,
        scale_x: 1.0,
        scale_y: 1.0,
        rotate: 0.0,
        duration: 100.0, // Short duration for testing
        delay: 0.0,
    };

    let animation_id = flip_system.start_animation(transition).unwrap();

    // Wait for animation to complete (simulated)
    std::thread::sleep(std::time::Duration::from_millis(150));

    let is_complete = flip_system.is_animation_complete(animation_id);
    assert!(is_complete);
}

/// Test layout animation state management
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_animation_state_management() {
    // Test animation state management
    let mut flip_system = FlipSystem::new().unwrap();

    // Test initial state
    assert_eq!(flip_system.active_animations(), 0);

    // Start multiple animations
    for i in 0..5 {
        let transition = LayoutTransition {
            element_id: format!("item{}", i),
            translate_x: 100.0,
            translate_y: 0.0,
            scale_x: 1.0,
            scale_y: 1.0,
            rotate: 0.0,
            duration: 300.0,
            delay: 0.0,
        };

        let _ = flip_system.start_animation(transition);
    }

    assert_eq!(flip_system.active_animations(), 5);

    // Cancel all animations
    flip_system.cancel_all_animations();
    assert_eq!(flip_system.active_animations(), 0);
}

/// Test layout animation interpolation
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_animation_interpolation() {
    // Test interpolation between positions
    let start_position = ElementPosition {
        x: 0.0,
        y: 0.0,
        width: 100.0,
        height: 100.0,
        rotation: 0.0,
        scale_x: 1.0,
        scale_y: 1.0,
    };

    let end_position = ElementPosition {
        x: 100.0,
        y: 100.0,
        width: 200.0,
        height: 200.0,
        rotation: 90.0,
        scale_x: 2.0,
        scale_y: 2.0,
    };

    // Test interpolation at 50% progress
    let interpolated = interpolate_position(&start_position, &end_position, 0.5);

    assert_eq!(interpolated.x, 50.0);
    assert_eq!(interpolated.y, 50.0);
    assert_eq!(interpolated.width, 150.0);
    assert_eq!(interpolated.height, 150.0);
    assert_eq!(interpolated.rotation, 45.0);
    assert_eq!(interpolated.scale_x, 1.5);
    assert_eq!(interpolated.scale_y, 1.5);
}

/// Test layout animation bounds checking
#[wasm_bindgen_test]
#[wasm_bindgen_test]
fn test_layout_animation_bounds_checking() {
    // Test bounds checking for layout animations
    let bounds = LayoutBounds {
        min_x: 0.0,
        max_x: 1000.0,
        min_y: 0.0,
        max_y: 1000.0,
    };

    let position = ElementPosition {
        x: 500.0,
        y: 500.0,
        width: 100.0,
        height: 100.0,
        rotation: 0.0,
        scale_x: 1.0,
        scale_y: 1.0,
    };

    // Test within bounds
    assert!(is_within_layout_bounds(&position, &bounds));

    // Test outside bounds
    let position_outside = ElementPosition {
        x: 1200.0,
        y: 500.0,
        width: 100.0,
        height: 100.0,
        rotation: 0.0,
        scale_x: 1.0,
        scale_y: 1.0,
    };

    assert!(!is_within_layout_bounds(&position_outside, &bounds));
}

// Helper types and functions for testing (these would be implemented in the actual layout system)

#[derive(Debug, Clone, PartialEq)]
pub struct ElementPosition {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
    pub rotation: f64,
    pub scale_x: f64,
    pub scale_y: f64,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ElementLayout {
    pub id: String,
    pub position: ElementPosition,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LayoutState {
    pub elements: Vec<ElementLayout>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LayoutTransition {
    pub element_id: String,
    pub translate_x: f64,
    pub translate_y: f64,
    pub scale_x: f64,
    pub scale_y: f64,
    pub rotate: f64,
    pub duration: f64,
    pub delay: f64,
}

#[derive(Debug, Clone, PartialEq)]
pub struct FlipAnimation {
    pub translate_x: f64,
    pub translate_y: f64,
    pub scale_x: f64,
    pub scale_y: f64,
    pub rotate: f64,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LayoutBounds {
    pub min_x: f64,
    pub max_x: f64,
    pub min_y: f64,
    pub max_y: f64,
}

pub struct FlipSystem {
    active_animations: std::collections::HashMap<u64, LayoutTransition>,
    next_id: u64,
}

impl FlipSystem {
    pub fn new() -> std::result::Result<Self, String> {
        Ok(Self {
            active_animations: std::collections::HashMap::new(),
            next_id: 1,
        })
    }

    pub fn start_animation(
        &mut self,
        transition: LayoutTransition,
    ) -> std::result::Result<u64, String> {
        let id = self.next_id;
        self.next_id += 1;
        self.active_animations.insert(id, transition);
        Ok(id)
    }

    pub fn cancel_animation(&mut self, id: u64) -> std::result::Result<(), String> {
        self.active_animations.remove(&id);
        Ok(())
    }

    pub fn cancel_all_animations(&mut self) {
        self.active_animations.clear();
    }

    pub fn active_animations(&self) -> usize {
        self.active_animations.len()
    }

    pub fn is_animation_complete(&self, _id: u64) -> bool {
        // Simulate animation completion
        true
    }
}

fn calculate_flip_animation(first: &ElementPosition, last: &ElementPosition) -> FlipAnimation {
    FlipAnimation {
        translate_x: last.x - first.x,
        translate_y: last.y - first.y,
        scale_x: last.scale_x / first.scale_x,
        scale_y: last.scale_y / first.scale_y,
        rotate: last.rotation - first.rotation,
    }
}

fn detect_layout_transitions(old: &LayoutState, new: &LayoutState) -> Vec<LayoutTransition> {
    let mut transitions = Vec::new();

    for new_element in &new.elements {
        if let Some(old_element) = old.elements.iter().find(|e| e.id == new_element.id) {
            let dx = new_element.position.x - old_element.position.x;
            let dy = new_element.position.y - old_element.position.y;
            let scale_x = new_element.position.scale_x / old_element.position.scale_x;
            let scale_y = new_element.position.scale_y / old_element.position.scale_y;
            let rotate = new_element.position.rotation - old_element.position.rotation;

            if dx != 0.0 || dy != 0.0 || scale_x != 1.0 || scale_y != 1.0 || rotate != 0.0 {
                transitions.push(LayoutTransition {
                    element_id: new_element.id.clone(),
                    translate_x: dx,
                    translate_y: dy,
                    scale_x,
                    scale_y,
                    rotate,
                    duration: 300.0,
                    delay: 0.0,
                });
            }
        }
    }

    transitions
}

fn apply_stagger(
    mut transitions: Vec<LayoutTransition>,
    stagger_delay: f64,
) -> Vec<LayoutTransition> {
    for (i, transition) in transitions.iter_mut().enumerate() {
        transition.delay = i as f64 * stagger_delay;
    }
    transitions
}

fn apply_easing(mut transition: LayoutTransition, _easing: Easing) -> LayoutTransition {
    // Simulate easing application
    transition
}

fn interpolate_position(
    start: &ElementPosition,
    end: &ElementPosition,
    progress: f64,
) -> ElementPosition {
    ElementPosition {
        x: start.x + (end.x - start.x) * progress,
        y: start.y + (end.y - start.y) * progress,
        width: start.width + (end.width - start.width) * progress,
        height: start.height + (end.height - start.height) * progress,
        rotation: start.rotation + (end.rotation - start.rotation) * progress,
        scale_x: start.scale_x + (end.scale_x - start.scale_x) * progress,
        scale_y: start.scale_y + (end.scale_y - start.scale_y) * progress,
    }
}

fn is_within_layout_bounds(position: &ElementPosition, bounds: &LayoutBounds) -> bool {
    position.x >= bounds.min_x
        && position.x + position.width <= bounds.max_x
        && position.y >= bounds.min_y
        && position.y + position.height <= bounds.max_y
}