hikari-animation 0.3.0

Animation hooks, easing functions and transition utilities for the Hikari design system
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
//! Timer facilities for temporary callbacks and delayed actions
//!
//! Provides a simple timer system for managing temporary state changes
//! and delayed callbacks, useful for:
//! - Scrollbar hover-after-scroll effects
//! - Toast auto-dismissal
//! - Temporary visual feedback
//! - Debouncing and throttling
//! - requestAnimationFrame-based animations

use std::{cell::RefCell, rc::Rc, time::Duration};

use wasm_bindgen::{JsCast, prelude::*};

/// Timer ID for tracking scheduled timers
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TimerId(u32);

impl TimerId {
    /// Create a new timer ID from raw value
    pub fn new(id: u32) -> Self {
        Self(id)
    }

    /// Get raw timer ID
    pub fn as_u32(&self) -> u32 {
        self.0
    }
}

/// Frame ID for tracking requestAnimationFrame callbacks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FrameId(u32);

impl FrameId {
    /// Create a new frame ID from raw value
    pub fn new(id: u32) -> Self {
        Self(id)
    }

    /// Get raw frame ID
    pub fn as_u32(&self) -> u32 {
        self.0
    }
}

/// Callback type for timer events
pub type TimerCallback = Rc<dyn Fn()>;

/// Frame callback type for requestAnimationFrame
pub type FrameCallback = Rc<RefCell<dyn FnMut(f64)>>;

/// Timer manager for scheduling and cancelling delayed callbacks
///
/// # Example
/// ```ignore
/// use animation::TimerManager;
///
/// let manager = TimerManager::new();
///
/// // Schedule a callback
/// let callback = Rc::new(|| {
///     web_sys::console::log_1(&"Timer fired!".into());
/// });
///
/// let id = manager.set_timeout(callback, Duration::from_millis(500));
///
/// // Cancel if needed
/// manager.clear_timeout(id);
/// ```
#[derive(Clone)]
pub struct TimerManager {
    next_id: Rc<RefCell<u32>>,
    timers: Rc<RefCell<std::collections::HashMap<TimerId, i32>>>,
    animation_frames: Rc<RefCell<std::collections::HashMap<FrameId, i32>>>,
}

impl TimerManager {
    /// Create a new timer manager
    pub fn new() -> Self {
        Self {
            next_id: Rc::new(RefCell::new(0)),
            timers: Rc::new(RefCell::new(std::collections::HashMap::new())),
            animation_frames: Rc::new(RefCell::new(std::collections::HashMap::new())),
        }
    }

    /// Schedule a one-shot callback
    ///
    /// # Arguments
    /// * `callback` - Function to call when timer fires
    /// * `delay` - Duration to wait before calling callback
    ///
    /// # Returns
    /// Timer ID that can be used to cancel the timer
    pub fn set_timeout(&self, callback: TimerCallback, delay: Duration) -> TimerId {
        let window = match web_sys::window() {
            Some(w) => w,
            None => return TimerId(0),
        };

        // Generate unique timer ID
        let id = {
            let mut next_id = self.next_id.borrow_mut();
            let id = TimerId(*next_id);
            *next_id = next_id.wrapping_add(1);
            id
        };

        // Schedule timeout
        let callback_clone = callback.clone();
        let callback_js = Closure::wrap(Box::new(move || {
            callback_clone();
        }) as Box<dyn FnMut()>);

        let handle = window.set_timeout_with_callback_and_timeout_and_arguments_0(
            callback_js.as_ref().unchecked_ref(),
            delay.as_millis() as i32,
        );

        match handle {
            Ok(handle) => {
                self.timers.borrow_mut().insert(id, handle);
                // Successfully scheduled, closure will be owned by browser
                callback_js.forget();
            }
            Err(_) => {
                // Failed to schedule, need to manually drop closure
                callback_js.forget();
            }
        }

        id
    }

    /// Cancel a scheduled timer
    ///
    /// # Arguments
    /// * `id` - Timer ID returned by `set_timeout`
    pub fn clear_timeout(&self, id: TimerId) {
        let window = match web_sys::window() {
            Some(w) => w,
            None => return,
        };

        if let Some(handle) = self.timers.borrow_mut().remove(&id) {
            window.clear_timeout_with_handle(handle);
        }
    }

    /// Schedule an interval callback
    ///
    /// # Arguments
    /// * `callback` - Function to call repeatedly
    /// * `interval` - Duration between calls
    ///
    /// # Returns
    /// Timer ID that can be used to cancel the interval
    pub fn set_interval(&self, callback: TimerCallback, interval: Duration) -> TimerId {
        let window = match web_sys::window() {
            Some(w) => w,
            None => return TimerId(0),
        };

        // Generate unique timer ID
        let id = {
            let mut next_id = self.next_id.borrow_mut();
            let id = TimerId(*next_id);
            *next_id = next_id.wrapping_add(1);
            id
        };

        // Schedule interval
        let callback_js = Closure::wrap(Box::new(move || {
            callback();
        }) as Box<dyn FnMut()>);

        let handle = window.set_interval_with_callback_and_timeout_and_arguments_0(
            callback_js.as_ref().unchecked_ref(),
            interval.as_millis() as i32,
        );

        match handle {
            Ok(handle) => {
                self.timers.borrow_mut().insert(id, handle);
                // Successfully scheduled, closure will be owned by browser
                callback_js.forget();
            }
            Err(_) => {
                // Failed to schedule, need to manually drop closure
                callback_js.forget();
            }
        }

        id
    }

