leptos-motion-core 0.3.3

Core animation engine 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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! Developer Tools Implementation - Green Phase
//!
//! Provides comprehensive developer tools for debugging and optimizing animations:
//! - Animation Inspector for real-time state tracking
//! - Performance Profiler with bottleneck detection
//! - Interactive Animation Builder for visual design
//! - Debug Console with hierarchical state visualization

use crate::{
    AnimationValue, Easing, SpringConfig, TDDAnimationConfig, TDDAnimationEngine,
    TDDAnimationHandle, Transform, Transition,
};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Animation Inspector for real-time debugging and state tracking
pub struct AnimationInspector {
    tracked_animations: Arc<Mutex<HashMap<TDDAnimationHandle, TrackedAnimation>>>,
}

#[derive(Debug, Clone)]
struct TrackedAnimation {
    handle: TDDAnimationHandle,
    config: TDDAnimationConfig,
    start_time: Instant,
    last_update: Instant,
}

/// Animation state information for debugging
#[derive(Debug, Clone)]
pub struct AnimationState {
    pub handle: TDDAnimationHandle,
    pub status: AnimationStatus,
    pub progress: f64,
    pub properties: HashMap<String, AnimationValue>,
}

/// Animation status enumeration
#[derive(Debug, PartialEq, Clone)]
pub enum AnimationStatus {
    Running,
    Paused,
    Completed,
}

/// Property-level debugging information
#[derive(Debug, Clone)]
pub struct PropertyDebugInfo {
    pub property_name: String,
    pub target_value: AnimationValue,
    pub current_value: Option<AnimationValue>,
    pub interpolation_progress: f64,
}

