Implement focus-follows-mouse
Browse files at this commitdf9d721f7462a7edaacb9de4530580a68ab6e8abParent: d970abe4 changed files +34 -0
- niri-config/src/lib.rs +4 -0
- resources/default-config.kdl +3 -0
- src/input.rs +5 -0
- src/niri.rs +22 -0
niri-config/src/lib.rsmodified+4 -0
| @@ -71,6 +71,8 @@ pub struct Input { | |||
| 71 | pub disable_power_key_handling: bool, | 71 | pub disable_power_key_handling: bool, |
| 72 | #[knuffel(child)] | 72 | #[knuffel(child)] |
| 73 | pub warp_mouse_to_focus: bool, | 73 | pub warp_mouse_to_focus: bool, |
| 74 | + #[knuffel(child)] | ||
| 75 | + pub focus_follows_mouse: bool, | ||
| 74 | } | 76 | } |
| 75 | 77 | ||
| 76 | #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] | 78 | #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] |
| @@ -1596,6 +1598,7 @@ mod tests { | |||
| 1596 | disable-power-key-handling | 1598 | disable-power-key-handling |
| 1597 | 1599 | ||
| 1598 | warp-mouse-to-focus | 1600 | warp-mouse-to-focus |
| 1601 | + focus-follows-mouse | ||
| 1599 | } | 1602 | } |
| 1600 | 1603 | ||
| 1601 | output "eDP-1" { | 1604 | output "eDP-1" { |
| @@ -1736,6 +1739,7 @@ mod tests { | |||
| 1736 | }, | 1739 | }, |
| 1737 | disable_power_key_handling: true, | 1740 | disable_power_key_handling: true, |
| 1738 | warp_mouse_to_focus: true, | 1741 | warp_mouse_to_focus: true, |
| 1742 | + focus_follows_mouse: true, | ||
| 1739 | }, | 1743 | }, |
| 1740 | outputs: vec![Output { | 1744 | outputs: vec![Output { |
| 1741 | off: false, | 1745 | off: false, |
resources/default-config.kdlmodified+3 -0
| @@ -70,6 +70,9 @@ input { | |||
| 70 | 70 | ||
| 71 | // Uncomment this to make the mouse warp to the center of newly focused windows. | 71 | // Uncomment this to make the mouse warp to the center of newly focused windows. |
| 72 | // warp-mouse-to-focus | 72 | // warp-mouse-to-focus |
| 73 | + | ||
| 74 | + // Focus windows and outputs automatically when moving the mouse into them. | ||
| 75 | + // focus-follows-mouse | ||
| 73 | } | 76 | } |
| 74 | 77 | ||
| 75 | // You can configure outputs by their name, which you can find | 78 | // You can configure outputs by their name, which you can find |
src/input.rsmodified+5 -0
| @@ -903,6 +903,8 @@ impl State { | |||
| 903 | } | 903 | } |
| 904 | } | 904 | } |
| 905 | 905 | ||
| 906 | + self.niri.handle_focus_follows_mouse(&under); | ||
| 907 | + | ||
| 906 | // Activate a new confinement if necessary. | 908 | // Activate a new confinement if necessary. |
| 907 | self.niri.maybe_activate_pointer_constraint(new_pos, &under); | 909 | self.niri.maybe_activate_pointer_constraint(new_pos, &under); |
| 908 | 910 | ||
| @@ -967,6 +969,9 @@ impl State { | |||
| 967 | } | 969 | } |
| 968 | 970 | ||
| 969 | let under = self.niri.surface_under_and_global_space(pos); | 971 | let under = self.niri.surface_under_and_global_space(pos); |
| 972 | + | ||
| 973 | + self.niri.handle_focus_follows_mouse(&under); | ||
| 974 | + | ||
| 970 | self.niri.maybe_activate_pointer_constraint(pos, &under); | 975 | self.niri.maybe_activate_pointer_constraint(pos, &under); |
| 971 | self.niri.pointer_focus.clone_from(&under); | 976 | self.niri.pointer_focus.clone_from(&under); |
| 972 | 977 | ||
src/niri.rsmodified+22 -0
| @@ -3305,6 +3305,28 @@ impl Niri { | |||
| 3305 | warn!("error spawning a thread to send MonitorsChanged: {err:?}"); | 3305 | warn!("error spawning a thread to send MonitorsChanged: {err:?}"); |
| 3306 | } | 3306 | } |
| 3307 | } | 3307 | } |
| 3308 | + | ||
| 3309 | + pub fn handle_focus_follows_mouse(&mut self, new_focus: &PointerFocus) { | ||
| 3310 | + if !self.config.borrow().input.focus_follows_mouse { | ||
| 3311 | + return; | ||
| 3312 | + } | ||
| 3313 | + | ||
| 3314 | + if self.seat.get_pointer().unwrap().is_grabbed() { | ||
| 3315 | + return; | ||
| 3316 | + } | ||
| 3317 | + | ||
| 3318 | + if let Some(output) = &new_focus.output { | ||
| 3319 | + if self.pointer_focus.output.as_ref() != Some(output) { | ||
| 3320 | + self.layout.focus_output(output); | ||
| 3321 | + } | ||
| 3322 | + } | ||
| 3323 | + | ||
| 3324 | + if let Some(window) = &new_focus.window { | ||
| 3325 | + if self.pointer_focus.window.as_ref() != Some(window) { | ||
| 3326 | + self.layout.activate_window(window); | ||
| 3327 | + } | ||
| 3328 | + } | ||
| 3329 | + } | ||
| 3308 | } | 3330 | } |
| 3309 | 3331 | ||
| 3310 | pub struct ClientState { | 3332 | pub struct ClientState { |