Add option to warp-mouse-to-focus to always center
Browse files at this commit7f72c358d56453633b57bc353a458709fe878b63Parent: 0d4f0f03 changed files +71 -15
- niri-config/src/lib.rs +32 -2
- src/niri.rs +23 -12
- wiki/Configuration:-Input.md +16 -1
niri-config/src/lib.rsmodified+32 -2
| @@ -95,7 +95,7 @@ pub struct Input { | |||
| 95 | #[knuffel(child)] | 95 | #[knuffel(child)] |
| 96 | pub disable_power_key_handling: bool, | 96 | pub disable_power_key_handling: bool, |
| 97 | #[knuffel(child)] | 97 | #[knuffel(child)] |
| 98 | - pub warp_mouse_to_focus: bool, | 98 | + pub warp_mouse_to_focus: Option<WarpMouseToFocus>, |
| 99 | #[knuffel(child)] | 99 | #[knuffel(child)] |
| 100 | pub focus_follows_mouse: Option<FocusFollowsMouse>, | 100 | pub focus_follows_mouse: Option<FocusFollowsMouse>, |
| 101 | #[knuffel(child)] | 101 | #[knuffel(child)] |
| @@ -369,6 +369,32 @@ pub struct FocusFollowsMouse { | |||
| 369 | pub max_scroll_amount: Option<Percent>, | 369 | pub max_scroll_amount: Option<Percent>, |
| 370 | } | 370 | } |
| 371 | 371 | ||
| 372 | +#[derive(knuffel::Decode, Debug, PartialEq, Eq, Clone, Copy)] | ||
| 373 | +pub struct WarpMouseToFocus { | ||
| 374 | + #[knuffel(property, str)] | ||
| 375 | + pub mode: Option<WarpMouseToFocusMode>, | ||
| 376 | +} | ||
| 377 | + | ||
| 378 | +#[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
| 379 | +pub enum WarpMouseToFocusMode { | ||
| 380 | + CenterXy, | ||
| 381 | + CenterXyAlways, | ||
| 382 | +} | ||
| 383 | + | ||
| 384 | +impl FromStr for WarpMouseToFocusMode { | ||
| 385 | + type Err = miette::Error; | ||
| 386 | + | ||
| 387 | + fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 388 | + match s { | ||
| 389 | + "center-xy" => Ok(Self::CenterXy), | ||
| 390 | + "center-xy-always" => Ok(Self::CenterXyAlways), | ||
| 391 | + _ => Err(miette!( | ||
| 392 | + r#"invalid mode for warp-mouse-to-focus, can be "center-xy" or "center-xy-always" (or leave unset for separate centering)"# | ||
| 393 | + )), | ||
| 394 | + } | ||
| 395 | + } | ||
| 396 | +} | ||
| 397 | + | ||
| 372 | #[derive(Debug, Clone, Copy, PartialEq)] | 398 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 373 | pub struct Percent(pub f64); | 399 | pub struct Percent(pub f64); |
| 374 | 400 | ||
| @@ -4023,7 +4049,11 @@ mod tests { | |||
| 4023 | ), | 4049 | ), |
| 4024 | }, | 4050 | }, |
| 4025 | disable_power_key_handling: true, | 4051 | disable_power_key_handling: true, |
| 4026 | - warp_mouse_to_focus: true, | 4052 | + warp_mouse_to_focus: Some( |
| 4053 | + WarpMouseToFocus { | ||
| 4054 | + mode: None, | ||
| 4055 | + }, | ||
| 4056 | + ), | ||
| 4027 | focus_follows_mouse: Some( | 4057 | focus_follows_mouse: Some( |
| 4028 | FocusFollowsMouse { | 4058 | FocusFollowsMouse { |
| 4029 | max_scroll_amount: None, | 4059 | max_scroll_amount: None, |
src/niri.rsmodified+23 -12
| @@ -14,8 +14,8 @@ use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as | |||
| 14 | use anyhow::{bail, ensure, Context}; | 14 | use anyhow::{bail, ensure, Context}; |
| 15 | use calloop::futures::Scheduler; | 15 | use calloop::futures::Scheduler; |
| 16 | use niri_config::{ | 16 | use niri_config::{ |
| 17 | - Config, FloatOrInt, Key, Modifiers, OutputName, PreviewRender, TrackLayout, WorkspaceReference, | 17 | + Config, FloatOrInt, Key, Modifiers, OutputName, PreviewRender, TrackLayout, |
| 18 | - DEFAULT_BACKGROUND_COLOR, | 18 | + WarpMouseToFocusMode, WorkspaceReference, DEFAULT_BACKGROUND_COLOR, |
| 19 | }; | 19 | }; |
| 20 | use smithay::backend::allocator::Fourcc; | 20 | use smithay::backend::allocator::Fourcc; |
| 21 | use smithay::backend::input::Keycode; | 21 | use smithay::backend::input::Keycode; |
| @@ -510,6 +510,8 @@ struct SurfaceFrameThrottlingState { | |||
| 510 | pub enum CenterCoords { | 510 | pub enum CenterCoords { |
| 511 | Separately, | 511 | Separately, |
| 512 | Both, | 512 | Both, |
| 513 | + // Force centering even if the cursor is already in the rectangle. | ||
| 514 | + BothAlways, | ||
| 513 | } | 515 | } |
| 514 | 516 | ||
| 515 | #[derive(Clone, PartialEq, Eq)] | 517 | #[derive(Clone, PartialEq, Eq)] |
| @@ -753,6 +755,7 @@ impl State { | |||
| 753 | center_f64(rect) | 755 | center_f64(rect) |
| 754 | } | 756 | } |
| 755 | } | 757 | } |
| 758 | + CenterCoords::BothAlways => center_f64(rect), | ||
| 756 | }; | 759 | }; |
| 757 | 760 | ||
| 758 | self.move_cursor(p); | 761 | self.move_cursor(p); |
| @@ -808,19 +811,27 @@ impl State { | |||
| 808 | } | 811 | } |
| 809 | 812 | ||
| 810 | pub fn maybe_warp_cursor_to_focus(&mut self) -> bool { | 813 | pub fn maybe_warp_cursor_to_focus(&mut self) -> bool { |
| 811 | - if !self.niri.config.borrow().input.warp_mouse_to_focus { | 814 | + let focused = match self.niri.config.borrow().input.warp_mouse_to_focus { |
| 812 | - return false; | 815 | + None => return false, |
| 813 | - } | 816 | + Some(inner) => match inner.mode { |
| 814 | - | 817 | + None => CenterCoords::Separately, |
| 815 | - self.move_cursor_to_focused_tile(CenterCoords::Separately) | 818 | + Some(WarpMouseToFocusMode::CenterXy) => CenterCoords::Both, |
| 819 | + Some(WarpMouseToFocusMode::CenterXyAlways) => CenterCoords::BothAlways, | ||
| 820 | + }, | ||
| 821 | + }; | ||
| 822 | + self.move_cursor_to_focused_tile(focused) | ||
| 816 | } | 823 | } |
| 817 | 824 | ||
| 818 | pub fn maybe_warp_cursor_to_focus_centered(&mut self) -> bool { | 825 | pub fn maybe_warp_cursor_to_focus_centered(&mut self) -> bool { |
| 819 | - if !self.niri.config.borrow().input.warp_mouse_to_focus { | 826 | + let focused = match self.niri.config.borrow().input.warp_mouse_to_focus { |
| 820 | - return false; | 827 | + None => return false, |
| 821 | - } | 828 | + Some(inner) => match inner.mode { |
| 822 | - | 829 | + None => CenterCoords::Both, |
| 823 | - self.move_cursor_to_focused_tile(CenterCoords::Both) | 830 | + Some(WarpMouseToFocusMode::CenterXy) => CenterCoords::Both, |
| 831 | + Some(WarpMouseToFocusMode::CenterXyAlways) => CenterCoords::BothAlways, | ||
| 832 | + }, | ||
| 833 | + }; | ||
| 834 | + self.move_cursor_to_focused_tile(focused) | ||
| 824 | } | 835 | } |
| 825 | 836 | ||
| 826 | pub fn refresh_pointer_contents(&mut self) { | 837 | pub fn refresh_pointer_contents(&mut self) { |
wiki/Configuration:-Input.mdmodified+16 -1
| @@ -243,7 +243,7 @@ input { | |||
| 243 | 243 | ||
| 244 | Makes the mouse warp to newly focused windows. | 244 | Makes the mouse warp to newly focused windows. |
| 245 | 245 | ||
| 246 | -X and Y coordinates are computed separately, i.e. if moving the mouse only horizontally is enough to put it inside the newly focused window, then it will move only horizontally. | 246 | +Does not make the cursor visible if it had been hidden. |
| 247 | 247 | ||
| 248 | ```kdl | 248 | ```kdl |
| 249 | input { | 249 | input { |
| @@ -251,6 +251,21 @@ input { | |||
| 251 | } | 251 | } |
| 252 | ``` | 252 | ``` |
| 253 | 253 | ||
| 254 | +By default, the cursor warps *separately* horizontally and vertically. | ||
| 255 | +I.e. if moving the mouse only horizontally is enough to put it inside the newly focused window, then the mouse will move only horizontally, and not vertically. | ||
| 256 | + | ||
| 257 | +<sup>Since: next release</sup> You can customize this with the `mode` property. | ||
| 258 | + | ||
| 259 | +- `mode="center-xy"`: warps by both X and Y coordinates together. | ||
| 260 | +So if the mouse was anywhere outside the newly focused window, it will warp to the center of the window. | ||
| 261 | +- `mode="center-xy-always"`: warps by both X and Y coordinates together, even if the mouse was already somewhere inside the newly focused window. | ||
| 262 | + | ||
| 263 | +```kdl | ||
| 264 | +input { | ||
| 265 | + warp-mouse-to-focus mode="center-xy" | ||
| 266 | +} | ||
| 267 | +``` | ||
| 268 | + | ||
| 254 | #### `focus-follows-mouse` | 269 | #### `focus-follows-mouse` |
| 255 | 270 | ||
| 256 | Focuses windows and outputs automatically when moving the mouse over them. | 271 | Focuses windows and outputs automatically when moving the mouse over them. |