fui-rs 0.2.5

Retained Rust UI for native desktop and WebAssembly applications
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
use crate::frame_scheduler::mark_needs_commit;
use std::cell::RefCell;
use std::rc::Rc;

const MAX_FRAME_DELTA_MS: f64 = 100.0;

fn clamp_unit(value: f32) -> f32 {
    value.clamp(0.0, 1.0)
}

fn clamp_frame_delta(delta_ms: f64) -> f64 {
    delta_ms.clamp(0.0, MAX_FRAME_DELTA_MS)
}

fn mix_float(from: f32, to: f32, amount: f32) -> f32 {
    from + ((to - from) * clamp_unit(amount))
}

fn mix_color_channel(from: u32, to: u32, amount: f32) -> u32 {
    let mixed = (from as f32) + (((to as f32) - (from as f32)) * clamp_unit(amount));
    mixed.round().clamp(0.0, 255.0) as u32
}

fn mix_color(from: u32, to: u32, amount: f32) -> u32 {
    let fr = (from >> 24) & 0xFF;
    let fg = (from >> 16) & 0xFF;
    let fb = (from >> 8) & 0xFF;
    let fa = from & 0xFF;
    let tr = (to >> 24) & 0xFF;
    let tg = (to >> 16) & 0xFF;
    let tb = (to >> 8) & 0xFF;
    let ta = to & 0xFF;
    (mix_color_channel(fr, tr, amount) << 24)
        | (mix_color_channel(fg, tg, amount) << 16)
        | (mix_color_channel(fb, tb, amount) << 8)
        | mix_color_channel(fa, ta, amount)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Easing {
    Linear,
    CubicIn,
    CubicOut,
    CubicInOut,
    QuadOut,
}

impl Easing {
    pub fn sample(self, progress: f32) -> f32 {
        let t = clamp_unit(progress);
        match self {
            Self::Linear => t,
            Self::CubicIn => t * t * t,
            Self::CubicOut => {
                let offset = t - 1.0;
                (offset * offset * offset) + 1.0
            }
            Self::CubicInOut => {
                if t < 0.5 {
                    4.0 * t * t * t
                } else {
                    let offset = (-2.0 * t) + 2.0;
                    1.0 - ((offset * offset * offset) * 0.5)
                }
            }
            Self::QuadOut => {
                let offset = 1.0 - t;
                1.0 - (offset * offset)
            }
        }
    }
}

pub struct Easings;

impl Easings {
    pub fn linear() -> Easing {
        Easing::Linear
    }

    pub fn cubic_in() -> Easing {
        Easing::CubicIn
    }

    pub fn cubic_out() -> Easing {
        Easing::CubicOut
    }

    pub fn cubic_in_out() -> Easing {
        Easing::CubicInOut
    }

    pub fn quad_out() -> Easing {
        Easing::QuadOut
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AnimationTiming {
    pub duration_ms: f64,
    pub easing: Easing,
}

impl AnimationTiming {
    pub fn new(duration_ms: f64) -> Self {
        Self {
            duration_ms: duration_ms.max(0.0),
            easing: Easing::Linear,
        }
    }

    pub fn with_easing(duration_ms: f64, easing: Easing) -> Self {
        Self {
            duration_ms: duration_ms.max(0.0),
            easing,
        }
    }
}

trait AnimationDriver {
    fn on_start(&mut self, _timestamp_ms: f64) {}
    fn on_sample(&mut self, eased_progress: f32, linear_progress: f32);
    fn on_stop(&mut self, _finished: bool) {}
}

struct AnimationState {
    timing: AnimationTiming,
    running: bool,
    started: bool,
    last_timestamp_ms: f64,
    elapsed_ms: f64,
    driver: Box<dyn AnimationDriver>,
}

#[derive(Clone)]
pub struct Animation {
    inner: Rc<RefCell<AnimationState>>,
}

impl Animation {
    fn new(timing: AnimationTiming, driver: Box<dyn AnimationDriver>) -> Self {
        Self {
            inner: Rc::new(RefCell::new(AnimationState {
                timing,
                running: false,
                started: false,
                last_timestamp_ms: 0.0,
                elapsed_ms: 0.0,
                driver,
            })),
        }
    }

    pub fn is_running(&self) -> bool {
        self.inner.borrow().running
    }

    pub fn cancel(&self) {
        get_animation_manager().cancel(self);
    }

    pub fn finish(&self) {
        get_animation_manager().finish(self);
    }

    pub fn dispose(&self) {
        self.cancel();
    }

    fn attach(&self, known_timestamp_ms: f64, has_known_timestamp: bool) {
        {
            let mut state = self.inner.borrow_mut();
            state.running = true;
            state.started = false;
            state.last_timestamp_ms = 0.0;
            state.elapsed_ms = 0.0;
        }
        if has_known_timestamp {
            self.start_internal(known_timestamp_ms);
        }
    }

    fn tick(&self, timestamp_ms: f64) {
        if !self.is_running() {
            return;
        }
        if !self.inner.borrow().started {
            self.start_internal(timestamp_ms);
            return;
        }
        let (duration_ms, easing, last_timestamp_ms) = {
            let state = self.inner.borrow();
            (
                state.timing.duration_ms,
                state.timing.easing,
                state.last_timestamp_ms,
            )
        };
        let delta_ms = clamp_frame_delta(timestamp_ms - last_timestamp_ms);
        let should_finish = {
            let mut state = self.inner.borrow_mut();
            state.last_timestamp_ms = timestamp_ms;
            state.elapsed_ms += delta_ms;
            let progress = if duration_ms <= 0.0 {
                1.0
            } else {
                clamp_unit((state.elapsed_ms / duration_ms) as f32)
            };
            state.driver.on_sample(easing.sample(progress), progress);
            progress >= 1.0
        };
        if should_finish {
            self.stop(true);
        }
    }

    fn cancel_internal(&self) {
        if self.is_running() {
            self.stop(false);
        }
    }

    fn finish_internal(&self) {
        if !self.is_running() {
            return;
        }
        if !self.inner.borrow().started {
            let mut state = self.inner.borrow_mut();
            state.started = true;
            state.last_timestamp_ms = 0.0;
            state.elapsed_ms = state.timing.duration_ms;
            state.driver.on_start(0.0);
        }
        self.inner.borrow_mut().driver.on_sample(1.0, 1.0);
        self.stop(true);
    }

    fn start_internal(&self, timestamp_ms: f64) {
        let duration_ms = {
            let mut state = self.inner.borrow_mut();
            state.started = true;
            state.last_timestamp_ms = timestamp_ms;
            state.elapsed_ms = 0.0;
            state.driver.on_start(timestamp_ms);
            state.timing.duration_ms
        };
        if duration_ms <= 0.0 {
            self.inner.borrow_mut().driver.on_sample(1.0, 1.0);
            self.stop(true);
            return;
        }
        let easing = self.inner.borrow().timing.easing;
        self.inner
            .borrow_mut()
            .driver
            .on_sample(easing.sample(0.0), 0.0);
    }

    fn stop(&self, finished: bool) {
        let mut state = self.inner.borrow_mut();
        state.running = false;
        state.driver.on_stop(finished);
    }
}

#[derive(Default)]
struct AnimationManagerState {
    active_animations: Vec<Animation>,
    last_timestamp_ms: f64,
    has_last_timestamp: bool,
}

thread_local! {
    static ANIMATION_MANAGER: RefCell<AnimationManagerState> = const { RefCell::new(AnimationManagerState {
        active_animations: Vec::new(),
        last_timestamp_ms: 0.0,
        has_last_timestamp: false,
    }) };
}

#[derive(Clone, Copy, Debug, Default)]
pub struct AnimationManager;

impl AnimationManager {
    pub fn start(&self, animation: Animation) -> Animation {
        self.cancel(&animation);
        ANIMATION_MANAGER.with(|slot| {
            let mut state = slot.borrow_mut();
            animation.attach(state.last_timestamp_ms, state.has_last_timestamp);
            if animation.is_running() {
                state.active_animations.push(animation.clone());
            }
        });
        mark_needs_commit();
        animation
    }

    pub fn cancel(&self, animation: &Animation) {
        ANIMATION_MANAGER.with(|slot| {
            let mut state = slot.borrow_mut();
            if let Some(index) = state
                .active_animations
                .iter()
                .position(|candidate| Rc::ptr_eq(&candidate.inner, &animation.inner))
            {
                state.active_animations.swap_remove(index);
                animation.cancel_internal();
            }
        });
    }

    pub fn finish(&self, animation: &Animation) {
        ANIMATION_MANAGER.with(|slot| {
            let mut state = slot.borrow_mut();
            if let Some(index) = state
                .active_animations
                .iter()
                .position(|candidate| Rc::ptr_eq(&candidate.inner, &animation.inner))
            {
                state.active_animations.swap_remove(index);
                animation.finish_internal();
            }
        });
    }

    pub fn tick(&self, timestamp_ms: f64) {
        let active_animations = ANIMATION_MANAGER.with(|slot| {
            let mut state = slot.borrow_mut();
            state.last_timestamp_ms = timestamp_ms;
            state.has_last_timestamp = true;
            state.active_animations.clone()
        });
        let mut completed = Vec::new();
        let mut has_active = false;
        for animation in active_animations {
            animation.tick(timestamp_ms);
            if animation.is_running() {
                has_active = true;
            } else {
                completed.push(animation);
            }
        }
        if !completed.is_empty() {
            ANIMATION_MANAGER.with(|slot| {
                let mut state = slot.borrow_mut();
                state.active_animations.retain(|candidate| {
                    !completed.iter().any(|completed_animation| {
                        Rc::ptr_eq(&candidate.inner, &completed_animation.inner)
                    })
                });
            });
        }
        if has_active {
            mark_needs_commit();
        }
    }

    pub fn has_active_animations(&self) -> bool {
        ANIMATION_MANAGER.with(|slot| !slot.borrow().active_animations.is_empty())
    }

    pub fn reset(&self) {
        ANIMATION_MANAGER.with(|slot| {
            let mut state = slot.borrow_mut();
            for animation in state.active_animations.drain(..) {
                animation.cancel_internal();
            }
            state.last_timestamp_ms = 0.0;
            state.has_last_timestamp = false;
        });
    }
}

pub fn get_animation_manager() -> AnimationManager {
    AnimationManager
}

pub fn tick_animations(timestamp_ms: f64) {
    get_animation_manager().tick(timestamp_ms);
}

pub fn reset_animations() {
    get_animation_manager().reset();
}

struct FloatAnimationDriver {
    from: f32,
    to: f32,
    handler: Box<dyn Fn(f32)>,
}

impl AnimationDriver for FloatAnimationDriver {
    fn on_sample(&mut self, eased_progress: f32, _linear_progress: f32) {
        (self.handler)(mix_float(self.from, self.to, eased_progress));
    }
}

struct ColorAnimationDriver {
    from: u32,
    to: u32,
    handler: Box<dyn Fn(u32)>,
}

impl AnimationDriver for ColorAnimationDriver {
    fn on_sample(&mut self, eased_progress: f32, _linear_progress: f32) {
        (self.handler)(mix_color(self.from, self.to, eased_progress));
    }
}

pub fn animate_float(
    from_value: f32,
    to_value: f32,
    timing: AnimationTiming,
    handler: impl Fn(f32) + 'static,
) -> Animation {
    get_animation_manager().start(Animation::new(
        timing,
        Box::new(FloatAnimationDriver {
            from: from_value,
            to: to_value,
            handler: Box::new(handler),
        }),
    ))
}

pub fn animate_float_with<Owner: 'static>(
    owner: Owner,
    from_value: f32,
    to_value: f32,
    timing: AnimationTiming,
    handler: impl Fn(&Owner, f32) + 'static,
) -> Animation {
    animate_float(from_value, to_value, timing, move |value| {
        handler(&owner, value);
    })
}

pub fn animate_color(
    from_value: u32,
    to_value: u32,
    timing: AnimationTiming,
    handler: impl Fn(u32) + 'static,
) -> Animation {
    get_animation_manager().start(Animation::new(
        timing,
        Box::new(ColorAnimationDriver {
            from: from_value,
            to: to_value,
            handler: Box::new(handler),
        }),
    ))
}

pub fn animate_color_with<Owner: 'static>(
    owner: Owner,
    from_value: u32,
    to_value: u32,
    timing: AnimationTiming,
    handler: impl Fn(&Owner, u32) + 'static,
) -> Animation {
    animate_color(from_value, to_value, timing, move |value| {
        handler(&owner, value);
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bridge_callbacks::__fui_on_frame;
    use crate::ffi::{self, Call};
    use crate::frame_scheduler::reset_commit_state;
    use std::cell::Cell;
    use std::rc::Rc;

    #[test]
    fn advances_float_animations_from_frame_timestamps() {
        reset_animations();
        reset_commit_state();
        let value = Rc::new(Cell::new(-1.0));
        let value_for_handler = value.clone();
        let animation = animate_float(10.0, 30.0, AnimationTiming::new(200.0), move |next| {
            value_for_handler.set(next);
        });
        let manager = get_animation_manager();

        manager.tick(1000.0);
        assert_eq!(value.get(), 10.0);
        assert!(animation.is_running());

        manager.tick(1050.0);
        assert_eq!(value.get(), 15.0);

        manager.tick(1150.0);
        assert_eq!(value.get(), 25.0);

        manager.tick(1250.0);
        assert_eq!(value.get(), 30.0);
        assert!(!animation.is_running());
        assert!(!manager.has_active_animations());
    }

    #[test]
    fn clamps_stale_frame_gaps_instead_of_jumping_to_completion() {
        reset_animations();
        reset_commit_state();
        let value = Rc::new(Cell::new(-1.0));
        let value_for_handler = value.clone();
        let manager = get_animation_manager();
        animate_float(0.0, 100.0, AnimationTiming::new(200.0), move |next| {
            value_for_handler.set(next);
        });

        manager.tick(1000.0);
        manager.tick(1800.0);

        assert_eq!(value.get(), 50.0);
        assert!(manager.has_active_animations());
    }

    #[test]
    fn supports_owner_bound_float_and_color_helpers() {
        reset_animations();
        reset_commit_state();
        let float_owner = Rc::new(Cell::new(-1.0));
        let color_owner = Rc::new(Cell::new(0u32));
        let manager = get_animation_manager();

        animate_float_with(
            float_owner.clone(),
            4.0,
            12.0,
            AnimationTiming::new(80.0),
            |owner, value| owner.set(value),
        );
        animate_color_with(
            color_owner.clone(),
            0x000000FF,
            0xFF0000FF,
            AnimationTiming::new(80.0),
            |owner, value| owner.set(value),
        );

        manager.tick(500.0);
        assert_eq!(float_owner.get(), 4.0);
        assert_eq!(color_owner.get(), 0x000000FF);

        manager.tick(540.0);
        assert_eq!(float_owner.get(), 8.0);
        assert_eq!(color_owner.get(), 0x800000FF);

        manager.tick(580.0);
        assert_eq!(float_owner.get(), 12.0);
        assert_eq!(color_owner.get(), 0xFF0000FF);
    }

    #[test]
    fn cancels_and_finishes_animations_through_the_animation_object() {
        reset_animations();
        reset_commit_state();
        let value = Rc::new(Cell::new(-1.0));
        let first_value = value.clone();
        let animation = animate_float(0.0, 100.0, AnimationTiming::new(100.0), move |next| {
            first_value.set(next);
        });
        let manager = get_animation_manager();

        manager.tick(100.0);
        manager.tick(140.0);
        assert_eq!(value.get(), 40.0);

        animation.cancel();
        manager.tick(200.0);
        assert_eq!(value.get(), 40.0);
        assert!(!animation.is_running());

        let second_value = value.clone();
        let second = animate_float(0.0, 100.0, AnimationTiming::new(100.0), move |next| {
            second_value.set(next);
        });
        manager.tick(300.0);
        second.finish();
        assert_eq!(value.get(), 100.0);
        assert!(!second.is_running());
        assert!(!manager.has_active_animations());
    }

    #[test]
    fn uses_the_shared_frame_hook_to_tick_active_animations() {
        reset_animations();
        reset_commit_state();
        let owner = Rc::new(Cell::new(-1.0));
        animate_float_with(
            owner.clone(),
            2.0,
            6.0,
            AnimationTiming::new(100.0),
            |slot, value| {
                slot.set(value);
            },
        );

        __fui_on_frame(1000.0);
        assert_eq!(crate::frame_signal::frame_time_signal().value(), 1000.0);
        assert_eq!(owner.get(), 2.0);

        __fui_on_frame(1050.0);
        assert_eq!(owner.get(), 4.0);
    }

    #[test]
    fn requests_render_when_animation_starts() {
        reset_animations();
        reset_commit_state();
        ffi::test::reset();
        animate_float(0.0, 1.0, AnimationTiming::new(50.0), |_| {});
        let calls = ffi::test::take_calls();
        assert_eq!(
            calls
                .iter()
                .filter(|call| matches!(call, Call::RequestRender))
                .count(),
            1
        );
    }
}