mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-19 07:08:48 +01:00
feat(core): the commands that move across workspaces and monitors, with native tests
Eight arms' worth of the router as functions of komorebi_core::command: cycle_move_container_to_workspace (the move and the send arm, by follow), cycle_focus_monitor, cycle_move_container_to_monitor (move and send), focus_monitor_workspace_number and move_container_to_monitor_workspace_number (move and send). They need five more operations of the window manager, in a second trait MonitorCommandHost: CommandHost (mouse_follows_focus, update_focused_workspace, direction_from_monitor_idx, move_container_to_monitor, focused_workspace_idx), so the daemons that implement only the first trait still compile. The text is the KDE text, which Windows and macOS have byte for byte. tests/commands.rs mirrors the e2e checks of commands.py: cycle-move-to-workspace and cycle-send-to-workspace, cycle-monitor (the wrap), cycle-move-to-monitor and cycle-send-to-monitor, focus-monitor-workspace, move-to-monitor-workspace and send-to-monitor-workspace. Verified: cargo test -p komorebi-core --features schemars (native): the commands test file 14 passed, 0 failed (the rest of the crate unchanged). Not run: the daemons (nothing calls the functions yet).
This commit is contained in:
1 parent
a0770266a8
commit
6e38ddc1f8
2 files changed
+243
-1
No files matched your search
@@ -218,3 +218,118 @@ pub fn move_container_to_last_workspace<H: CommandHost>(
|
||||
.last_focused_workspace = Option::from(idx);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// What the commands that move across monitors need besides [`CommandHost`].
|
||||
pub trait MonitorCommandHost: CommandHost {
|
||||
/// Whether the pointer follows the focus.
|
||||
fn mouse_follows_focus(&self) -> bool;
|
||||
|
||||
/// Lays the focused workspace out again and, with `trigger_focus`, focuses its window.
|
||||
fn update_focused_workspace(
|
||||
&mut self,
|
||||
follow_focus: bool,
|
||||
trigger_focus: bool,
|
||||
) -> eyre::Result<()>;
|
||||
|
||||
/// The direction in which one goes from the focused monitor to the one at `idx`, if they are
|
||||
/// next to each other.
|
||||
fn direction_from_monitor_idx(&self, idx: usize) -> Option<OperationDirection>;
|
||||
|
||||
/// Moves the focused container to the monitor at `idx` (to workspace `workspace_idx` of it, or
|
||||
/// the one it shows), and follows it when `follow` is set.
|
||||
fn move_container_to_monitor(
|
||||
&mut self,
|
||||
idx: usize,
|
||||
workspace_idx: Option<usize>,
|
||||
follow: bool,
|
||||
direction: Option<OperationDirection>,
|
||||
) -> eyre::Result<()>;
|
||||
|
||||
/// The index of the workspace shown on the focused monitor.
|
||||
fn focused_workspace_idx(&self) -> eyre::Result<usize>;
|
||||
}
|
||||
|
||||
/// Moves the focused container to the next or previous workspace of the focused monitor, and follows
|
||||
/// it when `follow` is set
|
||||
pub fn cycle_move_container_to_workspace<H: CommandHost>(
|
||||
host: &mut H,
|
||||
direction: CycleDirection,
|
||||
follow: bool,
|
||||
) -> eyre::Result<()> {
|
||||
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.move_container_to_workspace(workspace_idx, follow, None)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Focuses the next or previous monitor
|
||||
pub fn cycle_focus_monitor<H: MonitorCommandHost>(
|
||||
host: &mut H,
|
||||
direction: CycleDirection,
|
||||
) -> eyre::Result<()> {
|
||||
let monitor_idx = direction.next_idx(
|
||||
host.focused_monitor_idx(),
|
||||
NonZeroUsize::new(host.monitors().len())
|
||||
.ok_or_eyre("there must be at least one monitor")?,
|
||||
);
|
||||
|
||||
host.focus_monitor(monitor_idx)?;
|
||||
let mouse_follows_focus = host.mouse_follows_focus();
|
||||
host.update_focused_workspace(mouse_follows_focus, true)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Moves the focused container to the next or previous monitor, and follows it when `follow` is set
|
||||
pub fn cycle_move_container_to_monitor<H: MonitorCommandHost>(
|
||||
host: &mut H,
|
||||
direction: CycleDirection,
|
||||
follow: bool,
|
||||
) -> eyre::Result<()> {
|
||||
let monitor_idx = direction.next_idx(
|
||||
host.focused_monitor_idx(),
|
||||
NonZeroUsize::new(host.monitors().len())
|
||||
.ok_or_eyre("there must be at least one monitor")?,
|
||||
);
|
||||
|
||||
let direction = host.direction_from_monitor_idx(monitor_idx);
|
||||
host.move_container_to_monitor(monitor_idx, None, follow, direction)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Shows workspace `workspace_idx` of monitor `monitor_idx`, unless that is what is in front
|
||||
pub fn focus_monitor_workspace_number<H: MonitorCommandHost>(
|
||||
host: &mut H,
|
||||
monitor_idx: usize,
|
||||
workspace_idx: usize,
|
||||
) -> eyre::Result<()> {
|
||||
let focused_monitor_idx = host.focused_monitor_idx();
|
||||
let focused_workspace_idx = host.focused_workspace_idx().unwrap_or_default();
|
||||
let focused_pair = (focused_monitor_idx, focused_workspace_idx);
|
||||
|
||||
if focused_pair != (monitor_idx, workspace_idx) {
|
||||
host.focus_monitor(monitor_idx)?;
|
||||
host.focus_workspace(workspace_idx)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Moves the focused container to workspace `workspace_idx` of monitor `monitor_idx`, and follows it
|
||||
/// when `follow` is set
|
||||
pub fn move_container_to_monitor_workspace_number<H: MonitorCommandHost>(
|
||||
host: &mut H,
|
||||
monitor_idx: usize,
|
||||
workspace_idx: usize,
|
||||
follow: bool,
|
||||
) -> eyre::Result<()> {
|
||||
let direction = host.direction_from_monitor_idx(monitor_idx);
|
||||
host.move_container_to_monitor(monitor_idx, Option::from(workspace_idx), follow, direction)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -274,7 +274,10 @@ fn only_windows_lets_the_pointer_pick_the_monitor_for_the_empty_and_close_comman
|
||||
other.pointer_on = Some(1);
|
||||
komorebi_core::close_workspace(&mut other).unwrap();
|
||||
assert!(
|
||||
!other.calls.iter().any(|call| call.starts_with("focus_monitor")),
|
||||
!other
|
||||
.calls
|
||||
.iter()
|
||||
.any(|call| call.starts_with("focus_monitor")),
|
||||
"KDE and macOS do not look at the pointer here"
|
||||
);
|
||||
|
||||
@@ -287,3 +290,127 @@ fn only_windows_lets_the_pointer_pick_the_monitor_for_the_empty_and_close_comman
|
||||
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
|
||||
}
|
||||
|
||||
fn update_focused_workspace(
|
||||
&mut self,
|
||||
follow_focus: bool,
|
||||
trigger_focus: bool,
|
||||
) -> eyre::Result<()> {
|
||||
self.calls.push(format!(
|
||||
"update_focused_workspace {follow_focus} {trigger_focus}"
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn direction_from_monitor_idx(&self, idx: usize) -> Option<OperationDirection> {
|
||||
(idx != self.focused).then_some(OperationDirection::Right)
|
||||
}
|
||||
|
||||
fn move_container_to_monitor(
|
||||
&mut self,
|
||||
idx: usize,
|
||||
workspace_idx: Option<usize>,
|
||||
follow: bool,
|
||||
direction: Option<OperationDirection>,
|
||||
) -> eyre::Result<()> {
|
||||
self.calls.push(format!(
|
||||
"move_to_monitor {idx} {workspace_idx:?} {follow} {}",
|
||||
direction.is_some()
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn focused_workspace_idx(&self) -> eyre::Result<usize> {
|
||||
Ok(self.shown())
|
||||
}
|
||||
}
|
||||
|
||||
fn two_monitors() -> Host {
|
||||
let mut host = Host::new(3);
|
||||
host.monitors.push_back(monitor(3));
|
||||
host
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cycle_move_to_workspace_moves_and_send_to_workspace_does_not_follow() {
|
||||
// commands.py: "cycle-move-to-workspace previous" and "cycle-send-to-workspace next"
|
||||
let mut host = Host::new(3);
|
||||
|
||||
komorebi_core::cycle_move_container_to_workspace(&mut host, CycleDirection::Previous, true)
|
||||
.unwrap();
|
||||
komorebi_core::cycle_move_container_to_workspace(&mut host, CycleDirection::Next, false)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
host.calls,
|
||||
["move_container 2 true", "move_container 1 false"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cycle_monitor_focuses_the_next_monitor_and_lays_it_out() {
|
||||
// commands.py: "cycle-monitor next"
|
||||
let mut host = two_monitors();
|
||||
|
||||
komorebi_core::cycle_focus_monitor(&mut host, CycleDirection::Next).unwrap();
|
||||
|
||||
assert_eq!(host.focused, 1);
|
||||
assert_eq!(
|
||||
host.calls,
|
||||
["focus_monitor 1", "update_focused_workspace true true"]
|
||||
);
|
||||
|
||||
komorebi_core::cycle_focus_monitor(&mut host, CycleDirection::Next).unwrap();
|
||||
assert_eq!(host.focused, 0, "it wraps");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cycle_move_and_send_to_monitor_go_in_the_direction_of_the_target() {
|
||||
// commands.py: "cycle-move-to-monitor next" and "cycle-send-to-monitor next"
|
||||
let mut host = two_monitors();
|
||||
|
||||
komorebi_core::cycle_move_container_to_monitor(&mut host, CycleDirection::Next, true).unwrap();
|
||||
komorebi_core::cycle_move_container_to_monitor(&mut host, CycleDirection::Next, false).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
host.calls,
|
||||
[
|
||||
"move_to_monitor 1 None true true",
|
||||
"move_to_monitor 1 None false true"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn focus_monitor_workspace_does_nothing_when_that_is_already_in_front() {
|
||||
// commands.py: "focus-monitor-workspace 0 1"
|
||||
let mut host = two_monitors();
|
||||
|
||||
komorebi_core::focus_monitor_workspace_number(&mut host, 0, 0).unwrap();
|
||||
assert!(host.calls.is_empty());
|
||||
|
||||
komorebi_core::focus_monitor_workspace_number(&mut host, 1, 2).unwrap();
|
||||
assert_eq!(host.calls, ["focus_monitor 1", "focus_workspace 2"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_and_send_to_monitor_workspace_name_the_workspace() {
|
||||
// commands.py: "move-to-monitor-workspace 0 1" and "send-to-monitor-workspace 0 1"
|
||||
let mut host = two_monitors();
|
||||
|
||||
komorebi_core::move_container_to_monitor_workspace_number(&mut host, 1, 2, true).unwrap();
|
||||
komorebi_core::move_container_to_monitor_workspace_number(&mut host, 1, 2, false).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
host.calls,
|
||||
[
|
||||
"move_to_monitor 1 Some(2) true true",
|
||||
"move_to_monitor 1 Some(2) false true"
|
||||
]
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user