diff --git a/crates/komorebi-core/src/command.rs b/crates/komorebi-core/src/command.rs index 33081d6b..90413878 100644 --- a/crates/komorebi-core/src/command.rs +++ b/crates/komorebi-core/src/command.rs @@ -218,3 +218,118 @@ pub fn move_container_to_last_workspace( .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; + + /// 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, + follow: bool, + direction: Option, + ) -> eyre::Result<()>; + + /// The index of the workspace shown on the focused monitor. + fn focused_workspace_idx(&self) -> eyre::Result; +} + +/// 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( + 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( + 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( + 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( + 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( + 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(()) +} diff --git a/crates/komorebi-core/tests/commands.rs b/crates/komorebi-core/tests/commands.rs index a9781f03..3cb37f25 100644 --- a/crates/komorebi-core/tests/commands.rs +++ b/crates/komorebi-core/tests/commands.rs @@ -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 { + (idx != self.focused).then_some(OperationDirection::Right) + } + + fn move_container_to_monitor( + &mut self, + idx: usize, + workspace_idx: Option, + follow: bool, + direction: Option, + ) -> eyre::Result<()> { + self.calls.push(format!( + "move_to_monitor {idx} {workspace_idx:?} {follow} {}", + direction.is_some() + )); + Ok(()) + } + + fn focused_workspace_idx(&self) -> eyre::Result { + 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" + ] + ); +}