uzor 1.0.13

Core UI engine — geometry, interaction, input state
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
//! Animation coordinator - manages active animations

use std::collections::HashMap;

use crate::types::WidgetId;
use super::{Decay, Easing, InterruptionStrategy, Spring};

use super::types::{ActiveAnimation, AnimationKey};

/// Central animation coordinator for uzor-core
///
/// Manages all active animations keyed by (WidgetId, property_name).
/// Integrates with uzor-animation primitives and the per-frame render loop.
pub struct AnimationCoordinator {
    /// Active animations: widget_id + property_key → ActiveAnimation
    active: HashMap<AnimationKey, ActiveAnimation>,
    /// Default interruption strategy
    default_interruption: InterruptionStrategy,
}

impl AnimationCoordinator {
    /// Create a new animation coordinator
    pub fn new() -> Self {
        Self {
            active: HashMap::new(),
            default_interruption: InterruptionStrategy::default(),
        }
    }

    /// Tick all active animations. Call once per frame.
    ///
    /// Returns true if any animation is still active (needs repaint).
    pub fn update(&mut self, time_secs: f64) -> bool {
        // Update all animations
        for anim in self.active.values_mut() {
            anim.update(time_secs);
        }

        // Clean up completed animations
        self.cleanup_completed();

        // Return true if any animations are still active
        !self.active.is_empty()
    }

    /// Get current animated value for a property.
    ///
    /// Returns None if no animation is active for this property.
    pub fn get(&self, widget_id: &WidgetId, property: &str) -> Option<f64> {
        let key = AnimationKey::new(widget_id.clone(), property);
        self.active.get(&key).map(|anim| anim.current_value)
    }

    /// Get value or return default if no animation.
    pub fn get_or(&self, widget_id: &WidgetId, property: &str, default: f64) -> f64 {
        self.get(widget_id, property).unwrap_or(default)
    }

    /// Start a tween animation.
    ///
    /// If an animation is already active on this property, it will be replaced
    /// according to the interruption strategy.
    #[allow(clippy::too_many_arguments)]
    pub fn tween(
        &mut self,
        widget_id: WidgetId,
        property: impl Into<String>,
        from: f64,
        to: f64,
        duration_secs: f64,
        easing: Easing,
        time_secs: f64,
    ) {
        let key = AnimationKey::new(widget_id, property);
        let anim = ActiveAnimation::tween(from, to, time_secs, duration_secs, easing);
        self.insert_animation(key, anim);
    }

    /// Start a spring-driven animation.
    ///
    /// The spring animates from its initial displacement (1.0) toward the target.
    pub fn spring(
        &mut self,
        widget_id: WidgetId,
        property: impl Into<String>,
        spring: Spring,
        target: f64,
        time_secs: f64,
    ) {
        let key = AnimationKey::new(widget_id, property);
        let anim = ActiveAnimation::spring(spring, time_secs, target);
        self.insert_animation(key, anim);
    }

    /// Start a decay animation (e.g. flick scroll).
    pub fn decay(
        &mut self,
        widget_id: WidgetId,
        property: impl Into<String>,
        decay: Decay,
        initial_value: f64,
        time_secs: f64,
    ) {
        let key = AnimationKey::new(widget_id, property);
        let anim = ActiveAnimation::decay(decay, time_secs, initial_value);
        self.insert_animation(key, anim);
    }

    /// Cancel all animations for a widget.
    pub fn cancel_widget(&mut self, widget_id: &WidgetId) {
        self.active
            .retain(|key, _| &key.widget_id != widget_id);
    }

    /// Cancel a specific property animation.
    pub fn cancel(&mut self, widget_id: &WidgetId, property: &str) {
        let key = AnimationKey::new(widget_id.clone(), property);
        self.active.remove(&key);
    }

    /// Check if any animations are active.
    pub fn has_active(&self) -> bool {
        !self.active.is_empty()
    }

