diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index c916bd81..eef2f699 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -131,6 +131,9 @@ pub enum SocketMessage { ResizeWindowAxis(Axis, Sizing), Retile, RetileWithResizeDimensions, + Promote, + PromoteFocus, + PromoteWindow(OperationDirection), } impl SocketMessage { diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index b775eaf6..622e6be0 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -65,6 +65,12 @@ impl WindowManager { tracing::info!("processing command: {message}"); match message { + SocketMessage::Promote => self.promote_container_to_front()?, + SocketMessage::PromoteFocus => self.promote_focus_to_front()?, + SocketMessage::PromoteWindow(direction) => { + self.focus_container_in_direction(direction)?; + self.promote_container_to_front()? + } SocketMessage::FocusWindow(direction) => { let focused_workspace = self.focused_workspace()?; match focused_workspace.layer { diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 5346fedc..0bb2d113 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -1135,4 +1135,38 @@ impl WindowManager { Ok(()) } + + #[tracing::instrument(skip(self))] + pub fn promote_container_to_front(&mut self) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + + if matches!(workspace.layout, Layout::Default(DefaultLayout::Grid)) { + tracing::debug!("ignoring promote command for grid layout"); + return Ok(()); + } + + tracing::info!("promoting container"); + + workspace.promote_container()?; + self.update_focused_workspace(self.mouse_follows_focus, true) + } + + #[tracing::instrument(skip(self))] + pub fn promote_focus_to_front(&mut self) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + + if matches!(workspace.layout, Layout::Default(DefaultLayout::Grid)) { + tracing::info!("ignoring promote focus command for grid layout"); + return Ok(()); + } + + tracing::info!("promoting focus"); + + let target_idx = match workspace.layout { + Layout::Default(_) => 0, + }; + + workspace.focus_container(target_idx); + self.update_focused_workspace(self.mouse_follows_focus, true) + } } diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index df3f733b..0be5b6fb 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -476,6 +476,23 @@ impl Workspace { container } + pub fn promote_container(&mut self) -> eyre::Result<()> { + let resize = self.resize_dimensions.remove(0); + let container = self + .remove_focused_container() + .ok_or_else(|| eyre!("there is no container"))?; + + let primary_idx = match self.layout { + Layout::Default(_) => 0, + }; + + let insertion_idx = self.insert_container_at_idx(primary_idx, container); + self.resize_dimensions[insertion_idx] = resize; + self.focus_container(primary_idx); + + Ok(()) + } + pub fn add_container_to_back(&mut self, container: Container) { self.containers_mut().push_back(container); self.focus_last_container(); diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index dce01bc8..96de658e 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -59,6 +59,7 @@ gen_enum_subcommand_args! { ChangeLayout: DefaultLayout, Stack: OperationDirection, CycleStack: CycleDirection, + PromoteWindow: OperationDirection, } macro_rules! gen_target_subcommand_args { @@ -137,6 +138,12 @@ enum SubCommand { /// Set the layout on the focused workspace #[clap(arg_required_else_help = true)] ChangeLayout(ChangeLayout), + /// Promote the focused window to the top of the tree + Promote, + /// Promote the user focus to the top of the tree + PromoteFocus, + /// Promote the window in the specified direction + PromoteWindow(PromoteWindow), /// Focus the specified workspace on the focused monitor #[clap(arg_required_else_help = true)] FocusWorkspace(FocusWorkspace), @@ -202,6 +209,15 @@ fn main() -> eyre::Result<()> { SubCommand::Retile => { send_message(&SocketMessage::Retile)?; } + SubCommand::Promote => { + send_message(&SocketMessage::Promote)?; + } + SubCommand::PromoteFocus => { + send_message(&SocketMessage::PromoteFocus)?; + } + SubCommand::PromoteWindow(arg) => { + send_message(&SocketMessage::PromoteWindow(arg.operation_direction))?; + } } Ok(())