From 0b177215807653fd066c3f81f5fccf9674426320 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 23 Sep 2025 21:29:01 -0700 Subject: [PATCH] feat(cli): implement eager-focus cmd This commit implements SocketMessage::EagerFocus and the corresponding komorebic eager-focus command. Mostly a straight migration from komorebi. --- komorebi/src/container.rs | 12 ++++++ komorebi/src/core/mod.rs | 2 +- komorebi/src/process_command.rs | 76 +++++++++++++++++++++++++++++++++ komorebi/src/workspace.rs | 42 ++++++++++++++++++ komorebic/src/main.rs | 15 +++++-- 5 files changed, 143 insertions(+), 4 deletions(-) diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 773aa6ae..d25f6696 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -39,6 +39,18 @@ impl Lockable for Container { } impl Container { + pub fn idx_from_exe(&self, exe: &str) -> Option { + for (idx, window) in self.windows().iter().enumerate() { + if let Some(window_exe) = window.exe() + && exe == window_exe + { + return Option::from(idx); + } + } + + None + } + pub fn contains_window(&self, window_id: u32) -> bool { for window in self.windows() { if window.id == window_id { diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index fc268b3a..d8eb163c 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -217,7 +217,7 @@ pub enum SocketMessage { Promote, PromoteFocus, PromoteWindow(OperationDirection), - // EagerFocus(String), + EagerFocus(String), // LockMonitorWorkspaceContainer(usize, usize, usize), // UnlockMonitorWorkspaceContainer(usize, usize, usize), ToggleLock, diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 394fc79a..90e8feaa 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -26,6 +26,7 @@ use crate::window::AdhocWindow; use crate::window_manager::WindowManager; use crate::workspace::Workspace; use crate::workspace::WorkspaceLayer; +use crate::workspace::WorkspaceWindowLocation; use color_eyre::eyre; use color_eyre::eyre::OptionExt; use parking_lot::Mutex; @@ -96,6 +97,81 @@ impl WindowManager { self.focus_container_in_direction(direction)?; self.promote_container_to_front()? } + SocketMessage::EagerFocus(ref exe) => { + let focused_monitor_idx = self.focused_monitor_idx(); + let mouse_follows_focus = self.mouse_follows_focus; + + let mut window_location = None; + let mut monitor_to_focus = None; + let mut needs_workspace_loading = false; + + 'search: for (monitor_idx, monitor) in self.monitors_mut().iter_mut().enumerate() { + for (workspace_idx, workspace) in monitor.workspaces().iter().enumerate() { + if let Some(location) = workspace.location_from_exe(exe) { + window_location = Some(location); + + if monitor_idx != focused_monitor_idx { + monitor_to_focus = Some(monitor_idx); + } + + // Focus workspace if it is not already the focused one, without + // loading it so that we don't give focus to the wrong window, we will + // load it later after focusing the wanted window + let focused_ws_idx = monitor.focused_workspace_idx(); + if focused_ws_idx != workspace_idx { + monitor.last_focused_workspace = Option::from(focused_ws_idx); + monitor.focus_workspace(workspace_idx)?; + needs_workspace_loading = true; + } + + break 'search; + } + } + } + + if let Some(monitor_idx) = monitor_to_focus { + self.focus_monitor(monitor_idx)?; + } + + if let Some(location) = window_location { + match location { + WorkspaceWindowLocation::Monocle(window_idx) => { + self.focus_container_window(window_idx)?; + } + WorkspaceWindowLocation::Maximized => { + if let Some(window) = + &mut self.focused_workspace_mut()?.maximized_window + { + window.focus(mouse_follows_focus)?; + } + } + WorkspaceWindowLocation::Container(container_idx, window_idx) => { + let focused_container_idx = self.focused_container_idx()?; + if container_idx != focused_container_idx { + self.focused_workspace_mut()?.focus_container(container_idx); + } + + self.focus_container_window(window_idx)?; + } + WorkspaceWindowLocation::Floating(window_idx) => { + if let Some(window) = self + .focused_workspace_mut()? + .floating_windows_mut() + .get_mut(window_idx) + { + window.focus(mouse_follows_focus)?; + } + } + } + + if needs_workspace_loading { + let mouse_follows_focus = self.mouse_follows_focus; + if let Some(monitor) = self.focused_monitor_mut() { + monitor.load_focused_workspace(mouse_follows_focus)?; + } + } + } + } SocketMessage::FocusWindow(direction) => { let focused_workspace = self.focused_workspace()?; match focused_workspace.layer { diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 5c8f647c..898bb430 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -107,6 +107,14 @@ impl Default for Workspace { } } +#[derive(Debug)] +pub enum WorkspaceWindowLocation { + Monocle(usize), // window_idx + Maximized, + Container(usize, usize), // container_idx, window_idx + Floating(usize), // idx in floating_windows +} + #[derive(Debug, Default, Copy, Clone, Serialize, Deserialize, PartialEq)] /// Settings setup either by the parent monitor or by the `WindowManager` pub struct WorkspaceGlobals { @@ -514,6 +522,40 @@ impl Workspace { false } + pub fn location_from_exe(&self, exe: &str) -> Option { + for (container_idx, container) in self.containers().iter().enumerate() { + if let Some(window_idx) = container.idx_from_exe(exe) { + return Some(WorkspaceWindowLocation::Container( + container_idx, + window_idx, + )); + } + } + + if let Some(window) = &self.maximized_window + && let Some(window_exe) = window.exe() + && exe == window_exe + { + return Some(WorkspaceWindowLocation::Maximized); + } + + if let Some(container) = &self.monocle_container + && let Some(window_idx) = container.idx_from_exe(exe) + { + return Some(WorkspaceWindowLocation::Monocle(window_idx)); + } + + for (window_idx, window) in self.floating_windows().iter().enumerate() { + if let Some(window_exe) = window.exe() + && exe == window_exe + { + return Some(WorkspaceWindowLocation::Floating(window_idx)); + } + } + + None + } + pub fn contains_managed_window(&self, window_id: u32) -> bool { for container in self.containers() { if container.contains_window(window_id) { diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 12b00e92..c7e44df3 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -279,6 +279,12 @@ struct Start { // clean_state: bool, } +#[derive(Parser)] +struct EagerFocus { + /// Case-sensitive exe identifier + exe: String, +} + #[derive(Parser)] #[clap(author, about, version = build::CLAP_LONG_VERSION)] struct Opts { @@ -379,9 +385,9 @@ enum SubCommand { /// Move the focused window in the specified cycle direction #[clap(arg_required_else_help = true)] CycleMove(CycleMove), - // /// Focus the first managed window matching the given exe - // #[clap(arg_required_else_help = true)] - // EagerFocus(EagerFocus), + /// Focus the first managed window matching the given exe + #[clap(arg_required_else_help = true)] + EagerFocus(EagerFocus), /// Stack the focused window in the specified direction #[clap(arg_required_else_help = true)] Stack(Stack), @@ -1239,6 +1245,9 @@ fn main() -> eyre::Result<()> { SubCommand::EnforceWorkspaceRules => { send_message(&SocketMessage::EnforceWorkspaceRules)?; } + SubCommand::EagerFocus(arg) => { + send_message(&SocketMessage::EagerFocus(arg.exe))?; + } } Ok(())