From 2d06e68b48d14b1e7ca5493f9e6db4d48e037d29 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Oct 2026 18:46:09 -0700 Subject: [PATCH] fix(monitor): show a workspace's windows all at once when switching Switching workspace hides the windows of the others and restores the windows of the one that is wanted, one window at a time, each through the accessibility API and each as slow as the app it belongs to. The handler took 260 to 430 ms in a recording, and frames taken in the middle of it show both workspaces on screen together: with Zed and Zen gone, two WezTerm windows from the old workspace still up and Beeper and Discord from the new one not there yet. Workspace::update already batches its window moves with SLSDisableUpdate and SLSReenableUpdate so that they appear together. Hiding and restoring did not. BatchedUpdates holds the screen back until it is dropped, which an early return does too, so an error cannot leave it frozen, and load_focused_workspace keeps one for the hiding and the restoring. The wallpaper, which can generate a theme, is put up after it is dropped, which is why Workspace::restore is split into the windows and the wallpaper. restore does what it did. The screen does not change at all for as long as it is held, which is now the length of the switch. How the private SkyLight calls behave when it is held for that long, and whether it is long enough to notice, was not tried on a screen. --- komorebi/src/monitor.rs | 12 ++++- komorebi/src/skylight.rs | 100 ++++++++++++++++++++++++++++++++++++++ komorebi/src/workspace.rs | 11 ++++- 3 files changed, 120 insertions(+), 3 deletions(-) diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index 338aefd8..4d5f078f 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -9,6 +9,7 @@ use crate::core::Rect; use crate::core::WindowHidingPosition; use crate::macos_api::MacosApi; use crate::ring::Ring; +use crate::skylight::BatchedUpdates; use crate::static_config::Wallpaper; use crate::workspace::Workspace; use crate::workspace::WorkspaceGlobals; @@ -228,6 +229,11 @@ impl Monitor { let mut restore = None; + // putting one workspace away and another up is a window at a time, each as slow as the app + // it belongs to, which is seen as the windows of both on screen together unless it all + // appears at once + let batched = BatchedUpdates::begin(); + for (i, workspace) in self.workspaces_mut().iter_mut().enumerate() { if i == focused_idx { restore = Some(workspace); @@ -237,7 +243,11 @@ impl Monitor { } if let Some(workspace) = restore { - workspace.restore(mouse_follows_focus, monitor_id, &monitor_wp)?; + workspace.restore_windows(mouse_follows_focus)?; + + // the screen has what it needs to show, and a wallpaper can take a while + drop(batched); + workspace.apply_wallpaper(monitor_id, &monitor_wp)?; } Ok(()) diff --git a/komorebi/src/skylight.rs b/komorebi/src/skylight.rs index 72c6a374..0e09142f 100644 --- a/komorebi/src/skylight.rs +++ b/komorebi/src/skylight.rs @@ -13,3 +13,103 @@ unsafe extern "C" { // SLSReenableUpdate resumes compositing - all batched changes appear at once pub fn SLSReenableUpdate(cid: i32) -> i32; } + +#[cfg(test)] +thread_local! { + /// How many [`BatchedUpdates`] there are on this thread, since there is no window server to + /// ask under test + static OPEN: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Everything that changes on screen is held back until this is dropped, and then appears at once. +/// +/// Moving a lot of windows one after another, which is what putting one workspace away and another +/// up is, is otherwise seen as it happens: the windows of both workspaces at the same time, with +/// the ones that take the longest to answer the last to go. Dropping it, which is also what an +/// early return does, is what lets the screen update again. +/// +/// Keep it for what has to appear together and for no longer: nothing on screen changes while it +/// is held. +#[must_use = "updates are only held back for as long as this is kept"] +pub struct BatchedUpdates { + connection_id: i32, +} + +impl BatchedUpdates { + pub fn begin() -> Self { + // there is no window server to ask when testing + let connection_id = if cfg!(test) { + 0 + } else { + unsafe { + let connection_id = CGSMainConnectionID(); + SLSDisableUpdate(connection_id); + connection_id + } + }; + + #[cfg(test)] + OPEN.with(|open| open.set(open.get() + 1)); + + Self { connection_id } + } +} + +impl Drop for BatchedUpdates { + fn drop(&mut self) { + if !cfg!(test) { + unsafe { SLSReenableUpdate(self.connection_id) }; + } + + #[cfg(test)] + OPEN.with(|open| open.set(open.get() - 1)); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn open() -> i32 { + OPEN.with(std::cell::Cell::get) + } + + #[test] + fn updates_are_held_back_for_as_long_as_it_is_kept() { + assert_eq!(open(), 0); + + let batched = BatchedUpdates::begin(); + assert_eq!(open(), 1); + + drop(batched); + assert_eq!(open(), 0); + } + + #[test] + fn an_early_return_lets_the_screen_update_again() { + fn fails() -> Result<(), ()> { + let _batched = BatchedUpdates::begin(); + assert_eq!(open(), 1); + + Err(())?; + + Ok(()) + } + + assert!(fails().is_err()); + assert_eq!(open(), 0); + } + + #[test] + fn they_may_be_nested() { + let outer = BatchedUpdates::begin(); + { + let _inner = BatchedUpdates::begin(); + assert_eq!(open(), 2); + } + assert_eq!(open(), 1); + + drop(outer); + assert_eq!(open(), 0); + } +} diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 43228082..a5203a5e 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -921,6 +921,13 @@ impl Workspace { monitor_id: u32, monitor_wp: &Option, ) -> eyre::Result<()> { + self.restore_windows(mouse_follows_focus)?; + self.apply_wallpaper(monitor_id, monitor_wp) + } + + /// Put the windows of the workspace back where they were and focus the one that has focus, + /// which is all of [`Workspace::restore`] that is windows being moved + pub fn restore_windows(&mut self, mouse_follows_focus: bool) -> eyre::Result<()> { if let Some(container) = &mut self.monocle_container { container.restore()?; @@ -928,7 +935,7 @@ impl Workspace { window.focus(mouse_follows_focus)?; } - return self.apply_wallpaper(monitor_id, monitor_wp); + return Ok(()); } let idx = self.focused_container_idx(); @@ -974,7 +981,7 @@ impl Workspace { floating_window.focus(mouse_follows_focus)?; } - self.apply_wallpaper(monitor_id, monitor_wp) + Ok(()) } pub fn remove_focused_container(&mut self) -> Option {