feat(scrolling): lay out the scrolling layout with the scrolling model

The Scrolling layout is now laid out by komorebi-scrolling instead of by
the layouts crate. A workspace in that layout keeps a ScrollingSpace next
to its containers, makes it match the containers and the focus whenever
the layout is calculated, and takes the rectangles from it. As a result:

- a window that opens gets the width of a column and no other column
  changes width, where every column used to be resized to share the
  viewport until there were as many as `columns`
- focusing a column that is already in view does not scroll the strip
- a column that has been resized keeps its width as windows come and go
- columns that were never resized follow the `columns` option, so
  changing it, or crossing a threshold of layout_options_rules, still
  lays them out again

Resizing changes the width of the focused column, by half of the delta
per edge as the other layouts do. A strip has no heights, so resizing a
height does nothing.

Windows that are partly in view keep their size and overhang the edge of
the screen, unless that would show them on another display, where they
are clamped to the work area as before.

What needs a Workspace is in the scrolling module, and what does not is
in komorebi_layouts::scrolling, which the model comes through. What only
the old Scrolling layout needed goes: the percentage override in
update(), laying the strip out again without resize adjustments in
hide(), the resize constraints, and the feedback of the unclamped
layout.
This commit is contained in:
LGUG2Z committed 2026-10-29 16:38:24 -07:00
1 parent bc962167b7
commit 03cb86d507
5 files changed
+511 -210

No files matched your search

