forge
Explore
Log in

Don't you dare go Hollow

Rework pointer inactivity hide as a timer

Browse files at this commit

6fb60dacd2f6743b13ee955234f025015dbe5f87Ivan MolodetskikhParent: 42a9dae
The previous way was prone to triggering late due to compositor idling
and therefore never calling the check function.
src/input/mod.rsmodified+4 -4
@@ -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, Instant};5+use std::time::Duration;
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,8 +87,8 @@ impl State {
87 }87 }
88 }88 }
89 89
90- if should_update_cursor_timeout(&event) {90+ if should_reset_pointer_inactivity_timer(&event) {
91- self.niri.last_cursor_movement = Instant::now();91+ self.niri.reset_pointer_inactivity_timer();
92 }92 }
93 93
94 let hide_hotkey_overlay =94 let hide_hotkey_overlay =
@@ -2561,7 +2561,7 @@ fn should_notify_activity<I: InputBackend>(event: &InputEvent<I>) -> bool {
2561 )2561 )
2562 }2562 }
2563 2563
2564-fn should_update_cursor_timeout<I: InputBackend>(event: &InputEvent<I>) -> bool {2564+fn should_reset_pointer_inactivity_timer<I: InputBackend>(event: &InputEvent<I>) -> bool {
2565 matches!(2565 matches!(
2566 event,2566 event,
2567 InputEvent::PointerAxis { .. }2567 InputEvent::PointerAxis { .. }
src/niri.rsmodified+44 -23
@@ -267,7 +267,7 @@ pub struct Niri {
267 /// When this happens, the pointer also loses any focus. This is so that touch can prevent267 /// 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+ pub pointer_inactivity_timer: Option<RegistrationToken>,
271 // 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.
272 pub pointer_grab_ongoing: bool,272 pub pointer_grab_ongoing: bool,
273 pub tablet_cursor_location: Option<Point<f64, Logical>>,273 pub tablet_cursor_location: Option<Point<f64, Logical>>,
@@ -589,6 +589,7 @@ impl State {
589 589
590 // We moved the pointer, show it.590 // We moved the pointer, show it.
591 self.niri.pointer_hidden = false;591 self.niri.pointer_hidden = false;
592+ self.niri.reset_pointer_inactivity_timer();
592 593
593 // FIXME: granular594 // FIXME: granular
594 self.niri.queue_redraw_all();595 self.niri.queue_redraw_all();
@@ -991,6 +992,7 @@ impl State {
991 let mut window_rules_changed = false;992 let mut window_rules_changed = false;
992 let mut debug_config_changed = false;993 let mut debug_config_changed = false;
993 let mut shaders_changed = false;994 let mut shaders_changed = false;
995+ let mut cursor_inactivity_timeout_changed = false;
994 let mut old_config = self.niri.config.borrow_mut();996 let mut old_config = self.niri.config.borrow_mut();
995 997
996 // Reload the cursor.998 // Reload the cursor.
@@ -1077,6 +1079,10 @@ impl State {
1077 shaders_changed = true;1079 shaders_changed = true;
1078 }1080 }
1079 1081
1082+ if config.cursor.hide_after_inactive_ms != old_config.cursor.hide_after_inactive_ms {
1083+ cursor_inactivity_timeout_changed = true;
1084+ }
1085+
1080 if config.debug != old_config.debug {1086 if config.debug != old_config.debug {
1081 debug_config_changed = true;1087 debug_config_changed = true;
1082 }1088 }
@@ -1123,6 +1129,10 @@ impl State {
1123 self.niri.layout.update_shaders();1129 self.niri.layout.update_shaders();
1124 }1130 }
1125 1131
1132+ if cursor_inactivity_timeout_changed {
1133+ self.niri.reset_pointer_inactivity_timer();
1134+ }
1135+
1126 // Can't really update xdg-decoration settings since we have to hide the globals for CSD1136 // Can't really update xdg-decoration settings since we have to hide the globals for CSD
1127 // due to the SDL2 bug... I don't imagine clients are prepared for the xdg-decoration1137 // due to the SDL2 bug... I don't imagine clients are prepared for the xdg-decoration
1128 // global suddenly appearing? Either way, right now it's live-reloaded in a sense that new1138 // global suddenly appearing? Either way, right now it's live-reloaded in a sense that new
@@ -1784,7 +1794,7 @@ impl Niri {
1784 .unwrap();1794 .unwrap();
1785 1795
1786 drop(config_);1796 drop(config_);
1787- Self {1797+ let mut niri = Self {
1788 config,1798 config,
1789 config_file_output_config,1799 config_file_output_config,
1790 1800
@@ -1861,7 +1871,7 @@ impl Niri {
1861 dnd_icon: None,1871 dnd_icon: None,
1862 pointer_focus: PointerFocus::default(),1872 pointer_focus: PointerFocus::default(),
1863 pointer_hidden: false,1873 pointer_hidden: false,
1864- last_cursor_movement: Instant::now(),1874+ pointer_inactivity_timer: None,
1865 pointer_grab_ongoing: false,1875 pointer_grab_ongoing: false,
1866 tablet_cursor_location: None,1876 tablet_cursor_location: None,
1867 gesture_swipe_3f_cumulative: None,1877 gesture_swipe_3f_cumulative: None,
@@ -1897,7 +1907,11 @@ impl Niri {
1897 1907
1898 #[cfg(feature = "xdp-gnome-screencast")]1908 #[cfg(feature = "xdp-gnome-screencast")]
1899 mapped_cast_output: HashMap::new(),1909 mapped_cast_output: HashMap::new(),
1900- }1910+ };
1911+
1912+ niri.reset_pointer_inactivity_timer();
1913+
1914+ niri
1901 }1915 }
1902 1916
1903 #[cfg(feature = "dbus")]1917 #[cfg(feature = "dbus")]
@@ -2665,26 +2679,7 @@ impl Niri {
2665 pointer_elements2679 pointer_elements
2666 }2680 }
2667 2681
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-
2685 pub fn refresh_pointer_outputs(&mut self) {2682 pub fn refresh_pointer_outputs(&mut self) {
2686- self.hide_cursor_after_timeout_if_needed();
2687-
2688 if self.pointer_hidden {2683 if self.pointer_hidden {
2689 return;2684 return;
2690 }2685 }
@@ -4728,6 +4723,32 @@ impl Niri {
4728 self.queue_redraw_all();4723 self.queue_redraw_all();
4729 }4724 }
4730 }4725 }
4726+
4727+ pub fn reset_pointer_inactivity_timer(&mut self) {
4728+ let _span = tracy_client::span!("Niri::reset_pointer_inactivity_timer");
4729+
4730+ if let Some(token) = self.pointer_inactivity_timer.take() {
4731+ self.event_loop.remove(token);
4732+ }
4733+
4734+ let Some(timeout_ms) = self.config.borrow().cursor.hide_after_inactive_ms else {
4735+ return;
4736+ };
4737+
4738+ let duration = Duration::from_millis(timeout_ms as u64);
4739+ let timer = Timer::from_duration(duration);
4740+ let token = self
4741+ .event_loop
4742+ .insert_source(timer, move |_, _, state| {
4743+ state.niri.pointer_inactivity_timer = None;
4744+ state.niri.pointer_hidden = true;
4745+ state.niri.queue_redraw_all();
4746+
4747+ TimeoutAction::Drop
4748+ })
4749+ .unwrap();
4750+ self.pointer_inactivity_timer = Some(token);
4751+ }
4731 }4752 }
4732 4753
4733 pub struct ClientState {4754 pub struct ClientState {