diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2025-05-12 14:11:38 +0300 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2025-05-12 14:13:51 +0300 |
| commit | defd4c5c4db8b22b2a5db468c0618c37b30e95dd (patch) | |
| tree | d1c52a2799c9e79b703ef5c8568b31edd836999a /src/layout/scrolling.rs | |
| parent | 7227e641496b7b25ba007371f69757153af13f75 (diff) | |
| download | niri-defd4c5c4db8b22b2a5db468c0618c37b30e95dd.tar.gz niri-defd4c5c4db8b22b2a5db468c0618c37b30e95dd.tar.bz2 niri-defd4c5c4db8b22b2a5db468c0618c37b30e95dd.zip | |
Add center-visible-columns action
Diffstat (limited to 'src/layout/scrolling.rs')
| -rw-r--r-- | src/layout/scrolling.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/layout/scrolling.rs b/src/layout/scrolling.rs index bd68a65a..e2d3a6db 100644 --- a/src/layout/scrolling.rs +++ b/src/layout/scrolling.rs @@ -2139,6 +2139,64 @@ impl<W: LayoutElement> ScrollingSpace<W> { self.center_column(); } + pub fn center_visible_columns(&mut self) { + if self.columns.is_empty() { + return; + } + + if self.is_centering_focused_column() { + return; + } + + // Consider the end of an ongoing animation because that's what compute to fit does too. + let view_x = self.target_view_pos(); + let working_x = self.working_area.loc.x; + let working_w = self.working_area.size.w; + + // Count all columns that are fully visible inside the working area. + let mut width_taken = 0.; + let mut leftmost_col_x = None; + let mut active_col_x = None; + + let gap = self.options.gaps; + let col_xs = self.column_xs(self.data.iter().copied()); + for (idx, col_x) in col_xs.take(self.columns.len()).enumerate() { + if col_x < view_x + working_x + gap { + // Column goes off-screen to the left. + continue; + } + + leftmost_col_x.get_or_insert(col_x); + + let width = self.data[idx].width; + if view_x + working_x + working_w < col_x + width + gap { + // Column goes off-screen to the right. We can stop here. + break; + } + + if idx == self.active_column_idx { + active_col_x = Some(col_x); + } + + width_taken += width + gap; + } + + if active_col_x.is_none() { + // The active column wasn't fully on screen, so we can't meaningfully do anything. + return; + } + + let col = &mut self.columns[self.active_column_idx]; + cancel_resize_for_column(&mut self.interactive_resize, col); + + let free_space = working_w - width_taken + gap; + let new_view_x = leftmost_col_x.unwrap() - free_space / 2. - working_x; + + self.animate_view_offset(self.active_column_idx, new_view_x - active_col_x.unwrap()); + // Just in case. + self.animate_view_offset_to_column(None, self.active_column_idx, None); + } + pub fn view_pos(&self) -> f64 { self.column_x(self.active_column_idx) + self.view_offset.current() } |
