Add center-visible-columns action
Browse files at this commitdefd4c5c4db8b22b2a5db468c0618c37b30e95ddParent: 7227e648 changed files +86 -0
- niri-config/src/lib.rs +2 -0
- niri-ipc/src/lib.rs +2 -0
- resources/default-config.kdl +3 -0
- src/input/mod.rs +5 -0
- src/layout/mod.rs +7 -0
- src/layout/scrolling.rs +58 -0
- src/layout/tests.rs +2 -0
- src/layout/workspace.rs +7 -0
niri-config/src/lib.rsmodified+2 -0
| @@ -1749,6 +1749,7 @@ pub enum Action { | |||
| 1749 | CenterWindow, | 1749 | CenterWindow, |
| 1750 | #[knuffel(skip)] | 1750 | #[knuffel(skip)] |
| 1751 | CenterWindowById(u64), | 1751 | CenterWindowById(u64), |
| 1752 | + CenterVisibleColumns, | ||
| 1752 | FocusWorkspaceDown, | 1753 | FocusWorkspaceDown, |
| 1753 | #[knuffel(skip)] | 1754 | #[knuffel(skip)] |
| 1754 | FocusWorkspaceDownUnderMouse, | 1755 | FocusWorkspaceDownUnderMouse, |
| @@ -1992,6 +1993,7 @@ impl From<niri_ipc::Action> for Action { | |||
| 1992 | niri_ipc::Action::CenterColumn {} => Self::CenterColumn, | 1993 | niri_ipc::Action::CenterColumn {} => Self::CenterColumn, |
| 1993 | niri_ipc::Action::CenterWindow { id: None } => Self::CenterWindow, | 1994 | niri_ipc::Action::CenterWindow { id: None } => Self::CenterWindow, |
| 1994 | niri_ipc::Action::CenterWindow { id: Some(id) } => Self::CenterWindowById(id), | 1995 | niri_ipc::Action::CenterWindow { id: Some(id) } => Self::CenterWindowById(id), |
| 1996 | + niri_ipc::Action::CenterVisibleColumns {} => Self::CenterVisibleColumns, | ||
| 1995 | niri_ipc::Action::FocusWorkspaceDown {} => Self::FocusWorkspaceDown, | 1997 | niri_ipc::Action::FocusWorkspaceDown {} => Self::FocusWorkspaceDown, |
| 1996 | niri_ipc::Action::FocusWorkspaceUp {} => Self::FocusWorkspaceUp, | 1998 | niri_ipc::Action::FocusWorkspaceUp {} => Self::FocusWorkspaceUp, |
| 1997 | niri_ipc::Action::FocusWorkspace { reference } => { | 1999 | niri_ipc::Action::FocusWorkspace { reference } => { |
niri-ipc/src/lib.rsmodified+2 -0
| @@ -426,6 +426,8 @@ pub enum Action { | |||
| 426 | #[cfg_attr(feature = "clap", arg(long))] | 426 | #[cfg_attr(feature = "clap", arg(long))] |
| 427 | id: Option<u64>, | 427 | id: Option<u64>, |
| 428 | }, | 428 | }, |
| 429 | + /// Center all fully visible columns on the screen. | ||
| 430 | + CenterVisibleColumns {}, | ||
| 429 | /// Focus the workspace below. | 431 | /// Focus the workspace below. |
| 430 | FocusWorkspaceDown {}, | 432 | FocusWorkspaceDown {}, |
| 431 | /// Focus the workspace above. | 433 | /// Focus the workspace above. |
resources/default-config.kdlmodified+3 -0
| @@ -525,6 +525,9 @@ binds { | |||
| 525 | 525 | ||
| 526 | Mod+C { center-column; } | 526 | Mod+C { center-column; } |
| 527 | 527 | ||
| 528 | + // Center all fully visible columns on screen. | ||
| 529 | + Mod+Ctrl+C { center-visible-columns; } | ||
| 530 | + | ||
| 528 | // Finer width adjustments. | 531 | // Finer width adjustments. |
| 529 | // This command can also: | 532 | // This command can also: |
| 530 | // * set width in pixels: "1000" | 533 | // * set width in pixels: "1000" |
src/input/mod.rsmodified+5 -0
| @@ -1445,6 +1445,11 @@ impl State { | |||
| 1445 | self.niri.queue_redraw_all(); | 1445 | self.niri.queue_redraw_all(); |
| 1446 | } | 1446 | } |
| 1447 | } | 1447 | } |
| 1448 | + Action::CenterVisibleColumns => { | ||
| 1449 | + self.niri.layout.center_visible_columns(); | ||
| 1450 | + // FIXME: granular | ||
| 1451 | + self.niri.queue_redraw_all(); | ||
| 1452 | + } | ||
| 1448 | Action::MaximizeColumn => { | 1453 | Action::MaximizeColumn => { |
| 1449 | self.niri.layout.toggle_full_width(); | 1454 | self.niri.layout.toggle_full_width(); |
| 1450 | } | 1455 | } |
src/layout/mod.rsmodified+7 -0
| @@ -2332,6 +2332,13 @@ impl<W: LayoutElement> Layout<W> { | |||
| 2332 | workspace.center_window(id); | 2332 | workspace.center_window(id); |
| 2333 | } | 2333 | } |
| 2334 | 2334 | ||
| 2335 | + pub fn center_visible_columns(&mut self) { | ||
| 2336 | + let Some(workspace) = self.active_workspace_mut() else { | ||
| 2337 | + return; | ||
| 2338 | + }; | ||
| 2339 | + workspace.center_visible_columns(); | ||
| 2340 | + } | ||
| 2341 | + | ||
| 2335 | pub fn focus(&self) -> Option<&W> { | 2342 | pub fn focus(&self) -> Option<&W> { |
| 2336 | self.focus_with_output().map(|(win, _out)| win) | 2343 | self.focus_with_output().map(|(win, _out)| win) |
| 2337 | } | 2344 | } |
src/layout/scrolling.rsmodified+58 -0
| @@ -2139,6 +2139,64 @@ impl<W: LayoutElement> ScrollingSpace<W> { | |||
| 2139 | self.center_column(); | 2139 | self.center_column(); |
| 2140 | } | 2140 | } |
| 2141 | 2141 | ||
| 2142 | + pub fn center_visible_columns(&mut self) { | ||
| 2143 | + if self.columns.is_empty() { | ||
| 2144 | + return; | ||
| 2145 | + } | ||
| 2146 | + | ||
| 2147 | + if self.is_centering_focused_column() { | ||
| 2148 | + return; | ||
| 2149 | + } | ||
| 2150 | + | ||
| 2151 | + // Consider the end of an ongoing animation because that's what compute to fit does too. | ||
| 2152 | + let view_x = self.target_view_pos(); | ||
| 2153 | + let working_x = self.working_area.loc.x; | ||
| 2154 | + let working_w = self.working_area.size.w; | ||
| 2155 | + | ||
| 2156 | + // Count all columns that are fully visible inside the working area. | ||
| 2157 | + let mut width_taken = 0.; | ||
| 2158 | + let mut leftmost_col_x = None; | ||
| 2159 | + let mut active_col_x = None; | ||
| 2160 | + | ||
| 2161 | + let gap = self.options.gaps; | ||
| 2162 | + let col_xs = self.column_xs(self.data.iter().copied()); | ||
| 2163 | + for (idx, col_x) in col_xs.take(self.columns.len()).enumerate() { | ||
| 2164 | + if col_x < view_x + working_x + gap { | ||
| 2165 | + // Column goes off-screen to the left. | ||
| 2166 | + continue; | ||
| 2167 | + } | ||
| 2168 | + | ||
| 2169 | + leftmost_col_x.get_or_insert(col_x); | ||
| 2170 | + | ||
| 2171 | + let width = self.data[idx].width; | ||
| 2172 | + if view_x + working_x + working_w < col_x + width + gap { | ||
| 2173 | + // Column goes off-screen to the right. We can stop here. | ||
| 2174 | + break; | ||
| 2175 | + } | ||
| 2176 | + | ||
| 2177 | + if idx == self.active_column_idx { | ||
| 2178 | + active_col_x = Some(col_x); | ||
| 2179 | + } | ||
| 2180 | + | ||
| 2181 | + width_taken += width + gap; | ||
| 2182 | + } | ||
| 2183 | + | ||
| 2184 | + if active_col_x.is_none() { | ||
| 2185 | + // The active column wasn't fully on screen, so we can't meaningfully do anything. | ||
| 2186 | + return; | ||
| 2187 | + } | ||
| 2188 | + | ||
| 2189 | + let col = &mut self.columns[self.active_column_idx]; | ||
| 2190 | + cancel_resize_for_column(&mut self.interactive_resize, col); | ||
| 2191 | + | ||
| 2192 | + let free_space = working_w - width_taken + gap; | ||
| 2193 | + let new_view_x = leftmost_col_x.unwrap() - free_space / 2. - working_x; | ||
| 2194 | + | ||
| 2195 | + self.animate_view_offset(self.active_column_idx, new_view_x - active_col_x.unwrap()); | ||
| 2196 | + // Just in case. | ||
| 2197 | + self.animate_view_offset_to_column(None, self.active_column_idx, None); | ||
| 2198 | + } | ||
| 2199 | + | ||
| 2142 | pub fn view_pos(&self) -> f64 { | 2200 | pub fn view_pos(&self) -> f64 { |
| 2143 | self.column_x(self.active_column_idx) + self.view_offset.current() | 2201 | self.column_x(self.active_column_idx) + self.view_offset.current() |
| 2144 | } | 2202 | } |
src/layout/tests.rsmodified+2 -0
| @@ -464,6 +464,7 @@ enum Op { | |||
| 464 | #[proptest(strategy = "proptest::option::of(1..=5usize)")] | 464 | #[proptest(strategy = "proptest::option::of(1..=5usize)")] |
| 465 | id: Option<usize>, | 465 | id: Option<usize>, |
| 466 | }, | 466 | }, |
| 467 | + CenterVisibleColumns, | ||
| 467 | FocusWorkspaceDown, | 468 | FocusWorkspaceDown, |
| 468 | FocusWorkspaceUp, | 469 | FocusWorkspaceUp, |
| 469 | FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize), | 470 | FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize), |
| @@ -1068,6 +1069,7 @@ impl Op { | |||
| 1068 | let id = id.filter(|id| layout.has_window(id)); | 1069 | let id = id.filter(|id| layout.has_window(id)); |
| 1069 | layout.center_window(id.as_ref()); | 1070 | layout.center_window(id.as_ref()); |
| 1070 | } | 1071 | } |
| 1072 | + Op::CenterVisibleColumns => layout.center_visible_columns(), | ||
| 1071 | Op::FocusWorkspaceDown => layout.switch_workspace_down(), | 1073 | Op::FocusWorkspaceDown => layout.switch_workspace_down(), |
| 1072 | Op::FocusWorkspaceUp => layout.switch_workspace_up(), | 1074 | Op::FocusWorkspaceUp => layout.switch_workspace_up(), |
| 1073 | Op::FocusWorkspace(idx) => layout.switch_workspace(idx), | 1075 | Op::FocusWorkspace(idx) => layout.switch_workspace(idx), |
src/layout/workspace.rsmodified+7 -0
| @@ -1100,6 +1100,13 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1100 | } | 1100 | } |
| 1101 | } | 1101 | } |
| 1102 | 1102 | ||
| 1103 | + pub fn center_visible_columns(&mut self) { | ||
| 1104 | + if self.floating_is_active.get() { | ||
| 1105 | + return; | ||
| 1106 | + } | ||
| 1107 | + self.scrolling.center_visible_columns(); | ||
| 1108 | + } | ||
| 1109 | + | ||
| 1103 | pub fn toggle_width(&mut self) { | 1110 | pub fn toggle_width(&mut self) { |
| 1104 | if self.floating_is_active.get() { | 1111 | if self.floating_is_active.get() { |
| 1105 | self.floating.toggle_window_width(None); | 1112 | self.floating.toggle_window_width(None); |