diff --git a/crates/komorebi-core/src/workspace.rs b/crates/komorebi-core/src/workspace.rs index 9462957d..bc46e047 100644 --- a/crates/komorebi-core/src/workspace.rs +++ b/crates/komorebi-core/src/workspace.rs @@ -164,6 +164,14 @@ pub trait WorkspacePlatform: Sized + Clone + std::fmt::Debug + PartialEq + 'stat Ok(0) } + /// The index of the container that `promote-focus` focuses: the primary position of the layout, + /// and the first container when the layout has none (a custom layout without a primary + /// column). Where [`WorkspacePlatform::primary_container_idx`] fails, the default falls back + /// to 0. + fn promote_focus_container_idx(layout: &Layout) -> usize { + Self::primary_container_idx(layout).unwrap_or(0) + } + /// Gives the maximized window back to the layout, for the platforms where that is something /// to tell the window (KDE asks the compositor; on Windows there is nothing to do). fn unmaximize_window(_window: &Self::Window) -> eyre::Result<()> { @@ -2190,4 +2198,12 @@ mod tests { assert!(workspace.tile); assert_eq!(workspace.layout, Layout::Default(DefaultLayout::BSP)); } + + #[test] + fn promote_focus_falls_back_to_the_primary_position_and_then_to_zero() { + assert_eq!( + Plat::promote_focus_container_idx(&Layout::Default(DefaultLayout::BSP)), + 0 + ); + } } diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index e3d66a57..9551ce3a 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -48,8 +48,10 @@ use crate::core::WindowContainerBehaviour; use crate::core::WindowManagementBehaviour; use crate::core::config_generation::MatchingRule; use crate::windows_backend::backend; +use crate::workspace::WindowsPlatform; use komorebi_backend::WindowId; use komorebi_core::ContainerWindow; +use komorebi_core::WorkspacePlatform; use crate::CrossBoundaryBehaviour; use crate::DATA_DIR; @@ -3048,10 +3050,7 @@ impl WindowManager { tracing::info!("promoting focus"); - let target_idx = match &workspace.layout { - Layout::Default(_) => 0, - Layout::Custom(custom) => custom.first_container_idx(custom.primary_idx().unwrap_or(0)), - }; + let target_idx = WindowsPlatform::promote_focus_container_idx(&workspace.layout); 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 6189503c..667dc9e2 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -86,6 +86,16 @@ impl komorebi_core::WorkspacePlatform for WindowsPlatform { }) } + /// `promote-focus` on a custom layout without a primary column focuses its first column rather + /// than failing, which is what the Windows daemon always did; the other layouts have the + /// primary position first. + fn promote_focus_container_idx(layout: &Layout) -> usize { + match layout { + Layout::Default(_) => 0, + Layout::Custom(custom) => custom.first_container_idx(custom.primary_idx().unwrap_or(0)), + } + } + fn begin_update(_workspace: &mut Workspace) -> eyre::Result { if !INITIAL_CONFIGURATION_LOADED.load(Ordering::SeqCst) { return Ok(komorebi_core::Flow::Skip);