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.
This commit is contained in:
LGUG2Z committed 2025-09-21 10:59:02 -07:00
1 parent 98616c4288
commit c45090c5c5
4 files changed
+60 -52

No files matched your search

+2
View File
@@ -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<Mutex<HashMap<u32, CGRect>>> =
Arc::new(Mutex::new(HashMap::new()));
pub static ref UPDATE_MONITOR_WORK_AREAS: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
}
pub static DEFAULT_WORKSPACE_PADDING: AtomicI32 = AtomicI32::new(5);
+42
View File
@@ -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<Mutex<WindowManager>>) -> 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();
+11
View File
@@ -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) });
}
+5 -52
View File
@@ -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<Mutex<WindowManager>>) -> color_eyre::Re
Ok(())
}
pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()> {
pub fn handle_notifications(_wm: Arc<Mutex<WindowManager>>) -> 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,
}
}