refactor(core,kde,windows,mac): FocusWorkspaceNumber and FocusWorkspaceNumbers are komorebi-core commands

komorebi_core::command gets focus_workspace_number (show a workspace unless
it is in front) and focus_workspace_numbers (show it on every other monitor,
then on the focused one), both with the pointer preamble when the host's
POINTER_PICKS_THE_MONITOR_FOR_ALL says so, over a new CommandHost method
show_workspace_on (focus the workspace of a monitor and load it, which the
daemons already did inline). The three daemons' arms call them (Windows keeps
its focus_workspace_numbers method as a wrapper) and implement
show_workspace_on. The const is unchanged: true for Windows, false for KDE and
macOS, so nothing changes yet; the next revisions flip it. Tests: the
Windows-switch test becomes a test of all five commands with the switch off and
on, and focus_workspace_number(s) are tested on the stub host (commands.py:
focus-workspace, focus-workspaces).

Verified: cargo test -p komorebi-core --features schemars --test commands: 18
passed; KDE cargo test -p komorebi-linux (lite env): 666 passed, 0 failed; fake-CC cargo
clippy -D warnings on the macOS packages: PASS; Windows cargo clippy -p
komorebi-windows --locked --all-targets -D warnings: PASS. Not run: Windows
tests, VM and Mac.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent 176dc0a8ba
commit 4c15358f7b
5 files changed
+219 -143

No files matched your search