    /// Check if a widget has active animations.
    pub fn is_animating(&self, widget_id: &WidgetId) -> bool {
        self.active.keys().any(|key| &key.widget_id == widget_id)
    }

    /// Set default interruption strategy.
    pub fn set_interruption_strategy(&mut self, strategy: InterruptionStrategy) {
        self.default_interruption = strategy;
    }

    /// Number of active animations (for diagnostics).
    pub fn active_count(&self) -> usize {
        self.active.len()
    }

    /// Insert an animation, handling interruption if necessary
    fn insert_animation(&mut self, key: AnimationKey, anim: ActiveAnimation) {
        // For now, just replace any existing animation (Instant strategy)
        // TODO: Implement other interruption strategies (Blend, InheritVelocity, Queue)
        self.active.insert(key, anim);
    }

    /// Remove completed animations (called internally by update)
    fn cleanup_completed(&mut self) {
        self.active.retain(|_, anim| !anim.completed);
    }
}

impl Default for AnimationCoordinator {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_tween_lifecycle() {
        let mut coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        // Start animation at t=0
        coord.tween(
            widget_id.clone(),
            "opacity",
            0.0,
            1.0,
            1.0,
            Easing::Linear,
            0.0,
        );

        assert!(coord.has_active());
        assert!(coord.is_animating(&widget_id));

        // At t=0, value should be 0.0 (initial value before any update)
        let val = coord.get(&widget_id, "opacity").unwrap();
        assert!((val - 0.0).abs() < 0.001);

        // Update to t=0.5 (halfway)
        coord.update(0.5);
        let val = coord.get(&widget_id, "opacity").unwrap();
        assert!((val - 0.5).abs() < 0.001);

        // Update to t=0.99 (almost at end, should still be active)
        coord.update(0.99);
        let val = coord.get(&widget_id, "opacity").unwrap();
        assert!((val - 0.99).abs() < 0.01);
        assert!(coord.has_active());

        // Update to t=1.0 (at end, should complete and be cleaned up)
        coord.update(1.0);
        assert!(!coord.has_active());
        assert_eq!(coord.get(&widget_id, "opacity"), None);
    }

    #[test]
    fn test_spring_animation() {
        let mut coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        let spring = Spring::stiff();

        // Start spring animation at t=0, targeting value 100.0
        coord.spring(widget_id.clone(), "x", spring, 100.0, 0.0);

        assert!(coord.has_active());

        // At t=0, spring is at max displacement, so value = target - 1.0
        coord.update(0.0);
        let val = coord.get(&widget_id, "x").unwrap();
        assert!((val - 99.0).abs() < 0.1); // Should be near 99.0

        // Advance time - spring should approach target
        coord.update(0.5);
        let val = coord.get(&widget_id, "x").unwrap();
        assert!(val > 99.0 && val < 101.0); // Should be closer to 100.0

        // Eventually settles (stiff spring settles quickly)
        coord.update(5.0);
        assert!(!coord.has_active()); // Should be cleaned up when settled
    }

    #[test]
    fn test_decay_animation() {
        let mut coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        let decay = Decay::new(500.0).friction(0.95); // Fast decay

        // Start decay at t=0, initial value 0.0
        coord.decay(widget_id.clone(), "scroll_y", decay, 0.0, 0.0);

        assert!(coord.has_active());

        // At t=0, should be at initial value
        coord.update(0.0);
        let val = coord.get(&widget_id, "scroll_y").unwrap();
        assert!((val - 0.0).abs() < 0.1);

        // Advance - position should increase
        coord.update(0.1);
        let val = coord.get(&widget_id, "scroll_y").unwrap();
        assert!(val > 0.0);

        // Eventually comes to rest and gets cleaned up
        coord.update(10.0);
        assert!(!coord.has_active());
    }

