From b12be4b1a7e955c4281628265e426cedfaf18c7c Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 15:43:17 -0700 Subject: [PATCH] refactor(windows): move window_handling_behaviour to the platform settings, scoped guard in the border test Plan step 10, part of S7 and the scoped guard asked for in S5. WINDOW_HANDLING_BEHAVIOUR is no longer a static: it is settings().platform.window_handling_behaviour (WindowsSettings, default Sync). The load writes it with one update when the key is present (a missing key keeps it, as before); windows_api (the position calls) and scrolling read it through settings::window_handling_behaviour(), once per call. The border paint test (border_manager/border.rs is_painted) saved the static, stored a value and stored the old one back, which overwrote any change made in between. It now holds Handle::scoped on the field, which puts the previous value back on drop only if the field still holds the value it set. BEHAVIOUR: the position calls read a snapshot instead of an atomic cell (one RwLock read and Arc clone per call), and the load publishes the key through the handle. Gate: Windows e2e (commands, config_reload, lifecycle; anything that moves windows with window_handling_behaviour async if configured). Verified: Windows clippy -D warnings --locked --all-targets on komorebi-windows (cross): clean. e2e not run; the border paint test needs a desktop and was not run. --- komorebi/src/border_manager/border.rs | 7 ++++--- komorebi/src/lib.rs | 4 ---- komorebi/src/scrolling.rs | 3 +-- komorebi/src/settings.rs | 24 +++++++++++++++++++++--- komorebi/src/static_config.rs | 7 ++++--- komorebi/src/windows_api.rs | 11 +++++------ 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/komorebi/src/border_manager/border.rs b/komorebi/src/border_manager/border.rs index 3723681f..cdb9b5fd 100644 --- a/komorebi/src/border_manager/border.rs +++ b/komorebi/src/border_manager/border.rs @@ -834,8 +834,9 @@ mod tests { .expect("a window to put a border around"); let tracked_hwnd = tracked.0 as isize; - let previous = crate::WINDOW_HANDLING_BEHAVIOUR.load(); - crate::WINDOW_HANDLING_BEHAVIOUR.store(handling); + // put back when the guard is dropped, unless something else changed it meanwhile + let handling_override = crate::settings::settings() + .scoped(|settings| &mut settings.platform.window_handling_behaviour, handling); let mut border = Border::create("komorebi-border-test", tracked_hwnd, 0).expect("a border to paint"); @@ -854,7 +855,7 @@ mod tests { let painted = unsafe { std::ptr::read_volatile(&raw const border.rounded_rect.rect.right) } > 0.0; - crate::WINDOW_HANDLING_BEHAVIOUR.store(previous); + drop(handling_override); let border = Box::into_raw(border); unsafe { diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 28cc2814..c6e2b36d 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -75,7 +75,6 @@ use crate::core::config_generation::MatchingRule; use crate::core::config_generation::MatchingStrategy; use crate::core::config_generation::WorkspaceMatchingRule; use color_eyre::eyre; -use crossbeam_utils::atomic::AtomicCell; use os_info::Version; use parking_lot::Mutex; use parking_lot::RwLock; @@ -277,9 +276,6 @@ pub static REMOVE_TITLEBARS: AtomicBool = AtomicBool::new(false); pub static SLOW_APPLICATION_COMPENSATION_TIME: AtomicU64 = AtomicU64::new(20); -pub static WINDOW_HANDLING_BEHAVIOUR: AtomicCell = - AtomicCell::new(WindowHandlingBehaviour::Sync); - shadow_rs::shadow!(build); pub const PUBLIC_KEY: [u8; 32] = [ diff --git a/komorebi/src/scrolling.rs b/komorebi/src/scrolling.rs index cf0d39e5..2352ddf7 100644 --- a/komorebi/src/scrolling.rs +++ b/komorebi/src/scrolling.rs @@ -4,7 +4,6 @@ //! `komorebi-scrolling` crate, so that every komorebi shares it. What is here is what needs a //! [`Workspace`]: a container is a tile in the strip, and is known to it by its id. -use crate::WINDOW_HANDLING_BEHAVIOUR; use crate::animation::ANIMATION_MANAGER; use crate::animation::prefix::AnimationPrefix; use crate::container::Container; @@ -37,7 +36,7 @@ const WIDTH_TOLERANCE: i32 = 16; /// moves are done by the time they are asked, and none is being animated. fn window_sizes_are_settled() -> bool { matches!( - WINDOW_HANDLING_BEHAVIOUR.load(), + crate::settings::window_handling_behaviour(), WindowHandlingBehaviour::Sync ) && ANIMATION_MANAGER.lock().count() == 0 } diff --git a/komorebi/src/settings.rs b/komorebi/src/settings.rs index 1fd67857..32a0a25a 100644 --- a/komorebi/src/settings.rs +++ b/komorebi/src/settings.rs @@ -13,6 +13,7 @@ use crate::animation::prefix::AnimationPrefix; use crate::core::animation::AnimationStyle; use komorebi_config::aspect_ratio::AspectRatio; +use komorebi_config::behaviour::WindowHandlingBehaviour; use komorebi_layouts::DefaultLayout; use komorebi_layouts::LayoutDefaultEntry; @@ -20,9 +21,21 @@ use komorebi_settings::Handle; use komorebi_settings::Shared; use komorebi_settings::shared::PlatformDefaults; -/// The part of the settings that only Windows has. Empty for now; the stackbar, hiding and handling behaviour and border style join it later. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct WindowsSettings {} +/// The part of the settings that only Windows has. The stackbar, hiding behaviour and border style +/// join it later. +#[derive(Clone, Debug, PartialEq)] +pub struct WindowsSettings { + /// `window_handling_behaviour`: whether windows are moved synchronously or asynchronously. + pub window_handling_behaviour: WindowHandlingBehaviour, +} + +impl Default for WindowsSettings { + fn default() -> Self { + Self { + window_handling_behaviour: WindowHandlingBehaviour::Sync, + } + } +} pub type Settings = komorebi_settings::Settings; @@ -144,6 +157,11 @@ pub fn animation_off_for_good() { }); } +/// `window_handling_behaviour`, read once per window move. +pub fn window_handling_behaviour() -> WindowHandlingBehaviour { + settings().snapshot().platform.window_handling_behaviour +} + #[cfg(test)] mod tests { use std::sync::atomic::Ordering; diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 89fd6dc2..1bd86d53 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -18,7 +18,6 @@ use crate::SLOW_APPLICATION_COMPENSATION_TIME; use crate::SLOW_APPLICATION_IDENTIFIERS; use crate::TRANSPARENCY_BLACKLIST; use crate::TRAY_AND_MULTI_WINDOW_IDENTIFIERS; -use crate::WINDOW_HANDLING_BEHAVIOUR; use crate::WINDOWS_11; use crate::WORKSPACE_MATCHING_RULES; use crate::animation::DEFAULT_GHOST_MOVEMENT; @@ -367,7 +366,7 @@ impl From<&WindowManager> for StaticConfig { floating_window_aspect_ratio: Option::from( crate::settings::floating_window_aspect_ratio(), ), - window_handling_behaviour: Option::from(WINDOW_HANDLING_BEHAVIOUR.load()), + window_handling_behaviour: Option::from(crate::settings::window_handling_behaviour()), ..Default::default() } } @@ -664,7 +663,9 @@ impl ApplyGlobals for StaticConfig { } if let Some(behaviour) = self.window_handling_behaviour { - WINDOW_HANDLING_BEHAVIOUR.store(behaviour); + crate::settings::settings().update(|settings| { + settings.platform.window_handling_behaviour = behaviour; + }); } Ok(()) diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 98d3bed4..7f679ce8 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -163,7 +163,6 @@ use crate::core::Rect; use crate::DUPLICATE_MONITOR_SERIAL_IDS; use crate::MONITOR_INDEX_PREFERENCES; -use crate::WINDOW_HANDLING_BEHAVIOUR; use crate::Window; use crate::WindowHandlingBehaviour; use crate::WindowManager; @@ -529,7 +528,7 @@ impl WindowsApi { // blocking our thread in case the target window is not responding. if with_async_window_pos && matches!( - WINDOW_HANDLING_BEHAVIOUR.load(), + crate::settings::window_handling_behaviour(), WindowHandlingBehaviour::Async ) { @@ -576,7 +575,7 @@ impl WindowsApi { | SetWindowPosition::SHOW_WINDOW; if matches!( - WINDOW_HANDLING_BEHAVIOUR.load(), + crate::settings::window_handling_behaviour(), WindowHandlingBehaviour::Async ) { flags |= SetWindowPosition::ASYNC_WINDOW_POS; @@ -600,7 +599,7 @@ impl WindowsApi { | SetWindowPosition::SHOW_WINDOW; if matches!( - WINDOW_HANDLING_BEHAVIOUR.load(), + crate::settings::window_handling_behaviour(), WindowHandlingBehaviour::Async ) { flags |= SetWindowPosition::ASYNC_WINDOW_POS; @@ -630,7 +629,7 @@ impl WindowsApi { } if matches!( - WINDOW_HANDLING_BEHAVIOUR.load(), + crate::settings::window_handling_behaviour(), WindowHandlingBehaviour::Async ) { flags |= SetWindowPosition::ASYNC_WINDOW_POS; @@ -679,7 +678,7 @@ impl WindowsApi { // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow // TODO: error handling if matches!( - WINDOW_HANDLING_BEHAVIOUR.load(), + crate::settings::window_handling_behaviour(), WindowHandlingBehaviour::Async ) { unsafe {