+42
View File
@@ -49,6 +49,10 @@ pub trait CommandHost {
/// Shows workspace `idx` on the focused monitor and focuses it.
fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()>;
/// Shows workspace `workspace_idx` on the monitor at `monitor_idx` (creating it if it is not
/// there) without focusing that monitor.
fn show_workspace_on(&mut self, monitor_idx: usize, workspace_idx: usize) -> eyre::Result<()>;
/// Moves the focused container to workspace `idx` of the focused monitor.
fn move_container_to_workspace(
&mut self,
@@ -367,3 +371,41 @@ pub fn set_container_locked<H: ContainerCommandHost>(
Ok(())
}
/// Shows workspace `workspace_idx` on the focused monitor, unless it is the one in front; the
/// monitor the pointer is on is focused first when its workspace is empty
pub fn focus_workspace_number<H: MonitorCommandHost>(
host: &mut H,
workspace_idx: usize,
) -> eyre::Result<()> {
if H::POINTER_PICKS_THE_MONITOR_FOR_ALL {
focus_the_monitor_under_the_pointer(host)?;
}
if host.focused_workspace_idx().unwrap_or_default() != workspace_idx {
host.focus_workspace(workspace_idx)?;
}
Ok(())
}
/// Shows workspace `workspace_idx` on every monitor, the focused one last; the monitor the pointer
/// is on is focused first when its workspace is empty
pub fn focus_workspace_numbers<H: CommandHost>(
host: &mut H,
workspace_idx: usize,
) -> eyre::Result<()> {
if H::POINTER_PICKS_THE_MONITOR_FOR_ALL {
focus_the_monitor_under_the_pointer(host)?;
}
let focused_monitor_idx = host.focused_monitor_idx();
for idx in 0..host.monitors().len() {
if idx != focused_monitor_idx {
host.show_workspace_on(idx, workspace_idx)?;
}
}
host.focus_workspace(workspace_idx)?;
Ok(())
}
+141 -63
View File
@@ -98,6 +98,12 @@ impl CommandHost for Host {
self.monitors[self.focused].focus_workspace(idx)
}
fn show_workspace_on(&mut self, monitor_idx: usize, workspace_idx: usize) -> eyre::Result<()> {
self.calls
.push(format!("show_workspace_on {monitor_idx} {workspace_idx}"));
self.monitors[monitor_idx].focus_workspace(workspace_idx)
}
fn move_container_to_workspace(
&mut self,
idx: usize,
@@ -227,69 +233,6 @@ fn move_to_last_workspace_moves_the_container_and_remembers_where_it_came_from()
);
}
#[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");
}
impl komorebi_core::MonitorCommandHost for Host {
fn mouse_follows_focus(&self) -> bool {
true
@@ -441,3 +384,138 @@ fn locking_a_container_that_is_not_there_is_ignored_but_a_missing_monitor_or_wor
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());
}
/// A host that says whether the pointer picks the monitor first, and does what `Host` does
struct Pointer<const ALL: bool>(Host);
impl<const ALL: bool> CommandHost for Pointer<ALL> {
type P = StubPlatform;
const POINTER_PICKS_THE_MONITOR_FOR_ALL: bool = ALL;
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 show_workspace_on(&mut self, monitor_idx: usize, workspace_idx: usize) -> eyre::Result<()> {
self.0.show_workspace_on(monitor_idx, 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)
}
}
impl<const ALL: bool> komorebi_core::MonitorCommandHost for Pointer<ALL> {
fn mouse_follows_focus(&self) -> bool {
self.0.mouse_follows_focus()
}
fn update_focused_workspace(&mut self, a: bool, b: bool) -> eyre::Result<()> {
self.0.update_focused_workspace(a, b)
}
fn direction_from_monitor_idx(&self, idx: usize) -> Option<OperationDirection> {
self.0.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.0
.move_container_to_monitor(idx, workspace_idx, follow, direction)
}
fn focused_workspace_idx(&self) -> eyre::Result<usize> {
self.0.focused_workspace_idx()
}
}
fn pointer_on_the_second_monitor() -> Host {
let mut host = two_monitors();
host.pointer_on = Some(1);
host
}
fn focused_monitor_calls(host: &Host) -> usize {
host.calls
.iter()
.filter(|call| call.starts_with("focus_monitor"))
.count()
}
#[test]
fn the_pointer_switch_gives_the_empty_close_last_and_number_commands_the_preamble() {
// commands.py: close-workspace, cycle-empty-workspace, focus-last-workspace, focus-workspace,
// focus-workspaces; with the switch off nothing looks at the pointer, with it on the monitor
// under the pointer is focused first when its workspace is empty
let mut off = Pointer::<false>(pointer_on_the_second_monitor());
komorebi_core::close_workspace(&mut off).unwrap();
komorebi_core::focus_last_workspace(&mut off).unwrap();
komorebi_core::cycle_focus_empty_workspace(&mut off, CycleDirection::Next).unwrap();
komorebi_core::focus_workspace_number(&mut off, 1).unwrap();
komorebi_core::focus_workspace_numbers(&mut off, 1).unwrap();
assert_eq!(focused_monitor_calls(&off.0), 0);
for command in 0..5 {
let mut on = Pointer::<true>(pointer_on_the_second_monitor());
match command {
0 => komorebi_core::close_workspace(&mut on).unwrap(),
1 => komorebi_core::focus_last_workspace(&mut on).unwrap(),
2 => komorebi_core::cycle_focus_empty_workspace(&mut on, CycleDirection::Next).unwrap(),
3 => komorebi_core::focus_workspace_number(&mut on, 1).unwrap(),
_ => komorebi_core::focus_workspace_numbers(&mut on, 1).unwrap(),
}
assert_eq!(on.0.calls[0], "focus_monitor 1", "command {command}");
}
}
#[test]
fn focus_workspace_number_only_switches_when_it_is_not_in_front() {
// commands.py: "focus-workspace 1", "focus-workspace 0"
let mut host = Host::new(3);
komorebi_core::focus_workspace_number(&mut host, 0).unwrap();
assert!(host.calls.is_empty());
komorebi_core::focus_workspace_number(&mut host, 2).unwrap();
assert_eq!(host.calls, ["focus_workspace 2"]);
}
#[test]
fn focus_workspaces_shows_the_workspace_on_every_other_monitor_then_on_the_focused_one() {
// commands.py: "focus-workspaces 1", "focus-workspaces 0"
let mut host = two_monitors();
host.monitors.push_back(monitor(3));
komorebi_core::focus_workspace_numbers(&mut host, 2).unwrap();
assert_eq!(
host.calls,
[
"show_workspace_on 1 2",
"show_workspace_on 2 2",
"focus_workspace 2"
]
);
}
@@ -1125,21 +1125,10 @@ impl WindowManager {
}
SocketMessage::FocusLastWorkspace => self.focus_last_workspace()?,
SocketMessage::FocusWorkspaceNumber(workspace_idx) => {
if self.focused_workspace_idx().unwrap_or_default() != workspace_idx {
self.focus_workspace(workspace_idx)?;
}
komorebi_core::focus_workspace_number(self, workspace_idx)?
}
SocketMessage::FocusWorkspaceNumbers(workspace_idx) => {
let focused_monitor_idx = self.focused_monitor_idx();
for (i, monitor) in self.monitors_mut().iter_mut().enumerate() {
if i != focused_monitor_idx {
monitor.focus_workspace(workspace_idx)?;
monitor.load_focused_workspace(false)?;
}
}
self.focus_workspace(workspace_idx)?;
komorebi_core::focus_workspace_numbers(self, workspace_idx)?
}
SocketMessage::FocusMonitorWorkspaceNumber(monitor_idx, workspace_idx) => {
komorebi_core::focus_monitor_workspace_number(self, monitor_idx, workspace_idx)?
@@ -2052,6 +2041,16 @@ impl komorebi_core::CommandHost for WindowManager {
WindowManager::focus_workspace(self, idx)
}
fn show_workspace_on(&mut self, monitor_idx: usize, workspace_idx: usize) -> eyre::Result<()> {
let monitor = self
.monitors_mut()
.get_mut(monitor_idx)
.ok_or_eyre("there is no monitor at this index")?;
monitor.focus_workspace(workspace_idx)?;
monitor.load_focused_workspace(false)?;
Ok(())
}
fn move_container_to_workspace(
&mut self,
idx: usize,
@@ -592,37 +592,10 @@ impl WindowManager {
}
SocketMessage::FocusLastWorkspace => self.focus_last_workspace()?,
SocketMessage::FocusWorkspaceNumber(workspace_idx) => {
if self.focused_workspace_idx().unwrap_or_default() != workspace_idx {
self.focus_workspace(workspace_idx)?;
}
komorebi_core::focus_workspace_number(self, workspace_idx)?
}
SocketMessage::FocusWorkspaceNumbers(workspace_idx) => {
// TODO: figure out if we need to do this on macOS
// // 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
// if let Some(monitor_idx) = self.monitor_idx_from_current_pos() {
// if monitor_idx != self.focused_monitor_idx() {
// if let Some(monitor) = self.monitors().get(monitor_idx) {
// if let Some(workspace) = monitor.focused_workspace() {
// if workspace.is_empty() {
// self.focus_monitor(monitor_idx)?;
// }
// }
// }
// }
// }
let focused_monitor_idx = self.focused_monitor_idx();
for (i, monitor) in self.monitors_mut().iter_mut().enumerate() {
if i != focused_monitor_idx {
monitor.focus_workspace(workspace_idx)?;
monitor.load_focused_workspace(false)?;
}
}
self.focus_workspace(workspace_idx)?;
komorebi_core::focus_workspace_numbers(self, workspace_idx)?
}
SocketMessage::FocusMonitorWorkspaceNumber(monitor_idx, workspace_idx) => {
komorebi_core::focus_monitor_workspace_number(self, monitor_idx, workspace_idx)?
@@ -1759,6 +1732,16 @@ impl komorebi_core::CommandHost for WindowManager {
WindowManager::focus_workspace(self, idx)
}
fn show_workspace_on(&mut self, monitor_idx: usize, workspace_idx: usize) -> eyre::Result<()> {
let monitor = self
.monitors_mut()
.get_mut(monitor_idx)
.ok_or_eyre("there is no monitor at this index")?;
monitor.focus_workspace(workspace_idx)?;
monitor.load_focused_workspace(false)?;
Ok(())
}
fn move_container_to_workspace(
&mut self,
idx: usize,
+12 -38
View File
@@ -927,21 +927,7 @@ impl WindowManager {
self.focus_last_workspace()?;
}
SocketMessage::FocusWorkspaceNumber(workspace_idx) => {
// 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
if let Some(monitor_idx) = self.monitor_idx_from_current_pos()
&& monitor_idx != self.focused_monitor_idx()
&& let Some(monitor) = self.monitors().get(monitor_idx)
&& let Some(workspace) = monitor.focused_workspace()
&& workspace.is_empty()
{
self.focus_monitor(monitor_idx)?;
}
if self.focused_workspace_idx().unwrap_or_default() != workspace_idx {
self.focus_workspace(workspace_idx)?;
}
komorebi_core::focus_workspace_number(self, workspace_idx)?
}
SocketMessage::FocusWorkspaceNumbers(workspace_idx) => {
self.focus_workspace_numbers(workspace_idx)?;
@@ -2145,29 +2131,7 @@ impl WindowManager {
}
pub fn focus_workspace_numbers(&mut self, workspace_idx: usize) -> 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
if let Some(monitor_idx) = self.monitor_idx_from_current_pos()
&& monitor_idx != self.focused_monitor_idx()
&& let Some(monitor) = self.monitors().get(monitor_idx)
&& let Some(workspace) = monitor.focused_workspace()
&& workspace.is_empty()
{
self.focus_monitor(monitor_idx)?;
}
let focused_monitor_idx = self.focused_monitor_idx();
for (i, monitor) in self.monitors_mut().iter_mut().enumerate() {
if i != focused_monitor_idx {
monitor.focus_workspace(workspace_idx)?;
monitor.load_focused_workspace(false)?;
}
}
self.focus_workspace(workspace_idx)?;
Ok(())
komorebi_core::focus_workspace_numbers(self, workspace_idx)
}
pub fn focus_last_workspace(&mut self) -> eyre::Result<()> {
@@ -2324,6 +2288,16 @@ impl komorebi_core::CommandHost for WindowManager {
WindowManager::focus_workspace(self, idx)
}
fn show_workspace_on(&mut self, monitor_idx: usize, workspace_idx: usize) -> eyre::Result<()> {
let monitor = self
.monitors_mut()
.get_mut(monitor_idx)
.ok_or_eyre("there is no monitor at this index")?;
monitor.focus_workspace(workspace_idx)?;
monitor.load_focused_workspace(false)?;
Ok(())
}
fn move_container_to_workspace(
&mut self,
idx: usize,