From 34db2e2c90c79b9d47f917b4e89b05a84dc064c6 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sun, 2 Nov 2025 10:24:17 -0800 Subject: [PATCH] feat(wm): add visual feedback for preselections This commit adds visual feedback in the form of a ghost tile for preselections made by the preselect-direction command. A container with the id "PRESELECT" will be added to the workspace, and replaced when the next manage-able window is spawned. A new command, cancel-preselect has been added to remove both the preselection index and the ghost tile if the user changes their mind. --- docs/cli/cancel-preselect.md | 12 ++++++++++++ komorebi/src/container.rs | 12 ++++++++++++ komorebi/src/core/mod.rs | 1 + komorebi/src/process_command.rs | 18 +++++++++++++++++- komorebi/src/window_manager.rs | 2 +- komorebi/src/workspace.rs | 16 +++++++++++++--- komorebic/src/main.rs | 5 +++++ mkdocs.yml | 1 + 8 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 docs/cli/cancel-preselect.md diff --git a/docs/cli/cancel-preselect.md b/docs/cli/cancel-preselect.md new file mode 100644 index 00000000..6e5a880c --- /dev/null +++ b/docs/cli/cancel-preselect.md @@ -0,0 +1,12 @@ +# cancel-preselect + +``` +Cancel a workspace preselect set by the preselect-direction command, if one exists + +Usage: komorebic cancel-preselect + +Options: + -h, --help + Print help + +``` \ No newline at end of file diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index f0d3a945..1cf9c48a 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -40,6 +40,18 @@ impl Lockable for Container { } impl Container { + pub fn preselect() -> Self { + Self { + id: "PRESELECT".to_string(), + locked: false, + windows: Default::default(), + } + } + + pub fn is_preselect(&self) -> bool { + self.id == "PRESELECT" + } + pub fn idx_from_exe(&self, exe: &str) -> Option { for (idx, window) in self.windows().iter().enumerate() { if let Some(window_exe) = window.exe() diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index fe6220a3..d40724fe 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -257,6 +257,7 @@ pub enum SocketMessage { FocusWindow(OperationDirection), MoveWindow(OperationDirection), PreselectDirection(OperationDirection), + CancelPreselect, CycleFocusWindow(CycleDirection), CycleMoveWindow(CycleDirection), StackWindow(OperationDirection), diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 78b3a664..3fa2842d 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -228,9 +228,25 @@ impl WindowManager { } SocketMessage::PreselectDirection(direction) => { let focused_workspace = self.focused_workspace()?; - if matches!(focused_workspace.layer, WorkspaceLayer::Tiling) { + let mut update = false; + + if focused_workspace.preselected_container_idx.is_some() { + tracing::warn!( + "ignoring command as this workspace already has a direction preselect set" + ); + } else if matches!(focused_workspace.layer, WorkspaceLayer::Tiling) { self.preselect_container_in_direction(direction)?; + update = true; } + + if update { + self.focused_workspace_mut()?.update()?; + } + } + SocketMessage::CancelPreselect => { + let focused_workspace = self.focused_workspace_mut()?; + focused_workspace.cancel_preselect(); + focused_workspace.update()?; } SocketMessage::MoveWindow(direction) => { let focused_workspace = self.focused_workspace()?; diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 7d59f8c4..6ce82a8c 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -482,7 +482,7 @@ impl WindowManager { _ => new_idx, }; - workspace.preselect_container_index(adjusted_idx); + workspace.preselect_container_idx(adjusted_idx); } None => { diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 79449a82..e7794bfe 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -220,8 +220,16 @@ impl Workspace { self.focus_container(j); } - pub fn preselect_container_index(&mut self, insertion_index: usize) { - self.preselected_container_idx = Some(insertion_index); + pub fn preselect_container_idx(&mut self, insertion_idx: usize) { + self.preselected_container_idx = Some(insertion_idx); + self.insert_container_at_idx(insertion_idx, Container::preselect()); + } + + pub fn cancel_preselect(&mut self) { + if let Some(idx) = self.preselected_container_idx { + self.containers_mut().remove_respecting_locks(idx); + self.preselected_container_idx = None; + } } pub fn new_idx_for_direction(&self, direction: OperationDirection) -> Option { @@ -297,6 +305,7 @@ impl Workspace { let next_idx = if let Some(idx) = self.preselected_container_idx { let next = idx; self.preselected_container_idx = None; + self.remove_container_by_idx(next); next } else if self.containers().is_empty() { 0 @@ -1055,7 +1064,8 @@ impl Workspace { // make sure we are never holding on to empty containers self.prune_duplicate_windows()?; let focused_container_idx = self.focused_container_idx(); - self.containers_mut().retain(|c| !c.windows().is_empty()); + self.containers_mut() + .retain(|c| c.is_preselect() || !c.windows().is_empty()); // make sure we are never focused on a no longer existent final empty container if focused_container_idx >= self.containers().len() { diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index a39eb49e..dd7ed95c 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -764,6 +764,8 @@ enum SubCommand { /// Preselect the specified direction for the next window to be spawned on supported layouts #[clap(arg_required_else_help = true)] PreselectDirection(PreselectDirection), + /// Cancel a workspace preselect set by the preselect-direction command, if one exists + CancelPreselect, /// Minimize the focused window Minimize, /// Close the focused window @@ -1680,6 +1682,9 @@ fn main() -> eyre::Result<()> { SubCommand::PreselectDirection(arg) => { send_message(&SocketMessage::PreselectDirection(arg.operation_direction))?; } + SubCommand::CancelPreselect => { + send_message(&SocketMessage::CancelPreselect)?; + } SubCommand::CycleFocus(arg) => { send_message(&SocketMessage::CycleFocusWindow(arg.cycle_direction))?; } diff --git a/mkdocs.yml b/mkdocs.yml index 9e466335..0b47fb4b 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -87,6 +87,7 @@ nav: - cli/focus.md - cli/move.md - cli/preselect-direction.md + - cli/cancel-preselect.md - cli/minimize.md - cli/close.md - cli/cycle-focus.md