diff --git a/README.md b/README.md index decc406d..1f2874e8 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ While it runs, the daemon takes messages on a Unix socket, `komorebi.sock` in `$XDG_RUNTIME_DIR/komorebi` (the local data directory when there is no runtime directory): a message can ask for the state of the window manager, ask which monitor, workspace or container has the focus, give the focus to another -window, retile every monitor, or stop the daemon. [`komorebi-client`](komorebi-client) writes them, and `komorebic`, +window, move the focused window to another tile, retile every monitor, or stop +the daemon. [`komorebi-client`](komorebi-client) writes them, and `komorebic`, the command line, is how a person sends them. `komorebic start` runs the `komorebi` on the `PATH`, and passes it what follows a `--`. In a Plasma session: @@ -132,7 +133,9 @@ Everything else reaches the daemon as a message on its socket: one JSON locked. A message that asks a question is answered on the same connection. A message that gives an order changes the model the way an event does: `focus` and `cycle-focus` give the focus to the container on one side of the focused -one in the layout, or to the next one in the workspace's order. +one in the layout, or to the next one in the workspace's order, and `move` and +`cycle-move` swap the focused container with that one and tile the workspace +again. A program such as a status bar can subscribe to the daemon's notifications: it listens on a socket of its own, and after each event and each message, the diff --git a/docs/cli/cycle-move.md b/docs/cli/cycle-move.md new file mode 100644 index 00000000..a06970e2 --- /dev/null +++ b/docs/cli/cycle-move.md @@ -0,0 +1,14 @@ +```text +Move the focused window in the specified cycle direction + +Usage: cycle-move + +Arguments: + + [possible values: previous, next] + +Options: + -h, --help + Print help + +``` diff --git a/docs/cli/move.md b/docs/cli/move.md new file mode 100644 index 00000000..08f1ef61 --- /dev/null +++ b/docs/cli/move.md @@ -0,0 +1,14 @@ +```text +Move the focused window in the specified direction + +Usage: move + +Arguments: + + [possible values: left, right, up, down] + +Options: + -h, --help + Print help + +``` diff --git a/docs/dev/ARCHITECTURE.md b/docs/dev/ARCHITECTURE.md index 706dc404..9ea8b995 100644 --- a/docs/dev/ARCHITECTURE.md +++ b/docs/dev/ARCHITECTURE.md @@ -27,7 +27,7 @@ its place here. - **Event**: what the compositor reports about a window: it was shown, destroyed, given the focus, minimized or restored ([`WindowManagerEvent`]). - **Message**: what a client sends the daemon on its socket, a [`SocketMessage`]: an order - (focus a window, retile, stop) or a question (the state, the monitors, one [`StateQuery`]). + (focus a window, move it, retile, stop) or a question (the state, the monitors, one [`StateQuery`]). - **Direction**: where an order goes from the focused container: a side of its tile ([`OperationDirection`]), or the next or the previous container of the workspace ([`CycleDirection`]). @@ -254,6 +254,14 @@ container in that direction, the message fails and changes nothing. Whether the the focus is [`WindowManager::mouse_follows_focus`], which `MouseFollowsFocus` and `ToggleMouseFollowsFocus` set. +`MoveWindow` and `CycleMoveWindow` swap the focused container with the container in that +direction ([`WindowManager::move_container_in_direction`], +[`WindowManager::move_container_in_cycle_direction`]), and tile the workspace again: the window +that moved takes the other's tile, keeps the focus, and the pointer follows it. The swap goes +through `LockableSequence`, so a lock stays with its index: the container swapped onto a locked +index is locked in its place. When there is no container in that direction, the message fails +and nothing moves. + ## The NixOS module The flake exports one module under two names, `nixosModules.default` and @@ -304,7 +312,7 @@ module's unit. changes its containers. - [`window_manager`](../../komorebi/src/window_manager.rs): the [`WindowManager`] and the operations on it (tile the workspace in front, tile every monitor, take a minimized - window out, move the focus). + window out, move the focus, move the focused container). - [`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 @@ -386,6 +394,7 @@ the fake. The window manager is its only caller, through [`backend`]. The contra | Messages are handled one at a time, and in order on one connection | framework path: [`read_commands_uds`] reads a connection line by line and handles each message with the window manager locked | `the_messages_on_one_connection_are_handled_in_order` | none | | A socket left behind by a daemon that did not stop cleanly does not keep the next one from starting | framework path: [`WindowManager::new`] removes the file before it binds | `a_socket_file_left_behind_is_replaced` | none | | A daemon that is told to stop leaves no socket behind | framework path: [`WindowManager::stop`] removes it before it exits | `the_daemon_answers_on_its_socket_and_stops_when_it_is_told_to` | `SIGKILL` | +| A container moved onto a locked index is locked in its place, and the lock leaves the container that moved away | framework path: [`Workspace::swap_containers`] swaps through `LockableSequence`, which keeps the lock with the index | `test_swap_respecting_locks_various_cases`, `a_lock_stays_with_its_index_when_a_window_is_moved_onto_it` | `containers_mut()` is the `VecDeque` itself | | Moving the focus moves no window | test | `focus_gives_the_focus_to_the_window_on_that_side_and_moves_nothing`, `vm-core-e2e` ("moving the focus moved no window") | none | | A subscriber that is gone is dropped, not retried | test | `a_subscriber_that_is_gone_is_dropped_and_not_tried_again` | none | | The bridge holds no window manager logic | convention, with every decision about an event in the table, which the `kwin::state` unit tests pin | the unit tests in `komorebi/src/kwin/state.rs` | none | @@ -494,8 +503,9 @@ A window is shown, and ends up on a tile: driven end to end against the fake. - [`komorebi/tests/socket.rs`](../../komorebi/tests/socket.rs): the daemon over its socket, from its start to a `Stop`. -- [`komorebi/tests/focus.rs`](../../komorebi/tests/focus.rs): the focus commands against the - fake. +- [`komorebi/tests/focus.rs`](../../komorebi/tests/focus.rs) and + [`komorebi/tests/moving.rs`](../../komorebi/tests/moving.rs): the focus and move commands + against the fake. - [`nix/nixos-module.nix`](../../nix/nixos-module.nix): the NixOS module; [`nix/tests/nixos-module.nix`](../../nix/tests/nixos-module.nix), its scenarios, evaluated in real NixOS systems; and [`nix/tests/vm/suites/module`](../../nix/tests/vm/suites/module/test.py), @@ -524,6 +534,9 @@ A window is shown, and ends up on a tile: [`WindowManager::focus_container_in_direction`]: ../../komorebi/src/window_manager.rs [`WindowManager::focus_container_in_cycle_direction`]: ../../komorebi/src/window_manager.rs [`WindowManager::mouse_follows_focus`]: ../../komorebi/src/window_manager.rs +[`WindowManager::move_container_in_direction`]: ../../komorebi/src/window_manager.rs +[`WindowManager::move_container_in_cycle_direction`]: ../../komorebi/src/window_manager.rs +[`Workspace::swap_containers`]: ../../komorebi/src/workspace.rs [`Workspace::new_idx_for_direction`]: ../../komorebi/src/workspace.rs [`Workspace::new_idx_for_cycle_direction`]: ../../komorebi/src/workspace.rs [`OperationDirection`]: ../../komorebi/src/core/mod.rs diff --git a/docs/index.md b/docs/index.md index 64b3fdfb..7c39a4a0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -32,7 +32,8 @@ key bindings --------> komorebic <------> komorebi the flake's module, which also starts komorebi with every Plasma session, and from source anywhere else. [Running komorebi](usage/running-komorebi.md) explains how to start and stop the 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. +[Focusing windows](usage/focusing-windows.md) explains how to give the focus to another window, +and [Moving windows](usage/moving-windows.md) how to move the focused window to another tile. [Subscribing to notifications](usage/subscribing-to-notifications.md) explains how a program such as a status bar follows what komorebi does. The [CLI reference](cli/start.md) has a page for every `komorebic` command, generated from its help. diff --git a/docs/usage/moving-windows.md b/docs/usage/moving-windows.md new file mode 100644 index 00000000..09237554 --- /dev/null +++ b/docs/usage/moving-windows.md @@ -0,0 +1,38 @@ +# Moving windows + +komorebi moves the focused window to another tile of its workspace when it is told to. The +window swaps places with the window on that tile, the workspace is tiled again, and the window +that moved keeps the focus. + +## In a direction + +[`komorebic move`](../cli/move.md) swaps the focused window with the window whose tile is on one +side of it: `left`, `right`, `up` or `down`. As with +[`komorebic focus`](focusing-windows.md#in-a-direction), the layout decides which tile is on +which side. + +```sh +komorebic move left +komorebic move down +komorebic move up +komorebic move right +``` + +When there is no tile on that side, nothing moves, and the daemon logs "there is no container +in this direction". + +## In order + +[`komorebic cycle-move`](../cli/cycle-move.md) swaps the focused window with the next or the +previous window in the order of the workspace, and wraps around: `next` from the last window +swaps it with the first. + +```sh +komorebic cycle-move next +komorebic cycle-move previous +``` + +## The pointer + +The pointer follows the window that moved, unless +[mouse follows focus](focusing-windows.md#the-pointer) is off. diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 155ea46d..91985bf9 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -338,7 +338,9 @@ pub struct SubscribeOptions { pub enum SocketMessage { // Window / Container Commands FocusWindow(OperationDirection), + MoveWindow(OperationDirection), CycleFocusWindow(CycleDirection), + CycleMoveWindow(CycleDirection), Stop, Retile, State, diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index c1f4c923..0b0bba22 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -16,6 +16,10 @@ //! focused one, or to the next or the previous one in the workspace //! ([`WindowManager::focus_container_in_direction`], //! [`WindowManager::focus_container_in_cycle_direction`]). +//! - **MoveWindow** and **CycleMoveWindow** swap the focused container with the one on one side +//! of it, or with the next or the previous one, and tile the workspace again +//! ([`WindowManager::move_container_in_direction`], +//! [`WindowManager::move_container_in_cycle_direction`]). //! - **MouseFollowsFocus** and **ToggleMouseFollowsFocus** set whether the pointer is moved to //! the window that is given the focus. //! - **Retile** tiles the focused workspace of every monitor again. @@ -107,6 +111,9 @@ impl WindowManager { /// - The focus cannot be moved, for one of the reasons /// [`WindowManager::focus_container_in_direction`] and /// [`WindowManager::focus_container_in_cycle_direction`] give. + /// - The focused container cannot be moved, for one of the reasons + /// [`WindowManager::move_container_in_direction`] and + /// [`WindowManager::move_container_in_cycle_direction`] give. /// - The window manager fails to retile, for one of the reasons /// [`WindowManager::retile_all`] gives. /// - The answer cannot be written to `reply`. @@ -128,9 +135,15 @@ impl WindowManager { SocketMessage::FocusWindow(direction) => { self.focus_container_in_direction(direction)?; } + SocketMessage::MoveWindow(direction) => { + self.move_container_in_direction(direction)?; + } SocketMessage::CycleFocusWindow(direction) => { self.focus_container_in_cycle_direction(direction)?; } + SocketMessage::CycleMoveWindow(direction) => { + self.move_container_in_cycle_direction(direction)?; + } SocketMessage::Retile => self.retile_all()?, SocketMessage::State => { let state = match serde_json::to_string_pretty(&State::from(&*self)) { diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 6a36c32a..15c4ff4e 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -9,7 +9,9 @@ //! It also owns the daemon's command socket, which [`listen_for_commands`] reads //! ([`WindowManager::process_command`]), carries out the orders that come in on it, such as //! moving the focus ([`WindowManager::focus_container_in_direction`], -//! [`WindowManager::focus_container_in_cycle_direction`]), and stops the daemon when it is asked +//! [`WindowManager::focus_container_in_cycle_direction`]) and moving the focused container +//! ([`WindowManager::move_container_in_direction`], +//! [`WindowManager::move_container_in_cycle_direction`]), and stops the daemon when it is asked //! to ([`WindowManager::stop`]). //! //! [`listen_for_commands`]: crate::process_command::listen_for_commands @@ -160,6 +162,44 @@ impl WindowManager { Ok(()) } + /// Swaps the focused container with the one whose tile is on the `direction` side of it, so + /// that the focused window takes that tile, and tiles the workspace again + /// + /// The container keeps the focus, and the pointer follows it if + /// [`WindowManager::mouse_follows_focus`] is set. A lock stays with its index: a container + /// that is swapped onto the index of a locked one is locked in its place + /// ([`Workspace::swap_containers`]). + /// + /// # Errors + /// + /// - There is no focused workspace, or no container on that side of the focused one. + /// - The workspace cannot be tiled, or the backend refuses to focus the window. + #[tracing::instrument(skip(self))] + pub fn move_container_in_direction( + &mut self, + direction: OperationDirection, + ) -> eyre::Result<()> { + let workspace = self.focused_workspace()?; + + tracing::info!("moving container"); + + let origin_container_idx = workspace.focused_container_idx(); + let target_container_idx = workspace.new_idx_for_direction(direction); + + match target_container_idx { + None => bail!("there is no container in this direction"), + Some(new_idx) => { + let workspace = self.focused_workspace_mut()?; + workspace.swap_containers(origin_container_idx, new_idx); + workspace.focus_container(new_idx); + } + } + + self.update_focused_workspace(self.mouse_follows_focus, true)?; + + Ok(()) + } + #[tracing::instrument(skip(self))] pub fn update_focused_workspace( &mut self, @@ -334,6 +374,35 @@ impl WindowManager { Ok(()) } + /// Swaps the focused container with the next (or the previous) one of the focused + /// workspace, wrapping around at the ends, and tiles the workspace again + /// + /// The container keeps the focus, and the pointer follows it if + /// [`WindowManager::mouse_follows_focus`] is set. + /// + /// # Errors + /// + /// - There is no focused workspace, or it has no containers. + /// - The workspace cannot be tiled, or the backend refuses to focus the window. + #[tracing::instrument(skip(self))] + pub fn move_container_in_cycle_direction( + &mut self, + direction: CycleDirection, + ) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + + tracing::info!("moving container"); + + let current_idx = workspace.focused_container_idx(); + let new_idx = workspace + .new_idx_for_cycle_direction(direction) + .ok_or_eyre("this is not a valid direction from the current position")?; + + workspace.swap_containers(current_idx, new_idx); + workspace.focus_container(new_idx); + self.update_focused_workspace(self.mouse_follows_focus, true) + } + pub fn update_known_window_ids(&mut self) { tracing::trace!("updating list of known window_ids"); let mut known_window_ids = HashMap::new(); diff --git a/komorebi/tests/moving.rs b/komorebi/tests/moving.rs new file mode 100644 index 00000000..9fad1850 --- /dev/null +++ b/komorebi/tests/moving.rs @@ -0,0 +1,177 @@ +//! Moving the focused window with `move` and `cycle-move`, against an in-memory desktop: the +//! messages go in through [`WindowManager::process_command`], and what comes out is the order of +//! the containers, where the backend is asked to put each window, and which window has the +//! focus. +//! +//! Window ids are in the 20_000s. + +use komorebi::backend::fake::Command; +use komorebi::backend::fake::FakeBackend; +use komorebi::backend::with_backend; +use komorebi::core::CycleDirection; +use komorebi::core::OperationDirection; +use komorebi::core::SocketMessage; +use komorebi::window::Window; +use komorebi::window_manager::WindowManager; +use komorebi::window_manager_event::KWinNotification; +use komorebi::window_manager_event::SystemNotification; +use komorebi::window_manager_event::WindowManagerEvent; +use std::sync::Arc; + +fn added(id: u32) -> WindowManagerEvent { + WindowManagerEvent::Show( + SystemNotification::KWin(KWinNotification::WindowAdded), + Window::from(id), + ) +} + +/// A window manager over a fake desktop with one 1920x1080 monitor, and a window for each of +/// `ids`, opened in that order. The last one has the focus, and the commands it took to get there +/// are cleared. +fn desktop_with(ids: &[u32]) -> (Arc, WindowManager) { + let fake = Arc::new(FakeBackend::with_one_monitor()); + let (_sender, receiver) = crossbeam_channel::bounded(1); + // unix socket paths are limited to ~104 bytes on macOS, which the temp dir alone can exceed + static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + let socket = std::path::PathBuf::from(format!( + "/tmp/kmm-{}-{}.sock", + std::process::id(), + NEXT.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + )); + + let wm = with_backend(fake.clone(), || { + let mut wm = WindowManager::new(receiver, Some(socket)).unwrap(); + wm.init().unwrap(); + for &id in ids { + fake.open_window(id, "foot", "foot", &format!("terminal {id}")); + wm.process_event(added(id)).unwrap(); + } + wm + }); + + fake.clear_commands(); + (fake, wm) +} + +fn send(fake: &Arc, wm: &mut WindowManager, message: SocketMessage) { + with_backend(fake.clone(), || wm.process_command(message, Vec::new())).unwrap(); +} + +/// The window of each container of the focused workspace, in order +fn order(wm: &WindowManager) -> Vec { + wm.focused_workspace() + .unwrap() + .containers() + .iter() + .map(|container| container.focused_window().unwrap().id) + .collect() +} + +fn focused(wm: &WindowManager) -> u32 { + wm.focused_window().unwrap().id +} + +#[test] +fn move_swaps_the_focused_window_with_the_one_on_that_side() { + let (fake, mut wm) = desktop_with(&[20_001, 20_002]); + let (left, right) = (fake.rect_of(20_001), fake.rect_of(20_002)); + + send( + &fake, + &mut wm, + SocketMessage::MoveWindow(OperationDirection::Left), + ); + + assert_eq!(order(&wm), [20_002, 20_001]); + assert_eq!( + focused(&wm), + 20_002, + "the window that moved keeps the focus" + ); + assert_eq!(fake.rect_of(20_002), left); + assert_eq!(fake.rect_of(20_001), right); + assert!( + fake.commands().contains(&Command::Focus(20_002, true)), + "{:?}", + fake.commands() + ); + + send( + &fake, + &mut wm, + SocketMessage::MoveWindow(OperationDirection::Right), + ); + + assert_eq!(order(&wm), [20_001, 20_002]); + assert_eq!((fake.rect_of(20_001), fake.rect_of(20_002)), (left, right)); +} + +#[test] +fn move_towards_a_side_with_no_window_is_an_error_and_moves_nothing() { + // the two windows are side by side: there is nothing below them + let (fake, mut wm) = desktop_with(&[20_011, 20_012]); + + let result = with_backend(fake.clone(), || { + wm.process_command( + SocketMessage::MoveWindow(OperationDirection::Down), + Vec::new(), + ) + }); + + assert_eq!( + result.map_err(|error| error.to_string()), + Err(String::from("there is no container in this direction")) + ); + assert_eq!(order(&wm), [20_011, 20_012]); + assert!(fake.commands().is_empty(), "{:?}", fake.commands()); +} + +#[test] +fn cycle_move_swaps_with_the_next_container_and_wraps_around() { + let (fake, mut wm) = desktop_with(&[20_021, 20_022, 20_023]); + let first_tile = fake.rect_of(20_021); + + send( + &fake, + &mut wm, + SocketMessage::CycleMoveWindow(CycleDirection::Next), + ); + + assert_eq!( + order(&wm), + [20_023, 20_022, 20_021], + "next from the last is the first" + ); + assert_eq!(focused(&wm), 20_023); + assert_eq!(fake.rect_of(20_023), first_tile); + + send( + &fake, + &mut wm, + SocketMessage::CycleMoveWindow(CycleDirection::Previous), + ); + + assert_eq!( + order(&wm), + [20_021, 20_022, 20_023], + "previous from the first is the last" + ); + assert_eq!(focused(&wm), 20_023); +} + +#[test] +fn a_lock_stays_with_its_index_when_a_window_is_moved_onto_it() { + let (fake, mut wm) = desktop_with(&[20_031, 20_032]); + wm.focused_workspace_mut().unwrap().containers_mut()[0].locked = true; + + send( + &fake, + &mut wm, + SocketMessage::MoveWindow(OperationDirection::Left), + ); + + let containers = wm.focused_workspace().unwrap().containers(); + assert_eq!(order(&wm), [20_032, 20_031]); + assert!(containers[0].locked, "the first index is still locked"); + assert!(!containers[1].locked, "the window that was locked is not"); +} diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index d09856d2..78892efe 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -7,6 +7,7 @@ //! //! `focus` and `cycle-focus` give the focus to another window of the focused workspace, and //! `mouse-follows-focus` and `toggle-mouse-follows-focus` set whether the pointer goes with it. +//! `move` and `cycle-move` move the focused window to another tile. //! //! `subscribe-socket` and `unsubscribe-socket` add and remove a subscriber: a program that listens //! on a socket in the daemon's data directory, to which the daemon sends a notification after each @@ -70,7 +71,9 @@ macro_rules! gen_enum_subcommand_args { gen_enum_subcommand_args! { Focus: OperationDirection, + Move: OperationDirection, CycleFocus: CycleDirection, + CycleMove: CycleDirection, MouseFollowsFocus: BooleanState, Query: StateQuery, } @@ -159,9 +162,15 @@ enum SubCommand { /// Change focus to the window in the specified direction #[clap(arg_required_else_help = true)] Focus(Focus), + /// Move the focused window in the specified direction + #[clap(arg_required_else_help = true)] + Move(Move), /// Change focus to the window in the specified cycle direction #[clap(arg_required_else_help = true)] CycleFocus(CycleFocus), + /// Move the focused window in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleMove(CycleMove), /// Force the retiling of all managed windows Retile, /// Enable or disable mouse follows focus on all workspaces @@ -289,9 +298,15 @@ fn main() -> eyre::Result<()> { SubCommand::Focus(args) => { send_message(&SocketMessage::FocusWindow(args.operation_direction))?; } + SubCommand::Move(args) => { + send_message(&SocketMessage::MoveWindow(args.operation_direction))?; + } SubCommand::CycleFocus(args) => { send_message(&SocketMessage::CycleFocusWindow(args.cycle_direction))?; } + SubCommand::CycleMove(args) => { + send_message(&SocketMessage::CycleMoveWindow(args.cycle_direction))?; + } SubCommand::Retile => { send_message(&SocketMessage::Retile)?; } diff --git a/komorebic/tests/cli.rs b/komorebic/tests/cli.rs index 1e25859e..41ae4097 100644 --- a/komorebic/tests/cli.rs +++ b/komorebic/tests/cli.rs @@ -177,6 +177,19 @@ fn focus_and_cycle_focus_send_the_direction_they_are_given() { ); } +#[test] +fn move_and_cycle_move_send_the_direction_they_are_given() { + sends( + &["move", "right"], + r#"{"type":"MoveWindow","content":"Right"}"#, + ); + sends(&["move", "up"], r#"{"type":"MoveWindow","content":"Up"}"#); + sends( + &["cycle-move", "previous"], + r#"{"type":"CycleMoveWindow","content":"Previous"}"#, + ); +} + #[test] fn mouse_follows_focus_is_set_with_enable_and_disable_or_toggled() { sends( @@ -234,11 +247,13 @@ fn docgen_writes_the_help_of_every_subcommand_in_a_text_fence() { [ "completions.md", "cycle-focus.md", + "cycle-move.md", "data-directory.md", "focus.md", "log.md", "monitor-information.md", "mouse-follows-focus.md", + "move.md", "query.md", "retile.md", "start.md", diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index 269c27a6..912d9fdf 100644 --- a/nix/tests/vm/suites/core-e2e/test.py +++ b/nix/tests/vm/suites/core-e2e/test.py @@ -3,8 +3,9 @@ # the daemon asked for it (`komorebi/tests/wm_events.rs` checks the window manager's side against # the fake), that the daemon and KWin agree on which window has the focus, that `komorebic` reaches # the daemon of a session and reads its state, that `komorebic focus` and `cycle-focus` move KWin's -# focus (`komorebi/tests/focus.rs` checks which window they pick), and that `komorebic stop` gives -# KWin back what the daemon set up in it. A window manager that agrees with itself proves nothing, so what the daemon +# focus (`komorebi/tests/focus.rs` checks which window they pick), that `komorebic move` and +# `cycle-move` swap windows between their tiles (`komorebi/tests/moving.rs`), and that +# `komorebic stop` gives KWin back what the daemon set up in it. A window manager that agrees with itself proves nothing, so what the daemon # believes is read from its debug log and from `komorebic state`, and what KWin has through kdotool. import json @@ -69,6 +70,12 @@ def state_agrees() -> bool: return active != "" and kde.title(active) == kde.focused_title() +def at(title: str) -> tuple[int, int]: + """Where KWin has the window: foot rounds its size to its cells, but not its position.""" + r = kde.rect(kde.wid(title)) + return (r.x, r.y) + + def report(titles: list[str]) -> None: for title in titles: print(f" {title}: asked {asked(title)}, has {kde.rect(kde.wid(title))}") @@ -134,7 +141,29 @@ checks.check( ) checks.check("moving the focus moved no window", all(where_asked(t) for t in titles)) -print("== 5. minimize and restore") +print("== 5. the windows move on komorebic's commands") +left, top_right = at("win1"), at("win2") +kde.komorebic("move", "right") +checks.check( + "after 'move right' win1 and win2 have swapped tiles", + kde.wait_for(lambda: at("win1") == top_right and at("win2") == left, timeout=20), +) +checks.check( + "... every window is where the daemon asked for it", + kde.wait_for(lambda: all(where_asked(t) for t in titles), timeout=20), +) +checks.check( + "... and win1 keeps the focus", + kde.wait_for(lambda: kde.title(kde.active()) == "win1" and state_agrees()), +) +kde.komorebic("cycle-move", "previous") +checks.check( + "'cycle-move previous' puts win1 and win2 back", + kde.wait_for(lambda: at("win1") == left and at("win2") == top_right, timeout=20), +) +report(titles) + +print("== 6. minimize and restore") kde.minimize(kde.wid("win2")) two = ["win1", "win3"] checks.check( @@ -161,7 +190,7 @@ checks.check( ) report(titles) -print("== 6. close") +print("== 7. close") kde.close(kde.wid("win3")) checks.check("KWin closed win3", kde.wait_for(lambda: kde.wid("win3") == "", timeout=10)) checks.check( @@ -178,7 +207,7 @@ report(["win1", "win2"]) checks.check("komorebi manages the two that are left", kde.wait_for(lambda: kde.managed() == 2)) checks.check("KWin carried out every command", "the bridge could not do" not in daemon_log()) -print("== 7. KWin restarts under the daemon") +print("== 8. KWin restarts under the daemon") kde.restart_kwin() checks.check( "the daemon loaded its script into the new KWin", @@ -199,7 +228,7 @@ checks.check( ) report(["afterwards"]) -print("== 8. komorebic stop") +print("== 9. komorebic stop") kde.komorebic("stop") checks.check("the daemon exited", kde.wait_for(lambda: not kde.komorebi_running())) checks.check("its socket is gone", kde.run(f"test -e {kde.runtime}/komorebi/komorebi.sock")[0] != 0)