From 8c4949e4050b7c194f4890897254915a54de7b7e Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Oct 2026 13:48:14 -0700 Subject: [PATCH] feat(scrolling): choose what has focus after a window is closed Closing a window moved focus to the container on its left, whatever had focus and wherever the window was. A strip of columns knows better. Focus goes back to the column that the closed one was opened next to, with the viewport as it was, if nothing has moved focus since. Otherwise it goes to the column that takes the closed one's place on the right. A window that is closed without having focus no longer takes focus with it. The strip is told what there is just before the container goes, so that it can answer. A workspace that has not laid out a strip yet moves focus left as before. --- komorebi/src/scrolling.rs | 153 ++++++++++++++++++++++++++++++++++++++ komorebi/src/workspace.rs | 9 ++- 2 files changed, 161 insertions(+), 1 deletion(-) diff --git a/komorebi/src/scrolling.rs b/komorebi/src/scrolling.rs index 8b0d7fad..c2b982e0 100644 --- a/komorebi/src/scrolling.rs +++ b/komorebi/src/scrolling.rs @@ -64,6 +64,52 @@ impl Workspace { ); } + /// Make the space match the containers and the focus as they are now, for a space that is + /// there to be made to match. + /// + /// This is for a change that is about to be made to the containers, and that the space is + /// going to be told about directly. The space has to know what there is before it is told + /// what goes. + pub(crate) fn sync_scrolling_containers(&mut self) { + let ids: Vec = self + .containers() + .iter() + .map(|container| container.id.clone()) + .collect(); + let focused = self + .focused_container() + .map(|container| container.id.clone()); + + if let Some(space) = self.scrolling.as_mut() { + space.reconcile(&ids, focused.as_ref()); + } + } + + /// Give focus to what the strip says should have it after the container `removed` has gone, + /// and tell whether the strip had anything to say. + /// + /// Focus goes back to the column that the closed one was opened next to, if nothing has + /// moved focus since, and otherwise to the column that takes its place on the right. Focus that + /// is not on the closed column stays where it is. This is not what the other layouts do, which + /// always move focus to the left. + pub(crate) fn focus_after_scrolling_removal(&mut self, removed: &str) -> bool { + let Some(space) = self.scrolling.as_mut() else { + return false; + }; + + if !space.remove_column(&removed.to_string()) { + return false; + } + + // a container is a tile, and the strip says where in reading order the one that has focus + // is, which is the index of the container + if let Some(idx) = space.active_tile_index() { + self.focus_container(idx); + } + + true + } + /// 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( @@ -195,6 +241,7 @@ mod tests { use super::*; use crate::container::Container; use crate::core::ScrollingLayoutOptions; + use crate::window::Window; use komorebi_layouts::scrolling::ScrollingCentering; use komorebi_layouts::scrolling::model::CenterFocusedColumn; @@ -454,6 +501,112 @@ mod tests { assert!(workspace.scrolling.is_none()); } + /// 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 { + let mut workspace = Workspace { + layout: Layout::Default(DefaultLayout::Scrolling), + layout_options: options(columns, None), + ..Default::default() + }; + + for id in 0..count { + workspace + .new_container_for_window(&Window::from(id)) + .unwrap(); + } + + workspace + } + + fn focused_window(workspace: &Workspace) -> u32 { + workspace + .focused_container() + .and_then(|container| container.focused_window()) + .map(|window| window.id) + .unwrap() + } + + #[test] + fn closing_a_window_that_was_just_opened_goes_back_to_the_one_before_it() { + let mut workspace = workspace_of_windows(3, 8); + workspace.focus_container(3); + workspace.layout_scrolling(&AREA, 0); + // window 5 is the last of the three that are in view, and 6 and 7 are beyond it + workspace.focus_container(5); + let before = workspace.layout_scrolling(&AREA, 0); + + workspace + .new_container_for_window(&Window::from(100)) + .unwrap(); + let opened = workspace.layout_scrolling(&AREA, 0); + assert_ne!(opened, before, "opening it scrolled the strip"); + + workspace.remove_window(100).unwrap(); + + assert_eq!(focused_window(&workspace), 5); + assert_eq!(workspace.layout_scrolling(&AREA, 0), before); + } + + #[test] + fn once_focus_has_moved_closing_a_window_goes_to_the_one_that_takes_its_place() { + let mut workspace = workspace_of_windows(3, 8); + workspace.focus_container(3); + workspace.layout_scrolling(&AREA, 0); + workspace.focus_container(5); + workspace.layout_scrolling(&AREA, 0); + + workspace + .new_container_for_window(&Window::from(100)) + .unwrap(); + workspace.layout_scrolling(&AREA, 0); + workspace.focus_container(5); + workspace.layout_scrolling(&AREA, 0); + workspace.focus_container(6); + workspace.layout_scrolling(&AREA, 0); + + workspace.remove_window(100).unwrap(); + + // window 6 was to the right of the closed window, and the other layouts would have gone left + assert_eq!(focused_window(&workspace), 6); + } + + #[test] + fn closing_the_last_window_goes_to_the_one_before_it() { + let mut workspace = workspace_of_windows(3, 5); + workspace.layout_scrolling(&AREA, 0); + assert_eq!(focused_window(&workspace), 4); + + workspace.remove_window(4).unwrap(); + + assert_eq!(focused_window(&workspace), 3); + } + + #[test] + fn closing_a_window_that_does_not_have_focus_leaves_focus_where_it_is() { + let mut workspace = workspace_of_windows(3, 6); + workspace.focus_container(2); + workspace.layout_scrolling(&AREA, 0); + + // to the right of the focused one, and then to the left of it + workspace.remove_window(5).unwrap(); + assert_eq!(focused_window(&workspace), 2); + + workspace.remove_window(0).unwrap(); + assert_eq!(focused_window(&workspace), 2); + assert_eq!(workspace.focused_container_idx(), 1); + } + + #[test] + fn before_there_is_a_strip_closing_a_window_moves_focus_left_like_the_other_layouts() { + let mut workspace = workspace_of_windows(3, 4); + workspace.focus_container(2); + assert!(workspace.scrolling.is_none()); + + workspace.remove_window(2).unwrap(); + + assert_eq!(focused_window(&workspace), 1); + } + #[test] fn changing_the_columns_option_changes_the_width_of_columns_that_have_not_been_resized() { let mut workspace = workspace(3, 3); diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index a7efa3c4..1c340cdc 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -653,6 +653,9 @@ impl Workspace { let window_hiding_position = self.globals.window_hiding_position; + // a strip of columns has to know what there is before it is told what goes + self.sync_scrolling_containers(); + let container = self .containers_mut() .get_mut(container_idx) @@ -669,8 +672,12 @@ impl Workspace { .ok_or_eyre("there is no window")?; if container.windows().is_empty() { + let container_id = container.id.clone(); self.remove_container_by_idx(container_idx); - self.focus_previous_container(); + + if !self.focus_after_scrolling_removal(&container_id) { + self.focus_previous_container(); + } } else { container.load_focused_window(window_hiding_position)?; if let Some(window) = container.focused_window() {