    /// Cancel a scheduled interval
    ///
    /// # Arguments
    /// * `id` - Timer ID returned by `set_interval`
    pub fn clear_interval(&self, id: TimerId) {
        self.clear_timeout(id); // Same implementation
    }

    /// Clear all active timers
    pub fn clear_all(&self) {
        let window = match web_sys::window() {
            Some(w) => w,
            None => return,
        };

        let mut timers = self.timers.borrow_mut();
        for (_id, handle) in timers.iter() {
            window.clear_timeout_with_handle(*handle);
        }
        timers.clear();
    }

    /// Schedule a requestAnimationFrame callback
    ///
    /// # Arguments
    ///
    /// * `callback` - Function to call each frame with timestamp (ms since page load)
    ///
    /// # Returns
    ///
    /// Frame ID that can be used to cancel the animation
    ///
    /// # Example
    ///
    /// ```ignore
    /// use animation::TimerManager;
    ///
    /// let manager = TimerManager::new();
    ///
    /// let callback = Rc::new(RefCell::new(move |timestamp| {
    ///     let angle = (timestamp / 100.0) % 360.0;
    ///     // Update element with angle...
    /// }));
    ///
    /// let id = manager.request_animation_frame(callback);
    ///
    /// // Cancel if needed
    /// manager.cancel_animation_frame(id);
    /// ```
    pub fn request_animation_frame(&self, callback: FrameCallback) -> FrameId {
        let window = match web_sys::window() {
            Some(w) => w,
            None => return FrameId(0),
        };

        // Generate unique frame ID
        let id = {
            let mut next_id = self.next_id.borrow_mut();
            let id = FrameId(*next_id);
            *next_id = next_id.wrapping_add(1);
            id
        };

        // Schedule requestAnimationFrame
        let callback_clone = callback.clone();
        let callback_js = Closure::wrap(Box::new(move |timestamp: f64| {
            if let Ok(mut cb) = callback_clone.try_borrow_mut() {
                cb(timestamp);
            }
        }) as Box<dyn FnMut(f64)>);

        let handle = window.request_animation_frame(callback_js.as_ref().unchecked_ref());

        match handle {
            Ok(handle) => {
                let mut frames = self.animation_frames.borrow_mut();
                frames.insert(id, handle);
                callback_js.forget();
            }
            Err(_) => {
                callback_js.forget();
            }
        }

        id
    }

    /// Cancel a requestAnimationFrame callback
    ///
    /// # Arguments
    ///
    /// * `id` - Frame ID returned by `request_animation_frame`
    pub fn cancel_animation_frame(&self, id: FrameId) {
        let window = match web_sys::window() {
            Some(w) => w,
            None => return,
        };

        if let Some(handle) = self.animation_frames.borrow_mut().remove(&id) {
            let _ = window.cancel_animation_frame(handle);
        }
    }

    /// Clear all active animation frames
    pub fn clear_all_animation_frames(&self) {
        let window = match web_sys::window() {
            Some(w) => w,
            None => return,
        };

        let mut frames = self.animation_frames.borrow_mut();
        for (_id, handle) in frames.iter() {
            let _ = window.cancel_animation_frame(*handle);
        }
        frames.clear();
    }
}

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

/// Debounce utility - only call function after delay expires without new calls
///
/// # Example
/// ```ignore
/// use animation::{TimerManager, debounce};
///
/// let manager = TimerManager::new();
/// let debounced = debounce(manager.clone(), Duration::from_millis(300), || {
///     // This will only fire 300ms after the last call
/// });
///
/// debounced(); // Will fire in 300ms
/// debounced(); // Resets timer, will fire in 300ms from now
/// ```
pub fn debounce(
    manager: TimerManager,
    delay: Duration,
    callback: TimerCallback,
) -> Rc<RefCell<dyn Fn()>> {
    struct DebounceState {
        timer_id: Option<TimerId>,
    }

    let state = Rc::new(RefCell::new(DebounceState { timer_id: None }));
    let state_clone = state.clone();

    let debounced = Rc::new(RefCell::new(move || {
        // Cancel existing timer
        if let Some(timer_id) = state_clone.borrow_mut().timer_id.take() {
            manager.clear_timeout(timer_id);
        }

        // Schedule new timer
        let callback_clone = callback.clone();
        let state_for_timer = state.clone();
        let timer_id = manager.set_timeout(
            Rc::new(move || {
                callback_clone();
                state_for_timer.borrow_mut().timer_id = None;
            }),
            delay,
        );
        state_clone.borrow_mut().timer_id = Some(timer_id);
    }));

    debounced
}

/// Throttle utility - only call function once per delay period
///
/// # Example
/// ```ignore
/// use animation::{TimerManager, throttle};
///
/// let manager = TimerManager::new();
/// let throttled = throttle(manager, Duration::from_millis(100), || {
///     // This will fire at most once every 100ms
/// });
///
/// throttled(); // Fires immediately
/// throttled(); // Ignored (within throttle period)
/// ```
pub fn throttle(
    _manager: TimerManager,
    interval: Duration,
    callback: TimerCallback,
) -> Rc<RefCell<dyn Fn()>> {
    struct ThrottleState {
        last_call: Option<f64>,
    }

    let state = Rc::new(RefCell::new(ThrottleState { last_call: None }));

    let throttled = Rc::new(RefCell::new(move || {
        let now = js_sys::Date::now();
        let mut state_ref = state.borrow_mut();

        if let Some(last_time) = state_ref.last_call {
            if now - last_time < interval.as_millis() as f64 {
                return; // Throttled
            }
        }

        state_ref.last_call = Some(now);
        callback();
    }));

    throttled
}