diff --git a/komorebi/src/border_manager/border.rs b/komorebi/src/border_manager/border.rs index 2a61ae50..e3f8114e 100644 --- a/komorebi/src/border_manager/border.rs +++ b/komorebi/src/border_manager/border.rs @@ -27,12 +27,33 @@ use objc2_core_foundation::CGFloat; use objc2_core_graphics::CGMainDisplayID; use objc2_foundation::NSPoint; use objc2_foundation::NSRect; +use objc2_foundation::NSSize; use objc2_quartz_core::CATransaction; use std::ffi::c_void; use std::ptr::NonNull; use std::sync::atomic::Ordering; use tracing::instrument; +/// The frame of the border drawn around a window with the given frame +/// +/// The frame is in the coordinates AX uses, with the origin at the top left of the main display, +/// and the result is in the coordinates AppKit uses, with the origin at the bottom left. +fn border_frame(frame: Rect) -> NSRect { + let display = Rect::from(CoreGraphicsApi::display_bounds(CGMainDisplayID())); + let offset = BORDER_OFFSET.load(Ordering::Relaxed) as f64 + BORDER_OFFSET_ADJUSTMENT as f64; + + NSRect::new( + NSPoint::new( + frame.left as CGFloat - offset, + display.bottom as CGFloat - frame.top as CGFloat - frame.bottom as CGFloat - offset, + ), + NSSize::new( + frame.right as CGFloat + offset * 2.0, + frame.bottom as CGFloat + offset * 2.0, + ), + ) +} + #[instrument(skip_all)] unsafe extern "C-unwind" fn border_observer_callback( _observer: NonNull, @@ -48,28 +69,12 @@ unsafe extern "C-unwind" fn border_observer_callback( } if let Ok(rect) = MacosApi::window_rect(&border.tracking_element) { - let frame = Rect::from(CoreGraphicsApi::display_bounds(CGMainDisplayID())); - let mut ns_rect = NSRect::new( - NSPoint::new( - rect.origin.x, - frame.bottom as CGFloat - rect.origin.y - rect.size.height, - ), - rect.size, - ); - - let offset = - BORDER_OFFSET.load(Ordering::Relaxed) as f64 + BORDER_OFFSET_ADJUSTMENT as f64; - - ns_rect.origin.x -= offset; - ns_rect.origin.y -= offset; - ns_rect.size.width += offset * 2.0; - ns_rect.size.height += offset * 2.0; - border.update(); - border - .ns_window - .window - .setFrame_display_animate(ns_rect, false, false); + border.ns_window.window.setFrame_display_animate( + border_frame(Rect::from(rect)), + false, + false, + ); } } } @@ -182,6 +187,19 @@ impl Border { self.tracking_element = new_element; } + /// Move the border to surround a window that has just been moved + /// + /// This has to be called on the main thread. + pub fn follow(&self, frame: Rect) { + if !self.ns_window.window.isVisible() { + return; + } + + self.ns_window + .window + .setFrame_display_animate(border_frame(frame), false, false); + } + pub fn update(&self) { autoreleasepool(|_| { let colour = Rgb::from(window_kind_colour(self.window_kind)); diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index 96ea157a..5408d8f1 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -5,6 +5,7 @@ use crate::AccessibilityUiElement; use crate::CoreFoundationRunLoop; use crate::accessibility::AccessibilityApi; use crate::border_manager::border::Border; +use crate::core::Rect; use crate::core::WindowKind; use crate::macos_api::MacosApi; use crate::ring::Ring; @@ -19,6 +20,7 @@ use dispatch2::DispatchQueue; use komorebi_themes::colour::Colour; use komorebi_themes::colour::Rgb; use lazy_static::lazy_static; +use objc2::rc::autoreleasepool; use parking_lot::Mutex; use std::collections::HashMap; use std::collections::hash_map::Entry; @@ -59,8 +61,18 @@ lazy_static! { lazy_static! { static ref BORDER_STATE: Mutex>> = Mutex::new(HashMap::new()); static ref WINDOWS_BORDERS: Mutex> = Mutex::new(HashMap::new()); + // the latest frame of each window the animator has moved that has not been applied to a border + static ref PENDING_FOLLOWS: Mutex> = Mutex::new(HashMap::new()); } +static FOLLOW_SCHEDULED: AtomicBool = AtomicBool::new(false); + +/// How many times in a row applying the frames has had to be put off because the borders were busy +static FOLLOW_DEFERRALS: AtomicU32 = AtomicU32::new(0); + +/// How many times applying the frames is put off before they are given up on +const MAX_FOLLOW_DEFERRALS: u32 = 50; + pub enum Notification { // NOTE: bool should only ever be set to true by the reaper // TODO: eventually the bool should be a source enum so we can match on emitters @@ -109,6 +121,66 @@ pub fn send_notification( } } +/// Move the border around a window to follow the window while the animator moves it +/// +/// This is called from the animator's threads once for every frame it writes. The frames are handed +/// to the main thread in batches, where only the latest frame of each window is applied. +pub fn follow(window_id: u32, frame: Rect) { + if !BORDER_ENABLED.load_consume() { + return; + } + + PENDING_FOLLOWS.lock().insert(window_id, frame); + + if !FOLLOW_SCHEDULED.swap(true, Ordering::AcqRel) { + DispatchQueue::main().exec_async(apply_follows); + } +} + +fn apply_follows() { + FOLLOW_SCHEDULED.store(false, Ordering::Release); + + let frames = std::mem::take(&mut *PENDING_FOLLOWS.lock()); + + // the border thread holds these while it creates borders, which it does by waiting for the main + // thread, which is where this runs: waiting for them here would deadlock. Instead the frames + // are put back, behind any newer ones, and this is tried again on the next turn of the main + // thread, by which time the border thread has been let go + let (Some(borders), Some(windows_borders)) = + (BORDER_STATE.try_lock(), WINDOWS_BORDERS.try_lock()) + else { + if FOLLOW_DEFERRALS.fetch_add(1, Ordering::Relaxed) < MAX_FOLLOW_DEFERRALS { + let mut pending = PENDING_FOLLOWS.lock(); + + for (window_id, frame) in frames { + pending.entry(window_id).or_insert(frame); + } + + drop(pending); + + if !FOLLOW_SCHEDULED.swap(true, Ordering::AcqRel) { + DispatchQueue::main().exec_async(apply_follows); + } + } else { + FOLLOW_DEFERRALS.store(0, Ordering::Relaxed); + } + + return; + }; + + FOLLOW_DEFERRALS.store(0, Ordering::Relaxed); + + autoreleasepool(|_| { + for (window_id, frame) in frames { + if let Some(border_id) = windows_borders.get(&window_id) + && let Some(border) = borders.get(border_id) + { + border.follow(frame); + } + } + }); +} + pub fn destroy_all_borders() -> eyre::Result<()> { let mut borders = BORDER_STATE.lock(); tracing::info!(