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 {