Add center-window by-id action
Browse files at this commit6c897d520165bfcaf3833b6e3d4bdbd836d26063Parent: 6cb51357 changed files +88 -4
- niri-config/src/lib.rs +5 -0
- niri-ipc/src/lib.rs +12 -0
- src/input/mod.rs +14 -0
- src/layout/floating.rs +3 -3
- src/layout/mod.rs +21 -0
- src/layout/scrolling.rs +22 -0
- src/layout/workspace.rs +11 -1
niri-config/src/lib.rsmodified+5 -0
| @@ -1225,6 +1225,9 @@ pub enum Action { | |||
| 1225 | ConsumeWindowIntoColumn, | 1225 | ConsumeWindowIntoColumn, |
| 1226 | ExpelWindowFromColumn, | 1226 | ExpelWindowFromColumn, |
| 1227 | CenterColumn, | 1227 | CenterColumn, |
| 1228 | + CenterWindow, | ||
| 1229 | + #[knuffel(skip)] | ||
| 1230 | + CenterWindowById(u64), | ||
| 1228 | FocusWorkspaceDown, | 1231 | FocusWorkspaceDown, |
| 1229 | FocusWorkspaceUp, | 1232 | FocusWorkspaceUp, |
| 1230 | FocusWorkspace(#[knuffel(argument)] WorkspaceReference), | 1233 | FocusWorkspace(#[knuffel(argument)] WorkspaceReference), |
| @@ -1368,6 +1371,8 @@ impl From<niri_ipc::Action> for Action { | |||
| 1368 | niri_ipc::Action::ConsumeWindowIntoColumn {} => Self::ConsumeWindowIntoColumn, | 1371 | niri_ipc::Action::ConsumeWindowIntoColumn {} => Self::ConsumeWindowIntoColumn, |
| 1369 | niri_ipc::Action::ExpelWindowFromColumn {} => Self::ExpelWindowFromColumn, | 1372 | niri_ipc::Action::ExpelWindowFromColumn {} => Self::ExpelWindowFromColumn, |
| 1370 | niri_ipc::Action::CenterColumn {} => Self::CenterColumn, | 1373 | niri_ipc::Action::CenterColumn {} => Self::CenterColumn, |
| 1374 | + niri_ipc::Action::CenterWindow { id: None } => Self::CenterWindow, | ||
| 1375 | + niri_ipc::Action::CenterWindow { id: Some(id) } => Self::CenterWindowById(id), | ||
| 1371 | niri_ipc::Action::FocusWorkspaceDown {} => Self::FocusWorkspaceDown, | 1376 | niri_ipc::Action::FocusWorkspaceDown {} => Self::FocusWorkspaceDown, |
| 1372 | niri_ipc::Action::FocusWorkspaceUp {} => Self::FocusWorkspaceUp, | 1377 | niri_ipc::Action::FocusWorkspaceUp {} => Self::FocusWorkspaceUp, |
| 1373 | niri_ipc::Action::FocusWorkspace { reference } => { | 1378 | niri_ipc::Action::FocusWorkspace { reference } => { |
niri-ipc/src/lib.rsmodified+12 -0
| @@ -292,6 +292,18 @@ pub enum Action { | |||
| 292 | ExpelWindowFromColumn {}, | 292 | ExpelWindowFromColumn {}, |
| 293 | /// Center the focused column on the screen. | 293 | /// Center the focused column on the screen. |
| 294 | CenterColumn {}, | 294 | CenterColumn {}, |
| 295 | + /// Center a window on the screen. | ||
| 296 | + #[cfg_attr( | ||
| 297 | + feature = "clap", | ||
| 298 | + clap(about = "Center the focused window on the screen") | ||
| 299 | + )] | ||
| 300 | + CenterWindow { | ||
| 301 | + /// Id of the window to center. | ||
| 302 | + /// | ||
| 303 | + /// If `None`, uses the focused window. | ||
| 304 | + #[cfg_attr(feature = "clap", arg(long))] | ||
| 305 | + id: Option<u64>, | ||
| 306 | + }, | ||
| 295 | /// Focus the workspace below. | 307 | /// Focus the workspace below. |
| 296 | FocusWorkspaceDown {}, | 308 | FocusWorkspaceDown {}, |
| 297 | /// Focus the workspace above. | 309 | /// Focus the workspace above. |
src/input/mod.rsmodified+14 -0
| @@ -1127,6 +1127,20 @@ impl State { | |||
| 1127 | // FIXME: granular | 1127 | // FIXME: granular |
| 1128 | self.niri.queue_redraw_all(); | 1128 | self.niri.queue_redraw_all(); |
| 1129 | } | 1129 | } |
| 1130 | + Action::CenterWindow => { | ||
| 1131 | + self.niri.layout.center_window(None); | ||
| 1132 | + // FIXME: granular | ||
| 1133 | + self.niri.queue_redraw_all(); | ||
| 1134 | + } | ||
| 1135 | + Action::CenterWindowById(id) => { | ||
| 1136 | + let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id); | ||
| 1137 | + let window = window.map(|(_, m)| m.window.clone()); | ||
| 1138 | + if let Some(window) = window { | ||
| 1139 | + self.niri.layout.center_window(Some(&window)); | ||
| 1140 | + // FIXME: granular | ||
| 1141 | + self.niri.queue_redraw_all(); | ||
| 1142 | + } | ||
| 1143 | + } | ||
| 1130 | Action::MaximizeColumn => { | 1144 | Action::MaximizeColumn => { |
| 1131 | self.niri.layout.toggle_full_width(); | 1145 | self.niri.layout.toggle_full_width(); |
| 1132 | } | 1146 | } |
src/layout/floating.rsmodified+3 -3
| @@ -902,11 +902,11 @@ impl<W: LayoutElement> FloatingSpace<W> { | |||
| 902 | self.move_to(idx, new_pos, animate); | 902 | self.move_to(idx, new_pos, animate); |
| 903 | } | 903 | } |
| 904 | 904 | ||
| 905 | - pub fn center_window(&mut self) { | 905 | + pub fn center_window(&mut self, id: Option<&W::Id>) { |
| 906 | - let Some(active_id) = &self.active_window_id else { | 906 | + let Some(id) = id.or(self.active_window_id.as_ref()).cloned() else { |
| 907 | return; | 907 | return; |
| 908 | }; | 908 | }; |
| 909 | - let idx = self.idx_of(active_id).unwrap(); | 909 | + let idx = self.idx_of(&id).unwrap(); |
| 910 | 910 | ||
| 911 | let new_pos = center_preferring_top_left_in_area(self.working_area, self.data[idx].size); | 911 | let new_pos = center_preferring_top_left_in_area(self.working_area, self.data[idx].size); |
| 912 | self.move_to(idx, new_pos, true); | 912 | self.move_to(idx, new_pos, true); |
src/layout/mod.rsmodified+21 -0
| @@ -1993,6 +1993,19 @@ impl<W: LayoutElement> Layout<W> { | |||
| 1993 | monitor.center_column(); | 1993 | monitor.center_column(); |
| 1994 | } | 1994 | } |
| 1995 | 1995 | ||
| 1996 | + pub fn center_window(&mut self, id: Option<&W::Id>) { | ||
| 1997 | + let workspace = if let Some(id) = id { | ||
| 1998 | + Some(self.workspaces_mut().find(|ws| ws.has_window(id)).unwrap()) | ||
| 1999 | + } else { | ||
| 2000 | + self.active_workspace_mut() | ||
| 2001 | + }; | ||
| 2002 | + | ||
| 2003 | + let Some(workspace) = workspace else { | ||
| 2004 | + return; | ||
| 2005 | + }; | ||
| 2006 | + workspace.center_window(id); | ||
| 2007 | + } | ||
| 2008 | + | ||
| 1996 | pub fn focus(&self) -> Option<&W> { | 2009 | pub fn focus(&self) -> Option<&W> { |
| 1997 | self.focus_with_output().map(|(win, _out)| win) | 2010 | self.focus_with_output().map(|(win, _out)| win) |
| 1998 | } | 2011 | } |
| @@ -4390,6 +4403,10 @@ mod tests { | |||
| 4390 | ConsumeWindowIntoColumn, | 4403 | ConsumeWindowIntoColumn, |
| 4391 | ExpelWindowFromColumn, | 4404 | ExpelWindowFromColumn, |
| 4392 | CenterColumn, | 4405 | CenterColumn, |
| 4406 | + CenterWindow { | ||
| 4407 | + #[proptest(strategy = "proptest::option::of(1..=5usize)")] | ||
| 4408 | + id: Option<usize>, | ||
| 4409 | + }, | ||
| 4393 | FocusWorkspaceDown, | 4410 | FocusWorkspaceDown, |
| 4394 | FocusWorkspaceUp, | 4411 | FocusWorkspaceUp, |
| 4395 | FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize), | 4412 | FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize), |
| @@ -4901,6 +4918,10 @@ mod tests { | |||
| 4901 | Op::ConsumeWindowIntoColumn => layout.consume_into_column(), | 4918 | Op::ConsumeWindowIntoColumn => layout.consume_into_column(), |
| 4902 | Op::ExpelWindowFromColumn => layout.expel_from_column(), | 4919 | Op::ExpelWindowFromColumn => layout.expel_from_column(), |
| 4903 | Op::CenterColumn => layout.center_column(), | 4920 | Op::CenterColumn => layout.center_column(), |
| 4921 | + Op::CenterWindow { id } => { | ||
| 4922 | + let id = id.filter(|id| layout.has_window(id)); | ||
| 4923 | + layout.center_window(id.as_ref()); | ||
| 4924 | + } | ||
| 4904 | Op::FocusWorkspaceDown => layout.switch_workspace_down(), | 4925 | Op::FocusWorkspaceDown => layout.switch_workspace_down(), |
| 4905 | Op::FocusWorkspaceUp => layout.switch_workspace_up(), | 4926 | Op::FocusWorkspaceUp => layout.switch_workspace_up(), |
| 4906 | Op::FocusWorkspace(idx) => layout.switch_workspace(idx), | 4927 | Op::FocusWorkspace(idx) => layout.switch_workspace(idx), |
src/layout/scrolling.rsmodified+22 -0
| @@ -1747,6 +1747,28 @@ impl<W: LayoutElement> ScrollingSpace<W> { | |||
| 1747 | cancel_resize_for_column(&mut self.interactive_resize, col); | 1747 | cancel_resize_for_column(&mut self.interactive_resize, col); |
| 1748 | } | 1748 | } |
| 1749 | 1749 | ||
| 1750 | + pub fn center_window(&mut self, window: Option<&W::Id>) { | ||
| 1751 | + if self.columns.is_empty() { | ||
| 1752 | + return; | ||
| 1753 | + } | ||
| 1754 | + | ||
| 1755 | + let col_idx = if let Some(window) = window { | ||
| 1756 | + self.columns | ||
| 1757 | + .iter() | ||
| 1758 | + .position(|col| col.contains(window)) | ||
| 1759 | + .unwrap() | ||
| 1760 | + } else { | ||
| 1761 | + self.active_column_idx | ||
| 1762 | + }; | ||
| 1763 | + | ||
| 1764 | + // We can reasonably center only the active column. | ||
| 1765 | + if col_idx != self.active_column_idx { | ||
| 1766 | + return; | ||
| 1767 | + } | ||
| 1768 | + | ||
| 1769 | + self.center_column(); | ||
| 1770 | + } | ||
| 1771 | + | ||
| 1750 | pub fn view_pos(&self) -> f64 { | 1772 | pub fn view_pos(&self) -> f64 { |
| 1751 | self.column_x(self.active_column_idx) + self.view_offset.current() | 1773 | self.column_x(self.active_column_idx) + self.view_offset.current() |
| 1752 | } | 1774 | } |
src/layout/workspace.rsmodified+11 -1
| @@ -971,12 +971,22 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 971 | 971 | ||
| 972 | pub fn center_column(&mut self) { | 972 | pub fn center_column(&mut self) { |
| 973 | if self.floating_is_active.get() { | 973 | if self.floating_is_active.get() { |
| 974 | - self.floating.center_window(); | 974 | + self.floating.center_window(None); |
| 975 | } else { | 975 | } else { |
| 976 | self.scrolling.center_column(); | 976 | self.scrolling.center_column(); |
| 977 | } | 977 | } |
| 978 | } | 978 | } |
| 979 | 979 | ||
| 980 | + pub fn center_window(&mut self, id: Option<&W::Id>) { | ||
| 981 | + if id.map_or(self.floating_is_active.get(), |id| { | ||
| 982 | + self.floating.has_window(id) | ||
| 983 | + }) { | ||
| 984 | + self.floating.center_window(id); | ||
| 985 | + } else { | ||
| 986 | + self.scrolling.center_window(id); | ||
| 987 | + } | ||
| 988 | + } | ||
| 989 | + | ||
| 980 | pub fn toggle_width(&mut self) { | 990 | pub fn toggle_width(&mut self) { |
| 981 | if self.floating_is_active.get() { | 991 | if self.floating_is_active.get() { |
| 982 | self.floating.toggle_window_width(None); | 992 | self.floating.toggle_window_width(None); |