forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB99bf2dfIvan Molodetskikh1 year ago2106 commits

clock.rs202 lines, 5.2 KiBHistoryRawDownload
1use std::cell::RefCell;
2use std::rc::Rc;
3use std::time::Duration;
4
5use crate::utils::get_monotonic_time;
6
7/// Shareable lazy clock that can change rate.
8///
9/// The clock will fetch the time once and then retain it until explicitly cleared with
10/// [`Clock::clear`].
11#[derive(Debug, Default, Clone)]
12pub struct Clock {
13 inner: Rc<RefCell<AdjustableClock>>,
14}
15
16#[derive(Debug, Default)]
17struct LazyClock {
18 time: Option<Duration>,
19}
20
21/// Clock that can adjust its rate.
22#[derive(Debug)]
23struct AdjustableClock {
24 inner: LazyClock,
25 current_time: Duration,
26 last_seen_time: Duration,
27 rate: f64,
28 complete_instantly: bool,
29}
30
31impl Clock {
32 /// Creates a new clock with the given time.
33 pub fn with_time(time: Duration) -> Self {
34 let clock = AdjustableClock::new(LazyClock::with_time(time));
35 Self {
36 inner: Rc::new(RefCell::new(clock)),
37 }
38 }
39
40 /// Returns the current time.
41 pub fn now(&self) -> Duration {
42 self.inner.borrow_mut().now()
43 }
44
45 /// Returns the underlying time not adjusted for rate change.
46 pub fn now_unadjusted(&self) -> Duration {
47 self.inner.borrow_mut().inner.now()
48 }
49
50 /// Sets the unadjusted clock time.
51 pub fn set_unadjusted(&mut self, time: Duration) {
52 self.inner.borrow_mut().inner.set(time);
53 }
54
55 /// Clears the stored time so it's re-fetched again next.
56 pub fn clear(&mut self) {
57 self.inner.borrow_mut().inner.clear();
58 }
59
60 /// Gets the clock rate.
61 pub fn rate(&self) -> f64 {
62 self.inner.borrow().rate()
63 }
64
65 /// Sets the clock rate.
66 pub fn set_rate(&mut self, rate: f64) {
67 self.inner.borrow_mut().set_rate(rate);
68 }
69
70 /// Returns whether animations should complete instantly.
71 pub fn should_complete_instantly(&self) -> bool {
72 self.inner.borrow().should_complete_instantly()
73 }
74
75 /// Sets whether animations should complete instantly.
76 pub fn set_complete_instantly(&mut self, value: bool) {
77 self.inner.borrow_mut().set_complete_instantly(value);
78 }
79}
80
81impl PartialEq for Clock {
82 fn eq(&self, other: &Self) -> bool {
83 Rc::ptr_eq(&self.inner, &other.inner)
84 }
85}
86
87impl Eq for Clock {}
88
89impl LazyClock {
90 pub fn with_time(time: Duration) -> Self {
91 Self { time: Some(time) }
92 }
93
94 pub fn clear(&mut self) {
95 self.time = None;
96 }
97
98 pub fn set(&mut self, time: Duration) {
99 self.time = Some(time);
100 }
101
102 pub fn now(&mut self) -> Duration {
103 *self.time.get_or_insert_with(get_monotonic_time)
104 }
105}
106
107impl AdjustableClock {
108 pub fn new(mut inner: LazyClock) -> Self {
109 let time = inner.now();
110 Self {
111 inner,
112 current_time: time,
113 last_seen_time: time,
114 rate: 1.,
115 complete_instantly: false,
116 }
117 }
118
119 pub fn rate(&self) -> f64 {
120 self.rate
121 }
122
123 pub fn set_rate(&mut self, rate: f64) {
124 self.rate = rate.clamp(0., 1000.);
125 }
126
127 pub fn should_complete_instantly(&self) -> bool {
128 self.complete_instantly
129 }
130
131 pub fn set_complete_instantly(&mut self, value: bool) {
132 self.complete_instantly = value;
133 }
134
135 pub fn now(&mut self) -> Duration {
136 let time = self.inner.now();
137
138 if self.last_seen_time == time {
139 return self.current_time;
140 }
141
142 if self.last_seen_time < time {
143 let delta = time - self.last_seen_time;
144 let delta = delta.mul_f64(self.rate);
145 self.current_time = self.current_time.saturating_add(delta);
146 } else {
147 let delta = self.last_seen_time - time;
148 let delta = delta.mul_f64(self.rate);
149 self.current_time = self.current_time.saturating_sub(delta);
150 }
151
152 self.last_seen_time = time;
153 self.current_time
154 }
155}
156
157impl Default for AdjustableClock {
158 fn default() -> Self {
159 Self::new(LazyClock::default())
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn frozen_clock() {
169 let mut clock = Clock::with_time(Duration::ZERO);
170 assert_eq!(clock.now(), Duration::ZERO);
171
172 clock.set_unadjusted(Duration::from_millis(100));
173 assert_eq!(clock.now(), Duration::from_millis(100));
174
175 clock.set_unadjusted(Duration::from_millis(200));
176 assert_eq!(clock.now(), Duration::from_millis(200));
177 }
178
179 #[test]
180 fn rate_change() {
181 let mut clock = Clock::with_time(Duration::ZERO);
182 clock.set_rate(0.5);
183
184 clock.set_unadjusted(Duration::from_millis(100));
185 assert_eq!(clock.now_unadjusted(), Duration::from_millis(100));
186 assert_eq!(clock.now(), Duration::from_millis(50));
187
188 clock.set_unadjusted(Duration::from_millis(200));
189 assert_eq!(clock.now_unadjusted(), Duration::from_millis(200));
190 assert_eq!(clock.now(), Duration::from_millis(100));
191
192 clock.set_unadjusted(Duration::from_millis(150));
193 assert_eq!(clock.now_unadjusted(), Duration::from_millis(150));
194 assert_eq!(clock.now(), Duration::from_millis(75));
195
196 clock.set_rate(2.0);
197
198 clock.set_unadjusted(Duration::from_millis(250));
199 assert_eq!(clock.now_unadjusted(), Duration::from_millis(250));
200 assert_eq!(clock.now(), Duration::from_millis(275));
201 }
202}