use std::time::Duration;
use web_time::Instant;
use crate::FrameUpdateResult;
const BOOST_HOLD_OFF: Duration = Duration::from_secs(3);
const CONTINUOUS_REDRAW_SPAN: Duration = Duration::from_millis(100);
#[derive(Clone, Copy, Debug, Default)]
pub struct FrameRateBoost {
boosted_at: Option<Instant>,
redraws: [Option<Instant>; 2],
}
impl FrameRateBoost {
pub fn note_input(&mut self, now: Instant) {
self.boosted_at = Some(now);
}
pub fn note_frame(&mut self, result: FrameUpdateResult, now: Instant) {
if result.content_moved {
self.boosted_at = Some(now);
}
if result.content_redrawn {
let [older, newer] = self.redraws;
if older.is_some_and(|at| now.saturating_duration_since(at) < CONTINUOUS_REDRAW_SPAN) {
self.boosted_at = Some(now);
}
self.redraws = [newer, Some(now)];
}
}
pub fn boosted(&self, now: Instant) -> bool {
self.boosted_at
.is_some_and(|at| now.saturating_duration_since(at) < BOOST_HOLD_OFF)
}
}
#[cfg(test)]
#[path = "tests/frame_rate_boost_tests.rs"]
mod tests;