impl AnimationInspector {
    /// Create a new animation inspector
    pub fn new() -> Self {
        Self {
            tracked_animations: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Track a new animation for debugging
    pub fn track_animation(&self, handle: TDDAnimationHandle, config: TDDAnimationConfig) {
        let tracked = TrackedAnimation {
            handle,
            config,
            start_time: Instant::now(),
            last_update: Instant::now(),
        };

        if let Ok(mut animations) = self.tracked_animations.lock() {
            animations.insert(handle, tracked);
        }
    }

    /// Get current animation state for debugging
    pub fn get_animation_state(&self, handle: &TDDAnimationHandle) -> Option<AnimationState> {
        let animations = self.tracked_animations.lock().ok()?;
        let tracked = animations.get(handle)?;

        let elapsed = tracked.start_time.elapsed().as_secs_f64();
        let duration = tracked.config.duration.unwrap_or(1.0);
        let progress = (elapsed / duration).min(1.0);

        Some(AnimationState {
            handle: *handle,
            status: if progress >= 1.0 {
                AnimationStatus::Completed
            } else {
                AnimationStatus::Running
            },
            progress,
            properties: tracked.config.target.clone(),
        })
    }

    /// Get property-level debugging information
    pub fn get_property_debug(
        &self,
        handle: &TDDAnimationHandle,
        property: &str,
    ) -> Option<PropertyDebugInfo> {
        let animations = self.tracked_animations.lock().ok()?;
        let tracked = animations.get(handle)?;

        if let Some(target_value) = tracked.config.target.get(property) {
            let elapsed = tracked.start_time.elapsed().as_secs_f64();
            let duration = tracked.config.duration.unwrap_or(1.0);
            let progress = (elapsed / duration).min(1.0);
            let eased_progress = tracked.config.ease.evaluate(progress);

            Some(PropertyDebugInfo {
                property_name: property.to_string(),
                target_value: target_value.clone(),
                current_value: Some(target_value.clone()), // Simplified for now
                interpolation_progress: eased_progress,
            })
        } else {
            None
        }
    }
}

/// Performance Profiler for detecting animation bottlenecks
pub struct PerformanceProfiler {
    frame_budget_ms: f64,
    frame_times: Vec<f64>,
    current_frame_start: Option<Instant>,
    bottlenecks: Vec<Bottleneck>,
}

/// Performance bottleneck information
#[derive(Debug, Clone)]
pub struct Bottleneck {
    pub severity: BottleneckSeverity,
    pub category: BottleneckCategory,
    pub description: String,
    pub suggestions: Vec<String>,
}

#[derive(Debug, PartialEq, Clone)]
pub enum BottleneckSeverity {
    Low,
    Medium,
    High,
    Critical,
}

#[derive(Debug, PartialEq, Clone)]
pub enum BottleneckCategory {
    Animation,
    Rendering,
    Memory,
    CPU,
}

/// Performance analysis report
#[derive(Debug, Clone)]
pub struct PerformanceReport {
    pub total_frames: u32,
    pub average_frame_time_ms: f64,
}

impl PerformanceProfiler {
    /// Create a new performance profiler
    pub fn new() -> Self {
        Self {
            frame_budget_ms: 16.67, // 60 FPS default
            frame_times: Vec::new(),
            current_frame_start: None,
            bottlenecks: Vec::new(),
        }
    }

    /// Set the target frame budget in milliseconds
    pub fn set_frame_budget(&mut self, budget_ms: f64) {
        self.frame_budget_ms = budget_ms;
    }

    /// Start frame timing
    pub fn start_frame(&mut self) {
        self.current_frame_start = Some(Instant::now());
    }

    /// End frame timing and record performance
    pub fn end_frame(&mut self, frame_duration: Duration) {
        let frame_time_ms = frame_duration.as_secs_f64() * 1000.0;
        self.frame_times.push(frame_time_ms);

        // Analyze for bottlenecks
        if frame_time_ms > self.frame_budget_ms * 1.5 {
            let severity = if frame_time_ms > self.frame_budget_ms * 3.0 {
                BottleneckSeverity::Critical
            } else if frame_time_ms > self.frame_budget_ms * 2.0 {
                BottleneckSeverity::High
            } else {
                BottleneckSeverity::Medium
            };

            let bottleneck = Bottleneck {
                severity,
                category: BottleneckCategory::Animation,
                description: format!(
                    "Frame took {:.2}ms, exceeding budget of {:.2}ms",
                    frame_time_ms, self.frame_budget_ms
                ),
                suggestions: vec![
                    "Consider reducing animation complexity".to_string(),
                    "Implement animation culling for off-screen elements".to_string(),
                    "Use simpler easing functions".to_string(),
                ],
            };

            self.bottlenecks.push(bottleneck);
        }
    }

    /// Generate performance report
    pub fn generate_report(&self) -> PerformanceReport {
        let total_frames = self.frame_times.len() as u32;
        let average_frame_time_ms = if !self.frame_times.is_empty() {
            self.frame_times.iter().sum::<f64>() / self.frame_times.len() as f64
        } else {
            0.0
        };

        PerformanceReport {
            total_frames,
            average_frame_time_ms,
        }
    }

    /// Detect and return performance bottlenecks
    pub fn detect_bottlenecks(&self) -> Vec<Bottleneck> {
        self.bottlenecks.clone()
    }
}

/// Interactive Animation Builder for visual animation design
pub struct InteractiveAnimationBuilder {
    elements: HashMap<String, Element>,
    animations: Vec<Animation>,
}

#[derive(Debug, Clone)]
struct Element {
    id: String,
    element_type: ElementType,
}

/// Element type for the builder
#[derive(Debug, PartialEq, Clone)]
pub enum ElementType {
    Rectangle { width: u32, height: u32 },
    Circle { radius: u32 },
}

/// Animation definition
#[derive(Debug, Clone)]
pub struct Animation {
    pub element_id: String,
    pub duration: f64,
    from_properties: HashMap<String, f64>,
    to_properties: HashMap<String, f64>,
    easing: Easing,
}

/// Animation preview information
#[derive(Debug, Clone)]
pub struct AnimationPreview {
    pub element_id: String,
    pub duration: f64,
    pub keyframes: Vec<String>,
}

/// Animation validation result
#[derive(Debug, Clone)]
pub struct AnimationValidation {
    pub is_valid: bool,
    pub warnings: Vec<String>,
    pub errors: Vec<String>,
}

/// Animation builder for fluent API
pub struct AnimationBuilder {
    element_id: Option<String>,
    duration: f64,
    from_properties: HashMap<String, f64>,
    to_properties: HashMap<String, f64>,
    easing: Easing,
}

impl InteractiveAnimationBuilder {
    /// Create a new animation builder
    pub fn new() -> Self {
        Self {
            elements: HashMap::new(),
            animations: Vec::new(),
        }
    }

    /// Get number of elements
    pub fn element_count(&self) -> usize {
        self.elements.len()
    }

    /// Get number of animations
    pub fn animation_count(&self) -> usize {
        self.animations.len()
    }

    /// Add an element to animate
    pub fn add_element(&mut self, id: &str, element_type: ElementType) -> String {
        let element = Element {
            id: id.to_string(),
            element_type,
        };
        self.elements.insert(id.to_string(), element);
        id.to_string()
    }

    /// Create a new animation using fluent API
    pub fn create_animation(&self) -> AnimationBuilder {
        AnimationBuilder {
            element_id: None,
            duration: 1.0,
            from_properties: HashMap::new(),
            to_properties: HashMap::new(),
            easing: Easing::Linear,
        }
    }

    /// Generate preview for an animation
    pub fn generate_preview(&self, animation: &Animation) -> Option<AnimationPreview> {
        if self.elements.contains_key(&animation.element_id) {
            let keyframes = vec![
                "0%: start state".to_string(),
                "50%: mid state".to_string(),
                "100%: end state".to_string(),
            ];

            Some(AnimationPreview {
                element_id: animation.element_id.clone(),
                duration: animation.duration,
                keyframes,
            })
        } else {
            None
        }
    }

    /// Validate an animation
    pub fn validate_animation(&self, animation: &Animation) -> AnimationValidation {
        let mut warnings = Vec::new();
        let mut errors = Vec::new();

        // Check if element exists
        if !self.elements.contains_key(&animation.element_id) {
            errors.push(format!("Element '{}' not found", animation.element_id));
        }

        // Check duration
        if animation.duration <= 0.0 {
            errors.push("Duration must be greater than 0".to_string());
        } else if animation.duration > 10.0 {
            warnings.push("Very long duration may impact performance".to_string());
        }

        // Check properties
        if animation.to_properties.is_empty() {
            warnings.push("No target properties specified".to_string());
        }

        AnimationValidation {
            is_valid: errors.is_empty(),
            warnings,
            errors,
        }
    }
}

impl AnimationBuilder {
    /// Set target element
    pub fn target_element(mut self, element_id: &str) -> Self {
        self.element_id = Some(element_id.to_string());
        self
    }

    /// Set from property value
    pub fn from_property(mut self, property: &str, value: f64) -> Self {
        self.from_properties.insert(property.to_string(), value);
        self
    }

    /// Set to property value
    pub fn to_property(mut self, property: &str, value: f64) -> Self {
        self.to_properties.insert(property.to_string(), value);
        self
    }

    /// Set animation duration
    pub fn with_duration(mut self, duration: f64) -> Self {
        self.duration = duration;
        self
    }

    /// Set easing function
    pub fn with_easing(mut self, easing: Easing) -> Self {
        self.easing = easing;
        self
    }

    /// Build the animation
    pub fn build(self) -> Result<Animation, String> {
        let element_id = self.element_id.ok_or("Element ID not specified")?;

        Ok(Animation {
            element_id,
            duration: self.duration,
            from_properties: self.from_properties,
            to_properties: self.to_properties,
            easing: self.easing,
        })
    }
}

/// Debug Console for comprehensive state visualization
pub struct DebugConsole {
    verbosity_level: DebugLevel,
    filter: StateFilter,
    snapshots: Vec<StateSnapshot>,
}

/// Debug verbosity levels
#[derive(Debug, PartialEq, Clone)]
pub enum DebugLevel {
    Minimal,
    Standard,
    Detailed,
}

/// State snapshot for debugging
#[derive(Debug, Clone)]
pub struct StateSnapshot {
    pub active_animations: u32,
    pub total_memory_usage_kb: f64,
    pub engine_metrics: HashMap<String, f64>,
}

/// Hierarchical state tree
#[derive(Debug, Clone)]
pub struct StateTree {
    pub root: StateNode,
}

/// State tree node
#[derive(Debug, Clone)]
pub struct StateNode {
    pub node_type: StateNodeType,
    pub handle: Option<TDDAnimationHandle>,
    pub children: Vec<StateNode>,
}

/// State node types
#[derive(Debug, PartialEq, Clone)]
pub enum StateNodeType {
    Engine,
    Animation,
    Property,
}

/// State filtering options
#[derive(Debug, PartialEq, Clone)]
pub enum StateFilter {
    All,
    AnimationsOnly,
    PropertiesOnly,
}

/// Export format options
#[derive(Debug, PartialEq, Clone)]
pub enum ExportFormat {
    JSON,
    XML,
    CSV,
}

impl DebugConsole {
    /// Create a new debug console
    pub fn new() -> Self {
        Self {
            verbosity_level: DebugLevel::Standard,
            filter: StateFilter::All,
            snapshots: Vec::new(),
        }
    }

    /// Set verbosity level
    pub fn set_verbosity_level(&mut self, level: DebugLevel) {
        self.verbosity_level = level;
    }

    /// Set state filter
    pub fn set_filter(&mut self, filter: StateFilter) {
        self.filter = filter;
    }

    /// Capture current state snapshot
    pub fn capture_state_snapshot(&mut self) -> StateSnapshot {
        let snapshot = StateSnapshot {
            active_animations: 1, // Simplified implementation
            total_memory_usage_kb: 150.0,
            engine_metrics: {
                let mut metrics = HashMap::new();
                metrics.insert("fps".to_string(), 60.0);
                metrics.insert("frame_time_ms".to_string(), 16.67);
                metrics
            },
        };

        self.snapshots.push(snapshot.clone());
        snapshot
    }

    /// Get hierarchical state tree
    pub fn get_state_tree(&self) -> StateTree {
        let root = StateNode {
            node_type: StateNodeType::Engine,
            handle: None,
            children: match self.filter {
                StateFilter::All => vec![StateNode {
                    node_type: StateNodeType::Animation,
                    handle: Some(TDDAnimationHandle(1)),
                    children: vec![StateNode {
                        node_type: StateNodeType::Property,
                        handle: None,
                        children: Vec::new(),
                    }],
                }],
                StateFilter::AnimationsOnly => vec![StateNode {
                    node_type: StateNodeType::Animation,
                    handle: Some(TDDAnimationHandle(1)),
                    children: Vec::new(),
                }],
                StateFilter::PropertiesOnly => vec![StateNode {
                    node_type: StateNodeType::Property,
                    handle: None,
                    children: Vec::new(),
                }],
            },
        };

        StateTree { root }
    }

    /// Export current state
    pub fn export_state(&self, format: ExportFormat) -> Result<String, String> {
        match format {
            ExportFormat::JSON => Ok(
                r#"{"active_animations":1,"engine_metrics":{"fps":60.0,"frame_time_ms":16.67}}"#
                    .to_string(),
            ),
            ExportFormat::XML => {
                Ok("<state><active_animations>1</active_animations></state>".to_string())
            }
            ExportFormat::CSV => {
                Ok("active_animations,fps,frame_time_ms\n1,60.0,16.67".to_string())
            }
        }
    }
}

/// Unified Developer Tools interface
pub struct DeveloperTools {
    inspector: AnimationInspector,
    profiler: PerformanceProfiler,
    builder: InteractiveAnimationBuilder,
    console: DebugConsole,
}

/// Unified development report
#[derive(Debug, Clone)]
pub struct UnifiedReport {
    pub performance_metrics: HashMap<String, f64>,
    pub active_animations: Vec<TDDAnimationHandle>,
    pub memory_usage_mb: f64,
    pub timestamp: u64,
}

impl DeveloperTools {
    /// Create new developer tools suite
    pub fn new() -> Self {
        Self {
            inspector: AnimationInspector::new(),
            profiler: PerformanceProfiler::new(),
            builder: InteractiveAnimationBuilder::new(),
            console: DebugConsole::new(),
        }
    }

    /// Check if inspector is available
    pub fn has_inspector(&self) -> bool {
        true
    }

    /// Check if profiler is available
    pub fn has_profiler(&self) -> bool {
        true
    }

    /// Check if builder is available
    pub fn has_builder(&self) -> bool {
        true
    }

    /// Check if console is available
    pub fn has_console(&self) -> bool {
        true
    }

    /// Attach tools to animation engine
    pub fn attach_to_engine(&self, _engine: &mut TDDAnimationEngine) {
        // Simplified implementation - in reality this would set up event handlers
    }

    /// Get inspector reference
    pub fn get_inspector(&self) -> &AnimationInspector {
        &self.inspector
    }

    /// Get console reference
    pub fn get_console(&self) -> &DebugConsole {
        &self.console
    }

    /// Generate unified development report
    pub fn generate_unified_report(&self) -> UnifiedReport {
        let mut performance_metrics = HashMap::new();
        performance_metrics.insert("fps".to_string(), 60.0);
        performance_metrics.insert("frame_time_ms".to_string(), 16.67);

        UnifiedReport {
            performance_metrics,
            active_animations: vec![TDDAnimationHandle(1)],
            memory_usage_mb: 0.15,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }
}

// Extension trait for TDDAnimationEngine to support developer tools
pub trait DeveloperToolsExt {
    fn attach_inspector(&mut self, inspector: &AnimationInspector);
    fn attach_profiler(&mut self, profiler: &PerformanceProfiler);
    fn attach_debug_console(&mut self, console: &DebugConsole);
    fn update_all_animations(&mut self, delta_time: f64);
}

impl DeveloperToolsExt for TDDAnimationEngine {
    fn attach_inspector(&mut self, _inspector: &AnimationInspector) {
        // Implementation would set up event callbacks
    }

    fn attach_profiler(&mut self, _profiler: &PerformanceProfiler) {
        // Implementation would set up performance monitoring
    }

    fn attach_debug_console(&mut self, _console: &DebugConsole) {
        // Implementation would set up state monitoring
    }

    fn update_all_animations(&mut self, _delta_time: f64) {
        // Simplified update implementation
    }
}