From ebc9d49d7788a4d2a0a20c7eb3525963fe83f97d Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Oct 2026 15:18:27 -0700 Subject: [PATCH] feat(scrolling): make room for windows that will not get as narrow Many apps will not make a window narrower than some width. The layout placed the next column as if the window had the width it was given, and so placed it on top of the window that had not. What the arrival module has been told, the layout picks up at the start of the next pass: a column is never narrower than the window in it needs, plus the padding and border around it and capped at the width of the screen, and the columns after it are placed where it ends. A limit only ever goes up, so that a column does not bounce between two widths, and a stack takes the widest of its windows. With animations off the layout asks every window that it resized what it is, with a timeout, and places everything once more if one refused. With animations on the animator says so when a journey ends, so a window that refuses is noticed then as well. --- docs/usage/scrolling-layout.md | 10 +- komorebi/src/scrolling.rs | 253 ++++++++++++++++++++++++++++++++- komorebi/src/window.rs | 29 ++++ 3 files changed, 286 insertions(+), 6 deletions(-) diff --git a/docs/usage/scrolling-layout.md b/docs/usage/scrolling-layout.md index acf71e49..5121ec30 100644 --- a/docs/usage/scrolling-layout.md +++ b/docs/usage/scrolling-layout.md @@ -91,6 +91,14 @@ so resizing a height does nothing. [`scrolling-layout-columns`](../cli/scrolling-layout-columns.md) gives, on the focused workspace of each monitor. The focused column stays where it is on screen, unless it has to scroll to stay in view. +## Windows with a minimum width + +Many apps will not make a window narrower than some width, whatever they are asked. komorebi asks a window how wide it is +once it has been resized, and if it did not get as narrow as its column, the column becomes as wide as the window needs. +With [animations](animations.md) on that is when the window arrives, and with them off it is straight after the resize. +The columns after it move along to make room, and nothing overlaps. A column never gets narrower again because of this, +and never gets wider than the screen. + ## Windows that are out of view A window that scrolls out of view comes to rest at the edge of the screen that it left by, with 5 pixels of it still on @@ -108,7 +116,5 @@ arranged above and below each other rather than side by side, none of this appli These are not done yet. -- A window that will not get as narrow as its column, which is what many apps do, is not noticed, so it can overlap the - column next to it. - Every window has a border, including the windows resting at the edges, so you may see a thin line of one at the edge of the screen. diff --git a/komorebi/src/scrolling.rs b/komorebi/src/scrolling.rs index 4a96d310..902033b1 100644 --- a/komorebi/src/scrolling.rs +++ b/komorebi/src/scrolling.rs @@ -11,6 +11,7 @@ //! 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::arrival; use crate::core::DefaultLayout; use crate::core::Layout; use crate::core::LayoutOptions; @@ -137,21 +138,126 @@ impl Workspace { container_padding: i32, border_offset: i32, border_width: i32, + ) -> eyre::Result<()> { + // what a column takes up around its window on either side + let around_window = container_padding + border_offset + border_width; + + // windows that were not what they were told to be since the last layout, which is where + // the animator says so + self.adopt_actual_sizes(around_window, adjusted_work_area.right); + + self.place_scrolling( + work_area, + adjusted_work_area, + container_padding, + border_offset, + border_width, + )?; + + // with animations off every window that changed width was just asked what it is, so a + // window that refused is known by now, and everything after it is placed again with room + // for it. Once is enough: the sizes that are read back this time round are picked up by + // the next layout, and a layout is never made to chase them. + if self.adopt_actual_sizes(around_window, adjusted_work_area.right) { + self.place_scrolling( + work_area, + adjusted_work_area, + container_padding, + border_offset, + border_width, + )?; + } + + Ok(()) + } + + /// Learn from what windows turned out to be when they were sent to where the layout put them, + /// which is that some of them are wider than the layout said and will not be any narrower. + /// + /// A column is never made narrower than the window in it needs, and everything after it is + /// placed where it ends. Only ever wider: what an app will not do is a fact about the app, and + /// a limit that came and went would make the strip bounce between two layouts. + /// + /// `around_window` is what a column takes up around its window on either side, and the strip + /// is `viewport_width` wide. Returns whether any column changed. + pub(crate) fn adopt_actual_sizes(&mut self, around_window: i32, viewport_width: i32) -> bool { + let Some(space) = self.scrolling.as_mut() else { + return false; + }; + + let mut adopted = false; + + for container in self.containers.elements() { + // a stack is one tile and as wide as the widest of its windows needs + let needed = container + .windows() + .iter() + .filter_map(|window| arrival::take(window.id)) + .filter_map(|arrival| { + arrival::column_width_for(&arrival, around_window, viewport_width) + }) + .max(); + + let Some(needed) = needed else { + continue; + }; + + let Some((min, max)) = space.width_limits(&container.id) else { + continue; + }; + + if min.is_some_and(|min| min >= needed) { + continue; + } + + tracing::debug!( + "a window in column {} will not be narrower than a column of {needed}px", + container.id + ); + + space.set_width_limits(&container.id, Some(needed), max); + adopted = true; + } + + adopted + } + + /// Put every window where the layout says that it goes. + fn place_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 previous = std::mem::take(&mut self.latest_layout); let mut latest_layout = Vec::with_capacity(placements.len()); - for (container, placement) in self.containers_mut().iter_mut().zip(&placements) { + for (idx, (container, placement)) in self + .containers_mut() + .iter_mut() + .zip(&placements) + .enumerate() + { let mut rect = placement.rect; rect.add_padding(border_offset); rect.add_padding(border_width); latest_layout.push(rect); + // a window that is asked for the width that it already has is not asked what it is + let resized = previous + .get(idx) + .is_none_or(|before| before.right != rect.right); + for window in container.windows_mut() { let result = match placement.visibility { - Visibility::Visible => window.set_position(&rect), + Visibility::Visible => window + .set_position(&rect) + .map(|()| window.note_arrival(&rect, resized)), 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, @@ -161,7 +267,9 @@ impl Workspace { clamp_to_work_area(&mut target, adjusted_work_area); } - window.set_position(&target) + window + .set_position(&target) + .map(|()| window.note_arrival(&target, resized)) } Visibility::OffLeft => { window.hide_scrolling(WindowHidingPosition::BottomLeft, screen) @@ -680,13 +788,18 @@ mod tests { /// A workspace with a window in every container, which is how closing a window finds a container. fn workspace_of_windows(columns: usize, count: u32) -> Workspace { + workspace_of_windows_from(columns, 0, count) + } + + /// A workspace with a window in every container, whose ids start at `first`. + fn workspace_of_windows_from(columns: usize, first: u32, count: u32) -> Workspace { let mut workspace = Workspace { layout: Layout::Default(DefaultLayout::Scrolling), layout_options: options(columns, None), ..Default::default() }; - for id in 0..count { + for id in first..first + count { workspace .new_container_for_window(&Window::from(id)) .unwrap(); @@ -695,6 +808,138 @@ mod tests { workspace } + fn lefts(placements: &[ScrollingPlacement]) -> Vec { + placements + .iter() + .map(|placement| placement.rect.left) + .collect() + } + + fn sent_to(width: i32) -> Rect { + Rect { + left: 0, + top: 0, + right: width, + bottom: 800, + } + } + + // The arrivals that these tests report are shared by every test that runs at the same time, + // so each of them has windows with ids of its own. + + #[test] + fn a_window_that_will_not_get_narrower_pushes_the_columns_after_it_along() { + let mut workspace = workspace_of_windows_from(3, 6_000_000, 3); + let before = workspace.layout_scrolling(&AREA, 0); + assert_eq!(widths(&before), [400, 400, 400]); + + // the second window was sent 400 and is 700 + arrival::report(6_000_001, sent_to(400), sent_to(700)); + assert!(workspace.adopt_actual_sizes(0, 1200)); + + let after = workspace.layout_scrolling(&AREA, 0); + assert_eq!(widths(&after), [400, 700, 400]); + + // and the next column starts where that one ends, where it would have overlapped it + assert_eq!(lefts(&after)[2], lefts(&after)[1] + 700); + } + + #[test] + fn the_room_around_a_window_is_taken_into_account() { + let mut workspace = workspace_of_windows_from(3, 6_000_010, 2); + workspace.layout_scrolling(&AREA, 0); + + // padding of 10, and a border of 2 and 0, on either side of a window of 700 + arrival::report(6_000_010, sent_to(376), sent_to(700)); + assert!(workspace.adopt_actual_sizes(12, 1200)); + + assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0))[0], 724); + } + + #[test] + fn what_a_window_will_not_do_is_only_ever_learned_upwards() { + let mut workspace = workspace_of_windows_from(3, 6_000_020, 2); + workspace.layout_scrolling(&AREA, 0); + + arrival::report(6_000_020, sent_to(400), sent_to(700)); + assert!(workspace.adopt_actual_sizes(0, 1200)); + + // a window that is smaller than that now is not a reason to forget it + arrival::report(6_000_020, sent_to(700), sent_to(750)); + assert!(workspace.adopt_actual_sizes(0, 1200)); + arrival::report(6_000_020, sent_to(750), sent_to(720)); + assert!(!workspace.adopt_actual_sizes(0, 1200)); + arrival::report(6_000_020, sent_to(750), sent_to(750)); + assert!(!workspace.adopt_actual_sizes(0, 1200)); + + assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0))[0], 750); + } + + #[test] + fn a_column_is_never_made_wider_than_the_screen() { + let mut workspace = workspace_of_windows_from(3, 6_000_030, 2); + workspace.layout_scrolling(&AREA, 0); + + arrival::report(6_000_030, sent_to(400), sent_to(1500)); + assert!(workspace.adopt_actual_sizes(12, 1200)); + + assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0))[0], 1200); + } + + #[test] + fn a_window_that_took_its_size_changes_nothing() { + let mut workspace = workspace_of_windows_from(3, 6_000_040, 3); + let before = workspace.layout_scrolling(&AREA, 0); + + // exactly, and within the rounding that apps do + arrival::report(6_000_040, sent_to(400), sent_to(400)); + arrival::report(6_000_041, sent_to(400), sent_to(402)); + + assert!(!workspace.adopt_actual_sizes(0, 1200)); + assert_eq!(workspace.layout_scrolling(&AREA, 0), before); + } + + #[test] + fn a_window_that_refused_and_then_took_its_size_is_forgotten() { + let mut workspace = workspace_of_windows_from(3, 6_000_050, 2); + workspace.layout_scrolling(&AREA, 0); + + arrival::report(6_000_050, sent_to(400), sent_to(700)); + arrival::report(6_000_050, sent_to(400), sent_to(400)); + + assert!(!workspace.adopt_actual_sizes(0, 1200)); + } + + #[test] + fn a_stack_is_as_wide_as_the_widest_of_its_windows_needs() { + let mut workspace = workspace_of_windows_from(3, 6_000_060, 2); + workspace + .containers_mut() + .front_mut() + .unwrap() + .add_window(&Window::from(6_000_062), WindowHidingPosition::default()) + .unwrap(); + workspace.layout_scrolling(&AREA, 0); + + arrival::report(6_000_060, sent_to(400), sent_to(500)); + arrival::report(6_000_062, sent_to(400), sent_to(650)); + + assert!(workspace.adopt_actual_sizes(0, 1200)); + assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0)), [650, 400]); + } + + #[test] + fn nothing_is_adopted_before_there_is_a_strip() { + let mut workspace = workspace_of_windows_from(3, 6_000_070, 2); + arrival::report(6_000_070, sent_to(400), sent_to(700)); + + assert!(!workspace.adopt_actual_sizes(0, 1200)); + + // and it is still there for when there is + workspace.layout_scrolling(&AREA, 0); + assert!(workspace.adopt_actual_sizes(0, 1200)); + } + fn focused_window(workspace: &Workspace) -> u32 { workspace .focused_container() diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index d233515d..2fddaca1 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -38,6 +38,7 @@ use crate::animation::prefix::AnimationPrefix; use crate::animation::step::MotionSpec; use crate::animation::tracks::Verdict; use crate::application::Application; +use crate::arrival; use crate::cf_dictionary_value; use crate::core::ApplicationIdentifier; use crate::core::Rect; @@ -119,6 +120,9 @@ lazy_static::lazy_static! { parking_lot::Mutex::new(HashMap::new()); } +/// How long, in seconds, to wait for a window to say how big it is after it was resized +const ARRIVAL_TIMEOUT: f32 = 1.0; + const NOTIFICATIONS: &[&str] = &[ kAXWindowMiniaturizedNotification, kAXWindowDeminiaturizedNotification, @@ -750,6 +754,31 @@ impl Window { } } + /// Find out what the window is after it was written the size of `target` without animating, + /// and say so if it did not take the width, so that the layout can make room for it. + /// + /// With animations on the animator finds out when the window arrives, and says so itself. A + /// window that was not resized is not asked: that costs a round trip to the app, and what + /// the window turned out to be is not going to be any different from last time. + pub fn note_arrival(&self, target: &Rect, resized: bool) { + if !resized || animation::motion_for(AnimationPrefix::Movement).is_some() { + return; + } + + // the window manager is waiting for this, and so is every window after this one + let _ = AccessibilityApi::set_messaging_timeout(&self.element, ARRIVAL_TIMEOUT); + + match MacosApi::window_rect(&self.element) { + Ok(actual) => arrival::report(self.id, *target, Rect::from(actual)), + Err(error) => { + tracing::debug!( + "could not read back the size of window {}: {error}", + self.id + ) + } + } + } + /// Bring a parked window back to the edge of the screen it left by, ready to slide in from there /// /// Without this it would fly in from the corner it was parked in. The display is the one that