feat(core): the first shared commands, over a CommandHost, with native tests

komorebi-core gets `command`: the workspace navigation commands as
functions generic over a CommandHost (the few window-manager operations
they build on: the monitors and the focused one, the monitor under the
pointer, focus_monitor, focus_workspace, move_container_to_workspace):
cycle_focus_workspace, cycle_focus_empty_workspace, close_workspace,
focus_last_workspace and move_container_to_last_workspace. The text is
KDE's (which is macOS's); the one platform difference, that Windows lets the
pointer pick the monitor first for cycle_focus_empty_workspace,
close_workspace and focus_last_workspace where KDE and macOS do not, is the
associated const CommandHost::POINTER_PICKS_THE_MONITOR_FOR_ALL (false by
default). Nothing uses them yet.

tests/commands.rs implements the host with a recording stub and mirrors the
e2e checks of commands.py (cycle-workspace, cycle-empty-workspace,
close-workspace, focus-last-workspace, move-to-last-workspace and
send-to-last-workspace): wrap-around, the pointer's monitor, which
workspaces close, going back and forth, follow and send, and the Windows
switch.

Verified: cargo test -p komorebi-core --features schemars (native): 30
unit + 9 command tests passed, 0 failed (and the other test files of the
crate). Not run: the daemons (nothing calls the functions yet).
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent 2d6919438b
commit 7b3f691194
3 files changed
+511

No files matched your search

