feat(cli): implement eager-focus cmd

This commit implements SocketMessage::EagerFocus and the corresponding
komorebic eager-focus command. Mostly a straight migration from
komorebi.
This commit is contained in:
LGUG2Z committed 2025-09-23 21:29:03 -07:00
1 parent 7178609daf
commit 0b17721580
5 files changed
+143 -4

No files matched your search

+12
View File
@@ -39,6 +39,18 @@ impl Lockable for Container {
}
impl Container {
pub fn idx_from_exe(&self, exe: &str) -> Option<usize> {
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 {
+1 -1
View File
@@ -217,7 +217,7 @@ pub enum SocketMessage {
Promote,
PromoteFocus,
PromoteWindow(OperationDirection),
// EagerFocus(String),
EagerFocus(String),
// LockMonitorWorkspaceContainer(usize, usize, usize),
// UnlockMonitorWorkspaceContainer(usize, usize, usize),
ToggleLock,
+76
View File
@@ -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 {
+42
View File
@@ -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<WorkspaceWindowLocation> {
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) {
+12 -3
View File
@@ -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(())