diff --git a/README.md b/README.md index 6e25d65d..bdeffcba 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,9 @@ At the edge of a monitor, `focus` and `move` cross to the monitor on that side; monitors, and `eager-focus` finds the window of an application wherever it is. `stack` puts the focused window and the one next to it in one stack, `unstack` takes it out again, and `stack-all` and `unstack-all` do it for a whole -workspace. `toggle-window-container-behaviour` makes a window that opens join +workspace; `cycle-stack` and `focus-stack-window` bring another window of a +stack to the front, and `cycle-stack-index` moves the front one along it. +`toggle-window-container-behaviour` makes a window that opens join the focused stack, and `toggle-float-override` makes it float. A program such as a status bar can subscribe to the daemon's notifications: it diff --git a/docs/cli/cycle-stack-index.md b/docs/cli/cycle-stack-index.md new file mode 100644 index 00000000..e6ca2d02 --- /dev/null +++ b/docs/cli/cycle-stack-index.md @@ -0,0 +1,14 @@ +```text +Cycle the index of the focused window in the focused stack in the specified cycle direction + +Usage: cycle-stack-index + +Arguments: + + [possible values: previous, next] + +Options: + -h, --help + Print help + +``` diff --git a/docs/cli/cycle-stack.md b/docs/cli/cycle-stack.md new file mode 100644 index 00000000..926b4555 --- /dev/null +++ b/docs/cli/cycle-stack.md @@ -0,0 +1,14 @@ +```text +Cycle the focused stack in the specified cycle direction + +Usage: cycle-stack + +Arguments: + + [possible values: previous, next] + +Options: + -h, --help + Print help + +``` diff --git a/docs/cli/focus-stack-window.md b/docs/cli/focus-stack-window.md new file mode 100644 index 00000000..2a7e5408 --- /dev/null +++ b/docs/cli/focus-stack-window.md @@ -0,0 +1,14 @@ +```text +Focus the specified window index in the focused stack + +Usage: focus-stack-window + +Arguments: + + Target index (zero-indexed) + +Options: + -h, --help + Print help + +``` diff --git a/docs/dev/ARCHITECTURE.md b/docs/dev/ARCHITECTURE.md index 56c35060..661e662e 100644 --- a/docs/dev/ARCHITECTURE.md +++ b/docs/dev/ARCHITECTURE.md @@ -249,7 +249,8 @@ other one is hidden. Operations that change which window is in front, or which c window is in, end with that call: stacking and unstacking ([`Workspace::move_window_to_container`], [`Workspace::new_container_for_focused_window`]), a window that closes ([`Workspace::remove_window`], which brings the next one forward and focuses it), a window that -is floated, and a focus change to a window that is behind ([`Workspace::focus_container_by_window`]). +is floated, a focus change to a window that is behind ([`Workspace::focus_container_by_window`]), +and cycling the stack or focusing a window of it by its index. [`Window::hide`] puts the window's id in [`HIDDEN_WINDOWS`] before it asks the backend, and [`Window::restore`] takes it out, so that when the compositor reports something about the window @@ -429,6 +430,17 @@ puts every window of the workspace in one container ([`WindowManager::stack_all` `UnstackAll` takes them out again ([`WindowManager::unstack_all`]). Each ends with the workspace tiled again, and the window that was in front has the focus. +`CycleStack` brings the next or the previous window of the focused container to the front, which +wraps around ([`WindowManager::cycle_container_window_in_direction`]), and `FocusStackWindow` the +window at an index ([`WindowManager::focus_container_window`]). Both show that window, hide the +one that was in front ([`Container::load_focused_window`]) and give the window the focus. +`FocusStackWindow` first focuses the monitor the pointer is on, so that a program such as a bar +that runs it for a click on its own monitor acts there. `CycleStackIndex` swaps the front window +with the next or the previous one in the container's order, and keeps it in front +([`WindowManager::cycle_container_window_index_in_direction`]): only the order of the stack +changes. A container of one window cannot be cycled, and an index it does not have cannot be +focused. + A window that opens gets the behavior [`WindowManager::window_management_behaviour`] says for its workspace: a container of its own (`Create`, the default), or a place in the focused container (`Append`), and a floating window with the float override on. A workspace's own @@ -597,10 +609,11 @@ module's unit. operations on it (tile the workspace in front, tile every monitor, take a minimized window out, move the focus, move the focused container, change the layout, resize or promote the focused container, float a window, lock a container, set the padding of a - workspace, manage and unmanage a window, stack and unstack windows, show a workspace, move a - container to another workspace, follow a workspace switch made by something else, name a - workspace and find it by its name, set the layout of a workspace that is not in front, focus - another monitor, and move containers and workspaces between monitors). + workspace, manage and unmanage a window, stack and unstack windows and choose the one in + front, show a workspace, move a container to another workspace, follow a workspace switch + made by something else, name a workspace and find it by its name, set the layout of a + workspace that is not in front, focus another monitor, and move containers and workspaces + between monitors). - [`desktop`](../../komorebi/src/desktop.rs): loads the monitors and the open windows from the backend. - [`window_manager_event`](../../komorebi/src/window_manager_event.rs) and @@ -713,6 +726,9 @@ the fake. The window manager is its only caller, through [`backend`]. The contra | Only the window in front of a stack is in sight | framework path: [`Container::add_window`] and [`Container::load_focused_window`] hide every window of the container but the focused one, and every operation that changes the front ends with the call | `a_window_added_to_a_container_is_in_front_and_the_others_are_hidden`, `loading_the_focused_window_shows_it_and_hides_the_rest`, `stacking_puts_two_windows_in_one_container_and_hides_the_one_behind`, `vm-stacks` ("exactly one window is on it") | none | | A hidden window is still on its workspace, and is not reported as moved | framework path: the table keeps the desktop a window came from, and a move to the parking desktop is its own | `parking_a_window_hides_it_without_unmanaging_it`, `komorebis_own_move_and_its_park_and_restore_raise_nothing_new`, `a_window_is_put_on_a_workspace_by_putting_it_on_its_desktop` | none | | What the compositor reports about a window komorebi hid is not taken for the user's doing | framework path: [`Window::hide`] records the window in [`HIDDEN_WINDOWS`] before it asks the backend, and the `Minimize` arm of `handle_event` checks it | `a_minimize_komorebi_asked_for_is_not_mistaken_for_the_user`, `a_minimize_the_user_asked_for_takes_the_window_out_of_the_layout` | none | +| The window the compositor focuses in a stack comes to the front | framework path: the `FocusChange` arm of `handle_event` focuses the container by the window, which loads it ([`Workspace::focus_container_by_window`]) | `focusing_a_hidden_window_of_a_stack_brings_it_to_the_front`, `the_window_the_user_focuses_in_the_back_of_a_stack_comes_to_the_front` | none | +| Cycling a stack changes which window is in front and nothing else | framework path: [`Container::load_focused_window`] shows the focused window and hides the rest; a cycle by index only swaps two places | `cycling_the_stack_brings_the_next_window_to_the_front_and_hides_the_one_that_was`, `cycling_the_index_moves_the_front_window_along_the_stack_and_keeps_it_in_front`, `focus_stack_window_brings_the_window_at_an_index_to_the_front`, `vm-stacks` ("cycle the stack") | none | +| A stack with one window cannot be cycled, and an index it lacks is refused | runtime error: the window manager's functions return it before they change anything | `cycling_a_window_that_is_alone_is_an_error`, `focus_stack_window_refuses_an_index_the_stack_does_not_have` | none | | Closing the window in front of a stack brings the next one forward | framework path: [`Workspace::remove_window`] shows the container's new front window and focuses it | `closing_the_window_in_front_of_a_stack_brings_the_next_one_forward`, `vm-stacks` ("closing the window in view") | none | | The back of a stack stays hidden when its workspace is shown again | framework path: [`Workspace::restore`] restores the front window of each container and no other | `the_back_of_a_stack_stays_hidden_across_a_workspace_round_trip`, `vm-stacks` ("a workspace round trip") | none | | A window is never sent to a parking desktop that is not there | framework path: [`Table::hide_commands`] asks for a new one unless KWin lists the one it made | `a_parking_desktop_that_has_gone_is_made_again_not_sent_windows` | none | @@ -908,6 +924,9 @@ A window is shown, and ends up on a tile: [`WindowManager::stack_all`]: ../../komorebi/src/window_manager.rs [`WindowManager::unstack_all`]: ../../komorebi/src/window_manager.rs [`WindowManager::restore_all_windows`]: ../../komorebi/src/window_manager.rs +[`WindowManager::cycle_container_window_in_direction`]: ../../komorebi/src/window_manager.rs +[`WindowManager::cycle_container_window_index_in_direction`]: ../../komorebi/src/window_manager.rs +[`WindowManager::focus_container_window`]: ../../komorebi/src/window_manager.rs [`WindowManager::window_management_behaviour`]: ../../komorebi/src/window_manager.rs [`Backend::hide`]: ../../komorebi/src/backend/mod.rs [`Table::hide_commands`]: ../../komorebi/src/kwin/state.rs diff --git a/docs/index.md b/docs/index.md index e1449e3c..9fbe9587 100644 --- a/docs/index.md +++ b/docs/index.md @@ -34,8 +34,8 @@ anywhere else. [Running komorebi](usage/running-komorebi.md) explains how to sta daemon, ask it what it is doing and read its log. [Focusing windows](usage/focusing-windows.md) explains how to give the focus to another window, [Moving windows](usage/moving-windows.md) how to move the focused window to another tile, -[Stacking windows](usage/stacking-windows.md) how to put windows on one tile and have the others -wait out of sight, +[Stacking windows](usage/stacking-windows.md) how to put windows on one tile, have the others wait +out of sight and bring one of them to the front, [Layouts](usage/layouts.md) how to change the arrangement of the tiles, [Resizing windows](usage/resizing-windows.md) how to change the size of one, [Floating windows](usage/floating-windows.md) how to take a window out of the layout, diff --git a/docs/usage/stacking-windows.md b/docs/usage/stacking-windows.md index 630aefc6..80eaf8b8 100644 --- a/docs/usage/stacking-windows.md +++ b/docs/usage/stacking-windows.md @@ -40,6 +40,40 @@ A stack moves as one: moving it with [`komorebic move`](moving-windows.md#in-a-d another workspace takes every window in it. When the window in front closes, the next window of the stack comes to the front and has the focus. +## Choosing the window in front + +[`komorebic cycle-stack`](../cli/cycle-stack.md) brings the next or the previous window of the +focused stack to the front and gives it the focus; the window that was in front is hidden. It +wraps around: `next` from the last window of the stack brings the first. + +```sh +komorebic cycle-stack next +komorebic cycle-stack previous +``` + +[`komorebic focus-stack-window`](../cli/focus-stack-window.md) brings the window at an index of +the stack to the front, counting from 0 in the order the stack has, which is the order of its +`windows` in [`komorebic state`](../cli/state.md). It works on the monitor the pointer is on, +which it focuses first, so that a program such as a status bar that runs it when one of its tabs +is clicked acts on the stack of its own monitor. + +```sh +komorebic focus-stack-window 0 +``` + +[`komorebic cycle-stack-index`](../cli/cycle-stack-index.md) moves the window in front one place +along the stack, swapping it with the window there, and wraps around the same way. It stays in +front and keeps the focus: only the order of the stack changes, which is what `cycle-stack` and +`focus-stack-window` count by. + +```sh +komorebic cycle-stack-index next +``` + +A stack of one window cannot be cycled, and an index the stack does not have cannot be focused: +the daemon logs "there is only one window in this container" or "there is no window in this +container at index 5". + ## Windows that open into a stack By default a window that opens gets a tile of its own. With the window container behavior set to diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 66b2731d..75575915 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -349,6 +349,9 @@ pub enum SocketMessage { CycleMoveWindow(CycleDirection), StackWindow(OperationDirection), UnstackWindow, + CycleStack(CycleDirection), + CycleStackIndex(CycleDirection), + FocusStackWindow(usize), StackAll, UnstackAll, ResizeWindowEdge(OperationDirection, Sizing), diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index cdf53395..a8b1f02a 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -34,7 +34,12 @@ //! ([`WindowManager::add_window_to_container`], //! [`WindowManager::remove_window_from_container`]). **StackAll** puts every window of the //! workspace in one stack, and **UnstackAll** takes every window out of the focused one -//! ([`WindowManager::stack_all`], [`WindowManager::unstack_all`]). +//! ([`WindowManager::stack_all`], [`WindowManager::unstack_all`]). **CycleStack** brings the next +//! or the previous window of the focused stack to the front, **CycleStackIndex** moves the front +//! window along the stack, and **FocusStackWindow** brings the window at an index to the front, +//! on the monitor the pointer is on ([`WindowManager::cycle_container_window_in_direction`], +//! [`WindowManager::cycle_container_window_index_in_direction`], +//! [`WindowManager::focus_container_window`]). //! - **ToggleWindowContainerBehaviour** and **ToggleWorkspaceWindowContainerBehaviour** set whether //! a window that opens gets a container of its own or joins the focused container, for every //! workspace or for the focused one, and **ToggleFloatOverride** and @@ -253,9 +258,12 @@ impl WindowManager { /// `UnlockMonitorWorkspaceContainer` is not there (a container that is not there is ignored). /// - The focused window cannot be floated or put back, for one of the reasons /// [`WindowManager::toggle_float`] gives. - /// - The focused window cannot be stacked or unstacked, for one of the reasons + /// - The focused window cannot be stacked, unstacked or cycled, for one of the reasons /// [`WindowManager::add_window_to_container`], [`WindowManager::remove_window_from_container`], - /// [`WindowManager::stack_all`] and [`WindowManager::unstack_all`] give, or the focused + /// [`WindowManager::stack_all`], [`WindowManager::unstack_all`], + /// [`WindowManager::cycle_container_window_in_direction`], + /// [`WindowManager::cycle_container_window_index_in_direction`] and + /// [`WindowManager::focus_container_window`] give, or the focused /// workspace is not there for a behavior to be set on (`ToggleWorkspaceWindowContainerBehaviour`, /// `ToggleWorkspaceFloatOverride`). /// - The monitor a monitor command names is not there, or the compositor reports no window @@ -414,6 +422,22 @@ impl WindowManager { SocketMessage::UnstackAll => { self.unstack_all(true)?; } + SocketMessage::CycleStack(direction) => { + self.cycle_container_window_in_direction(direction)?; + } + SocketMessage::CycleStackIndex(direction) => { + self.cycle_container_window_index_in_direction(direction)?; + } + SocketMessage::FocusStackWindow(idx) => { + // In case you are using this command on a bar on a monitor + // different from the currently focused one, you'd want that + // monitor to be focused so that the FocusStackWindow happens + // on the monitor with the bar you just pressed. + if let Some(monitor_idx) = self.monitor_idx_from_current_pos() { + self.focus_monitor(monitor_idx)?; + } + self.focus_container_window(idx)?; + } SocketMessage::CycleFocusMonitor(direction) => { let monitor_idx = direction.next_idx( self.focused_monitor_idx(), diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 8a64594f..80d0b17b 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -28,7 +28,10 @@ //! ([`WindowManager::add_window_to_container`], [`WindowManager::remove_window_from_container`]) //! or all of a workspace together ([`WindowManager::stack_all`], [`WindowManager::unstack_all`]), //! and a window that opens joins the focused container instead of getting one of its own when the -//! window container behavior says so ([`WindowManager::window_management_behaviour`]). +//! window container behavior says so ([`WindowManager::window_management_behaviour`]). The window +//! in front is changed by cycling the stack ([`WindowManager::cycle_container_window_in_direction`]) +//! or by the index of a window in it ([`WindowManager::focus_container_window`]), and a window +//! moves along the stack with [`WindowManager::cycle_container_window_index_in_direction`]. //! [`WindowManager::stop`] brings back every window the window manager hid //! ([`WindowManager::restore_all_windows`]). //! @@ -887,6 +890,45 @@ impl WindowManager { Ok(()) } + /// Brings the next or the previous window of the focused stack to the front, wrapping around, + /// and gives it the focus; the window that was in front is hidden + /// + /// # Errors + /// + /// - The focused container has no window, or only one. + /// - There is no focused workspace or container, or the window cannot be focused. + /// - The workspace cannot be tiled again. + #[tracing::instrument(skip(self))] + pub fn cycle_container_window_in_direction( + &mut self, + direction: CycleDirection, + ) -> eyre::Result<()> { + tracing::info!("cycling container windows"); + + let mouse_follows_focus = self.mouse_follows_focus; + + let container = self.focused_container_mut()?; + + let len = NonZeroUsize::new(container.windows().len()) + .ok_or_eyre("there must be at least one window in a container")?; + + if len.get() == 1 { + eyre::bail!("there is only one window in this container"); + } + + let current_idx = container.focused_window_idx(); + let next_idx = direction.next_idx(current_idx, len); + + container.focus_window(next_idx); + container.load_focused_window()?; + + if let Some(window) = container.focused_window() { + window.focus(mouse_follows_focus)?; + } + + self.update_focused_workspace(mouse_follows_focus, true) + } + /// Shows workspace `idx` on the focused monitor, creating it (and the ones before it) if it /// does not exist yet. /// @@ -1819,6 +1861,47 @@ impl WindowManager { self.update_focused_workspace(self.mouse_follows_focus, true) } + /// Moves the front window of the focused stack one place along the stack, which wraps around + /// at both ends, by swapping it with the window there. It stays in front and keeps the focus: + /// only the order of the stack changes. + /// + /// # Errors + /// + /// - The focused container has no window, or only one. + /// - There is no focused workspace or container, or the window cannot be focused. + /// - The workspace cannot be tiled again. + #[tracing::instrument(skip(self))] + pub fn cycle_container_window_index_in_direction( + &mut self, + direction: CycleDirection, + ) -> eyre::Result<()> { + tracing::info!("cycling container window index"); + + let mouse_follows_focus = self.mouse_follows_focus; + + let container = self.focused_container_mut()?; + + let len = NonZeroUsize::new(container.windows().len()) + .ok_or_eyre("there must be at least one window in a container")?; + + if len.get() == 1 { + eyre::bail!("there is only one window in this container"); + } + + let current_idx = container.focused_window_idx(); + let next_idx = direction.next_idx(current_idx, len); + container.windows_mut().swap(current_idx, next_idx); + + container.focus_window(next_idx); + container.load_focused_window()?; + + if let Some(window) = container.focused_window() { + window.focus(mouse_follows_focus)?; + } + + self.update_focused_workspace(mouse_follows_focus, true) + } + /// Gives the focus to the window at `idx` in the focused container, and brings it to the front /// when the container is a stack /// @@ -3301,6 +3384,176 @@ mod tests { ); } + #[test] + fn cycle_container_window_in_direction() { + let (mut wm, _context) = setup_window_manager(); + + { + let mut m = monitor::new( + 0, + Rect::default(), + Rect::default(), + "TestMonitor".to_string(), + "TestDeviceID".to_string(), + ); + + let workspace = m.focused_workspace_mut().unwrap(); + + { + let mut container = Container::default(); + + for i in 0..3 { + container.windows_mut().push_back(Window::from(i)); + } + + // Should have 3 windows in the container + assert_eq!(container.windows().len(), 3); + + // Add container to workspace + workspace.add_container_to_back(container); + } + + // Add monitor to the window manager + wm.monitors_mut().push_back(m); + } + + // Cycle to the next window + wm.cycle_container_window_in_direction(CycleDirection::Next) + .ok(); + + { + // Should be on Window 1 + let workspace = wm.focused_workspace_mut().unwrap(); + let container = workspace.focused_container_mut().unwrap(); + assert_eq!(container.focused_window_idx(), 1); + } + + // Cycle to the next window + wm.cycle_container_window_in_direction(CycleDirection::Next) + .ok(); + + { + // Should be on Window 2 + let workspace = wm.focused_workspace_mut().unwrap(); + let container = workspace.focused_container_mut().unwrap(); + assert_eq!(container.focused_window_idx(), 2); + } + + // Cycle to the previous window + wm.cycle_container_window_in_direction(CycleDirection::Previous) + .ok(); + + { + // Should be on Window 1 + let workspace = wm.focused_workspace_mut().unwrap(); + let container = workspace.focused_container_mut().unwrap(); + assert_eq!(container.focused_window_idx(), 1); + } + } + + #[test] + fn test_cycle_nonexistent_windows() { + let (mut wm, _context) = setup_window_manager(); + + { + // Create a first monitor + let mut m = monitor::new( + 0, + Rect::default(), + Rect::default(), + "TestMonitor1".to_string(), + "TestDeviceID1".to_string(), + ); + + // Create a container + let container = Container::default(); + + // Should have 3 windows in the container + assert_eq!(container.windows().len(), 0); + + // Add the container to a workspace + let workspace = m.focused_workspace_mut().unwrap(); + workspace.add_container_to_back(container); + + // Add monitor to the window manager + wm.monitors_mut().push_back(m); + } + + // Should return an error when trying to cycle through windows in an empty container + let result = wm.cycle_container_window_in_direction(CycleDirection::Next); + assert!( + result.is_err(), + "Expected an error when cycling through windows in an empty container" + ); + } + + #[test] + fn test_cycle_container_window_index_in_direction() { + let (mut wm, _context) = setup_window_manager(); + + { + let mut m = monitor::new( + 0, + Rect::default(), + Rect::default(), + "TestMonitor".to_string(), + "TestDeviceID".to_string(), + ); + + let workspace = m.focused_workspace_mut().unwrap(); + + { + let mut container = Container::default(); + + for i in 0..3 { + container.windows_mut().push_back(Window::from(i)); + } + + // Should have 3 windows in the container + assert_eq!(container.windows().len(), 3); + + // Add container to workspace + workspace.add_container_to_back(container); + } + + // Add monitor to the window manager + wm.monitors_mut().push_back(m); + } + + // Cycle to the next window + wm.cycle_container_window_index_in_direction(CycleDirection::Next) + .ok(); + + { + // Should be on Window 1 + let workspace = wm.focused_workspace_mut().unwrap(); + let container = workspace.focused_container_mut().unwrap(); + assert_eq!(container.focused_window_idx(), 1); + } + + // Cycle to the next window + wm.cycle_container_window_index_in_direction(CycleDirection::Next) + .ok(); + + { + // Should be on Window 2 + let workspace = wm.focused_workspace_mut().unwrap(); + let container = workspace.focused_container_mut().unwrap(); + assert_eq!(container.focused_window_idx(), 2); + } + + // Cycle to the Previous window + wm.cycle_container_window_index_in_direction(CycleDirection::Previous) + .ok(); + + { + // Should be on Window 1 + let workspace = wm.focused_workspace_mut().unwrap(); + let container = workspace.focused_container_mut().unwrap(); + assert_eq!(container.focused_window_idx(), 1); + } + } + #[test] fn test_swap_containers() { let (mut wm, _context) = setup_window_manager(); diff --git a/komorebi/tests/stacks.rs b/komorebi/tests/stacks.rs index ad66436c..eaf1f11a 100644 --- a/komorebi/tests/stacks.rs +++ b/komorebi/tests/stacks.rs @@ -1,5 +1,5 @@ -//! Stacking windows in one container, and taking the windows a stack does not show out of sight, -//! against an in-memory desktop. +//! Stacking windows in one container, taking the windows a stack does not show out of sight, and +//! choosing which window of a stack is in front, against an in-memory desktop. //! //! A container tiles one window, so the other windows of a stack are hidden: the backend is asked //! to take them out of sight, and the window manager records them as hidden so that what the @@ -12,6 +12,7 @@ use komorebi::backend::Backend; use komorebi::backend::fake::FakeBackend; use komorebi::backend::with_backend; +use komorebi::core::CycleDirection; use komorebi::core::OperationDirection; use komorebi::core::SocketMessage; use komorebi::core::WindowContainerBehaviour; @@ -479,3 +480,141 @@ fn the_state_has_the_new_window_behavior_and_the_float_override() { assert_eq!(state["new_window_behaviour"], "Append"); assert_eq!(state["float_override"], true); } + +/// A fake desktop with the windows `ids` in one stack, the last in front +fn stack_of(ids: &[u32]) -> (Arc, WindowManager) { + let (fake, mut wm) = desktop_with(ids); + send(&fake, &mut wm, SocketMessage::StackAll).unwrap(); + (fake, wm) +} + +#[test] +fn cycling_the_stack_brings_the_next_window_to_the_front_and_hides_the_one_that_was() { + let (fake, mut wm) = stack_of(&[33_201, 33_202, 33_203]); + let ids = [33_201, 33_202, 33_203]; + assert_eq!(containers(&wm), [vec![33_203, 33_201, 33_202]]); + + // the stack is 33_201, 33_202, 33_203 with the last in front: next wraps to the first + send( + &fake, + &mut wm, + SocketMessage::CycleStack(CycleDirection::Next), + ) + .unwrap(); + + assert_eq!(wm.focused_window().unwrap().id, 33_201); + assert_eq!(hidden(&fake, &ids), [33_202, 33_203]); + assert_eq!(fake.foreground_window(), Some(33_201)); + + send( + &fake, + &mut wm, + SocketMessage::CycleStack(CycleDirection::Previous), + ) + .unwrap(); + + assert_eq!(wm.focused_window().unwrap().id, 33_203); + assert_eq!(hidden(&fake, &ids), [33_201, 33_202]); + assert_eq!(fake.foreground_window(), Some(33_203)); +} + +#[test] +fn cycling_a_window_that_is_alone_is_an_error() { + let (fake, mut wm) = desktop_with(&[33_211, 33_212]); + + for message in [ + SocketMessage::CycleStack(CycleDirection::Next), + SocketMessage::CycleStackIndex(CycleDirection::Previous), + ] { + let error = send(&fake, &mut wm, message).unwrap_err(); + assert_eq!( + error.to_string(), + "there is only one window in this container" + ); + } + assert_eq!(containers(&wm), [vec![33_211], vec![33_212]]); +} + +#[test] +fn cycling_the_index_moves_the_front_window_along_the_stack_and_keeps_it_in_front() { + let (fake, mut wm) = stack_of(&[33_221, 33_222, 33_223]); + let ids = [33_221, 33_222, 33_223]; + let order = |wm: &WindowManager| -> Vec { + wm.focused_workspace() + .unwrap() + .containers() + .iter() + .flat_map(|container| container.windows().iter().map(|window| window.id)) + .collect() + }; + assert_eq!(order(&wm), [33_221, 33_222, 33_223]); + + // 33_223 is in front and last: the next place wraps to the first + send( + &fake, + &mut wm, + SocketMessage::CycleStackIndex(CycleDirection::Next), + ) + .unwrap(); + + assert_eq!(order(&wm), [33_223, 33_222, 33_221]); + assert_eq!(wm.focused_window().unwrap().id, 33_223); + assert_eq!(hidden(&fake, &ids), [33_221, 33_222]); + assert_eq!(fake.foreground_window(), Some(33_223)); +} + +#[test] +fn focus_stack_window_brings_the_window_at_an_index_to_the_front() { + let (fake, mut wm) = stack_of(&[33_231, 33_232, 33_233]); + let ids = [33_231, 33_232, 33_233]; + + send(&fake, &mut wm, SocketMessage::FocusStackWindow(1)).unwrap(); + + assert_eq!(wm.focused_window().unwrap().id, 33_232); + assert_eq!(hidden(&fake, &ids), [33_231, 33_233]); + assert_eq!(fake.foreground_window(), Some(33_232)); + + send(&fake, &mut wm, SocketMessage::FocusStackWindow(0)).unwrap(); + assert_eq!(wm.focused_window().unwrap().id, 33_231); + assert_eq!(hidden(&fake, &ids), [33_232, 33_233]); +} + +#[test] +fn focus_stack_window_refuses_an_index_the_stack_does_not_have() { + let (fake, mut wm) = stack_of(&[33_241, 33_242]); + + let error = send(&fake, &mut wm, SocketMessage::FocusStackWindow(5)).unwrap_err(); + + assert_eq!( + error.to_string(), + "there is no window in this container at index 5" + ); + assert_eq!(wm.focused_window().unwrap().id, 33_242); +} + +#[test] +fn focus_stack_window_on_a_window_that_is_alone_takes_index_0_only() { + let (fake, mut wm) = desktop_with(&[33_251]); + + send(&fake, &mut wm, SocketMessage::FocusStackWindow(0)).unwrap(); + let error = send(&fake, &mut wm, SocketMessage::FocusStackWindow(1)).unwrap_err(); + + assert_eq!( + error.to_string(), + "there is only one window in this container" + ); +} + +#[test] +fn the_window_the_user_focuses_in_the_back_of_a_stack_comes_to_the_front() { + let (fake, mut wm) = stack_of(&[33_261, 33_262]); + let ids = [33_261, 33_262]; + assert_eq!(hidden(&fake, &ids), [33_261]); + + // the user raises the hidden window (a click in the task manager): KWin gives it the focus + fake.set_foreground(Some(33_261)); + process(&fake, &mut wm, activated(33_261)).unwrap(); + + assert_eq!(wm.focused_window().unwrap().id, 33_261); + assert_eq!(hidden(&fake, &ids), [33_262]); +} diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 8d484f9c..881f0b49 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -16,10 +16,13 @@ //! next window and give it up. //! - `stack` puts the focused window in one container with the window on one side of it, so that //! they share a tile and only one is in sight, `unstack` takes it out again, and `stack-all` and -//! `unstack-all` do it for the whole workspace and for the focused stack. -//! `toggle-window-container-behaviour` and `toggle-workspace-window-container-behaviour` make a -//! window that opens join the focused container instead of getting a tile, and -//! `toggle-float-override` and `toggle-workspace-float-override` make it float. +//! `unstack-all` do it for the whole workspace and for the focused stack. `cycle-stack` brings +//! the next or the previous window of the focused stack to the front, `cycle-stack-index` moves +//! the front window along the stack, and `focus-stack-window` brings the window at an index to +//! the front. `toggle-window-container-behaviour` and +//! `toggle-workspace-window-container-behaviour` make a window that opens join the focused +//! container instead of getting a tile, and `toggle-float-override` and +//! `toggle-workspace-float-override` make it float. //! - `promote`, `promote-swap`, `promote-focus` and `promote-window` bring a window to the largest //! tile, or the focus to it. //! - `toggle-float` takes the focused window out of the layout, or puts it back, and @@ -148,6 +151,8 @@ gen_enum_subcommand_args! { CycleEmptyWorkspace: CycleDirection, CycleMoveWorkspaceToMonitor: CycleDirection, Stack: OperationDirection, + CycleStack: CycleDirection, + CycleStackIndex: CycleDirection, FlipLayout: Axis, ChangeLayout: DefaultLayout, CycleLayout: CycleDirection, @@ -182,6 +187,7 @@ gen_target_subcommand_args! { FocusWorkspaces, MoveWorkspaceToMonitor, SwapWorkspacesWithMonitor, + FocusStackWindow, } macro_rules! gen_named_target_subcommand_args { @@ -651,6 +657,15 @@ enum SubCommand { Stack(Stack), /// Unstack the focused window Unstack, + /// Cycle the focused stack in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleStack(CycleStack), + /// Cycle the index of the focused window in the focused stack in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleStackIndex(CycleStackIndex), + /// Focus the specified window index in the focused stack + #[clap(arg_required_else_help = true)] + FocusStackWindow(FocusStackWindow), /// Stack all windows on the focused workspace StackAll, /// Unstack all windows in the focused container @@ -1060,6 +1075,15 @@ fn main() -> eyre::Result<()> { SubCommand::UnstackAll => { send_message(&SocketMessage::UnstackAll)?; } + SubCommand::FocusStackWindow(args) => { + send_message(&SocketMessage::FocusStackWindow(args.target))?; + } + SubCommand::CycleStack(args) => { + send_message(&SocketMessage::CycleStack(args.cycle_direction))?; + } + SubCommand::CycleStackIndex(args) => { + send_message(&SocketMessage::CycleStackIndex(args.cycle_direction))?; + } SubCommand::FocusWorkspace(args) => { send_message(&SocketMessage::FocusWorkspaceNumber(args.target))?; } diff --git a/komorebic/tests/cli.rs b/komorebic/tests/cli.rs index f669e1e2..0218f8a2 100644 --- a/komorebic/tests/cli.rs +++ b/komorebic/tests/cli.rs @@ -231,7 +231,7 @@ fn the_layout_subcommands_send_the_layout_axis_or_direction_they_are_given() { } #[test] -fn the_stack_subcommands_send_the_direction_they_are_given() { +fn the_stack_subcommands_send_the_direction_or_the_index_they_are_given() { sends( &["stack", "left"], r#"{"type":"StackWindow","content":"Left"}"#, @@ -243,6 +243,22 @@ fn the_stack_subcommands_send_the_direction_they_are_given() { sends(&["unstack"], r#"{"type":"UnstackWindow"}"#); sends(&["stack-all"], r#"{"type":"StackAll"}"#); sends(&["unstack-all"], r#"{"type":"UnstackAll"}"#); + sends( + &["cycle-stack", "next"], + r#"{"type":"CycleStack","content":"Next"}"#, + ); + sends( + &["cycle-stack", "previous"], + r#"{"type":"CycleStack","content":"Previous"}"#, + ); + sends( + &["cycle-stack-index", "next"], + r#"{"type":"CycleStackIndex","content":"Next"}"#, + ); + sends( + &["focus-stack-window", "2"], + r#"{"type":"FocusStackWindow","content":2}"#, + ); } #[test] @@ -708,6 +724,8 @@ fn docgen_writes_the_help_of_every_subcommand_in_a_text_fence() { "cycle-move.md", "cycle-send-to-monitor.md", "cycle-send-to-workspace.md", + "cycle-stack-index.md", + "cycle-stack.md", "cycle-workspace.md", "data-directory.md", "display-index-preference.md", @@ -720,6 +738,7 @@ fn docgen_writes_the_help_of_every_subcommand_in_a_text_fence() { "focus-monitor-workspace.md", "focus-monitor.md", "focus-named-workspace.md", + "focus-stack-window.md", "focus-workspace.md", "focus-workspaces.md", "focus.md", diff --git a/nix/tests/vm/suites/commands/test.py b/nix/tests/vm/suites/commands/test.py index 1c867fd6..92c1420a 100644 --- a/nix/tests/vm/suites/commands/test.py +++ b/nix/tests/vm/suites/commands/test.py @@ -160,6 +160,10 @@ cycle-focus previous cycle-move next cycle-move previous stack left +cycle-stack next +cycle-stack previous +cycle-stack-index next +focus-stack-window 0 unstack stack-all unstack-all diff --git a/nix/tests/vm/suites/stacks/test.py b/nix/tests/vm/suites/stacks/test.py index 7c6355a5..aa55b146 100644 --- a/nix/tests/vm/suites/stacks/test.py +++ b/nix/tests/vm/suites/stacks/test.py @@ -4,7 +4,9 @@ # focus. KWin keeps its virtual desktops in `kwinrc` as soon as one is made, so what only a real # KWin can tell is that the parking desktop is made when it is needed, that it is removed when # komorebi stops, and that one a crashed komorebi left behind is swept by the next. The windows a -# stack hides are on a desktop of their own, not minimized: their events are not the user's. +# stack hides are on a desktop of their own, not minimized: their events are not the user's, and +# the window that is in view is always the one komorebi and KWin both have focused, also after +# the stack is cycled or a window of it is focused by its place in it. # `komorebi/tests/stacks.rs` checks which windows a stack holds against the fake. from komorebi_vm import Checks, Kde, Rect @@ -94,7 +96,52 @@ checks.check( rect_shown = kde.rect(kde.wid(shown)) checks.check("the one in view is inside the screen", inside(rect_shown)) -print("== 3. a stack is still a stack after a workspace round trip") +print("== 3. cycle the stack") +kde.komorebic("cycle-stack", "next") +checks.check( + "the other window is in view now, and the first is the one out of sight", + kde.wait_for(lambda: active_title() != shown and parked() == [shown]), +) +checks.check("komorebi and KWin agree on the focus", kde.wait_for(agree)) +checks.check( + "it fills the tile the first one had", + kde.wait_for(lambda: kde.rect(kde.wid(active_title())) == rect_shown), +) +checks.check("still exactly one window is out of sight", len(parked()) == 1) +kde.komorebic("cycle-stack", "previous") +checks.check( + "cycling back, the first is in view again", + kde.wait_for(lambda: active_title() == shown and parked() != [shown] and len(parked()) == 1), +) +checks.check("and the focus agrees", kde.wait_for(agree)) + +print("== 4. focus a window of the stack by its place in it, and move the one in view along") +in_view = kde.state()["monitors"]["elements"][0]["workspaces"]["elements"][0] +stack = in_view["containers"]["elements"][in_view["containers"]["focused"]]["windows"] +front = stack["focused"] +kde.komorebic("focus-stack-window", str(1 - front)) +checks.check( + "focus-stack-window shows the other one", + kde.wait_for(lambda: active_title() != shown and parked() == [shown] and agree()), +) +kde.komorebic("focus-stack-window", str(front)) +checks.check( + "and by the other index shows the first again", + kde.wait_for(lambda: active_title() == shown and parked() != [shown] and agree()), +) +order = next(c for c in kde.containers() if len(c) == 2) +out_of_sight = parked() +kde.komorebic("cycle-stack-index", "next") +checks.check( + "cycle-stack-index swaps the two places in the stack", + kde.wait_for(lambda: next(c for c in kde.containers() if len(c) == 2) == order[::-1]), +) +checks.check( + "and the window in view stays in view and keeps the focus", + active_title() == shown and parked() == out_of_sight and agree(), +) + +print("== 5. a stack is still a stack after a workspace round trip") before = parked() kde.komorebic("focus-workspace", "1") checks.check("workspace 1 is showing", kde.wait_for(lambda: kde.shown() == 2)) @@ -106,7 +153,7 @@ checks.check( ) checks.check("and the focus agrees", kde.wait_for(agree)) -print("== 4. unstack") +print("== 6. unstack") kde.komorebic("unstack") checks.check("three tiles again", kde.wait_for(lambda: sizes() == [1, 1, 1])) checks.check("nothing is out of sight", kde.wait_for(lambda: parked() == [])) @@ -117,7 +164,7 @@ checks.check( not any(a.overlaps(b) for i, a in enumerate(rects) for b in rects[i + 1 :]), ) -print("== 5. closing the window in view brings the next one") +print("== 7. closing the window in view brings the next one") kde.komorebic("stack", "left") checks.check("stacked again", kde.wait_for(lambda: len(parked()) == 1)) hidden_one = parked()[0] @@ -128,7 +175,7 @@ checks.check( ) checks.check("two tiles are left", kde.wait_for(lambda: sizes() == [1, 1])) -print("== 6. stack all, and unstack all") +print("== 8. stack all, and unstack all") kde.komorebic("stack-all") checks.check( "every window is in one stack, with one in view", @@ -141,7 +188,7 @@ checks.check( kde.wait_for(lambda: sizes() == [1, 1] and parked() == []), ) -print("== 7. a crash leaves a parking desktop, which the next start sweeps") +print("== 9. a crash leaves a parking desktop, which the next start sweeps") kde.komorebic("stack-all") checks.check("a window is parked", kde.wait_for(lambda: len(parked()) == 1)) names_before = kde.desktop_names() @@ -167,7 +214,7 @@ checks.check( kde.wait_for(lambda: park_desktop() != 0 and len(parked()) == 1), ) -print("== 8. a clean stop puts every window back") +print("== 10. a clean stop puts every window back") still_open = [t for t in TITLES if kde.wid(t) != ""] kde.komorebic("stop") checks.check("komorebi exited", kde.wait_for(lambda: not kde.komorebi_running()))