feat(core): the window, named-workspace and behaviour-toggle commands as shared functions

Adds to komorebi_core::command: move_window, focus_window,
cycle_focus_window (layer dispatch: tiling or floating function of the
host), preselect_direction, cancel_preselect, promote_window,
monitor_workspace_index_by_name, focus_named_workspace,
move_container_to_named_workspace(follow), and the three behaviour toggles
(toggle_window_container_behaviour, toggle_cross_monitor_move_behaviour,
toggle_monocle_focus_behaviour). The host traits are WindowCommandHost
(thin forwards to each daemon's direction functions plus
update_focused_workspace_layout) and BehaviourCommandHost (get and set of
three settings). The bodies are the text the router arms have on all three
daemons today. Nothing calls them yet: the daemons converge in the next
three revisions. No behaviour change.

Native tests on the recording stub host, each naming its commands.py
check (mirrors: commands / ...): 5 new tests, komorebi-core commands
test file 23 passed (was 18).
Verified: cargo test -p komorebi-core --features schemars all pass;
clippy -D warnings on komorebi-core all targets passes; format-all
--check clean.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:27 -08:00
1 parent 0f8a160ee3
commit f4b4d0ff05
2 files changed
+493

No files matched your search

+211
View File
@@ -10,11 +10,15 @@
use crate::Monitor;
use crate::MonitorPlatform;
use crate::WorkspaceLayer;
use color_eyre::eyre;
use color_eyre::eyre::OptionExt;
use komorebi_config::behaviour::WindowContainerBehaviour;
use komorebi_layouts::CycleDirection;
use komorebi_layouts::OperationDirection;
use komorebi_protocol::message::SocketMessage;
use komorebi_protocol::types::MonocleFocusBehaviour;
use komorebi_protocol::types::MoveBehaviour;
use std::collections::VecDeque;
use std::num::NonZeroUsize;
@@ -403,3 +407,210 @@ pub fn focus_workspace_numbers<H: CommandHost>(
host.focus_workspace(workspace_idx)?;
Ok(())
}
/// What the commands on the focused window need from the window manager, beyond [`CommandHost`]:
/// the moves and focus changes by direction (each daemon's own text, they differ in the borders,
/// the animation guard and the window API) and the layout of the focused workspace.
pub trait WindowCommandHost: CommandHost {
/// Moves the focused container in `direction` (tiling layer).
fn move_container_in_direction(&mut self, direction: OperationDirection) -> eyre::Result<()>;
/// Moves the focused floating window in `direction` (floating layer).
fn move_floating_window_in_direction(
&mut self,
direction: OperationDirection,
) -> eyre::Result<()>;
/// Focuses the container in `direction` (tiling layer).
fn focus_container_in_direction(&mut self, direction: OperationDirection) -> eyre::Result<()>;
/// Focuses the floating window in `direction` (floating layer).
fn focus_floating_window_in_direction(
&mut self,
direction: OperationDirection,
) -> eyre::Result<()>;
/// Focuses the next or previous container (tiling layer).
fn focus_container_in_cycle_direction(&mut self, direction: CycleDirection)
-> eyre::Result<()>;
/// Focuses the next or previous floating window (floating layer).
fn focus_floating_window_in_cycle_direction(
&mut self,
direction: CycleDirection,
) -> eyre::Result<()>;
/// Preselects the place in `direction` of the focused container for the next window.
fn preselect_container_in_direction(
&mut self,
direction: OperationDirection,
) -> eyre::Result<()>;
/// Moves the focused container to the front of the layout.
fn promote_container_to_front(&mut self) -> eyre::Result<()>;
/// Lays the focused workspace out again (`Workspace::update`).
fn update_focused_workspace_layout(&mut self) -> eyre::Result<()>;
}
fn focused_layer<H: CommandHost>(host: &H) -> eyre::Result<WorkspaceLayer> {
Ok(host
.focused_monitor()
.and_then(|monitor| monitor.focused_workspace())
.ok_or_eyre("there is no workspace")?
.layer)
}
/// Moves the focused container (tiling layer) or floating window (floating layer) in `direction`
pub fn move_window<H: WindowCommandHost>(
host: &mut H,
direction: OperationDirection,
) -> eyre::Result<()> {
match focused_layer(host)? {
WorkspaceLayer::Tiling => host.move_container_in_direction(direction),
WorkspaceLayer::Floating => host.move_floating_window_in_direction(direction),
}
}
/// Focuses the container (tiling layer) or floating window (floating layer) in `direction`
pub fn focus_window<H: WindowCommandHost>(
host: &mut H,
direction: OperationDirection,
) -> eyre::Result<()> {
match focused_layer(host)? {
WorkspaceLayer::Tiling => host.focus_container_in_direction(direction),
WorkspaceLayer::Floating => host.focus_floating_window_in_direction(direction),
}
}
/// Focuses the next or previous container (tiling layer) or floating window (floating layer)
pub fn cycle_focus_window<H: WindowCommandHost>(
host: &mut H,
direction: CycleDirection,
) -> eyre::Result<()> {
match focused_layer(host)? {
WorkspaceLayer::Tiling => host.focus_container_in_cycle_direction(direction),
WorkspaceLayer::Floating => host.focus_floating_window_in_cycle_direction(direction),
}
}
/// Preselects the place in `direction` of the focused container for the next window, on the
/// tiling layer and only when the workspace has no preselect yet, and lays the workspace out
pub fn preselect_direction<H: WindowCommandHost>(
host: &mut H,
direction: OperationDirection,
) -> eyre::Result<()> {
let workspace = host
.focused_monitor()
.and_then(|monitor| monitor.focused_workspace())
.ok_or_eyre("there is no workspace")?;
if workspace.preselected_container_idx.is_some() {
tracing::warn!("ignoring command as this workspace already has a direction preselect set");
} else if matches!(workspace.layer, WorkspaceLayer::Tiling) {
host.preselect_container_in_direction(direction)?;
host.update_focused_workspace_layout()?;
}
Ok(())
}
/// Forgets the preselect of the focused workspace and lays it out
pub fn cancel_preselect<H: WindowCommandHost>(host: &mut H) -> eyre::Result<()> {
host.focused_monitor_mut()
.and_then(|monitor| monitor.focused_workspace_mut())
.ok_or_eyre("there is no workspace")?
.cancel_preselect();
host.update_focused_workspace_layout()
}
/// Focuses the container in `direction`, then moves it to the front of the layout
pub fn promote_window<H: WindowCommandHost>(
host: &mut H,
direction: OperationDirection,
) -> eyre::Result<()> {
host.focus_container_in_direction(direction)?;
host.promote_container_to_front()
}
/// The index of the workspace called `name` and the monitor that has it, searching the monitors in
/// order
pub fn monitor_workspace_index_by_name<H: CommandHost>(
host: &H,
name: &str,
) -> Option<(usize, usize)> {
host.monitors()
.iter()
.enumerate()
.find_map(|(monitor_idx, monitor)| {
monitor
.workspaces()
.iter()
.position(|workspace| workspace.name.as_deref() == Some(name))
.map(|workspace_idx| (monitor_idx, workspace_idx))
})
}
/// Focuses the workspace called `name`, on the monitor that has it
pub fn focus_named_workspace<H: CommandHost>(host: &mut H, name: &str) -> eyre::Result<()> {
if let Some((monitor_idx, workspace_idx)) = monitor_workspace_index_by_name(host, name) {
host.focus_monitor(monitor_idx)?;
host.focus_workspace(workspace_idx)?;
}
Ok(())
}
/// Moves the focused container to the workspace called `name`, on whichever monitor has it, and
/// follows it when `follow` is set
pub fn move_container_to_named_workspace<H: MonitorCommandHost>(
host: &mut H,
name: &str,
follow: bool,
) -> eyre::Result<()> {
if let Some((monitor_idx, workspace_idx)) = monitor_workspace_index_by_name(host, name) {
let direction = host.direction_from_monitor_idx(monitor_idx);
host.move_container_to_monitor(monitor_idx, Some(workspace_idx), follow, direction)?;
}
Ok(())
}
/// The settings the behaviour toggles flip.
pub trait BehaviourCommandHost {
/// Whether a new window gets a container of its own or joins the focused one.
fn window_container_behaviour(&self) -> WindowContainerBehaviour;
/// Sets [`BehaviourCommandHost::window_container_behaviour`].
fn set_window_container_behaviour(&mut self, behaviour: WindowContainerBehaviour);
/// What a cross-monitor move does with the container it meets.
fn cross_monitor_move_behaviour(&self) -> MoveBehaviour;
/// Sets [`BehaviourCommandHost::cross_monitor_move_behaviour`].
fn set_cross_monitor_move_behaviour(&mut self, behaviour: MoveBehaviour);
/// What focusing does while a monocle container is in front.
fn monocle_focus_behaviour(&self) -> MonocleFocusBehaviour;
/// Sets [`BehaviourCommandHost::monocle_focus_behaviour`].
fn set_monocle_focus_behaviour(&mut self, behaviour: MonocleFocusBehaviour);
}
/// Flips between creating a container for a new window and appending it to the focused one
pub fn toggle_window_container_behaviour<H: BehaviourCommandHost>(host: &mut H) {
host.set_window_container_behaviour(match host.window_container_behaviour() {
WindowContainerBehaviour::Create => WindowContainerBehaviour::Append,
WindowContainerBehaviour::Append => WindowContainerBehaviour::Create,
});
}
/// Flips a cross-monitor move between swapping and inserting; `NoOp` stays as it is
pub fn toggle_cross_monitor_move_behaviour<H: BehaviourCommandHost>(host: &mut H) {
match host.cross_monitor_move_behaviour() {
MoveBehaviour::Swap => host.set_cross_monitor_move_behaviour(MoveBehaviour::Insert),
MoveBehaviour::Insert => host.set_cross_monitor_move_behaviour(MoveBehaviour::Swap),
_ => {}
}
}
/// Flips the focus behaviour of a monocle container between cycling and doing nothing
pub fn toggle_monocle_focus_behaviour<H: BehaviourCommandHost>(host: &mut H) {
host.set_monocle_focus_behaviour(match host.monocle_focus_behaviour() {
MonocleFocusBehaviour::Cycle => MonocleFocusBehaviour::NoOp,
MonocleFocusBehaviour::NoOp => MonocleFocusBehaviour::Cycle,
});
}
+282
View File
@@ -440,3 +440,285 @@ fn focus_workspaces_shows_the_workspace_on_every_other_monitor_then_on_the_focus
]
);
}
/// A host of the window commands: records which of the daemon's functions the shared command called
struct Windows {
host: Host,
container_behaviour: komorebi_config::behaviour::WindowContainerBehaviour,
move_behaviour: komorebi_protocol::types::MoveBehaviour,
monocle_behaviour: komorebi_protocol::types::MonocleFocusBehaviour,
}
impl Windows {
fn new() -> Self {
Self {
host: Host::new(3),
container_behaviour: Default::default(),
move_behaviour: komorebi_protocol::types::MoveBehaviour::Swap,
monocle_behaviour: komorebi_protocol::types::MonocleFocusBehaviour::Cycle,
}
}
fn floating() -> Self {
let mut windows = Self::new();
windows.host.monitors[0]
.focused_workspace_mut()
.unwrap()
.layer = komorebi_core::WorkspaceLayer::Floating;
windows
}
fn record(&mut self, call: impl Into<String>) -> eyre::Result<()> {
self.host.calls.push(call.into());
Ok(())
}
}
impl CommandHost for Windows {
type P = StubPlatform;
fn monitors(&self) -> &VecDeque<Monitor<StubPlatform>> {
self.host.monitors()
}
fn focused_monitor(&self) -> Option<&Monitor<StubPlatform>> {
self.host.focused_monitor()
}
fn focused_monitor_mut(&mut self) -> Option<&mut Monitor<StubPlatform>> {
self.host.focused_monitor_mut()
}
fn focused_monitor_idx(&self) -> usize {
self.host.focused_monitor_idx()
}
fn monitor_idx_from_current_pos(&mut self) -> Option<usize> {
self.host.monitor_idx_from_current_pos()
}
fn focus_monitor(&mut self, idx: usize) -> eyre::Result<()> {
self.host.focus_monitor(idx)
}
fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()> {
self.host.focus_workspace(idx)
}
fn show_workspace_on(&mut self, monitor_idx: usize, workspace_idx: usize) -> eyre::Result<()> {
self.host.show_workspace_on(monitor_idx, workspace_idx)
}
fn move_container_to_workspace(
&mut self,
idx: usize,
follow: bool,
direction: Option<OperationDirection>,
) -> eyre::Result<()> {
self.host
.move_container_to_workspace(idx, follow, direction)
}
}
impl komorebi_core::MonitorCommandHost for Windows {
fn mouse_follows_focus(&self) -> bool {
self.host.mouse_follows_focus()
}
fn update_focused_workspace(&mut self, a: bool, b: bool) -> eyre::Result<()> {
self.host.update_focused_workspace(a, b)
}
fn direction_from_monitor_idx(&self, idx: usize) -> Option<OperationDirection> {
self.host.direction_from_monitor_idx(idx)
}
fn move_container_to_monitor(
&mut self,
idx: usize,
workspace_idx: Option<usize>,
follow: bool,
direction: Option<OperationDirection>,
) -> eyre::Result<()> {
self.host
.move_container_to_monitor(idx, workspace_idx, follow, direction)
}
fn focused_workspace_idx(&self) -> eyre::Result<usize> {
self.host.focused_workspace_idx()
}
}
impl komorebi_core::WindowCommandHost for Windows {
fn move_container_in_direction(&mut self, d: OperationDirection) -> eyre::Result<()> {
self.record(format!("move_container {d}"))
}
fn move_floating_window_in_direction(&mut self, d: OperationDirection) -> eyre::Result<()> {
self.record(format!("move_floating {d}"))
}
fn focus_container_in_direction(&mut self, d: OperationDirection) -> eyre::Result<()> {
self.record(format!("focus_container {d}"))
}
fn focus_floating_window_in_direction(&mut self, d: OperationDirection) -> eyre::Result<()> {
self.record(format!("focus_floating {d}"))
}
fn focus_container_in_cycle_direction(&mut self, d: CycleDirection) -> eyre::Result<()> {
self.record(format!("cycle_container {d}"))
}
fn focus_floating_window_in_cycle_direction(&mut self, d: CycleDirection) -> eyre::Result<()> {
self.record(format!("cycle_floating {d}"))
}
fn preselect_container_in_direction(&mut self, d: OperationDirection) -> eyre::Result<()> {
self.record(format!("preselect {d}"))
}
fn promote_container_to_front(&mut self) -> eyre::Result<()> {
self.record("promote")
}
fn update_focused_workspace_layout(&mut self) -> eyre::Result<()> {
self.record("update")
}
}
impl komorebi_core::BehaviourCommandHost for Windows {
fn window_container_behaviour(&self) -> komorebi_config::behaviour::WindowContainerBehaviour {
self.container_behaviour
}
fn set_window_container_behaviour(
&mut self,
behaviour: komorebi_config::behaviour::WindowContainerBehaviour,
) {
self.container_behaviour = behaviour;
}
fn cross_monitor_move_behaviour(&self) -> komorebi_protocol::types::MoveBehaviour {
self.move_behaviour
}
fn set_cross_monitor_move_behaviour(&mut self, b: komorebi_protocol::types::MoveBehaviour) {
self.move_behaviour = b;
}
fn monocle_focus_behaviour(&self) -> komorebi_protocol::types::MonocleFocusBehaviour {
self.monocle_behaviour
}
fn set_monocle_focus_behaviour(&mut self, b: komorebi_protocol::types::MonocleFocusBehaviour) {
self.monocle_behaviour = b;
}
}
#[test]
fn the_window_commands_pick_the_tiling_or_the_floating_function_by_the_layer() {
// mirrors: commands / "move left", "focus left", "cycle-focus next", "cycle-move next"
let mut tiling = Windows::new();
komorebi_core::move_window(&mut tiling, OperationDirection::Left).unwrap();
komorebi_core::focus_window(&mut tiling, OperationDirection::Right).unwrap();
komorebi_core::cycle_focus_window(&mut tiling, CycleDirection::Next).unwrap();
assert_eq!(
tiling.host.calls,
[
"move_container Left",
"focus_container Right",
"cycle_container Next"
]
);
let mut floating = Windows::floating();
komorebi_core::move_window(&mut floating, OperationDirection::Left).unwrap();
komorebi_core::focus_window(&mut floating, OperationDirection::Right).unwrap();
komorebi_core::cycle_focus_window(&mut floating, CycleDirection::Previous).unwrap();
assert_eq!(
floating.host.calls,
[
"move_floating Left",
"focus_floating Right",
"cycle_floating Previous"
]
);
}
#[test]
fn preselect_sets_a_direction_once_on_the_tiling_layer_and_lays_the_workspace_out() {
// mirrors: commands / "preselect left", "cancel-preselect"
let mut windows = Windows::new();
komorebi_core::preselect_direction(&mut windows, OperationDirection::Left).unwrap();
assert_eq!(windows.host.calls, ["preselect Left", "update"]);
// a workspace that already has a preselect ignores the command
windows.host.calls.clear();
windows.host.monitors[0]
.focused_workspace_mut()
.unwrap()
.preselected_container_idx = Some(0);
komorebi_core::preselect_direction(&mut windows, OperationDirection::Up).unwrap();
assert!(windows.host.calls.is_empty());
// cancelling forgets it and lays the workspace out
komorebi_core::cancel_preselect(&mut windows).unwrap();
assert_eq!(windows.host.calls, ["update"]);
assert_eq!(
windows.host.monitors[0]
.focused_workspace()
.unwrap()
.preselected_container_idx,
None
);
// the floating layer has no preselect
let mut floating = Windows::floating();
komorebi_core::preselect_direction(&mut floating, OperationDirection::Left).unwrap();
assert!(floating.host.calls.is_empty());
}
#[test]
fn promote_window_focuses_in_the_direction_then_promotes() {
// mirrors: commands / "promote-window left"
let mut windows = Windows::new();
komorebi_core::promote_window(&mut windows, OperationDirection::Left).unwrap();
assert_eq!(windows.host.calls, ["focus_container Left", "promote"]);
}
fn name_workspace(host: &mut Host, monitor: usize, workspace: usize, name: &str) {
host.monitors[monitor].workspaces_mut()[workspace].name = Some(name.to_string());
}
#[test]
fn named_workspace_commands_find_the_workspace_on_any_monitor() {
// mirrors: commands / "focus-named-workspace", "move-to-named-workspace",
// "send-to-named-workspace" (the stub host has focused the monitor with the workspace by then,
// so there is no direction to it)
let mut windows = Windows::new();
windows.host.monitors.push_back(monitor(3));
name_workspace(&mut windows.host, 1, 2, "web");
komorebi_core::focus_named_workspace(&mut windows, "web").unwrap();
assert_eq!(windows.host.calls, ["focus_monitor 1", "focus_workspace 2"]);
windows.host.calls.clear();
komorebi_core::move_container_to_named_workspace(&mut windows, "web", true).unwrap();
komorebi_core::move_container_to_named_workspace(&mut windows, "web", false).unwrap();
assert_eq!(
windows.host.calls,
[
"move_to_monitor 1 Some(2) true false",
"move_to_monitor 1 Some(2) false false"
]
);
// a name that is nowhere does nothing
windows.host.calls.clear();
komorebi_core::focus_named_workspace(&mut windows, "nowhere").unwrap();
komorebi_core::move_container_to_named_workspace(&mut windows, "nowhere", true).unwrap();
assert!(windows.host.calls.is_empty());
}
#[test]
fn the_behaviour_toggles_flip_between_two_values_and_leave_noop_alone() {
// mirrors: commands / "toggle-window-container-behaviour", "toggle-cross-monitor-move-behaviour",
// "toggle-monocle-focus-behaviour"
use komorebi_config::behaviour::WindowContainerBehaviour as Container;
use komorebi_protocol::types::MonocleFocusBehaviour as Monocle;
use komorebi_protocol::types::MoveBehaviour as Move;
let mut windows = Windows::new();
komorebi_core::toggle_window_container_behaviour(&mut windows);
assert_eq!(windows.container_behaviour, Container::Append);
komorebi_core::toggle_window_container_behaviour(&mut windows);
assert_eq!(windows.container_behaviour, Container::Create);
komorebi_core::toggle_cross_monitor_move_behaviour(&mut windows);
assert_eq!(windows.move_behaviour, Move::Insert);
komorebi_core::toggle_cross_monitor_move_behaviour(&mut windows);
assert_eq!(windows.move_behaviour, Move::Swap);
windows.move_behaviour = Move::NoOp;
komorebi_core::toggle_cross_monitor_move_behaviour(&mut windows);
assert_eq!(windows.move_behaviour, Move::NoOp);
komorebi_core::toggle_monocle_focus_behaviour(&mut windows);
assert_eq!(windows.monocle_behaviour, Monocle::NoOp);
komorebi_core::toggle_monocle_focus_behaviour(&mut windows);
assert_eq!(windows.monocle_behaviour, Monocle::Cycle);
}