| 1 | use std::sync::Arc; |
| 2 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 3 | use std::time::{Duration, Instant}; |
| 4 | |
| 5 | use gpui_kit::{ |
| 6 | DispatchPhase, IntoElement, KeyDownEvent, MouseDownEvent, MouseMoveEvent, ScrollWheelEvent, |
| 7 | canvas, |
| 8 | }; |
| 9 | use tokio::sync::watch; |
| 10 | |
| 11 | pub const IDLE_AFTER: Duration = Duration::from_secs(5 * 60); |
| 12 | const SAMPLE_INTERVAL: Duration = Duration::from_secs(15); |
| 13 | |
| 14 | pub struct UserInput { |
| 15 | origin: Instant, |
| 16 | last_input_millis: AtomicU64, |
| 17 | } |
| 18 | |
| 19 | const NO_INPUT: u64 = u64::MAX; |
| 20 | |
| 21 | impl UserInput { |
| 22 | pub fn new() -> Arc<Self> { |
| 23 | Arc::new(UserInput { |
| 24 | origin: Instant::now(), |
| 25 | last_input_millis: AtomicU64::new(NO_INPUT), |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | pub fn touch(&self) { |
| 30 | let elapsed = self.origin.elapsed().as_millis() as u64; |
| 31 | self.last_input_millis.store(elapsed, Ordering::Relaxed); |
| 32 | } |
| 33 | |
| 34 | fn last_input(&self) -> Option<Instant> { |
| 35 | match self.last_input_millis.load(Ordering::Relaxed) { |
| 36 | NO_INPUT => None, |
| 37 | millis => Some(self.origin + Duration::from_millis(millis)), |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | pub fn input_probe(input: Arc<UserInput>) -> impl IntoElement { |
| 43 | canvas( |
| 44 | |_, _, _| {}, |
| 45 | move |_, _, window, _| { |
| 46 | let moved = input.clone(); |
| 47 | window.on_mouse_event(move |_: &MouseMoveEvent, phase, _, _| { |
| 48 | if phase == DispatchPhase::Capture { |
| 49 | moved.touch(); |
| 50 | } |
| 51 | }); |
| 52 | let pressed = input.clone(); |
| 53 | window.on_mouse_event(move |_: &MouseDownEvent, phase, _, _| { |
| 54 | if phase == DispatchPhase::Capture { |
| 55 | pressed.touch(); |
| 56 | } |
| 57 | }); |
| 58 | let scrolled = input.clone(); |
| 59 | window.on_mouse_event(move |_: &ScrollWheelEvent, phase, _, _| { |
| 60 | if phase == DispatchPhase::Capture { |
| 61 | scrolled.touch(); |
| 62 | } |
| 63 | }); |
| 64 | let typed = input.clone(); |
| 65 | window.on_key_event(move |_: &KeyDownEvent, phase, _, _| { |
| 66 | if phase == DispatchPhase::Capture { |
| 67 | typed.touch(); |
| 68 | } |
| 69 | }); |
| 70 | }, |
| 71 | ) |
| 72 | } |
| 73 | |
| 74 | pub fn latest_input( |
| 75 | app_input: Option<Instant>, |
| 76 | system_idle: Option<Duration>, |
| 77 | now: Instant, |
| 78 | ) -> Option<Instant> { |
| 79 | let system_input = system_idle.and_then(|idle| now.checked_sub(idle)); |
| 80 | app_input.max(system_input) |
| 81 | } |
| 82 | |
| 83 | pub fn is_active(last_input: Option<Instant>, now: Instant, idle_after: Duration) -> bool { |
| 84 | last_input.is_some_and(|last_input| now.saturating_duration_since(last_input) < idle_after) |
| 85 | } |
| 86 | |
| 87 | pub fn activity_change( |
| 88 | previous: bool, |
| 89 | last_input: Option<Instant>, |
| 90 | now: Instant, |
| 91 | idle_after: Duration, |
| 92 | ) -> Option<bool> { |
| 93 | let current = is_active(last_input, now, idle_after); |
| 94 | (current != previous).then_some(current) |
| 95 | } |
| 96 | |
| 97 | pub fn track(input: Arc<UserInput>) -> watch::Receiver<bool> { |
| 98 | let (sender, receiver) = watch::channel(false); |
| 99 | crate::runtime::handle().spawn(async move { |
| 100 | let mut interval = tokio::time::interval(SAMPLE_INTERVAL); |
| 101 | loop { |
| 102 | interval.tick().await; |
| 103 | let now = Instant::now(); |
| 104 | let last_input = latest_input(input.last_input(), system_idle(), now); |
| 105 | let previous = *sender.borrow(); |
| 106 | if let Some(current) = activity_change(previous, last_input, now, IDLE_AFTER) { |
| 107 | sender.send_replace(current); |
| 108 | } |
| 109 | } |
| 110 | }); |
| 111 | receiver |
| 112 | } |
| 113 | |
| 114 | #[cfg(windows)] |
| 115 | fn system_idle() -> Option<Duration> { |
| 116 | use windows::Win32::System::SystemInformation::GetTickCount; |
| 117 | use windows::Win32::UI::Input::KeyboardAndMouse::{GetLastInputInfo, LASTINPUTINFO}; |
| 118 | |
| 119 | let mut info = LASTINPUTINFO { |
| 120 | cbSize: std::mem::size_of::<LASTINPUTINFO>() as u32, |
| 121 | dwTime: 0, |
| 122 | }; |
| 123 | unsafe { GetLastInputInfo(&mut info) }.as_bool().then(|| { |
| 124 | let idle_millis = unsafe { GetTickCount() }.wrapping_sub(info.dwTime); |
| 125 | Duration::from_millis(u64::from(idle_millis)) |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | #[cfg(not(windows))] |
| 130 | fn system_idle() -> Option<Duration> { |
| 131 | None |
| 132 | } |
| 133 | |
| 134 | #[cfg(test)] |
| 135 | mod tests { |
| 136 | use super::*; |
| 137 | |
| 138 | fn seconds(count: u64) -> Duration { |
| 139 | Duration::from_secs(count) |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn active_until_the_idle_threshold() { |
| 144 | let last_input = Instant::now(); |
| 145 | assert!(is_active( |
| 146 | Some(last_input), |
| 147 | last_input + seconds(299), |
| 148 | IDLE_AFTER |
| 149 | )); |
| 150 | assert!(!is_active( |
| 151 | Some(last_input), |
| 152 | last_input + seconds(300), |
| 153 | IDLE_AFTER |
| 154 | )); |
| 155 | assert!(is_active( |
| 156 | Some(last_input + seconds(10)), |
| 157 | last_input, |
| 158 | IDLE_AFTER |
| 159 | )); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn without_any_input_the_user_is_inactive() { |
| 164 | assert!(!is_active(None, Instant::now(), IDLE_AFTER)); |
| 165 | assert_eq!( |
| 166 | activity_change(false, None, Instant::now(), IDLE_AFTER), |
| 167 | None |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn only_a_change_of_state_is_reported() { |
| 173 | let last_input = Some(Instant::now()); |
| 174 | let at = |offset: u64| last_input.unwrap() + seconds(offset); |
| 175 | assert_eq!(activity_change(true, last_input, at(10), IDLE_AFTER), None); |
| 176 | assert_eq!( |
| 177 | activity_change(true, last_input, at(301), IDLE_AFTER), |
| 178 | Some(false) |
| 179 | ); |
| 180 | assert_eq!( |
| 181 | activity_change(false, last_input, at(301), IDLE_AFTER), |
| 182 | None |
| 183 | ); |
| 184 | assert_eq!( |
| 185 | activity_change(false, Some(at(400)), at(401), IDLE_AFTER), |
| 186 | Some(true) |
| 187 | ); |
| 188 | assert_eq!( |
| 189 | activity_change(false, last_input, at(10), IDLE_AFTER), |
| 190 | Some(true) |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn system_input_counts_when_newer_than_app_input() { |
| 196 | let now = Instant::now() + seconds(1000); |
| 197 | let app_input = Some(now - seconds(900)); |
| 198 | assert_eq!( |
| 199 | latest_input(app_input, Some(seconds(20)), now), |
| 200 | Some(now - seconds(20)) |
| 201 | ); |
| 202 | assert_eq!(latest_input(app_input, Some(seconds(2000)), now), app_input); |
| 203 | assert_eq!(latest_input(app_input, None, now), app_input); |
| 204 | assert_eq!( |
| 205 | latest_input(None, Some(seconds(20)), now), |
| 206 | Some(now - seconds(20)) |
| 207 | ); |
| 208 | assert_eq!(latest_input(None, None, now), None); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn touch_sets_and_moves_the_last_input_forward() { |
| 213 | let input = UserInput::new(); |
| 214 | assert_eq!(input.last_input(), None); |
| 215 | input.touch(); |
| 216 | let first = input.last_input().unwrap(); |
| 217 | std::thread::sleep(Duration::from_millis(5)); |
| 218 | input.touch(); |
| 219 | assert!(input.last_input().unwrap() > first); |
| 220 | } |
| 221 | } |