leptos-motion-layout 1.1.0

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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Performance Regression Tests for Layout Operations
//!
//! These tests ensure that layout-related performance characteristics are maintained
//! across code changes and detect performance regressions early in development.
//!
//! The tests establish baseline performance metrics for layout operations and fail
//! if performance degrades beyond acceptable thresholds.

use super::*;
use std::time::Instant;

#[cfg(test)]
mod layout_performance_regression_tests {
    use super::*;

    /// Performance regression test for LayoutInfo creation
    #[test]
    fn test_layout_info_creation_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let _layout_info = LayoutInfo::new(
                (i as f64) * 0.1,
                (i as f64) * 0.1,
                100.0 + (i as f64) * 0.1,
                200.0 + (i as f64) * 0.1,
            );
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should create at least 2,000,000 layout infos per second
        assert!(
            operations_per_second > 2_000_000.0,
            "LayoutInfo creation performance regression: {} ops/sec (expected > 2,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 50ms
        assert!(
            duration.as_millis() < 50,
            "LayoutInfo creation too slow: {}ms for {} operations (expected < 50ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for LayoutAnimationConfig creation
    #[test]
    fn test_layout_animation_config_creation_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let _config = LayoutAnimationConfig::new()
                .with_duration(0.3 + (i as f64) * 0.0001)
                .with_easing(EasingFunction::EaseOut)
                .hardware_accelerated(i % 2 == 0)
                .enabled(true);
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should create at least 1,000,000 configs per second
        assert!(
            operations_per_second > 1_000_000.0,
            "LayoutAnimationConfig creation performance regression: {} ops/sec (expected > 1,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 100ms
        assert!(
            duration.as_millis() < 100,
            "LayoutAnimationConfig creation too slow: {}ms for {} operations (expected < 100ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for FLIPState creation
    #[test]
    fn test_flip_state_creation_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let _flip_state = FLIPState {
                first: web_sys::DomRect::new_with_x_and_y_and_width_and_height(
                    (i as f64) * 0.1,
                    (i as f64) * 0.1,
                    100.0,
                    200.0,
                ).unwrap(),
                last: web_sys::DomRect::new_with_x_and_y_and_width_and_height(
                    (i as f64) * 0.1 + 50.0,
                    (i as f64) * 0.1 + 50.0,
                    100.0,
                    200.0,
                ).unwrap(),
                inverted: TransformValues {
                    translate_x: (i as f64) * 0.1,
                    translate_y: (i as f64) * 0.1,
                    scale_x: 1.0,
                    scale_y: 1.0,
                    rotation: 0.0,
                },
                progress: 0.0,
                active: false,
            };
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should create at least 1,000,000 FLIP states per second
        assert!(
            operations_per_second > 1_000_000.0,
            "FLIPState creation performance regression: {} ops/sec (expected > 1,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 100ms
        assert!(
            duration.as_millis() < 100,
            "FLIPState creation too slow: {}ms for {} operations (expected < 100ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for TransformValues operations
    #[test]
    fn test_transform_values_operations_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let mut transform = TransformValues {
                translate_x: (i as f64) * 0.1,
                translate_y: (i as f64) * 0.1,
                scale_x: 1.0 + (i as f64) * 0.001,
                scale_y: 1.0 + (i as f64) * 0.001,
                rotation: 0.0,
            };

            // Test various operations
            let _x = transform.translate_x;
            let _y = transform.translate_y;
            let _scale_x = transform.scale_x;
            let _scale_y = transform.scale_y;

            // Test updates
            transform.translate_x = (i as f64) * 0.2;
            transform.translate_y = (i as f64) * 0.2;
            transform.scale_x = 1.0 + (i as f64) * 0.002;
            transform.scale_y = 1.0 + (i as f64) * 0.002;
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should handle at least 2,000,000 operations per second
        assert!(
            operations_per_second > 2_000_000.0,
            "TransformValues operations performance regression: {} ops/sec (expected > 2,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 50ms
        assert!(
            duration.as_millis() < 50,
            "TransformValues operations too slow: {}ms for {} operations (expected < 50ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for SharedElementConfig creation
    #[test]
    fn test_shared_element_config_creation_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let _config = SharedElementConfig {
                duration: 0.3 + (i as f64) * 0.0001,
                easing: EasingFunction::EaseOut,
                maintain_aspect_ratio: i % 2 == 0,
                hardware_accelerated: i % 3 == 0,
            };
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should create at least 1,000,000 configs per second
        assert!(
            operations_per_second > 1_000_000.0,
            "SharedElementConfig creation performance regression: {} ops/sec (expected > 1,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 100ms
        assert!(
            duration.as_millis() < 100,
            "SharedElementConfig creation too slow: {}ms for {} operations (expected < 100ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for ZIndexStrategy operations
    #[test]
    fn test_z_index_strategy_operations_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let strategies = vec![
                ZIndexStrategy::Fixed { base: 1000, increment: 1 },
                ZIndexStrategy::Dynamic { base: 1000, max: 2000 },
                ZIndexStrategy::Elevate,
            ];

            let strategy = &strategies[i % strategies.len()];

            // Test strategy operations
            match strategy {
                ZIndexStrategy::Fixed { base, increment } => {
                    let _z_index = *base + (i as i32) * *increment;
                }
                ZIndexStrategy::Dynamic { base, max } => {
                    let _z_index = *base + (i as i32) % (*max - *base);
                }
                ZIndexStrategy::Elevate => {
                    let _z_index = 1000 + i as i32;
                }
                ZIndexStrategy::CustomProperty { property } => {
                    let _z_index = 1000 + i as i32;
                    let _prop = property;
                }
                ZIndexStrategy::Custom(value) => {
                    let _z_index = *value + i as i32;
                }
                ZIndexStrategy::Maintain => {
                    let _z_index = 1000 + i as i32;
                }
            }
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should handle at least 1,000,000 operations per second
        assert!(
            operations_per_second > 1_000_000.0,
            "ZIndexStrategy operations performance regression: {} ops/sec (expected > 1,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 100ms
        assert!(
            duration.as_millis() < 100,
            "ZIndexStrategy operations too slow: {}ms for {} operations (expected < 100ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for SimplifiedLayoutConfig creation
    #[test]
    fn test_simplified_layout_config_creation_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let _config = SimplifiedLayoutConfig {
                duration: 0.3 + (i as f64) * 0.0001,
                easing: SimplifiedEasing::EaseOut,
                hardware_accelerated: i % 2 == 0,
                enable_flip: i % 3 == 0,
                enable_shared_elements: i % 4 == 0,
            };
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should create at least 2,000,000 configs per second
        assert!(
            operations_per_second > 2_000_000.0,
            "SimplifiedLayoutConfig creation performance regression: {} ops/sec (expected > 2,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 50ms
        assert!(
            duration.as_millis() < 50,
            "SimplifiedLayoutConfig creation too slow: {}ms for {} operations (expected < 50ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for SimplifiedEasing operations
    #[test]
    fn test_simplified_easing_operations_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let easings = vec![
                SimplifiedEasing::Linear,
                SimplifiedEasing::EaseIn,
                SimplifiedEasing::EaseOut,
                SimplifiedEasing::EaseInOut,
            ];

            let easing = &easings[i % easings.len()];
            let progress = (i as f64) / iterations as f64;

            // Test easing calculations
            let _result = match easing {
                SimplifiedEasing::Linear => progress,
                SimplifiedEasing::EaseIn => progress * progress,
                SimplifiedEasing::EaseOut => 1.0 - (1.0 - progress) * (1.0 - progress),
                SimplifiedEasing::EaseInOut => {
                    if progress < 0.5 {
                        2.0 * progress * progress
                    } else {
                        1.0 - 2.0 * (1.0 - progress) * (1.0 - progress)
                    }
                }
                SimplifiedEasing::EaseInCubic => progress * progress * progress,
                SimplifiedEasing::EaseOutCubic => 1.0 - (1.0 - progress).powi(3),
                SimplifiedEasing::EaseInOutCubic => {
                    if progress < 0.5 {
                        4.0 * progress * progress * progress
                    } else {
                        1.0 - 4.0 * (1.0 - progress).powi(3)
                    }
                }
            };
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should handle at least 1,000,000 easing operations per second
        assert!(
            operations_per_second > 1_000_000.0,
            "SimplifiedEasing operations performance regression: {} ops/sec (expected > 1,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 100ms
        assert!(
            duration.as_millis() < 100,
            "SimplifiedEasing operations too slow: {}ms for {} operations (expected < 100ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for SimplifiedAnimationStatus operations
    #[test]
    fn test_simplified_animation_status_operations_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let _status = SimplifiedAnimationStatus {
                is_animating: i % 2 == 0,
                is_paused: i % 3 == 0,
                progress: (i as f64) / iterations as f64,
                start_time: Some(Instant::now()),
                duration: 0.3 + (i as f64) * 0.0001,
            };

            // Test status operations
            let _is_animating = _status.is_animating;
            let _is_paused = _status.is_paused;
            let _progress = _status.progress;
            let _duration = _status.duration;
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should handle at least 2,000,000 operations per second
        assert!(
            operations_per_second > 2_000_000.0,
            "SimplifiedAnimationStatus operations performance regression: {} ops/sec (expected > 2,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 50ms
        assert!(
            duration.as_millis() < 50,
            "SimplifiedAnimationStatus operations too slow: {}ms for {} operations (expected < 50ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for SimplifiedPerformanceMetrics operations
    #[test]
    fn test_simplified_performance_metrics_operations_performance_regression() {
        let iterations = 100_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            let mut metrics = SimplifiedPerformanceMetrics {
                total_animations: i,
                average_duration: 0.3 + (i as f64) * 0.0001,
                frame_rate: 60.0 - (i as f64) * 0.001,
                memory_usage: i * 1024,
                cpu_usage: 10.0 + (i as f64) * 0.001,
            };

            // Test metrics operations
            let _total_animations = metrics.total_animations;
            let _average_duration = metrics.average_duration;
            let _frame_rate = metrics.frame_rate;
            let _memory_usage = metrics.memory_usage;
            let _cpu_usage = metrics.cpu_usage;

            // Test metrics updates
            metrics.total_animations += 1;
            metrics.average_duration = 0.3 + ((i + 1) as f64) * 0.0001;
            metrics.frame_rate = 60.0 - ((i + 1) as f64) * 0.001;
            metrics.memory_usage = (i + 1) * 1024;
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should handle at least 2,000,000 operations per second
        assert!(
            operations_per_second > 2_000_000.0,
            "SimplifiedPerformanceMetrics operations performance regression: {} ops/sec (expected > 2,000,000)",
            operations_per_second
        );

        // Should complete 100,000 operations in under 50ms
        assert!(
            duration.as_millis() < 50,
            "SimplifiedPerformanceMetrics operations too slow: {}ms for {} operations (expected < 50ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for layout calculations simulation
    #[test]
    fn test_layout_calculations_simulation_performance_regression() {
        let iterations = 10_000;
        let start_time = Instant::now();

        for i in 0..iterations {
            // Simulate layout calculations
            let first_layout = LayoutInfo::new(
                (i as f64) * 0.1,
                (i as f64) * 0.1,
                100.0,
                200.0,
            );

            let last_layout = LayoutInfo::new(
                (i as f64) * 0.1 + 50.0,
                (i as f64) * 0.1 + 50.0,
                100.0,
                200.0,
            );

            // Calculate differences
            let delta_x = last_layout.x - first_layout.x;
            let delta_y = last_layout.y - first_layout.y;
            let delta_width = last_layout.width - first_layout.width;
            let delta_height = last_layout.height - first_layout.height;

            // Calculate scale factors
            let scale_x = if first_layout.width > 0.0 {
                last_layout.width / first_layout.width
            } else {
                1.0
            };
            let scale_y = if first_layout.height > 0.0 {
                last_layout.height / first_layout.height
            } else {
                1.0
            };

            // Create inverted transform
            let inverted = TransformValues {
                translate_x: -delta_x,
                translate_y: -delta_y,
                scale_x: 1.0 / scale_x,
                scale_y: 1.0 / scale_y,
                rotation: 0.0,
            };

            // Simulate FLIP state creation
            let _flip_state = FLIPState {
                first: web_sys::DomRect::new_with_x_and_y_and_width_and_height(
                    first_layout.x,
                    first_layout.y,
                    first_layout.width,
                    first_layout.height,
                ).unwrap(),
                last: web_sys::DomRect::new_with_x_and_y_and_width_and_height(
                    last_layout.x,
                    last_layout.y,
                    last_layout.width,
                    last_layout.height,
                ).unwrap(),
                inverted,
                progress: 0.0,
                active: false,
            };
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should handle at least 100,000 layout calculations per second
        assert!(
            operations_per_second > 100_000.0,
            "Layout calculations simulation performance regression: {} ops/sec (expected > 100,000)",
            operations_per_second
        );

        // Should complete 10,000 operations in under 100ms
        assert!(
            duration.as_millis() < 100,
            "Layout calculations simulation too slow: {}ms for {} operations (expected < 100ms)",
            duration.as_millis(),
            iterations
        );
    }

    /// Performance regression test for memory allocation patterns
    #[test]
    fn test_layout_memory_allocation_performance_regression() {
        let iterations = 5_000;
        let start_time = Instant::now();

        // Test memory allocation patterns that might occur in real usage
        let mut layout_infos = Vec::with_capacity(iterations);
        let mut configs = Vec::with_capacity(iterations);
        
        for i in 0..iterations {
            let layout_info = LayoutInfo::new(
                (i as f64) * 0.1,
                (i as f64) * 0.1,
                100.0 + (i as f64) * 0.1,
                200.0 + (i as f64) * 0.1,
            );
            layout_infos.push(layout_info);

            let config = LayoutAnimationConfig::new()
                .with_duration(0.3 + (i as f64) * 0.0001)
                .with_easing(EasingFunction::EaseOut)
                .hardware_accelerated(i % 2 == 0)
                .enabled(true);
            configs.push(config);
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should allocate at least 20,000 objects per second
        assert!(
            operations_per_second > 20_000.0,
            "Layout memory allocation performance regression: {} ops/sec (expected > 20,000)",
            operations_per_second
        );

        // Should complete 5,000 allocations in under 250ms
        assert!(
            duration.as_millis() < 250,
            "Layout memory allocation too slow: {}ms for {} operations (expected < 250ms)",
            duration.as_millis(),
            iterations
        );

        // Verify all objects were created
        assert_eq!(layout_infos.len(), iterations);
        assert_eq!(configs.len(), iterations);
        
        // Test memory cleanup
        drop(layout_infos);
        drop(configs);
    }

    /// Performance regression test for concurrent access simulation
    #[test]
    fn test_layout_concurrent_access_simulation_performance_regression() {
        let iterations = 2_500;
        let start_time = Instant::now();

        // Simulate concurrent access patterns
        let mut shared_layout = LayoutInfo::new(0.0, 0.0, 100.0, 200.0);
        
        for i in 0..iterations {
            // Simulate multiple threads accessing the same layout
            for thread_id in 0..4 {
                // Simulate read operations
                let _x = shared_layout.x;
                let _y = shared_layout.y;
                let _width = shared_layout.width;
                let _height = shared_layout.height;
                
                // Simulate write operations
                shared_layout.x = (i as f64) * 0.1 + thread_id as f64;
                shared_layout.y = (i as f64) * 0.1 + thread_id as f64;
            }
        }

        let duration = start_time.elapsed();
        let operations_per_second = iterations as f64 / duration.as_secs_f64();

        // Performance regression threshold: should handle at least 50,000 operations per second
        assert!(
            operations_per_second > 50_000.0,
            "Layout concurrent access simulation performance regression: {} ops/sec (expected > 50,000)",
            operations_per_second
        );

        // Should complete 2,500 iterations in under 50ms
        assert!(
            duration.as_millis() < 50,
            "Layout concurrent access simulation too slow: {}ms for {} operations (expected < 50ms)",
            duration.as_millis(),
            iterations
        );
    }
}