+220
View File
@@ -0,0 +1,220 @@
//! The commands that only move the focus and the containers around the shared model.
//!
//! A daemon's `process_command` is a router over its `WindowManager`, which is per platform. What a
//! command does to the monitors, workspaces and containers does not depend on the platform, so the
//! commands whose work is only that are functions here, generic over a [`CommandHost`]: the few
//! operations of the window manager they build on (focus a monitor, show a workspace, move a
//! container). Each daemon implements the host with its own window manager, and the arm of its router
//! calls the function; a test implements it with a recording stub and checks the calls, which is how a
//! command's logic is tested without a Windows or macOS machine.
use crate::Monitor;
use crate::MonitorPlatform;
use color_eyre::eyre;
use color_eyre::eyre::OptionExt;
use komorebi_layouts::CycleDirection;
use komorebi_layouts::OperationDirection;
use std::collections::VecDeque;
use std::num::NonZeroUsize;
/// The window manager as the shared commands see it.
pub trait CommandHost {
/// The platform whose monitors the host has.
type P: MonitorPlatform;
/// Whether the commands that act on the focused monitor first focus the monitor the pointer is
/// on when the workspace shown there is empty, for `cycle_focus_empty_workspace`,
/// `close_workspace` and `focus_last_workspace` (the other commands always do). Windows does it
/// for all of them; KDE and macOS do not.
const POINTER_PICKS_THE_MONITOR_FOR_ALL: bool = false;
/// The monitors.
fn monitors(&self) -> &VecDeque<Monitor<Self::P>>;
/// The monitor that has the focus, if there is one.
fn focused_monitor(&self) -> Option<&Monitor<Self::P>>;
/// The monitor that has the focus, mutably.
fn focused_monitor_mut(&mut self) -> Option<&mut Monitor<Self::P>>;
/// The index of the monitor that has the focus.
fn focused_monitor_idx(&self) -> usize;
/// The index of the monitor the pointer is on, if the platform says where it is.
fn monitor_idx_from_current_pos(&mut self) -> Option<usize>;
/// Gives the focus to the monitor at `idx`.
fn focus_monitor(&mut self, idx: usize) -> eyre::Result<()>;
/// Shows workspace `idx` on the focused monitor and focuses it.
fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()>;
/// Moves the focused container to workspace `idx` of the focused monitor.
fn move_container_to_workspace(
&mut self,
idx: usize,
follow: bool,
direction: Option<OperationDirection>,
) -> eyre::Result<()>;
}
/// This is to ensure that even on an empty workspace on a secondary monitor, the secondary monitor
/// where the cursor is focused will be used as the target for the workspace switch op
fn focus_the_monitor_under_the_pointer<H: CommandHost>(host: &mut H) -> eyre::Result<()> {
if let Some(monitor_idx) = host.monitor_idx_from_current_pos()
&& monitor_idx != host.focused_monitor_idx()
&& let Some(monitor) = host.monitors().get(monitor_idx)
&& let Some(workspace) = monitor.focused_workspace()
&& workspace.is_empty()
{
host.focus_monitor(monitor_idx)?;
}
Ok(())
}
/// Focuses the next or previous workspace of the monitor that is focused, or of the monitor the
/// pointer is on when its workspace is empty
pub fn cycle_focus_workspace<H: CommandHost>(
host: &mut H,
direction: CycleDirection,
) -> eyre::Result<()> {
focus_the_monitor_under_the_pointer(host)?;
let focused_monitor = host.focused_monitor().ok_or_eyre("there is no monitor")?;
let focused_workspace_idx = focused_monitor.focused_workspace_idx();
let workspaces = focused_monitor.workspaces().len();
let workspace_idx = direction.next_idx(
focused_workspace_idx,
NonZeroUsize::new(workspaces).ok_or_eyre("there must be at least one workspace")?,
);
host.focus_workspace(workspace_idx)?;
Ok(())
}
/// Focuses the next or previous empty workspace of the focused monitor, if it has one
pub fn cycle_focus_empty_workspace<H: CommandHost>(
host: &mut H,
direction: CycleDirection,
) -> eyre::Result<()> {
if H::POINTER_PICKS_THE_MONITOR_FOR_ALL {
focus_the_monitor_under_the_pointer(host)?;
}
let focused_monitor = host.focused_monitor().ok_or_eyre("there is no monitor")?;
let focused_workspace_idx = focused_monitor.focused_workspace_idx();
let workspaces = focused_monitor.workspaces().len();
let mut empty_workspaces = vec![];
for (idx, w) in focused_monitor.workspaces().iter().enumerate() {
if w.is_empty() {
empty_workspaces.push(idx);
}
}
if !empty_workspaces.is_empty() {
let mut workspace_idx = direction.next_idx(
focused_workspace_idx,
NonZeroUsize::new(workspaces).ok_or_eyre("there must be at least one workspace")?,
);
while !empty_workspaces.contains(&workspace_idx) {
workspace_idx = direction.next_idx(
workspace_idx,
NonZeroUsize::new(workspaces).ok_or_eyre("there must be at least one workspace")?,
);
}
host.focus_workspace(workspace_idx)?;
}
Ok(())
}
/// Removes the focused workspace when it is empty, unnamed and not the monitor's only one, and
/// focuses the one before it
pub fn close_workspace<H: CommandHost>(host: &mut H) -> eyre::Result<()> {
if H::POINTER_PICKS_THE_MONITOR_FOR_ALL {
focus_the_monitor_under_the_pointer(host)?;
}
let mut can_close = false;
if let Some(monitor) = host.focused_monitor_mut() {
let focused_workspace_idx = monitor.focused_workspace_idx();
let next_focused_workspace_idx = focused_workspace_idx.saturating_sub(1);
if let Some(workspace) = monitor.focused_workspace()
&& monitor.workspaces().len() > 1
&& workspace.containers().is_empty()
&& workspace.floating_windows().is_empty()
&& workspace.monocle_container.is_none()
&& workspace.maximized_window.is_none()
&& workspace.name.is_none()
{
can_close = true;
}
if can_close
&& monitor
.workspaces_mut()
.remove(focused_workspace_idx)
.is_some()
{
host.focus_workspace(next_focused_workspace_idx)?;
}
}
Ok(())
}
/// Focuses the workspace that was focused before this one
pub fn focus_last_workspace<H: CommandHost>(host: &mut H) -> eyre::Result<()> {
if H::POINTER_PICKS_THE_MONITOR_FOR_ALL {
focus_the_monitor_under_the_pointer(host)?;
}
let idx = host
.focused_monitor()
.ok_or_eyre("there is no monitor")?
.focused_workspace_idx();
if let Some(monitor) = host.focused_monitor_mut()
&& let Some(last_focused_workspace) = monitor.last_focused_workspace
{
host.focus_workspace(last_focused_workspace)?;
}
host.focused_monitor_mut()
.ok_or_eyre("there is no monitor")?
.last_focused_workspace = Option::from(idx);
Ok(())
}
/// Moves the focused container to the workspace that was focused before this one, and follows it
/// when `follow` is set
pub fn move_container_to_last_workspace<H: CommandHost>(
host: &mut H,
follow: bool,
) -> eyre::Result<()> {
focus_the_monitor_under_the_pointer(host)?;
let idx = host
.focused_monitor()
.ok_or_eyre("there is no monitor")?
.focused_workspace_idx();
if let Some(monitor) = host.focused_monitor_mut()
&& let Some(last_focused_workspace) = monitor.last_focused_workspace
{
host.move_container_to_workspace(last_focused_workspace, follow, None)?;
}
host.focused_monitor_mut()
.ok_or_eyre("there is no monitor")?
.last_focused_workspace = Option::from(idx);
Ok(())
}
+2
View File
@@ -7,6 +7,7 @@
#![deny(missing_docs)]
mod command;
mod container;
mod globals;
#[allow(missing_docs)]
@@ -14,6 +15,7 @@ mod monitor;
#[allow(missing_docs)]
mod workspace;
pub use command::*;
pub use container::Container;
pub use container::ContainerWindow;
pub use container::WorkspaceWindow;
+289
View File
@@ -0,0 +1,289 @@
//! The shared commands of `komorebi_core::command`, on a recording host.
//!
//! Each test names the e2e check of `commands.py` (SWEEP) it mirrors: the e2e sends the command to a
//! running daemon and checks that it is answered and the daemon survives; here the effect on the
//! model is checked, for all three daemons at once.
mod common;
use color_eyre::eyre;
use common::*;
use komorebi_core::CommandHost;
use komorebi_core::Monitor;
use komorebi_layouts::CycleDirection;
use komorebi_layouts::OperationDirection;
use std::collections::VecDeque;
struct Host {
monitors: VecDeque<Monitor<StubPlatform>>,
focused: usize,
pointer_on: Option<usize>,
calls: Vec<String>,
pointer_for_all: bool,
}
fn monitor(workspaces: usize) -> Monitor<StubPlatform> {
let mut monitor: Monitor<StubPlatform> = serde_json::from_value(serde_json::json!({
"id": 7,
"device": "Virtual-0",
"size": { "left": 0, "top": 0, "right": 1920, "bottom": 1080 },
"work_area_size": { "left": 0, "top": 0, "right": 1920, "bottom": 1040 },
"work_area_offset": null,
"window_based_work_area_offset": null,
"window_based_work_area_offset_limit": 1,
"workspaces": { "elements": [], "focused": 0 },
"container_padding": null,
"workspace_padding": null,
"wallpaper": null,
"floating_layer_behaviour": null
}))
.unwrap();
monitor.ensure_workspace_count(workspaces);
monitor
}
impl Host {
fn new(workspaces: usize) -> Self {
fresh();
Self {
monitors: VecDeque::from([monitor(workspaces)]),
focused: 0,
pointer_on: None,
calls: vec![],
pointer_for_all: false,
}
}
fn shown(&self) -> usize {
self.monitors[self.focused].focused_workspace_idx()
}
/// puts a window on workspace `idx` of the focused monitor
fn occupy(&mut self, idx: usize, window: u32) {
self.monitors[self.focused].workspaces_mut()[idx]
.new_container_for_window(StubWindow { id: window });
}
}
impl CommandHost for Host {
type P = StubPlatform;
fn monitors(&self) -> &VecDeque<Monitor<StubPlatform>> {
&self.monitors
}
fn focused_monitor(&self) -> Option<&Monitor<StubPlatform>> {
self.monitors.get(self.focused)
}
fn focused_monitor_mut(&mut self) -> Option<&mut Monitor<StubPlatform>> {
self.monitors.get_mut(self.focused)
}
fn focused_monitor_idx(&self) -> usize {
self.focused
}
fn monitor_idx_from_current_pos(&mut self) -> Option<usize> {
self.pointer_on
}
fn focus_monitor(&mut self, idx: usize) -> eyre::Result<()> {
self.calls.push(format!("focus_monitor {idx}"));
self.focused = idx;
Ok(())
}
fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()> {
self.calls.push(format!("focus_workspace {idx}"));
self.monitors[self.focused].focus_workspace(idx)
}
fn move_container_to_workspace(
&mut self,
idx: usize,
follow: bool,
_: Option<OperationDirection>,
) -> eyre::Result<()> {
self.calls.push(format!("move_container {idx} {follow}"));
Ok(())
}
}
#[test]
fn cycle_workspace_next_and_previous_wrap_around() {
// commands.py: "cycle-workspace next" / "cycle-workspace previous"
let mut host = Host::new(3);
komorebi_core::cycle_focus_workspace(&mut host, CycleDirection::Next).unwrap();
assert_eq!(host.shown(), 1);
komorebi_core::cycle_focus_workspace(&mut host, CycleDirection::Next).unwrap();
komorebi_core::cycle_focus_workspace(&mut host, CycleDirection::Next).unwrap();
assert_eq!(
host.shown(),
0,
"next wraps from the last workspace to the first"
);
komorebi_core::cycle_focus_workspace(&mut host, CycleDirection::Previous).unwrap();
assert_eq!(host.shown(), 2, "previous wraps from the first to the last");
}
#[test]
fn cycling_focuses_the_monitor_under_the_pointer_when_its_workspace_is_empty() {
// the preamble of cycle-workspace on a second monitor
let mut host = Host::new(2);
host.monitors.push_back(monitor(2));
host.pointer_on = Some(1);
komorebi_core::cycle_focus_workspace(&mut host, CycleDirection::Next).unwrap();
assert_eq!(host.calls[0], "focus_monitor 1");
assert_eq!(host.focused, 1);
}
#[test]
fn cycle_empty_workspace_skips_the_ones_with_windows() {
// commands.py: "cycle-empty-workspace next"
let mut host = Host::new(4);
host.occupy(0, 1);
host.occupy(1, 2);
komorebi_core::cycle_focus_empty_workspace(&mut host, CycleDirection::Next).unwrap();
assert_eq!(host.shown(), 2);
}
#[test]
fn cycle_empty_workspace_does_nothing_when_none_is_empty() {
let mut host = Host::new(2);
host.occupy(0, 1);
host.occupy(1, 2);
komorebi_core::cycle_focus_empty_workspace(&mut host, CycleDirection::Next).unwrap();
assert!(host.calls.is_empty());
}
#[test]
fn close_workspace_removes_an_empty_unnamed_one_and_focuses_the_one_before() {
// commands.py: "close-workspace"
let mut host = Host::new(3);
host.monitors[0].focus_workspace(2).unwrap();
komorebi_core::close_workspace(&mut host).unwrap();
assert_eq!(host.monitors[0].workspaces().len(), 2);
assert_eq!(host.calls, ["focus_workspace 1"]);
}
#[test]
fn close_workspace_keeps_a_workspace_with_a_window_a_name_or_the_only_one() {
let mut host = Host::new(2);
host.monitors[0].focus_workspace(1).unwrap();
host.occupy(1, 1);
komorebi_core::close_workspace(&mut host).unwrap();
assert_eq!(host.monitors[0].workspaces().len(), 2, "a window keeps it");
let mut host = Host::new(2);
host.monitors[0].focus_workspace(1).unwrap();
host.monitors[0].workspaces_mut()[1].name = Some("web".to_string());
komorebi_core::close_workspace(&mut host).unwrap();
assert_eq!(host.monitors[0].workspaces().len(), 2, "a name keeps it");
let mut host = Host::new(1);
komorebi_core::close_workspace(&mut host).unwrap();
assert_eq!(host.monitors[0].workspaces().len(), 1, "the only one stays");
}
#[test]
fn focus_last_workspace_goes_back_and_forth() {
// commands.py: "focus-last-workspace" twice
let mut host = Host::new(3);
host.monitors[0].focus_workspace(2).unwrap();
komorebi_core::focus_last_workspace(&mut host).unwrap();
assert_eq!(host.shown(), 0, "the workspace shown before this one");
komorebi_core::focus_last_workspace(&mut host).unwrap();
assert_eq!(host.shown(), 2, "and back again");
}
#[test]
fn move_to_last_workspace_moves_the_container_and_remembers_where_it_came_from() {
// commands.py: "move-to-last-workspace" and "send-to-last-workspace"
let mut host = Host::new(3);
host.monitors[0].focus_workspace(2).unwrap();
komorebi_core::move_container_to_last_workspace(&mut host, true).unwrap();
assert_eq!(host.calls, ["move_container 0 true"]);
assert_eq!(host.monitors[0].last_focused_workspace, Some(2));
host.calls.clear();
komorebi_core::move_container_to_last_workspace(&mut host, false).unwrap();
assert_eq!(
host.calls,
["move_container 2 false"],
"send does not follow"
);
}
#[test]
fn only_windows_lets_the_pointer_pick_the_monitor_for_the_empty_and_close_commands() {
// the platform switch of CommandHost::POINTER_PICKS_THE_MONITOR_FOR_ALL (default false: KDE and
// macOS); a host that says true gets the preamble for close and cycle-empty too
struct WindowsLike(Host);
impl CommandHost for WindowsLike {
type P = StubPlatform;
const POINTER_PICKS_THE_MONITOR_FOR_ALL: bool = true;
fn monitors(&self) -> &VecDeque<Monitor<StubPlatform>> {
self.0.monitors()
}
fn focused_monitor(&self) -> Option<&Monitor<StubPlatform>> {
self.0.focused_monitor()
}
fn focused_monitor_mut(&mut self) -> Option<&mut Monitor<StubPlatform>> {
self.0.focused_monitor_mut()
}
fn focused_monitor_idx(&self) -> usize {
self.0.focused_monitor_idx()
}
fn monitor_idx_from_current_pos(&mut self) -> Option<usize> {
self.0.monitor_idx_from_current_pos()
}
fn focus_monitor(&mut self, idx: usize) -> eyre::Result<()> {
self.0.focus_monitor(idx)
}
fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()> {
self.0.focus_workspace(idx)
}
fn move_container_to_workspace(
&mut self,
idx: usize,
follow: bool,
direction: Option<OperationDirection>,
) -> eyre::Result<()> {
self.0.move_container_to_workspace(idx, follow, direction)
}
}
let mut other = Host::new(2);
other.monitors.push_back(monitor(2));
other.pointer_on = Some(1);
komorebi_core::close_workspace(&mut other).unwrap();
assert!(
!other.calls.iter().any(|call| call.starts_with("focus_monitor")),
"KDE and macOS do not look at the pointer here"
);
let mut host = WindowsLike({
let mut host = Host::new(2);
host.monitors.push_back(monitor(2));
host.pointer_on = Some(1);
host
});
komorebi_core::close_workspace(&mut host).unwrap();
assert_eq!(host.0.calls[0], "focus_monitor 1");
}