From 270e0847c2dad63b3068ebca8eb490c2280aaadf Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 30 Oct 2025 13:45:22 -0700 Subject: [PATCH] fix(wm): account for partially-shown scrolling windows This commit makes some small tweaks to account for the hiding and showing of partially visible windows on the Scrolling layout. This can happen when resize dimensions have been applied which leaves only a certain percentage of the window within the work area. If a window is only partially within the work area, the call to hide will silently fail. In order to deal with this there is some jank added when hiding workspaces and updating workspaces to see if we are leaving a Scrolling workspace with a partially-visible window, and if we are, to reposition that window to be fully on within the work area before hiding it. --- komorebi/src/core/rect.rs | 15 +++++++++++---- komorebi/src/monitor.rs | 9 ++++++++- komorebi/src/workspace.rs | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/komorebi/src/core/rect.rs b/komorebi/src/core/rect.rs index c16d3371..2c63395a 100644 --- a/komorebi/src/core/rect.rs +++ b/komorebi/src/core/rect.rs @@ -69,10 +69,17 @@ impl Rect { CoreGraphicsApi::contains_rect(other.into(), self.into()) } - pub fn contains_within_horizontal_bounds(&self, other: &Rect) -> bool { - let within_left_bound = other.left >= self.left; - let within_right_bound = other.left < self.left + self.right; - within_left_bound && within_right_bound + pub fn percentage_within_horizontal_bounds(&self, other: &Rect) -> f64 { + let overlap_left = self.left.max(other.left); + let overlap_right = (self.left + self.right).min(other.left + other.right); + + let overlap_width = overlap_right - overlap_left; + + if overlap_width <= 0 { + 0.0 + } else { + (overlap_width as f64) / (other.right as f64) * 100.0 + } } /// Decrease the size of self by the padding amount. diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index 63641121..8d3e8270 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -225,14 +225,21 @@ impl Monitor { let focused_idx = self.focused_workspace_idx(); let monitor_id = self.id; let monitor_wp = self.wallpaper.clone(); + + let mut restore = None; + for (i, workspace) in self.workspaces_mut().iter_mut().enumerate() { if i == focused_idx { - workspace.restore(mouse_follows_focus, monitor_id, &monitor_wp)?; + restore = Some(workspace); } else { workspace.hide(None)?; } } + if let Some(workspace) = restore { + workspace.restore(mouse_follows_focus, monitor_id, &monitor_wp)?; + } + Ok(()) } diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 1541afbb..d26bb43e 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -690,10 +690,25 @@ impl Workspace { } } + let mut resize_dimensions = None; + if matches!(self.layout, Layout::Default(DefaultLayout::Scrolling)) + && self.resize_dimensions.iter().any(|r| r.is_some()) + { + resize_dimensions = Some(self.resize_dimensions.clone()); + self.resize_dimensions = Vec::new(); + if let Err(error) = self.update() { + tracing::error!("{error}"); + } + } + for container in self.containers_mut() { container.hide(window_hiding_position, omit)?; } + if let Some(resize_dimensions) = resize_dimensions { + self.resize_dimensions = resize_dimensions; + } + // if let Some(window) = self.maximized_window() { // window.hide(); // } @@ -1135,6 +1150,7 @@ impl Workspace { let is_scrolling = matches!(self.layout, Layout::Default(DefaultLayout::Scrolling)); let window_hiding_position = self.globals.window_hiding_position; + let resize_dimensions_is_empty = self.resize_dimensions.is_empty(); let containers = self.containers_mut(); @@ -1144,11 +1160,25 @@ impl Workspace { layout.add_padding(border_width); for window in container.windows_mut() { - if is_scrolling && !work_area.contains_within_horizontal_bounds(layout) - { + let current_percentage = work_area.percentage_within_horizontal_bounds( + &Rect::from(MacosApi::window_rect(&window.element)?), + ); + let proposed_percentage = + work_area.percentage_within_horizontal_bounds(layout); + + let percentage_override = proposed_percentage == 0.0 + && current_percentage > 0.1 + && current_percentage < 100.0 + && resize_dimensions_is_empty; + + if is_scrolling && proposed_percentage < 0.1 && !percentage_override { if let Err(error) = window.hide(window_hiding_position) { - tracing::warn!("failed to set window position: {error}") + tracing::warn!( + "failed to set hide window for scrolling layout: {error}" + ) } + } else if percentage_override { + window.center(&work_area, false)?; } else if let Err(error) = window.set_position(layout) { tracing::warn!("failed to set window position: {error}") }