From df9d721f7462a7edaacb9de4530580a68ab6e8ab Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 18 Mar 2024 18:17:04 +0400 Subject: Implement focus-follows-mouse --- niri-config/src/lib.rs | 4 ++++ resources/default-config.kdl | 3 +++ src/input.rs | 5 +++++ src/niri.rs | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index 2df97cef..5ae57b46 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -71,6 +71,8 @@ pub struct Input { pub disable_power_key_handling: bool, #[knuffel(child)] pub warp_mouse_to_focus: bool, + #[knuffel(child)] + pub focus_follows_mouse: bool, } #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] @@ -1596,6 +1598,7 @@ mod tests { disable-power-key-handling warp-mouse-to-focus + focus-follows-mouse } output "eDP-1" { @@ -1736,6 +1739,7 @@ mod tests { }, disable_power_key_handling: true, warp_mouse_to_focus: true, + focus_follows_mouse: true, }, outputs: vec![Output { off: false, diff --git a/resources/default-config.kdl b/resources/default-config.kdl index 7e4a4bd0..ed3cdc3d 100644 --- a/resources/default-config.kdl +++ b/resources/default-config.kdl @@ -70,6 +70,9 @@ input { // Uncomment this to make the mouse warp to the center of newly focused windows. // warp-mouse-to-focus + + // Focus windows and outputs automatically when moving the mouse into them. + // focus-follows-mouse } // You can configure outputs by their name, which you can find diff --git a/src/input.rs b/src/input.rs index c7a0e264..1d99f550 100644 --- a/src/input.rs +++ b/src/input.rs @@ -903,6 +903,8 @@ impl State { } } + self.niri.handle_focus_follows_mouse(&under); + // Activate a new confinement if necessary. self.niri.maybe_activate_pointer_constraint(new_pos, &under); @@ -967,6 +969,9 @@ impl State { } let under = self.niri.surface_under_and_global_space(pos); + + self.niri.handle_focus_follows_mouse(&under); + self.niri.maybe_activate_pointer_constraint(pos, &under); self.niri.pointer_focus.clone_from(&under); diff --git a/src/niri.rs b/src/niri.rs index ef369e35..f63f05a3 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -3305,6 +3305,28 @@ impl Niri { warn!("error spawning a thread to send MonitorsChanged: {err:?}"); } } + + pub fn handle_focus_follows_mouse(&mut self, new_focus: &PointerFocus) { + if !self.config.borrow().input.focus_follows_mouse { + return; + } + + if self.seat.get_pointer().unwrap().is_grabbed() { + return; + } + + if let Some(output) = &new_focus.output { + if self.pointer_focus.output.as_ref() != Some(output) { + self.layout.focus_output(output); + } + } + + if let Some(window) = &new_focus.window { + if self.pointer_focus.window.as_ref() != Some(window) { + self.layout.activate_window(window); + } + } + } } pub struct ClientState { -- cgit