+1
View File
@@ -68,6 +68,7 @@ pub mod notification_center_listener;
pub mod process_command;
pub mod process_event;
pub mod reaper;
pub mod scrolling;
pub mod skylight;
pub mod splash;
pub mod state;
+472
View File
@@ -0,0 +1,472 @@
//! The Scrolling layout of a workspace, which is laid out by [`komorebi_scrolling`] and not by the
//! layouts in `komorebi_layouts`.
//!
//! The other layouts are stateless: where a container goes is a function of the list of
//! containers. A strip of columns that a viewport scrolls over is not, because where the viewport
//! is and how wide each column has been made are facts that cannot be worked out from the list. So
//! a workspace in the Scrolling layout keeps a [`ScrollingSpace`] next to its containers, which is
//! told after every change what the containers are and which one has focus, and which answers with
//! where they all go.
//!
//! The list of containers stays what is true. Every layout change starts from it, and the space is
//! made to match it, so that nothing that changes the list has to know the space exists.
use crate::core::DefaultLayout;
use crate::core::Layout;
use crate::core::LayoutOptions;
use crate::core::OperationDirection;
use crate::core::Rect;
use crate::core::Sizing;
use crate::core::WindowHidingPosition;
use crate::core_graphics::CoreGraphicsApi;
use crate::is_visible_on_another_display;
use crate::workspace::Workspace;
use crate::workspace::clamp_to_work_area;
use color_eyre::eyre;
use komorebi_layouts::scrolling;
use komorebi_layouts::scrolling::MINIMUM_COLUMN_WIDTH;
use komorebi_layouts::scrolling::ScrollingPlacement;
use komorebi_layouts::scrolling::model::ColumnWidth;
use komorebi_layouts::scrolling::model::Options;
use komorebi_layouts::scrolling::model::Viewport;
use komorebi_layouts::scrolling::model::Visibility;
use objc2_core_foundation::CGRect;
/// What the layout options of a workspace come to for the model.
pub(crate) fn scrolling_options(layout_options: Option<LayoutOptions>) -> Options {
scrolling::scrolling_options(layout_options)
}
impl Workspace {
/// Whether the workspace is laid out as a strip of columns.
pub(crate) fn is_scrolling(&self) -> bool {
matches!(self.layout, Layout::Default(DefaultLayout::Scrolling))
}
/// Make the space match the containers, and the focus, as they are now.
fn sync_scrolling(&mut self, viewport: Viewport) {
let options = scrolling_options(self.effective_layout_options());
let ids: Vec<String> = self
.containers()
.iter()
.map(|container| container.id.clone())
.collect();
let focused = self
.focused_container()
.map(|container| container.id.clone());
scrolling::sync(
&mut self.scrolling,
options,
viewport,
&ids,
focused.as_ref(),
);
}
/// Where every container goes in `work_area`, from left to right, with `container_padding`
/// taken off every side of each.
pub(crate) fn layout_scrolling(
&mut self,
work_area: &Rect,
container_padding: i32,
) -> Vec<ScrollingPlacement> {
self.sync_scrolling(scrolling::viewport_of(work_area));
let Some(space) = &self.scrolling else {
return vec![];
};
scrolling::place(space, work_area, container_padding)
}
/// Lay the workspace out as a strip of columns and put every window where it goes.
///
/// * `work_area` is the work area of the monitor, and `adjusted_work_area` is what is left of it
/// once offsets and padding are taken out.
pub(crate) fn arrange_scrolling(
&mut self,
work_area: &Rect,
adjusted_work_area: &Rect,
container_padding: i32,
border_offset: i32,
border_width: i32,
) -> eyre::Result<()> {
let placements = self.layout_scrolling(adjusted_work_area, container_padding);
let screen = CoreGraphicsApi::display_bounds_for_window_rect(CGRect::from(*work_area));
let mut latest_layout = Vec::with_capacity(placements.len());
for (container, placement) in self.containers_mut().iter_mut().zip(&placements) {
let mut rect = placement.rect;
rect.add_padding(border_offset);
rect.add_padding(border_width);
latest_layout.push(rect);
for window in container.windows_mut() {
let result = match placement.visibility {
Visibility::Visible => window.set_position(&rect),
Visibility::Clipped => {
// part of it is in view and the rest is past the edge of the screen,
// which is all right unless there is another display past that edge,
// where the rest would be seen
let mut target = rect;
if overhangs_another_display(&rect, screen) {
clamp_to_work_area(&mut target, adjusted_work_area);
}
window.set_position(&target)
}
Visibility::OffLeft => window.hide_scrolling(WindowHidingPosition::BottomLeft),
Visibility::OffRight => {
window.hide_scrolling(WindowHidingPosition::BottomRight)
}
};
if let Err(error) = result {
tracing::warn!("failed to place window for scrolling layout: {error}");
}
}
}
self.latest_layout = latest_layout;
Ok(())
}
/// Make the focused column wider or narrower. Only the horizontal edges mean anything, since
/// a column is as tall as the viewport.
///
/// The layouts that are made of splits move one edge of a container by half of `delta`, and
/// callers that want to move both, or to follow a mouse, rely on that, so a column changes
/// width by half of `delta` per edge too.
pub(crate) fn resize_scrolling_column(
&mut self,
direction: OperationDirection,
sizing: Sizing,
delta: i32,
) {
if !matches!(
direction,
OperationDirection::Left | OperationDirection::Right
) {
return;
}
let Some(id) = self
.focused_container()
.map(|container| container.id.clone())
else {
return;
};
let Some(space) = self.scrolling.as_mut() else {
return;
};
let Some(width) = space.column_width(&id) else {
return;
};
let change = f64::from(delta) / 2.0;
let width = match sizing {
Sizing::Increase => width + change,
Sizing::Decrease => width - change,
};
let width = (width.round() as i32).max(MINIMUM_COLUMN_WIDTH);
space.set_width(&id, ColumnWidth::Fixed(width));
}
}
/// Whether a window with this rectangle would be seen on a display other than the one that
/// `screen` is the bounds of.
///
/// If the display is not known, it is safer to say that it would.
fn overhangs_another_display(rect: &Rect, screen: Option<CGRect>) -> bool {
match screen {
Some(screen) => is_visible_on_another_display(CGRect::from(*rect), screen),
None => true,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::container::Container;
use crate::core::ScrollingLayoutOptions;
use komorebi_layouts::scrolling::ScrollingCentering;
use komorebi_layouts::scrolling::model::CenterFocusedColumn;
const AREA: Rect = Rect {
left: 100,
top: 50,
right: 1200,
bottom: 800,
};
fn options(
columns: usize,
center_focused_column: Option<ScrollingCentering>,
) -> Option<LayoutOptions> {
Some(LayoutOptions {
scrolling: Some(ScrollingLayoutOptions {
columns,
center_focused_column,
}),
grid: None,
column_ratios: None,
row_ratios: None,
})
}
fn workspace(columns: usize, containers: usize) -> Workspace {
let mut workspace = Workspace {
layout: Layout::Default(DefaultLayout::Scrolling),
layout_options: options(columns, None),
..Default::default()
};
for _ in 0..containers {
workspace.add_container_to_back(Container::default());
}
workspace
}
/// Open a container the way a new window does: to the right of the focused one, and in focus.
fn open(workspace: &mut Workspace) {
let at = if workspace.containers().is_empty() {
0
} else {
workspace.focused_container_idx() + 1
};
workspace.insert_container_at_idx(at, Container::default());
}
fn widths(placements: &[ScrollingPlacement]) -> Vec<i32> {
placements
.iter()
.map(|placement| placement.rect.right)
.collect()
}
#[test]
fn a_column_is_a_share_of_the_viewport_as_wide_as_the_columns_option_says() {
let options = scrolling_options(options(4, None));
assert_eq!(options.default_width, ColumnWidth::Proportion(0.25));
// and three if it says nothing at all
let options = scrolling_options(None);
assert_eq!(options.default_width, ColumnWidth::Proportion(1.0 / 3.0));
// a count of nought cannot be a share of anything
let options = scrolling_options(options_for(0));
assert_eq!(options.default_width, ColumnWidth::Proportion(1.0));
}
fn options_for(columns: usize) -> Option<LayoutOptions> {
options(columns, None)
}
#[test]
fn centring_the_focused_column_is_what_the_layout_option_says() {
for (given, expected) in [
(None, CenterFocusedColumn::Never),
(Some(ScrollingCentering::Never), CenterFocusedColumn::Never),
(
Some(ScrollingCentering::Always),
CenterFocusedColumn::Always,
),
(
Some(ScrollingCentering::OnOverflow),
CenterFocusedColumn::OnOverflow,
),
] {
assert_eq!(
scrolling_options(options(3, given)).center_focused_column,
expected
);
}
}
#[test]
fn containers_are_laid_out_inside_the_work_area_with_their_padding() {
let mut workspace = workspace(4, 3);
let placements = workspace.layout_scrolling(&AREA, 10);
// 1200 / 4 = 300 a column, less 10 off every side
assert_eq!(widths(&placements), [280, 280, 280]);
assert_eq!(placements[0].rect.left, 100 + 10);
assert_eq!(placements[1].rect.left, 100 + 300 + 10);
assert_eq!(placements[0].rect.top, 50 + 10);
assert_eq!(placements[0].rect.bottom, 800 - 20);
}
#[test]
fn a_lone_container_is_not_stretched_to_fill_the_work_area() {
let mut workspace = workspace(3, 1);
let placements = workspace.layout_scrolling(&AREA, 0);
assert_eq!(widths(&placements), [400]);
}
#[test]
fn opening_a_window_does_not_resize_the_others() {
let mut workspace = workspace(3, 0);
for count in 1..=7 {
open(&mut workspace);
let placements = workspace.layout_scrolling(&AREA, 0);
assert_eq!(placements.len(), count);
assert_eq!(widths(&placements), vec![400; count]);
}
}
#[test]
fn the_view_stays_put_while_focus_moves_within_it() {
let mut workspace = workspace(3, 6);
workspace.focus_container(5);
let initial = workspace.layout_scrolling(&AREA, 0);
assert_eq!(visible(&initial), [3, 4, 5]);
// moving focus left across columns that are already in view must not scroll the strip
for idx in [4, 3] {
workspace.focus_container(idx);
assert_eq!(
workspace.layout_scrolling(&AREA, 0),
initial,
"column {idx}"
);
}
// leaving the view on the left scrolls just far enough to bring the column in
workspace.focus_container(2);
let scrolled = workspace.layout_scrolling(&AREA, 0);
assert_eq!(visible(&scrolled), [2, 3, 4]);
// and moving right within the new view leaves it where it is
for idx in [3, 4] {
workspace.focus_container(idx);
assert_eq!(
workspace.layout_scrolling(&AREA, 0),
scrolled,
"column {idx}"
);
}
}
fn visible(placements: &[ScrollingPlacement]) -> Vec<usize> {
placements
.iter()
.enumerate()
.filter(|(_, placement)| placement.visibility == Visibility::Visible)
.map(|(idx, _)| idx)
.collect()
}
#[test]
fn a_container_that_scrolls_out_of_view_says_which_side_it_went_by() {
let mut workspace = workspace(3, 6);
// the strip scrolls to the end, and then focus moves back to where the last three begin
workspace.focus_container(5);
workspace.layout_scrolling(&AREA, 0);
workspace.focus_container(3);
let placements = workspace.layout_scrolling(&AREA, 0);
let visibility: Vec<_> = placements
.iter()
.map(|placement| placement.visibility)
.collect();
assert_eq!(
visibility,
[
Visibility::OffLeft,
Visibility::OffLeft,
Visibility::OffLeft,
Visibility::Visible,
Visibility::Visible,
Visibility::Visible,
]
);
// a column that is out of view keeps the rectangle it has in the strip
assert_eq!(placements[2].rect.left, 100 - 400);
}
#[test]
fn a_column_that_was_made_wider_stays_wider_when_others_come_and_go() {
let mut workspace = workspace(3, 3);
workspace.layout_scrolling(&AREA, 0);
workspace.focus_container(1);
workspace.resize_scrolling_column(OperationDirection::Right, Sizing::Increase, 200);
let placements = workspace.layout_scrolling(&AREA, 0);
assert_eq!(widths(&placements), [400, 500, 400]);
// a window opens to the right of the focused column, and moving focus does not change a width
open(&mut workspace);
workspace.focus_container(0);
let placements = workspace.layout_scrolling(&AREA, 0);
assert_eq!(widths(&placements), [400, 500, 400, 400]);
}
#[test]
fn resizing_changes_the_width_by_half_of_the_delta_and_never_below_the_minimum() {
let mut workspace = workspace(3, 2);
workspace.layout_scrolling(&AREA, 0);
workspace.resize_scrolling_column(OperationDirection::Left, Sizing::Increase, 100);
assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0))[1], 450);
workspace.resize_scrolling_column(OperationDirection::Right, Sizing::Decrease, 100);
assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0))[1], 400);
workspace.resize_scrolling_column(OperationDirection::Right, Sizing::Decrease, 10_000);
assert_eq!(
widths(&workspace.layout_scrolling(&AREA, 0))[1],
MINIMUM_COLUMN_WIDTH
);
}
#[test]
fn a_column_is_as_tall_as_the_work_area_so_resizing_its_height_does_nothing() {
let mut workspace = workspace(3, 2);
let before = workspace.layout_scrolling(&AREA, 0);
workspace.resize_scrolling_column(OperationDirection::Up, Sizing::Increase, 100);
workspace.resize_scrolling_column(OperationDirection::Down, Sizing::Decrease, 100);
assert_eq!(workspace.layout_scrolling(&AREA, 0), before);
}
#[test]
fn resizing_before_there_is_a_layout_does_nothing() {
// the space is made by the first layout, and there has not been one
let mut workspace = workspace(3, 2);
workspace.resize_scrolling_column(OperationDirection::Right, Sizing::Increase, 100);
assert!(workspace.scrolling.is_none());
}
#[test]
fn changing_the_columns_option_changes_the_width_of_columns_that_have_not_been_resized() {
let mut workspace = workspace(3, 3);
assert_eq!(
widths(&workspace.layout_scrolling(&AREA, 0)),
[400, 400, 400]
);
workspace.set_scrolling_columns(4);
assert_eq!(
widths(&workspace.layout_scrolling(&AREA, 0)),
[300, 300, 300]
);
}
}
+1 -1
View File
@@ -129,7 +129,7 @@ impl From<&WindowManager> for State {
workspace_padding: workspace.workspace_padding,
container_padding: workspace.container_padding,
latest_layout: workspace.latest_layout.clone(),
scrolling_viewport_layout: workspace.scrolling_viewport_layout.clone(),
scrolling: workspace.scrolling.clone(),
resize_dimensions: workspace.resize_dimensions.clone(),
tile: workspace.tile,
work_area_offset: workspace.work_area_offset,
+11 -1
View File
@@ -1561,6 +1561,16 @@ impl WindowManager {
}
WorkspaceLayer::Tiling => {
match workspace.layout {
Layout::Default(DefaultLayout::Scrolling) => {
tracing::info!("resizing column");
workspace.resize_scrolling_column(direction, sizing, delta);
return if update {
self.update_focused_workspace(false, false)
} else {
Ok(())
};
}
Layout::Default(layout) => {
tracing::info!("resizing window");
let len = NonZeroUsize::new(workspace.containers().len())
@@ -1589,7 +1599,7 @@ impl WindowManager {
&[],
workspace.focused_container_idx(),
workspace.layout_options,
workspace.layout_feedback(),
&workspace.latest_layout,
);
let mut direction = direction;
+26 -208
View File
@@ -25,6 +25,7 @@ use crate::window::Window;
use crate::window::WindowDetails;
use color_eyre::eyre;
use color_eyre::eyre::OptionExt;
use komorebi_layouts::scrolling::model::ScrollingSpace;
use komorebi_themes::Base16ColourPalette;
use komorebi_themes::KomorebiTheme;
use komorebi_themes::KomorebiThemeCustom;
@@ -65,11 +66,11 @@ pub struct Workspace {
pub workspace_padding: Option<i32>,
pub container_padding: Option<i32>,
pub latest_layout: Vec<Rect>,
/// The most recent layout exactly as the arrangement algorithm produced it, before it was
/// clamped to the work area. The Scrolling layout derives its viewport from the previous
/// layout, which only works if that layout still says where the off-screen columns are.
/// The strip of columns that the Scrolling layout lays out, which has state of its own: how
/// wide each column is, and which part of the strip is in view. It is made to match the
/// containers whenever the layout is calculated. See [`crate::scrolling`].
#[serde(skip)]
pub scrolling_viewport_layout: Vec<Rect>,
pub(crate) scrolling: Option<ScrollingSpace<String>>,
pub resize_dimensions: Vec<Option<Rect>>,
pub tile: bool,
pub work_area_offset: Option<Rect>,
@@ -127,7 +128,7 @@ impl Default for Workspace {
work_area_offset: None,
work_area_offset_rules: vec![],
latest_layout: vec![],
scrolling_viewport_layout: vec![],
scrolling: None,
layout_flip: None,
layout_options: None,
layout_rules: vec![],
@@ -328,7 +329,7 @@ impl Workspace {
/// 1. Try threshold match from rules (highest matching threshold wins)
/// 2. If a rule matches -> use it (full replacement of base)
/// 3. Else -> use the base `layout_options`
fn effective_layout_options(&self) -> Option<LayoutOptions> {
pub(crate) fn effective_layout_options(&self) -> Option<LayoutOptions> {
let container_count = self.containers().len();
let has_workspace_overrides =
@@ -892,25 +893,10 @@ impl Workspace {
}
}
let mut resize_dimensions = None;
if matches!(self.layout, Layout::Default(DefaultLayout::Scrolling))
&& self.resize_dimensions.iter().any(|r| r.is_some())
{
resize_dimensions = Some(self.resize_dimensions.clone());
self.resize_dimensions = Vec::new();
if let Err(error) = self.update() {
tracing::error!("{error}");
}
}
for container in self.containers_mut() {
container.hide(window_hiding_position, omit)?;
}
if let Some(resize_dimensions) = resize_dimensions {
self.resize_dimensions = resize_dimensions;
}
// if let Some(window) = self.maximized_window() {
// window.hide();
// }
@@ -1296,21 +1282,6 @@ pub(crate) fn clamp_to_work_area(layout: &mut Rect, work_area: &Rect) {
}
impl Workspace {
/// The previous layout, as it is handed back to the arrangement algorithm.
///
/// The Scrolling layout works out which column was first in view from where the previous
/// layout put its columns, so it has to see them where the algorithm placed them. The layout
/// in `latest_layout` has been clamped to the work area (see [`clamp_to_work_area`]), which
/// pulls every off-screen column on to the left edge and makes the strip look like it has
/// never scrolled.
pub fn layout_feedback(&self) -> &[Rect] {
if matches!(self.layout, Layout::Default(DefaultLayout::Scrolling)) {
&self.scrolling_viewport_layout
} else {
&self.latest_layout
}
}
/// Set how many columns the Scrolling layout shows at once, keeping every other layout option.
pub fn set_scrolling_columns(&mut self, columns: usize) {
let mut options = self.layout_options.unwrap_or(LayoutOptions {
@@ -1349,7 +1320,7 @@ impl Workspace {
self.layout_options_rules.len(),
);
let layouts = self.layout.as_boxed_arrangement().calculate(
Ok(self.layout.as_boxed_arrangement().calculate(
work_area,
NonZeroUsize::new(self.containers().len()).ok_or_eyre(
"there must be at least one container to calculate a workspace layout",
@@ -1359,20 +1330,16 @@ impl Workspace {
&self.resize_dimensions,
self.focused_container_idx(),
effective_layout_options,
self.layout_feedback(),
);
if matches!(self.layout, Layout::Default(DefaultLayout::Scrolling)) {
self.scrolling_viewport_layout = layouts.clone();
} else {
// a layout from another algorithm says nothing about where Scrolling left its columns
self.scrolling_viewport_layout.clear();
}
Ok(layouts)
&self.latest_layout,
))
}
pub fn update(&mut self) -> eyre::Result<()> {
// a strip of columns is only kept for as long as the workspace is laid out as one
if !self.is_scrolling() {
self.scrolling = None;
}
// make sure we are never holding on to empty containers
self.prune_duplicate_windows()?;
let focused_container_idx = self.focused_container_idx();
@@ -1491,13 +1458,19 @@ impl Workspace {
window.set_position(&adjusted_work_area)?;
};
} else if !self.containers().is_empty() {
if self.is_scrolling() {
return self.arrange_scrolling(
&work_area,
&adjusted_work_area,
container_padding,
border_offset,
border_width,
);
}
let mut layouts =
self.calculate_layouts(&adjusted_work_area, Some(container_padding))?;
let is_scrolling =
matches!(self.layout, Layout::Default(DefaultLayout::Scrolling));
let resize_dimensions_is_empty = self.resize_dimensions.is_empty();
let containers = self.containers_mut();
for (i, container) in containers.iter_mut().enumerate() {
@@ -1509,46 +1482,7 @@ impl Workspace {
layout.add_padding(border_width);
for window in container.windows_mut() {
let proposed_percentage =
work_area.percentage_within_horizontal_bounds(layout);
// where the window is now only matters when the layout would put it
// entirely out of view, and finding out means asking the app
let percentage_override =
proposed_percentage == 0.0 && resize_dimensions_is_empty && {
let current_rect = MacosApi::window_rect(&window.element)
.unwrap_or_default();
let current_percentage = work_area
.percentage_within_horizontal_bounds(&Rect::from(
current_rect,
));
current_percentage > 0.1 && current_percentage < 100.0
};
if is_scrolling && proposed_percentage < 0.1 && !percentage_override
{
// for the scrolling layout we pick the hiding position based
// on the closest edge to make the animations back in to the
// scrolling viewport nicer (i.e. not all the way across the
// screen in the worst case scenario)
let scrolling_hiding_position = if layout.left < work_area.left
{
WindowHidingPosition::BottomLeft
} else {
WindowHidingPosition::BottomRight
};
if let Err(error) =
window.hide_scrolling(scrolling_hiding_position)
{
tracing::warn!(
"failed to set hide window for scrolling layout: {error}"
)
}
} else if percentage_override {
window.center(&work_area, false)?;
} else if let Err(error) = window.set_position(layout) {
if let Err(error) = window.set_position(layout) {
tracing::warn!("failed to set window position: {error}")
}
}
@@ -1733,9 +1667,6 @@ impl Workspace {
Layout::Default(DefaultLayout::UltrawideVerticalStack) => {
self.enforce_resize_for_ultrawide();
}
Layout::Default(DefaultLayout::Scrolling) => {
self.enforce_resize_for_scrolling();
}
_ => self.enforce_no_resize(),
}
}
@@ -1963,28 +1894,6 @@ impl Workspace {
}
}
fn enforce_resize_for_scrolling(&mut self) {
let resize_dimensions = &mut self.resize_dimensions;
match resize_dimensions.len() {
0 | 1 => self.enforce_no_resize(),
_ => {
let len = resize_dimensions.len();
for (i, rect) in resize_dimensions.iter_mut().enumerate() {
if let Some(rect) = rect {
rect.top = 0;
rect.bottom = 0;
if i == 0 {
rect.left = 0;
} else if i == len - 1 {
rect.right = 0;
}
}
}
}
}
}
fn enforce_no_resize(&mut self) {
for rect in self.resize_dimensions.iter_mut().flatten() {
rect.left = 0;
@@ -3014,95 +2923,4 @@ mod tests {
Some(crate::core::ScrollingCentering::Always)
);
}
fn scrolling_workspace(containers: usize, columns: usize) -> Workspace {
let mut workspace = Workspace {
layout: Layout::Default(DefaultLayout::Scrolling),
layout_options: Some(LayoutOptions {
scrolling: Some(ScrollingLayoutOptions {
columns,
center_focused_column: None,
}),
grid: None,
column_ratios: None,
row_ratios: None,
}),
..Default::default()
};
for _ in 0..containers {
workspace.add_container_to_back(Container::default());
}
workspace.resize_dimensions = vec![None; containers];
workspace
}
/// Lay out a workspace the way `Workspace::update` does between two focus changes: ask the
/// layout, clamp the result to the work area, and remember it for the next pass. Returns the
/// layout as the arrangement algorithm produced it.
fn scrolling_update(workspace: &mut Workspace, work_area: &Rect) -> Vec<Rect> {
let layouts = workspace.calculate_layouts(work_area, Some(10)).unwrap();
let mut clamped = layouts.clone();
for layout in &mut clamped {
clamp_to_work_area(layout, work_area);
}
workspace.latest_layout = clamped;
layouts
}
/// The columns that lie completely inside the work area.
fn fully_visible(layouts: &[Rect], work_area: &Rect) -> Vec<usize> {
layouts
.iter()
.enumerate()
.filter(|(_, layout)| {
layout.left >= work_area.left
&& layout.left + layout.right <= work_area.left + work_area.right
})
.map(|(idx, _)| idx)
.collect()
}
#[test]
fn test_scrolling_view_stays_put_while_focus_moves_within_it() {
let work_area = Rect {
left: 0,
top: 0,
right: 1200,
bottom: 800,
};
let mut workspace = scrolling_workspace(6, 3);
// focusing the last column scrolls it in as the rightmost of three
workspace.focus_container(5);
let initial = scrolling_update(&mut workspace, &work_area);
assert_eq!(fully_visible(&initial, &work_area), vec![3, 4, 5]);
// moving focus left across columns that are already in view must not scroll the strip
for idx in [4, 3] {
workspace.focus_container(idx);
let layout = scrolling_update(&mut workspace, &work_area);
assert_eq!(layout, initial, "focusing column {idx} scrolled the view");
}
// leaving the view on the left scrolls just far enough to bring the column in
workspace.focus_container(2);
let scrolled = scrolling_update(&mut workspace, &work_area);
assert_eq!(fully_visible(&scrolled, &work_area), vec![2, 3, 4]);
// and moving right within the new view leaves it where it is
for idx in [3, 4] {
workspace.focus_container(idx);
let layout = scrolling_update(&mut workspace, &work_area);
assert_eq!(layout, scrolled, "focusing column {idx} scrolled the view");
}
// leaving the view on the right scrolls just far enough, too
workspace.focus_container(5);
let layout = scrolling_update(&mut workspace, &work_area);
assert_eq!(fully_visible(&layout, &work_area), vec![3, 4, 5]);
}
}