| 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 | interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); |
| 102 | loop { |
| 103 | interval.tick().await; |
| 104 | let now = Instant::now(); |
| 105 | let last_input = latest_input(input.last_input(), system_idle(), now); |
| 106 | let previous = *sender.borrow(); |
| 107 | if let Some(current) = activity_change(previous, last_input, now, IDLE_AFTER) { |
| 108 | sender.send_replace(current); |
| 109 | } |
| 110 | } |
| 111 | }); |
| 112 | receiver |
| 113 | } |
| 114 | |
| 115 | #[cfg(windows)] |
| 116 | fn system_idle() -> Option<Duration> { |
| 117 | use windows::Win32::System::SystemInformation::GetTickCount; |
| 118 | use windows::Win32::UI::Input::KeyboardAndMouse::{GetLastInputInfo, LASTINPUTINFO}; |
| 119 | |
| 120 | let mut info = LASTINPUTINFO { |
| 121 | cbSize: std::mem::size_of::<LASTINPUTINFO>() as u32, |
| 122 | dwTime: 0, |
| 123 | }; |
| 124 | unsafe { GetLastInputInfo(&mut info) }.as_bool().then(|| { |
| 125 | let idle_millis = unsafe { GetTickCount() }.wrapping_sub(info.dwTime); |
| 126 | Duration::from_millis(u64::from(idle_millis)) |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | #[cfg(target_os = "linux")] |
| 131 | fn system_idle() -> Option<Duration> { |
| 132 | crate::linux_idle::system_idle() |
| 133 | } |
| 134 | |
| 135 | #[cfg(not(any(windows, target_os = "linux")))] |
| 136 | fn system_idle() -> Option<Duration> { |
| 137 | None |
| 138 | } |
| 139 | |
| 140 | #[cfg(test)] |
| 141 | mod tests { |
| 142 | use super::*; |
| 143 | |
| 144 | fn seconds(count: u64) -> Duration { |
| 145 | Duration::from_secs(count) |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn active_until_the_idle_threshold() { |
| 150 | let last_input = Instant::now(); |
| 151 | assert!(is_active( |
| 152 | Some(last_input), |
| 153 | last_input + seconds(299), |
| 154 | IDLE_AFTER |
| 155 | )); |
| 156 | assert!(!is_active( |
| 157 | Some(last_input), |
| 158 | last_input + seconds(300), |
| 159 | IDLE_AFTER |
| 160 | )); |
| 161 | assert!(is_active( |
| 162 | Some(last_input + seconds(10)), |
| 163 | last_input, |
| 164 | IDLE_AFTER |
| 165 | )); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn without_any_input_the_user_is_inactive() { |
| 170 | assert!(!is_active(None, Instant::now(), IDLE_AFTER)); |
| 171 | assert_eq!( |
| 172 | activity_change(false, None, Instant::now(), IDLE_AFTER), |
| 173 | None |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn only_a_change_of_state_is_reported() { |
| 179 | let last_input = Some(Instant::now()); |
| 180 | let at = |offset: u64| last_input.unwrap() + seconds(offset); |
| 181 | assert_eq!(activity_change(true, last_input, at(10), IDLE_AFTER), None); |
| 182 | assert_eq!( |
| 183 | activity_change(true, last_input, at(301), IDLE_AFTER), |
| 184 | Some(false) |
| 185 | ); |
| 186 | assert_eq!( |
| 187 | activity_change(false, last_input, at(301), IDLE_AFTER), |
| 188 | None |
| 189 | ); |
| 190 | assert_eq!( |
| 191 | activity_change(false, Some(at(400)), at(401), IDLE_AFTER), |
| 192 | Some(true) |
| 193 | ); |
| 194 | assert_eq!( |
| 195 | activity_change(false, last_input, at(10), IDLE_AFTER), |
| 196 | Some(true) |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn system_input_counts_when_newer_than_app_input() { |
| 202 | let now = Instant::now() + seconds(1000); |
| 203 | let app_input = Some(now - seconds(900)); |
| 204 | assert_eq!( |
| 205 | latest_input(app_input, Some(seconds(20)), now), |
| 206 | Some(now - seconds(20)) |
| 207 | ); |
| 208 | assert_eq!(latest_input(app_input, Some(seconds(2000)), now), app_input); |
| 209 | assert_eq!(latest_input(app_input, None, now), app_input); |
| 210 | assert_eq!( |
| 211 | latest_input(None, Some(seconds(20)), now), |
| 212 | Some(now - seconds(20)) |
| 213 | ); |
| 214 | assert_eq!(latest_input(None, None, now), None); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn touch_sets_and_moves_the_last_input_forward() { |
| 219 | let input = UserInput::new(); |
| 220 | assert_eq!(input.last_input(), None); |
| 221 | input.touch(); |
| 222 | let first = input.last_input().unwrap(); |
| 223 | std::thread::sleep(Duration::from_millis(5)); |
| 224 | input.touch(); |
| 225 | assert!(input.last_input().unwrap() > first); |
| 226 | } |
| 227 | } |