From 0bf26a6d14ce503f9c35d2e54b2d8d8983586e9b Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 18:51:49 -0700 Subject: [PATCH] feat(core): lock and unlock a container by index, with native tests komorebi_core::set_container_locked is the body of the LockMonitorWorkspaceContainer and UnlockMonitorWorkspaceContainer arms (the two differ by the flag; the text is the same on all three daemons), over a third small host trait ContainerCommandHost: CommandHost that has monitors_mut. Tests: lock then unlock, a container index that is not there is ignored and a missing monitor or workspace is an error. Verified: cargo test -p komorebi-core --features schemars --test commands (native): 16 passed, 0 failed. Not run: the daemons. --- crates/komorebi-core/src/command.rs | 34 ++++++++++++++++++++++++++ crates/komorebi-core/tests/commands.rs | 28 +++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/crates/komorebi-core/src/command.rs b/crates/komorebi-core/src/command.rs index 90413878..712ee73e 100644 --- a/crates/komorebi-core/src/command.rs +++ b/crates/komorebi-core/src/command.rs @@ -333,3 +333,37 @@ pub fn move_container_to_monitor_workspace_number( host.move_container_to_monitor(monitor_idx, Option::from(workspace_idx), follow, direction)?; Ok(()) } + +/// What the commands that reach into a monitor's workspaces by index need besides [`CommandHost`]. +pub trait ContainerCommandHost: CommandHost { + /// The monitors, mutably. + fn monitors_mut(&mut self) -> &mut VecDeque>; +} + +/// Locks or unlocks container `container_idx` of workspace `workspace_idx` of monitor +/// `monitor_idx`, so that it keeps its index (or not) while others are inserted, removed or swapped. +/// A container index that is not there is ignored; a monitor or workspace that is not there is an +/// error. +pub fn set_container_locked( + host: &mut H, + monitor_idx: usize, + workspace_idx: usize, + container_idx: usize, + locked: bool, +) -> eyre::Result<()> { + let monitor = host + .monitors_mut() + .get_mut(monitor_idx) + .ok_or_eyre("no monitor at the given index")?; + + let workspace = monitor + .workspaces_mut() + .get_mut(workspace_idx) + .ok_or_eyre("no workspace at the given index")?; + + if let Some(container) = workspace.containers_mut().get_mut(container_idx) { + container.locked = locked; + } + + Ok(()) +} diff --git a/crates/komorebi-core/tests/commands.rs b/crates/komorebi-core/tests/commands.rs index 3cb37f25..d57e4123 100644 --- a/crates/komorebi-core/tests/commands.rs +++ b/crates/komorebi-core/tests/commands.rs @@ -414,3 +414,31 @@ fn move_and_send_to_monitor_workspace_name_the_workspace() { ] ); } + +impl komorebi_core::ContainerCommandHost for Host { + fn monitors_mut(&mut self) -> &mut VecDeque> { + &mut self.monitors + } +} + +#[test] +fn lock_and_unlock_a_container_by_index() { + // commands.py has toggle-lock; the by-index forms (lock/unlock-monitor-workspace-container) are in the KDE command_sweep + let mut host = Host::new(2); + host.occupy(0, 1); + + komorebi_core::set_container_locked(&mut host, 0, 0, 0, true).unwrap(); + assert!(host.monitors[0].workspaces()[0].containers()[0].locked); + + komorebi_core::set_container_locked(&mut host, 0, 0, 0, false).unwrap(); + assert!(!host.monitors[0].workspaces()[0].containers()[0].locked); +} + +#[test] +fn locking_a_container_that_is_not_there_is_ignored_but_a_missing_monitor_or_workspace_is_not() { + let mut host = Host::new(1); + + assert!(komorebi_core::set_container_locked(&mut host, 0, 0, 5, true).is_ok()); + assert!(komorebi_core::set_container_locked(&mut host, 3, 0, 0, true).is_err()); + assert!(komorebi_core::set_container_locked(&mut host, 0, 9, 0, true).is_err()); +}