From c45090c5c55aa70aeb3a0f20cefd3675173b46a1 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sun, 21 Sep 2025 10:59:00 -0700 Subject: [PATCH] feat(wm): handle monitor resizes on the main thread Got some weird behaviour when calculating the work area offset when resuming from sleep so I bit the bullet and added an AtomicBool flag that gets set when we need to call into NSScreen to get the correct work area and size after a display resizes. The flag gets checked in the same loop on the main thread which is checking for ctrl-c signals. --- komorebi/src/lib.rs | 2 + komorebi/src/macos_api.rs | 42 ++++++++++++++++++++ komorebi/src/main.rs | 11 ++++++ komorebi/src/monitor_reconciliator.rs | 57 +++------------------------ 4 files changed, 60 insertions(+), 52 deletions(-) diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 1fcd7823..bdc42f49 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -22,6 +22,7 @@ use std::ops::Deref; use std::path::PathBuf; use std::ptr::NonNull; use std::sync::Arc; +use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicI32; #[macro_use] @@ -69,6 +70,7 @@ lazy_static! { )); static ref WINDOW_RESTORE_POSITIONS: Arc>> = Arc::new(Mutex::new(HashMap::new())); + pub static ref UPDATE_MONITOR_WORK_AREAS: Arc = Arc::new(AtomicBool::new(false)); } pub static DEFAULT_WORKSPACE_PADDING: AtomicI32 = AtomicI32::new(5); diff --git a/komorebi/src/macos_api.rs b/komorebi/src/macos_api.rs index 51f584ea..d33e86fa 100644 --- a/komorebi/src/macos_api.rs +++ b/komorebi/src/macos_api.rs @@ -14,6 +14,7 @@ use crate::core_graphics::CoreGraphicsApi; use crate::monitor::Monitor; use crate::window::WindowInfo; use crate::window_manager::WindowManager; +use color_eyre::eyre; use objc2::MainThreadMarker; use objc2_app_kit::NSEvent; use objc2_app_kit::NSScreen; @@ -25,10 +26,12 @@ use objc2_core_foundation::CFRetained; use objc2_core_foundation::CFString; use objc2_core_foundation::CGPoint; use objc2_core_foundation::CGRect; +use parking_lot::Mutex; use std::collections::HashMap; use std::collections::hash_map::Entry; use std::ffi::c_void; use std::ptr::NonNull; +use std::sync::Arc; pub struct MacosApi; @@ -59,6 +62,45 @@ impl MacosApi { Ok(()) } + pub fn update_monitor_work_areas(wm: Arc>) -> eyre::Result<()> { + let screens = NSScreen::screens(MainThreadMarker::new().unwrap()); + + for display_id in CoreGraphicsApi::connected_display_ids()? { + let display_bounds = CoreGraphicsApi::display_bounds(display_id); + + let mut wm = wm.lock(); + + for screen in &screens { + let menu_bar_height = screen.frame().size.height + - screen.visibleFrame().size.height + - screen.visibleFrame().origin.y; + + if screen.frame() == display_bounds { + let size = Rect::from(display_bounds); + let mut work_area_size = Rect::from(display_bounds); + work_area_size.top += menu_bar_height as i32; + work_area_size.bottom = screen.visibleFrame().size.height as i32; + + for monitor in wm.monitors_mut() { + if monitor.id == display_id { + monitor.size = size; + monitor.work_area_size = work_area_size; + } + + tracing::info!( + "updated monitor size and work area for monitor {display_id}" + ) + } + } + } + + let focus_follows_mouse = wm.mouse_follows_focus; + wm.update_focused_workspace(focus_follows_mouse, true)?; + } + + Ok(()) + } + pub fn load_workspace_information(wm: &mut WindowManager) -> Result<(), LibraryError> { let mut monitor_size_map = HashMap::new(); let mut monitor_workspace_map = HashMap::new(); diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 323280d2..9cf0f0c9 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -3,8 +3,10 @@ use color_eyre::eyre; use color_eyre::eyre::OptionExt; use komorebi::DATA_DIR; +use komorebi::UPDATE_MONITOR_WORK_AREAS; use komorebi::ax_event_listener; use komorebi::display_reconfiguration_listener::DisplayReconfigurationListener; +use komorebi::macos_api::MacosApi; use komorebi::monitor_reconciliator; use komorebi::notification_center_listener::NotificationCenterListener; use komorebi::process_command::listen_for_commands; @@ -149,6 +151,15 @@ fn main() -> eyre::Result<()> { break; } + if UPDATE_MONITOR_WORK_AREAS.load(Ordering::Relaxed) { + // this can only be called on the main thread + if let Err(error) = MacosApi::update_monitor_work_areas(wm.clone()) { + tracing::error!("failed to update montior work areas: {error}"); + } + + UPDATE_MONITOR_WORK_AREAS.store(false, Ordering::Relaxed); + } + // this gets our observer notification callbacks firing autoreleasepool(|_| unsafe { CFRunLoop::run_in_mode(kCFRunLoopDefaultMode, 2.0, false) }); } diff --git a/komorebi/src/monitor_reconciliator.rs b/komorebi/src/monitor_reconciliator.rs index f0c2be23..d88b8114 100644 --- a/komorebi/src/monitor_reconciliator.rs +++ b/komorebi/src/monitor_reconciliator.rs @@ -1,5 +1,4 @@ -use crate::core::rect::Rect; -use crate::core_graphics::CoreGraphicsApi; +use crate::UPDATE_MONITOR_WORK_AREAS; use crate::window_manager::WindowManager; use crossbeam_channel::Receiver; use crossbeam_channel::Sender; @@ -9,6 +8,7 @@ use serde::Deserialize; use serde::Serialize; use std::sync::Arc; use std::sync::OnceLock; +use std::sync::atomic::Ordering; use strum::Display; #[derive(Debug, Copy, Clone, Serialize, Deserialize, Display)] @@ -58,66 +58,19 @@ pub fn listen_for_notifications(wm: Arc>) -> color_eyre::Re Ok(()) } -pub fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> { +pub fn handle_notifications(_wm: Arc>) -> color_eyre::Result<()> { tracing::info!("listening"); let receiver = event_rx(); for notification in receiver { - let mut wm = wm.lock(); - match notification { - MonitorNotification::Resize(display_id) => { + MonitorNotification::Resize(_display_id) => { tracing::debug!("handling resize notification"); - let new_size = CoreGraphicsApi::display_bounds(display_id); - for monitor in wm.monitors_mut() { - let mut should_update = false; - - if Rect::from(new_size) != monitor.size { - // need to do this because we can't call NSScreen from a background thread - monitor.work_area_size = calculate_scaled_work_area( - &Rect::from(new_size), - &monitor.size, - &monitor.work_area_size, - ); - monitor.size = Rect::from(new_size); - should_update = true; - } - - if should_update { - tracing::info!("updated size and work area for monitor: {}", monitor.id); - monitor.update_focused_workspace(None)?; - } - } + UPDATE_MONITOR_WORK_AREAS.store(true, Ordering::Relaxed); } } } Ok(()) } - -fn calculate_scaled_work_area( - new_size: &Rect, - monitor_size: &Rect, - monitor_work_area_size: &Rect, -) -> Rect { - let scale_x = new_size.right as f64 / monitor_size.right as f64; - let scale_y = new_size.bottom as f64 / monitor_size.bottom as f64; - - let top_offset = monitor_work_area_size.top - monitor_size.top; - let bottom_offset = monitor_size.bottom - monitor_work_area_size.bottom; - let left_offset = monitor_work_area_size.left - monitor_size.left; - let right_offset = monitor_size.right - monitor_work_area_size.right; - - let scaled_top_offset = (top_offset as f64 * scale_y).round() as i32; - let scaled_bottom_offset = (bottom_offset as f64 * scale_y).round() as i32; - let scaled_left_offset = (left_offset as f64 * scale_x).round() as i32; - let scaled_right_offset = (right_offset as f64 * scale_x).round() as i32; - - Rect { - left: new_size.left + scaled_left_offset, - top: new_size.top + scaled_top_offset, - right: new_size.right - scaled_right_offset, - bottom: new_size.bottom - scaled_bottom_offset, - } -}