cranpose_app_shell/
frame_rate_boost.rs1use std::time::Duration;
2
3use web_time::Instant;
4
5use crate::FrameUpdateResult;
6
7const BOOST_HOLD_OFF: Duration = Duration::from_secs(3);
9
10const CONTINUOUS_REDRAW_SPAN: Duration = Duration::from_millis(100);
13
14#[derive(Clone, Copy, Debug, Default)]
22pub struct FrameRateBoost {
23 boosted_at: Option<Instant>,
24 redraws: [Option<Instant>; 2],
25}
26
27impl FrameRateBoost {
28 pub fn note_input(&mut self, now: Instant) {
30 self.boosted_at = Some(now);
31 }
32
33 pub fn note_frame(&mut self, result: FrameUpdateResult, now: Instant) {
35 if result.content_moved {
36 self.boosted_at = Some(now);
37 }
38 if result.content_redrawn {
39 let [older, newer] = self.redraws;
40 if older.is_some_and(|at| now.saturating_duration_since(at) < CONTINUOUS_REDRAW_SPAN) {
41 self.boosted_at = Some(now);
42 }
43 self.redraws = [newer, Some(now)];
44 }
45 }
46
47 pub fn boosted(&self, now: Instant) -> bool {
49 self.boosted_at
50 .is_some_and(|at| now.saturating_duration_since(at) < BOOST_HOLD_OFF)
51 }
52}
53
54#[cfg(test)]
55#[path = "tests/frame_rate_boost_tests.rs"]
56mod tests;