From 3128968634c0b47dc9d53201d7bb58f7f6681aa0 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 31 Oct 2025 14:28:33 -0700 Subject: [PATCH] feat(wm): add direction preselection This commit adds a new feature to preselect the direction of the next spawned window with a corresponding komorebic preselect-direction command which takes an OperationDirection. If the OperationDirection is valid from the current position, it will be stored in the Workspace state, and then read, applied, and deleted when the next manage-able window is spawned. --- docs/cli/preselect-direction.md | 16 +++++++++++ komorebi/src/core/mod.rs | 1 + komorebi/src/process_command.rs | 6 ++++ komorebi/src/state.rs | 1 + komorebi/src/window_manager.rs | 49 +++++++++++++++++++++++++++++++++ komorebi/src/workspace.rs | 13 ++++++++- komorebic/src/main.rs | 8 ++++++ mkdocs.yml | 1 + 8 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 docs/cli/preselect-direction.md diff --git a/docs/cli/preselect-direction.md b/docs/cli/preselect-direction.md new file mode 100644 index 00000000..118d56f3 --- /dev/null +++ b/docs/cli/preselect-direction.md @@ -0,0 +1,16 @@ +# preselect-direction + +``` +Preselect the specified direction for the next window to be spawned on supported layouts + +Usage: komorebic preselect-direction + +Arguments: + + [possible values: left, right, up, down] + +Options: + -h, --help + Print help + +``` \ No newline at end of file diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 432d4976..fe6220a3 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -256,6 +256,7 @@ pub enum SocketMessage { // Window / Container Commands FocusWindow(OperationDirection), MoveWindow(OperationDirection), + PreselectDirection(OperationDirection), CycleFocusWindow(CycleDirection), CycleMoveWindow(CycleDirection), StackWindow(OperationDirection), diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 19200fce..78b3a664 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -226,6 +226,12 @@ impl WindowManager { } } } + SocketMessage::PreselectDirection(direction) => { + let focused_workspace = self.focused_workspace()?; + if matches!(focused_workspace.layer, WorkspaceLayer::Tiling) { + self.preselect_container_in_direction(direction)?; + } + } SocketMessage::MoveWindow(direction) => { let focused_workspace = self.focused_workspace()?; match focused_workspace.layer { diff --git a/komorebi/src/state.rs b/komorebi/src/state.rs index f36850cc..7c30ac1c 100644 --- a/komorebi/src/state.rs +++ b/komorebi/src/state.rs @@ -140,6 +140,7 @@ impl From<&WindowManager> for State { globals: workspace.globals, wallpaper: workspace.wallpaper.clone(), workspace_config: None, + preselected_container_idx: None, }) .collect::>(); ws.focus(monitor.workspaces.focused_idx()); diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 93a46e43..7d59f8c4 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -446,6 +446,55 @@ impl WindowManager { Ok(()) } + #[tracing::instrument(skip(self))] + pub fn preselect_container_in_direction( + &mut self, + direction: OperationDirection, + ) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + let focused_idx = workspace.focused_container_idx(); + + if matches!(workspace.layout, Layout::Default(DefaultLayout::Grid)) { + tracing::warn!("preselection is not supported on the grid layout"); + return Ok(()); + } + + tracing::info!("preselecting container"); + + let new_idx = + if workspace.maximized_window.is_some() || workspace.monocle_container.is_some() { + None + } else { + workspace.new_idx_for_direction(direction) + }; + + match new_idx { + Some(new_idx) => { + let adjusted_idx = match direction { + OperationDirection::Left | OperationDirection::Up => { + if focused_idx.abs_diff(new_idx) == 1 { + new_idx + 1 + } else { + new_idx + } + } + + _ => new_idx, + }; + + workspace.preselect_container_index(adjusted_idx); + } + + None => { + tracing::debug!( + "this is not a valid preselection direction from the current position" + ) + } + } + + Ok(()) + } + #[tracing::instrument(skip(self))] pub fn focus_container_in_direction( &mut self, diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index d26bb43e..c9b22f38 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -65,6 +65,8 @@ pub struct Workspace { pub wallpaper: Option, #[serde(skip_serializing_if = "Option::is_none")] pub workspace_config: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub preselected_container_idx: Option, } #[derive(Debug, Default, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -116,6 +118,7 @@ impl Default for Workspace { floating_layer_behaviour: None, wallpaper: None, workspace_config: None, + preselected_container_idx: None, } } } @@ -217,6 +220,10 @@ 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 new_idx_for_direction(&self, direction: OperationDirection) -> Option { let len = NonZeroUsize::new(self.containers().len())?; @@ -287,7 +294,11 @@ impl Workspace { } pub fn new_container_for_window(&mut self, window: &Window) -> eyre::Result<()> { - let next_idx = if self.containers().is_empty() { + let next_idx = if let Some(idx) = self.preselected_container_idx { + let next = idx; + self.preselected_container_idx = None; + next + } else if self.containers().is_empty() { 0 } else { self.focused_container_idx() + 1 diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 264cfaf8..a39eb49e 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -96,6 +96,7 @@ macro_rules! gen_enum_subcommand_args { gen_enum_subcommand_args! { Focus: OperationDirection, Move: OperationDirection, + PreselectDirection: OperationDirection, CycleFocus: CycleDirection, CycleMove: CycleDirection, CycleMoveToWorkspace: CycleDirection, @@ -760,6 +761,9 @@ enum SubCommand { /// Move the focused window in the specified direction #[clap(arg_required_else_help = true)] Move(Move), + /// Preselect the specified direction for the next window to be spawned on supported layouts + #[clap(arg_required_else_help = true)] + PreselectDirection(PreselectDirection), /// Minimize the focused window Minimize, /// Close the focused window @@ -1224,6 +1228,7 @@ fn main() -> eyre::Result<()> { let ignore = [ "docgen", + "splash", "alt-focus-hack", "identify-border-overflow-application", "load-custom-layout", @@ -1672,6 +1677,9 @@ fn main() -> eyre::Result<()> { SubCommand::Move(arg) => { send_message(&SocketMessage::MoveWindow(arg.operation_direction))?; } + SubCommand::PreselectDirection(arg) => { + send_message(&SocketMessage::PreselectDirection(arg.operation_direction))?; + } SubCommand::CycleFocus(arg) => { send_message(&SocketMessage::CycleFocusWindow(arg.cycle_direction))?; } diff --git a/mkdocs.yml b/mkdocs.yml index c2719bf9..9e466335 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -86,6 +86,7 @@ nav: - cli/load-resize.md - cli/focus.md - cli/move.md + - cli/preselect-direction.md - cli/minimize.md - cli/close.md - cli/cycle-focus.md