Implement hide cursor on key press and on timeout
Browse files at this commit42a9daec9db57e16541b6ef236b695b71bfb1199Parent: 1ba2be33 changed files +74 -2
- niri-config/src/lib.rs +10 -0
- src/input/mod.rs +43 -2
- src/niri.rs +21 -0
niri-config/src/lib.rsmodified+10 -0
| @@ -615,6 +615,10 @@ pub struct Cursor { | |||
| 615 | pub xcursor_theme: String, | 615 | pub xcursor_theme: String, |
| 616 | #[knuffel(child, unwrap(argument), default = 24)] | 616 | #[knuffel(child, unwrap(argument), default = 24)] |
| 617 | pub xcursor_size: u8, | 617 | pub xcursor_size: u8, |
| 618 | + #[knuffel(child)] | ||
| 619 | + pub hide_on_key_press: bool, | ||
| 620 | + #[knuffel(child, unwrap(argument))] | ||
| 621 | + pub hide_after_inactive_ms: Option<u32>, | ||
| 618 | } | 622 | } |
| 619 | 623 | ||
| 620 | impl Default for Cursor { | 624 | impl Default for Cursor { |
| @@ -622,6 +626,8 @@ impl Default for Cursor { | |||
| 622 | Self { | 626 | Self { |
| 623 | xcursor_theme: String::from("default"), | 627 | xcursor_theme: String::from("default"), |
| 624 | xcursor_size: 24, | 628 | xcursor_size: 24, |
| 629 | + hide_on_key_press: false, | ||
| 630 | + hide_after_inactive_ms: None, | ||
| 625 | } | 631 | } |
| 626 | } | 632 | } |
| 627 | } | 633 | } |
| @@ -2953,6 +2959,8 @@ mod tests { | |||
| 2953 | cursor { | 2959 | cursor { |
| 2954 | xcursor-theme "breeze_cursors" | 2960 | xcursor-theme "breeze_cursors" |
| 2955 | xcursor-size 16 | 2961 | xcursor-size 16 |
| 2962 | + hide-on-key-press | ||
| 2963 | + hide-after-inactive-ms 3000 | ||
| 2956 | } | 2964 | } |
| 2957 | 2965 | ||
| 2958 | screenshot-path "~/Screenshots/screenshot.png" | 2966 | screenshot-path "~/Screenshots/screenshot.png" |
| @@ -3154,6 +3162,8 @@ mod tests { | |||
| 3154 | cursor: Cursor { | 3162 | cursor: Cursor { |
| 3155 | xcursor_theme: String::from("breeze_cursors"), | 3163 | xcursor_theme: String::from("breeze_cursors"), |
| 3156 | xcursor_size: 16, | 3164 | xcursor_size: 16, |
| 3165 | + hide_on_key_press: true, | ||
| 3166 | + hide_after_inactive_ms: Some(3000), | ||
| 3157 | }, | 3167 | }, |
| 3158 | screenshot_path: Some(String::from("~/Screenshots/screenshot.png")), | 3168 | screenshot_path: Some(String::from("~/Screenshots/screenshot.png")), |
| 3159 | hotkey_overlay: HotkeyOverlay { | 3169 | hotkey_overlay: HotkeyOverlay { |
src/input/mod.rsmodified+43 -2
| @@ -2,7 +2,7 @@ use std::any::Any; | |||
| 2 | use std::cmp::min; | 2 | use std::cmp::min; |
| 3 | use std::collections::hash_map::Entry; | 3 | use std::collections::hash_map::Entry; |
| 4 | use std::collections::HashSet; | 4 | use std::collections::HashSet; |
| 5 | -use std::time::Duration; | 5 | +use std::time::{Duration, Instant}; |
| 6 | 6 | ||
| 7 | use calloop::timer::{TimeoutAction, Timer}; | 7 | use calloop::timer::{TimeoutAction, Timer}; |
| 8 | use input::event::gesture::GestureEventCoordinates as _; | 8 | use input::event::gesture::GestureEventCoordinates as _; |
| @@ -87,6 +87,10 @@ impl State { | |||
| 87 | } | 87 | } |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | + if should_update_cursor_timeout(&event) { | ||
| 91 | + self.niri.last_cursor_movement = Instant::now(); | ||
| 92 | + } | ||
| 93 | + | ||
| 90 | let hide_hotkey_overlay = | 94 | let hide_hotkey_overlay = |
| 91 | self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event); | 95 | self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event); |
| 92 | 96 | ||
| @@ -307,6 +311,10 @@ impl State { | |||
| 307 | } | 311 | } |
| 308 | } | 312 | } |
| 309 | 313 | ||
| 314 | + if pressed { | ||
| 315 | + self.hide_cursor_if_needed(); | ||
| 316 | + } | ||
| 317 | + | ||
| 310 | let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input( | 318 | let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input( |
| 311 | self, | 319 | self, |
| 312 | event.key_code(), | 320 | event.key_code(), |
| @@ -349,7 +357,10 @@ impl State { | |||
| 349 | 357 | ||
| 350 | self.handle_bind(bind.clone()); | 358 | self.handle_bind(bind.clone()); |
| 351 | 359 | ||
| 352 | - // Start the key repeat timer if necessary. | 360 | + self.start_key_repeat(bind); |
| 361 | + } | ||
| 362 | + | ||
| 363 | + fn start_key_repeat(&mut self, bind: Bind) { | ||
| 353 | if !bind.repeat { | 364 | if !bind.repeat { |
| 354 | return; | 365 | return; |
| 355 | } | 366 | } |
| @@ -383,6 +394,22 @@ impl State { | |||
| 383 | self.niri.bind_repeat_timer = Some(token); | 394 | self.niri.bind_repeat_timer = Some(token); |
| 384 | } | 395 | } |
| 385 | 396 | ||
| 397 | + fn hide_cursor_if_needed(&mut self) { | ||
| 398 | + if !self.niri.config.borrow().cursor.hide_on_key_press { | ||
| 399 | + return; | ||
| 400 | + } | ||
| 401 | + | ||
| 402 | + // niri keeps this set only while actively using a tablet, which means the cursor position | ||
| 403 | + // is likely to change almost immediately, causing pointer_hidden to just flicker back and | ||
| 404 | + // forth. | ||
| 405 | + if self.niri.tablet_cursor_location.is_some() { | ||
| 406 | + return; | ||
| 407 | + } | ||
| 408 | + | ||
| 409 | + self.niri.pointer_hidden = true; | ||
| 410 | + self.niri.queue_redraw_all(); | ||
| 411 | + } | ||
| 412 | + | ||
| 386 | pub fn handle_bind(&mut self, bind: Bind) { | 413 | pub fn handle_bind(&mut self, bind: Bind) { |
| 387 | let Some(cooldown) = bind.cooldown else { | 414 | let Some(cooldown) = bind.cooldown else { |
| 388 | self.do_action(bind.action, bind.allow_when_locked); | 415 | self.do_action(bind.action, bind.allow_when_locked); |
| @@ -2534,6 +2561,20 @@ fn should_notify_activity<I: InputBackend>(event: &InputEvent<I>) -> bool { | |||
| 2534 | ) | 2561 | ) |
| 2535 | } | 2562 | } |
| 2536 | 2563 | ||
| 2564 | +fn should_update_cursor_timeout<I: InputBackend>(event: &InputEvent<I>) -> bool { | ||
| 2565 | + matches!( | ||
| 2566 | + event, | ||
| 2567 | + InputEvent::PointerAxis { .. } | ||
| 2568 | + | InputEvent::PointerButton { .. } | ||
| 2569 | + | InputEvent::PointerMotion { .. } | ||
| 2570 | + | InputEvent::PointerMotionAbsolute { .. } | ||
| 2571 | + | InputEvent::TabletToolAxis { .. } | ||
| 2572 | + | InputEvent::TabletToolButton { .. } | ||
| 2573 | + | InputEvent::TabletToolProximity { .. } | ||
| 2574 | + | InputEvent::TabletToolTip { .. } | ||
| 2575 | + ) | ||
| 2576 | +} | ||
| 2577 | + | ||
| 2537 | fn allowed_when_locked(action: &Action) -> bool { | 2578 | fn allowed_when_locked(action: &Action) -> bool { |
| 2538 | matches!( | 2579 | matches!( |
| 2539 | action, | 2580 | action, |
src/niri.rsmodified+21 -0
| @@ -267,6 +267,7 @@ pub struct Niri { | |||
| 267 | /// When this happens, the pointer also loses any focus. This is so that touch can prevent | 267 | /// When this happens, the pointer also loses any focus. This is so that touch can prevent |
| 268 | /// various tooltips from sticking around. | 268 | /// various tooltips from sticking around. |
| 269 | pub pointer_hidden: bool, | 269 | pub pointer_hidden: bool, |
| 270 | + pub last_cursor_movement: Instant, | ||
| 270 | // FIXME: this should be able to be removed once PointerFocus takes grabs into account. | 271 | // FIXME: this should be able to be removed once PointerFocus takes grabs into account. |
| 271 | pub pointer_grab_ongoing: bool, | 272 | pub pointer_grab_ongoing: bool, |
| 272 | pub tablet_cursor_location: Option<Point<f64, Logical>>, | 273 | pub tablet_cursor_location: Option<Point<f64, Logical>>, |
| @@ -1860,6 +1861,7 @@ impl Niri { | |||
| 1860 | dnd_icon: None, | 1861 | dnd_icon: None, |
| 1861 | pointer_focus: PointerFocus::default(), | 1862 | pointer_focus: PointerFocus::default(), |
| 1862 | pointer_hidden: false, | 1863 | pointer_hidden: false, |
| 1864 | + last_cursor_movement: Instant::now(), | ||
| 1863 | pointer_grab_ongoing: false, | 1865 | pointer_grab_ongoing: false, |
| 1864 | tablet_cursor_location: None, | 1866 | tablet_cursor_location: None, |
| 1865 | gesture_swipe_3f_cumulative: None, | 1867 | gesture_swipe_3f_cumulative: None, |
| @@ -2663,7 +2665,26 @@ impl Niri { | |||
| 2663 | pointer_elements | 2665 | pointer_elements |
| 2664 | } | 2666 | } |
| 2665 | 2667 | ||
| 2668 | + fn hide_cursor_after_timeout_if_needed(&mut self) { | ||
| 2669 | + if self.pointer_hidden { | ||
| 2670 | + return; | ||
| 2671 | + } | ||
| 2672 | + | ||
| 2673 | + let config = self.config.borrow(); | ||
| 2674 | + let timeout = &config.cursor.hide_after_inactive_ms; | ||
| 2675 | + | ||
| 2676 | + if let Some(duration_ms) = timeout { | ||
| 2677 | + let timeout = Duration::from_millis(*duration_ms as u64); | ||
| 2678 | + | ||
| 2679 | + if self.last_cursor_movement.elapsed() >= timeout { | ||
| 2680 | + self.pointer_hidden = true; | ||
| 2681 | + } | ||
| 2682 | + } | ||
| 2683 | + } | ||
| 2684 | + | ||
| 2666 | pub fn refresh_pointer_outputs(&mut self) { | 2685 | pub fn refresh_pointer_outputs(&mut self) { |
| 2686 | + self.hide_cursor_after_timeout_if_needed(); | ||
| 2687 | + | ||
| 2667 | if self.pointer_hidden { | 2688 | if self.pointer_hidden { |
| 2668 | return; | 2689 | return; |
| 2669 | } | 2690 | } |