Implement focus-follows-mouse max-scroll-amount
Browse files at this commit1da99f4003c100180b4821afc4e32e35c86dfdb4Parent: 120eaa66 changed files +116 -6
- niri-config/src/lib.rs +40 -2
- resources/default-config.kdl +2 -1
- src/layout/mod.rs +16 -0
- src/layout/workspace.rs +31 -0
- src/niri.rs +8 -2
- wiki/Configuration:-Input.md +19 -1
niri-config/src/lib.rsmodified+40 -2
| @@ -75,7 +75,7 @@ pub struct Input { | |||
| 75 | #[knuffel(child)] | 75 | #[knuffel(child)] |
| 76 | pub warp_mouse_to_focus: bool, | 76 | pub warp_mouse_to_focus: bool, |
| 77 | #[knuffel(child)] | 77 | #[knuffel(child)] |
| 78 | - pub focus_follows_mouse: bool, | 78 | + pub focus_follows_mouse: Option<FocusFollowsMouse>, |
| 79 | #[knuffel(child)] | 79 | #[knuffel(child)] |
| 80 | pub workspace_auto_back_and_forth: bool, | 80 | pub workspace_auto_back_and_forth: bool, |
| 81 | } | 81 | } |
| @@ -289,6 +289,15 @@ pub struct Touch { | |||
| 289 | pub map_to_output: Option<String>, | 289 | pub map_to_output: Option<String>, |
| 290 | } | 290 | } |
| 291 | 291 | ||
| 292 | +#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)] | ||
| 293 | +pub struct FocusFollowsMouse { | ||
| 294 | + #[knuffel(property, str)] | ||
| 295 | + pub max_scroll_amount: Option<Percent>, | ||
| 296 | +} | ||
| 297 | + | ||
| 298 | +#[derive(Debug, Clone, Copy, PartialEq)] | ||
| 299 | +pub struct Percent(pub f64); | ||
| 300 | + | ||
| 292 | #[derive(Debug, Default, Clone, PartialEq)] | 301 | #[derive(Debug, Default, Clone, PartialEq)] |
| 293 | pub struct Outputs(pub Vec<Output>); | 302 | pub struct Outputs(pub Vec<Output>); |
| 294 | 303 | ||
| @@ -1794,6 +1803,16 @@ where | |||
| 1794 | } | 1803 | } |
| 1795 | 1804 | ||
| 1796 | impl Animation { | 1805 | impl Animation { |
| 1806 | + pub fn new_off() -> Self { | ||
| 1807 | + Self { | ||
| 1808 | + off: true, | ||
| 1809 | + kind: AnimationKind::Easing(EasingParams { | ||
| 1810 | + duration_ms: 0, | ||
| 1811 | + curve: AnimationCurve::Linear, | ||
| 1812 | + }), | ||
| 1813 | + } | ||
| 1814 | + } | ||
| 1815 | + | ||
| 1797 | fn decode_node<S: knuffel::traits::ErrorSpan>( | 1816 | fn decode_node<S: knuffel::traits::ErrorSpan>( |
| 1798 | node: &knuffel::ast::SpannedNode<S>, | 1817 | node: &knuffel::ast::SpannedNode<S>, |
| 1799 | ctx: &mut knuffel::decode::Context<S>, | 1818 | ctx: &mut knuffel::decode::Context<S>, |
| @@ -2418,6 +2437,23 @@ impl FromStr for TapButtonMap { | |||
| 2418 | } | 2437 | } |
| 2419 | } | 2438 | } |
| 2420 | 2439 | ||
| 2440 | +impl FromStr for Percent { | ||
| 2441 | + type Err = miette::Error; | ||
| 2442 | + | ||
| 2443 | + fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 2444 | + let Some((value, empty)) = s.split_once('%') else { | ||
| 2445 | + return Err(miette!("value must end with '%'")); | ||
| 2446 | + }; | ||
| 2447 | + | ||
| 2448 | + if !empty.is_empty() { | ||
| 2449 | + return Err(miette!("trailing characters after '%' are not allowed")); | ||
| 2450 | + } | ||
| 2451 | + | ||
| 2452 | + let value: f64 = value.parse().map_err(|_| miette!("error parsing value"))?; | ||
| 2453 | + Ok(Percent(value / 100.)) | ||
| 2454 | + } | ||
| 2455 | +} | ||
| 2456 | + | ||
| 2421 | pub fn set_miette_hook() -> Result<(), miette::InstallError> { | 2457 | pub fn set_miette_hook() -> Result<(), miette::InstallError> { |
| 2422 | miette::set_hook(Box::new(|_| Box::new(NarratableReportHandler::new()))) | 2458 | miette::set_hook(Box::new(|_| Box::new(NarratableReportHandler::new()))) |
| 2423 | } | 2459 | } |
| @@ -2664,7 +2700,9 @@ mod tests { | |||
| 2664 | }, | 2700 | }, |
| 2665 | disable_power_key_handling: true, | 2701 | disable_power_key_handling: true, |
| 2666 | warp_mouse_to_focus: true, | 2702 | warp_mouse_to_focus: true, |
| 2667 | - focus_follows_mouse: true, | 2703 | + focus_follows_mouse: Some(FocusFollowsMouse { |
| 2704 | + max_scroll_amount: None, | ||
| 2705 | + }), | ||
| 2668 | workspace_auto_back_and_forth: true, | 2706 | workspace_auto_back_and_forth: true, |
| 2669 | }, | 2707 | }, |
| 2670 | outputs: Outputs(vec![Output { | 2708 | outputs: Outputs(vec![Output { |
resources/default-config.kdlmodified+2 -1
| @@ -44,7 +44,8 @@ input { | |||
| 44 | // warp-mouse-to-focus | 44 | // warp-mouse-to-focus |
| 45 | 45 | ||
| 46 | // Focus windows and outputs automatically when moving the mouse into them. | 46 | // Focus windows and outputs automatically when moving the mouse into them. |
| 47 | - // focus-follows-mouse | 47 | + // Setting max-scroll-amount="0%" makes it work only on windows already fully on screen. |
| 48 | + // focus-follows-mouse max-scroll-amount="0%" | ||
| 48 | } | 49 | } |
| 49 | 50 | ||
| 50 | // You can configure outputs by their name, which you can find | 51 | // You can configure outputs by their name, which you can find |
src/layout/mod.rsmodified+16 -0
| @@ -947,6 +947,22 @@ impl<W: LayoutElement> Layout<W> { | |||
| 947 | } | 947 | } |
| 948 | } | 948 | } |
| 949 | 949 | ||
| 950 | + pub fn scroll_amount_to_activate(&self, window: &W::Id) -> f64 { | ||
| 951 | + let MonitorSet::Normal { monitors, .. } = &self.monitor_set else { | ||
| 952 | + return 0.; | ||
| 953 | + }; | ||
| 954 | + | ||
| 955 | + for mon in monitors { | ||
| 956 | + for ws in &mon.workspaces { | ||
| 957 | + if ws.has_window(window) { | ||
| 958 | + return ws.scroll_amount_to_activate(window); | ||
| 959 | + } | ||
| 960 | + } | ||
| 961 | + } | ||
| 962 | + | ||
| 963 | + 0. | ||
| 964 | + } | ||
| 965 | + | ||
| 950 | pub fn activate_window(&mut self, window: &W::Id) { | 966 | pub fn activate_window(&mut self, window: &W::Id) { |
| 951 | let MonitorSet::Normal { | 967 | let MonitorSet::Normal { |
| 952 | monitors, | 968 | monitors, |
src/layout/workspace.rsmodified+31 -0
| @@ -1391,6 +1391,37 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1391 | } | 1391 | } |
| 1392 | } | 1392 | } |
| 1393 | 1393 | ||
| 1394 | + pub fn scroll_amount_to_activate(&self, window: &W::Id) -> f64 { | ||
| 1395 | + let column_idx = self | ||
| 1396 | + .columns | ||
| 1397 | + .iter() | ||
| 1398 | + .position(|col| col.contains(window)) | ||
| 1399 | + .unwrap(); | ||
| 1400 | + | ||
| 1401 | + if self.active_column_idx == column_idx { | ||
| 1402 | + return 0.; | ||
| 1403 | + } | ||
| 1404 | + | ||
| 1405 | + let current_x = self.view_pos(); | ||
| 1406 | + let new_view_offset = self.compute_new_view_offset_for_column( | ||
| 1407 | + current_x, | ||
| 1408 | + column_idx, | ||
| 1409 | + Some(self.active_column_idx), | ||
| 1410 | + ); | ||
| 1411 | + | ||
| 1412 | + // Consider the end of an ongoing animation because that's what compute to fit does too. | ||
| 1413 | + let final_x = if let Some(ViewOffsetAdjustment::Animation(anim)) = &self.view_offset_adj { | ||
| 1414 | + current_x - self.view_offset + anim.to() | ||
| 1415 | + } else { | ||
| 1416 | + current_x | ||
| 1417 | + }; | ||
| 1418 | + | ||
| 1419 | + let new_col_x = self.column_x(column_idx); | ||
| 1420 | + let from_view_offset = final_x - new_col_x; | ||
| 1421 | + | ||
| 1422 | + (from_view_offset - new_view_offset).abs() / self.working_area.size.w | ||
| 1423 | + } | ||
| 1424 | + | ||
| 1394 | pub fn activate_window(&mut self, window: &W::Id) { | 1425 | pub fn activate_window(&mut self, window: &W::Id) { |
| 1395 | let column_idx = self | 1426 | let column_idx = self |
| 1396 | .columns | 1427 | .columns |
src/niri.rsmodified+8 -2
| @@ -4140,9 +4140,9 @@ impl Niri { | |||
| 4140 | } | 4140 | } |
| 4141 | 4141 | ||
| 4142 | pub fn handle_focus_follows_mouse(&mut self, new_focus: &PointerFocus) { | 4142 | pub fn handle_focus_follows_mouse(&mut self, new_focus: &PointerFocus) { |
| 4143 | - if !self.config.borrow().input.focus_follows_mouse { | 4143 | + let Some(ffm) = self.config.borrow().input.focus_follows_mouse else { |
| 4144 | return; | 4144 | return; |
| 4145 | - } | 4145 | + }; |
| 4146 | 4146 | ||
| 4147 | let pointer = &self.seat.get_pointer().unwrap(); | 4147 | let pointer = &self.seat.get_pointer().unwrap(); |
| 4148 | if pointer.is_grabbed() { | 4148 | if pointer.is_grabbed() { |
| @@ -4160,6 +4160,12 @@ impl Niri { | |||
| 4160 | 4160 | ||
| 4161 | if let Some(window) = &new_focus.window { | 4161 | if let Some(window) = &new_focus.window { |
| 4162 | if current_focus.window.as_ref() != Some(window) { | 4162 | if current_focus.window.as_ref() != Some(window) { |
| 4163 | + if let Some(threshold) = ffm.max_scroll_amount { | ||
| 4164 | + if self.layout.scroll_amount_to_activate(window) > threshold.0 { | ||
| 4165 | + return; | ||
| 4166 | + } | ||
| 4167 | + } | ||
| 4168 | + | ||
| 4163 | self.layout.activate_window(window); | 4169 | self.layout.activate_window(window); |
| 4164 | } | 4170 | } |
| 4165 | } | 4171 | } |
wiki/Configuration:-Input.mdmodified+19 -1
| @@ -68,7 +68,7 @@ input { | |||
| 68 | 68 | ||
| 69 | // disable-power-key-handling | 69 | // disable-power-key-handling |
| 70 | // warp-mouse-to-focus | 70 | // warp-mouse-to-focus |
| 71 | - // focus-follows-mouse | 71 | + // focus-follows-mouse max-scroll-amount="0%" |
| 72 | // workspace-auto-back-and-forth | 72 | // workspace-auto-back-and-forth |
| 73 | } | 73 | } |
| 74 | ``` | 74 | ``` |
| @@ -207,6 +207,24 @@ input { | |||
| 207 | } | 207 | } |
| 208 | ``` | 208 | ``` |
| 209 | 209 | ||
| 210 | +<sup>Since: 0.1.8</sup> You can optionally set `max-scroll-amount`. | ||
| 211 | +Then, focus-follows-mouse won't focus a window if it will result in the view scrolling more than the set amount. | ||
| 212 | +The value is a percentage of the working area width. | ||
| 213 | + | ||
| 214 | +``` | ||
| 215 | +input { | ||
| 216 | + // Allow focus-follows-mouse when it results in scrolling at most 10% of the screen. | ||
| 217 | + focus-follows-mouse max-scroll-amount="10%" | ||
| 218 | +} | ||
| 219 | +``` | ||
| 220 | + | ||
| 221 | +``` | ||
| 222 | +input { | ||
| 223 | + // Allow focus-follows-mouse only when it will not scroll the view. | ||
| 224 | + focus-follows-mouse max-scroll-amount="0%" | ||
| 225 | +} | ||
| 226 | +``` | ||
| 227 | + | ||
| 210 | #### `workspace-auto-back-and-forth` | 228 | #### `workspace-auto-back-and-forth` |
| 211 | 229 | ||
| 212 | Normally, switching to the same workspace by index twice will do nothing (since you're already on that workspace). | 230 | Normally, switching to the same workspace by index twice will do nothing (since you're already on that workspace). |