Skip to main content

argui_animation/
clock.rs

1use crate::{Duration, Time};
2use std::cell::Cell;
3
4/// Supplies monotonic time to animation sampling.
5pub trait Clock {
6    fn now(&self) -> Time;
7}
8
9/// A deterministic clock intended for tests, previews, and explicit playback.
10#[derive(Debug, Default)]
11pub struct ManualClock {
12    now: Cell<Time>,
13}
14
15impl ManualClock {
16    #[must_use]
17    pub const fn new(now: Time) -> Self {
18        Self {
19            now: Cell::new(now),
20        }
21    }
22
23    pub fn set(&self, now: Time) {
24        self.now.set(now);
25    }
26
27    pub fn advance(&self, duration: Duration) {
28        self.now.set(self.now.get() + duration);
29    }
30}
31
32impl Clock for ManualClock {
33    fn now(&self) -> Time {
34        self.now.get()
35    }
36}