forge
Explore
Log in

Don't you dare go Hollow

Add center-visible-columns action

Browse files at this commit

defd4c5c4db8b22b2a5db468c0618c37b30e95ddIvan MolodetskikhParent: 7227e64
niri-config/src/lib.rsmodified+2 -0
@@ -1749,6 +1749,7 @@ pub enum Action {
17491749 CenterWindow,
17501750 #[knuffel(skip)]
17511751 CenterWindowById(u64),
1752+ CenterVisibleColumns,
17521753 FocusWorkspaceDown,
17531754 #[knuffel(skip)]
17541755 FocusWorkspaceDownUnderMouse,
@@ -1992,6 +1993,7 @@ impl From<niri_ipc::Action> for Action {
19921993 niri_ipc::Action::CenterColumn {} => Self::CenterColumn,
19931994 niri_ipc::Action::CenterWindow { id: None } => Self::CenterWindow,
19941995 niri_ipc::Action::CenterWindow { id: Some(id) } => Self::CenterWindowById(id),
1996+ niri_ipc::Action::CenterVisibleColumns {} => Self::CenterVisibleColumns,
19951997 niri_ipc::Action::FocusWorkspaceDown {} => Self::FocusWorkspaceDown,
19961998 niri_ipc::Action::FocusWorkspaceUp {} => Self::FocusWorkspaceUp,
19971999 niri_ipc::Action::FocusWorkspace { reference } => {
niri-ipc/src/lib.rsmodified+2 -0
@@ -426,6 +426,8 @@ pub enum Action {
426426 #[cfg_attr(feature = "clap", arg(long))]
427427 id: Option<u64>,
428428 },
429+ /// Center all fully visible columns on the screen.
430+ CenterVisibleColumns {},
429431 /// Focus the workspace below.
430432 FocusWorkspaceDown {},
431433 /// Focus the workspace above.
resources/default-config.kdlmodified+3 -0
@@ -525,6 +525,9 @@ binds {
525525
526526 Mod+C { center-column; }
527527
528+ // Center all fully visible columns on screen.
529+ Mod+Ctrl+C { center-visible-columns; }
530+
528531 // Finer width adjustments.
529532 // This command can also:
530533 // * set width in pixels: "1000"
src/input/mod.rsmodified+5 -0
@@ -1445,6 +1445,11 @@ impl State {
14451445 self.niri.queue_redraw_all();
14461446 }
14471447 }
1448+ Action::CenterVisibleColumns => {
1449+ self.niri.layout.center_visible_columns();
1450+ // FIXME: granular
1451+ self.niri.queue_redraw_all();
1452+ }
14481453 Action::MaximizeColumn => {
14491454 self.niri.layout.toggle_full_width();
14501455 }
src/layout/mod.rsmodified+7 -0
@@ -2332,6 +2332,13 @@ impl<W: LayoutElement> Layout<W> {
23322332 workspace.center_window(id);
23332333 }
23342334
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+
23352342 pub fn focus(&self) -> Option<&W> {
23362343 self.focus_with_output().map(|(win, _out)| win)
23372344 }
src/layout/scrolling.rsmodified+58 -0
@@ -2139,6 +2139,64 @@ impl<W: LayoutElement> ScrollingSpace<W> {
21392139 self.center_column();
21402140 }
21412141
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+
21422200 pub fn view_pos(&self) -> f64 {
21432201 self.column_x(self.active_column_idx) + self.view_offset.current()
21442202 }
src/layout/tests.rsmodified+2 -0
@@ -464,6 +464,7 @@ enum Op {
464464 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
465465 id: Option<usize>,
466466 },
467+ CenterVisibleColumns,
467468 FocusWorkspaceDown,
468469 FocusWorkspaceUp,
469470 FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize),
@@ -1068,6 +1069,7 @@ impl Op {
10681069 let id = id.filter(|id| layout.has_window(id));
10691070 layout.center_window(id.as_ref());
10701071 }
1072+ Op::CenterVisibleColumns => layout.center_visible_columns(),
10711073 Op::FocusWorkspaceDown => layout.switch_workspace_down(),
10721074 Op::FocusWorkspaceUp => layout.switch_workspace_up(),
10731075 Op::FocusWorkspace(idx) => layout.switch_workspace(idx),
src/layout/workspace.rsmodified+7 -0
@@ -1100,6 +1100,13 @@ impl<W: LayoutElement> Workspace<W> {
11001100 }
11011101 }
11021102
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+
11031110 pub fn toggle_width(&mut self) {
11041111 if self.floating_is_active.get() {
11051112 self.floating.toggle_window_width(None);