| 1 | use std::time::{Duration, Instant}; |
| 2 | |
| 3 | pub const CHROME_IDLE: Duration = Duration::from_secs(3); |
| 4 | pub const HINT_SHOWN: Duration = Duration::from_secs(2); |
| 5 | |
| 6 | #[derive(Debug, Clone, Copy)] |
| 7 | pub struct FullscreenState { |
| 8 | pub entered: Instant, |
| 9 | pub active_at: Instant, |
| 10 | pub hovered: bool, |
| 11 | pub timer_running: bool, |
| 12 | } |
| 13 | |
| 14 | impl FullscreenState { |
| 15 | pub fn new(now: Instant) -> Self { |
| 16 | FullscreenState { |
| 17 | entered: now, |
| 18 | active_at: now, |
| 19 | hovered: false, |
| 20 | timer_running: false, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | pub fn chrome_visible(&self, now: Instant) -> bool { |
| 25 | self.hovered || now.saturating_duration_since(self.active_at) < CHROME_IDLE |
| 26 | } |
| 27 | |
| 28 | pub fn hint_visible(&self, now: Instant) -> bool { |
| 29 | now.saturating_duration_since(self.entered) < HINT_SHOWN |
| 30 | } |
| 31 | |
| 32 | pub fn next_change(&self, now: Instant) -> Option<Duration> { |
| 33 | if self.hovered { |
| 34 | return None; |
| 35 | } |
| 36 | let hide_at = self.active_at + CHROME_IDLE; |
| 37 | (hide_at > now).then(|| hide_at - now) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[cfg(test)] |
| 42 | mod tests { |
| 43 | use super::*; |
| 44 | |
| 45 | #[test] |
| 46 | fn controls_hide_after_three_idle_seconds_and_return_on_movement() { |
| 47 | let start = Instant::now(); |
| 48 | let mut state = FullscreenState::new(start); |
| 49 | assert!(state.chrome_visible(start + Duration::from_millis(2900))); |
| 50 | assert!(!state.chrome_visible(start + CHROME_IDLE)); |
| 51 | state.active_at = start + Duration::from_secs(10); |
| 52 | assert!(state.chrome_visible(start + Duration::from_secs(11))); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn controls_stay_while_the_pointer_is_over_them() { |
| 57 | let start = Instant::now(); |
| 58 | let mut state = FullscreenState::new(start); |
| 59 | state.hovered = true; |
| 60 | assert!(state.chrome_visible(start + Duration::from_secs(60))); |
| 61 | assert_eq!(state.next_change(start), None); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn the_exit_hint_shows_for_two_seconds() { |
| 66 | let start = Instant::now(); |
| 67 | let state = FullscreenState::new(start); |
| 68 | assert!(state.hint_visible(start + Duration::from_millis(1900))); |
| 69 | assert!(!state.hint_visible(start + HINT_SHOWN)); |
| 70 | } |
| 71 | } |