mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-17 22:33:12 +01:00
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.
This commit is contained in:
1 parent
d0a547cda0
commit
0bf26a6d14
2 files changed
+62
No files matched your search
@@ -333,3 +333,37 @@ pub fn move_container_to_monitor_workspace_number<H: MonitorCommandHost>(
|
||||
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<Monitor<Self::P>>;
|
||||
}
|
||||
|
||||
/// 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<H: ContainerCommandHost>(
|
||||
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(())
|
||||
}
|
||||
@@ -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<Monitor<StubPlatform>> {
|
||||
&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());
|
||||
}
|
||||
Reference in new issue
Block a user