    #[test]
    fn test_multiple_widgets_simultaneously() {
        let mut coord = AnimationCoordinator::new();
        let widget1 = WidgetId::new("widget1");
        let widget2 = WidgetId::new("widget2");

        coord.tween(widget1.clone(), "x", 0.0, 100.0, 1.0, Easing::Linear, 0.0);
        coord.tween(widget2.clone(), "y", 0.0, 200.0, 1.0, Easing::Linear, 0.0);

        assert_eq!(coord.active_count(), 2);

        coord.update(0.5);

        let val1 = coord.get(&widget1, "x").unwrap();
        let val2 = coord.get(&widget2, "y").unwrap();

        assert!((val1 - 50.0).abs() < 0.1);
        assert!((val2 - 100.0).abs() < 0.1);
    }

    #[test]
    fn test_cancel_widget() {
        let mut coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        coord.tween(
            widget_id.clone(),
            "x",
            0.0,
            100.0,
            1.0,
            Easing::Linear,
            0.0,
        );
        coord.tween(
            widget_id.clone(),
            "y",
            0.0,
            100.0,
            1.0,
            Easing::Linear,
            0.0,
        );

        assert_eq!(coord.active_count(), 2);

        coord.cancel_widget(&widget_id);
        assert_eq!(coord.active_count(), 0);
    }

    #[test]
    fn test_cancel_property() {
        let mut coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        coord.tween(
            widget_id.clone(),
            "x",
            0.0,
            100.0,
            1.0,
            Easing::Linear,
            0.0,
        );
        coord.tween(
            widget_id.clone(),
            "y",
            0.0,
            100.0,
            1.0,
            Easing::Linear,
            0.0,
        );

        assert_eq!(coord.active_count(), 2);

        coord.cancel(&widget_id, "x");
        assert_eq!(coord.active_count(), 1);
        assert_eq!(coord.get(&widget_id, "x"), None);
        assert!(coord.get(&widget_id, "y").is_some());
    }

    #[test]
    fn test_interruption_replaces_old() {
        let mut coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        // Start first animation
        coord.tween(
            widget_id.clone(),
            "x",
            0.0,
            100.0,
            1.0,
            Easing::Linear,
            0.0,
        );

        coord.update(0.5);
        let val = coord.get(&widget_id, "x").unwrap();
        assert!((val - 50.0).abs() < 0.1);

        // Interrupt with new animation
        coord.tween(
            widget_id.clone(),
            "x",
            50.0,
            200.0,
            1.0,
            Easing::Linear,
            0.5,
        );

        // Old animation should be replaced
        coord.update(0.5);
        let val = coord.get(&widget_id, "x").unwrap();
        assert!((val - 50.0).abs() < 0.1); // New animation starts at 50.0
    }

    #[test]
    fn test_get_or_with_default() {
        let coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        let val = coord.get_or(&widget_id, "opacity", 1.0);
        assert_eq!(val, 1.0);
    }

    #[test]
    fn test_has_active_is_animating() {
        let mut coord = AnimationCoordinator::new();
        let widget1 = WidgetId::new("widget1");
        let widget2 = WidgetId::new("widget2");

        assert!(!coord.has_active());
        assert!(!coord.is_animating(&widget1));

        coord.tween(widget1.clone(), "x", 0.0, 100.0, 1.0, Easing::Linear, 0.0);

        assert!(coord.has_active());
        assert!(coord.is_animating(&widget1));
        assert!(!coord.is_animating(&widget2));
    }

    #[test]
    fn test_easing_functions() {
        let mut coord = AnimationCoordinator::new();
        let widget_id = WidgetId::new("widget1");

        // Test EaseInQuad
        coord.tween(
            widget_id.clone(),
            "x",
            0.0,
            100.0,
            1.0,
            Easing::EaseInQuad,
            0.0,
        );

        coord.update(0.5);
        let val = coord.get(&widget_id, "x").unwrap();
        // EaseInQuad at t=0.5 should give 0.25, so value = 0 + 100*0.25 = 25
        assert!((val - 25.0).abs() < 0.1);
    }
}