Skip to main content

webizen_studio/render/
motion_loop.rs

1//! RequestAnimationFrame / interval driver for spring-smoothed UI motion.
2
3use crate::render::motion::Spring;
4
5/// Advance a selection-highlight spring toward `1.0` when selected, `0.0` otherwise.
6pub fn step_selection_spring(
7    spring: &mut Spring,
8    selected: bool,
9    theme_class: Option<&str>,
10    dt: f64,
11) -> f64 {
12    spring.set_target(if selected { 1.0 } else { 0.0 });
13    let timeline = crate::render::motion::timeline_from_theme(0.0, dt, theme_class);
14    let v = spring.step(&timeline);
15    (1.0 + v * 0.035).clamp(1.0, 1.04)
16}
17
18/// Impulse the presentation-mode toolbar spring (one-shot pulse on mode switch).
19pub fn trigger_mode_pulse(spring: &mut Spring) {
20    spring.value = 0.0;
21    spring.velocity = 14.0;
22    spring.set_target(0.0);
23}
24
25/// Advance the toolbar pulse spring; returns a scale bump in `[0.0, 0.045]`.
26pub fn step_mode_pulse_spring(spring: &mut Spring, theme_class: Option<&str>, dt: f64) -> f64 {
27    let timeline = crate::render::motion::timeline_from_theme(0.0, dt, theme_class);
28    if timeline.reduced_motion {
29        spring.value = 0.0;
30        spring.velocity = 0.0;
31        return 0.0;
32    }
33    spring.set_target(0.0);
34    spring.step(&timeline);
35    (spring.value.abs() + spring.velocity.abs() * 0.018).clamp(0.0, 0.045)
36}
37
38#[cfg(target_arch = "wasm32")]
39/// Tauri bridge — invokes the host command and decodes the JSON response.
40pub fn spawn_ui_motion_loop(mut on_frame: impl FnMut(f64) + 'static) {
41    use std::cell::RefCell;
42    use std::rc::Rc;
43
44    use gloo_timers::callback::Interval;
45
46    let last = Rc::new(RefCell::new(0.0f64));
47    let interval = Interval::new(16, move || {
48        let now = js_sys::Date::now();
49        let dt = {
50            let mut prev = last.borrow_mut();
51            let dt = if *prev > 0.0 {
52                ((now - *prev) / 1000.0).clamp(0.001, 0.05)
53            } else {
54                0.016
55            };
56            *prev = now;
57            dt
58        };
59        on_frame(dt);
60    });
61    interval.forget();
62}
63
64#[cfg(not(target_arch = "wasm32"))]
65/// Non-desktop fallback.
66pub fn spawn_ui_motion_loop(mut on_frame: impl FnMut(f64) + 'static) {
67    // UI signals are !Send — drive springs on the Aether runtime thread, not a worker.
68    aether::prelude::spawn(async move {
69        let mut last = std::time::Instant::now();
70        loop {
71            tokio::time::sleep(std::time::Duration::from_millis(16)).await;
72            let now = std::time::Instant::now();
73            let dt = now.duration_since(last).as_secs_f64().clamp(0.001, 0.05);
74            last = now;
75            on_frame(dt);
76        }
77    });
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn selection_spring_reaches_target_without_overshoot() {
86        let mut spring = Spring::new(0.0);
87        for _ in 0..120 {
88            step_selection_spring(&mut spring, true, None, 0.016);
89        }
90        assert!(spring.is_at_target(0.02));
91    }
92}