From acee420bd3c883b1e6be608bb60d81cd14fdf662 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 9 Oct 2025 12:37:27 -0700 Subject: [PATCH] feat(wm): implement ws reconciliator with source debouncing Hello infinite workspace switching loop, my old friend. Reimplemented the old workspace reconciliator from komorebi for Windows with additional source debouncing and tested by furiously mashing alt-1 and alt+2 for 20s. No loop! --- komorebi/src/lib.rs | 1 + komorebi/src/main.rs | 2 + komorebi/src/process_event.rs | 23 +++-- komorebi/src/window.rs | 2 +- komorebi/src/workspace_reconciliator.rs | 108 ++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 komorebi/src/workspace_reconciliator.rs diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index cb51198a..408315f3 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -72,6 +72,7 @@ pub mod window_manager; pub mod window_manager_event; pub mod window_manager_event_listener; pub mod workspace; +pub mod workspace_reconciliator; lazy_static! { pub static ref HOME_DIR: PathBuf = { diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 3b57add4..4f26514a 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -20,6 +20,7 @@ use komorebi::static_config::StaticConfig; use komorebi::theme_manager; use komorebi::window_manager::WindowManager; use komorebi::window_manager_event_listener; +use komorebi::workspace_reconciliator; use objc2::rc::autoreleasepool; use objc2_application_services::AXIsProcessTrusted; use objc2_core_foundation::CFRunLoop; @@ -263,6 +264,7 @@ fn main() -> eyre::Result<()> { theme_manager::listen_for_notifications(); monitor_reconciliator::listen_for_notifications(wm.clone())?; reaper::listen_for_notifications(wm.clone()); + workspace_reconciliator::listen_for_notifications(wm.clone()); listen_for_commands(wm.clone()); listen_for_events(wm.clone()); diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 25dde249..a2f9f5fa 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -34,6 +34,7 @@ use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; use crate::window_manager_event_listener; use crate::workspace::WorkspaceLayer; +use crate::workspace_reconciliator; use color_eyre::eyre; use color_eyre::eyre::OptionExt; use parking_lot::Mutex; @@ -176,6 +177,7 @@ impl WindowManager { let application_name = application.name().unwrap_or_default().clone(); let mut should_switch_workspace_layer_to_tiling = true; let mut tabbed_window = false; + let mut needs_reconciliation = false; if let Some(window_id) = application.main_window_id() && matches!( @@ -350,20 +352,14 @@ impl WindowManager { } } - // maybe we don't need a separate reconciliator module?? this works pretty well! - if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known { - self.focus_monitor(m_idx)?; - self.focus_workspace(w_idx)?; - let event = WindowManagerEvent::from_system_notification( - SystemNotification::Manual(ManualNotification::FocusOnCmdTab), - process_id, - Some(window_id), - ); - - if let Some(manual_event) = event { - window_manager_event_listener::send_notification(manual_event); + if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known + && matches!( + notification, + SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification) + ) { + workspace_reconciliator::send_notification(m_idx, w_idx, event); + needs_reconciliation = true; } - } } } @@ -373,6 +369,7 @@ impl WindowManager { AccessibilityNotification::AXMainWindowChanged ) ) && !tabbed_window + && !needs_reconciliation { self.reap_invalid_windows_for_application(process_id)?; self.update_focused_workspace(false, false)?; diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index e8feba9f..82a75040 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -41,6 +41,7 @@ use crate::hidden_frame_bottom_left; use crate::hidden_frame_bottom_right; use crate::macos_api::MacosApi; use crate::reaper; +use crate::reaper::ReaperNotification; use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; use crate::window_manager_event_listener; @@ -85,7 +86,6 @@ use std::str::FromStr; use strum::Display; use strum::EnumString; use tracing::instrument; -use crate::reaper::ReaperNotification; const NOTIFICATIONS: &[&str] = &[ kAXWindowMiniaturizedNotification, diff --git a/komorebi/src/workspace_reconciliator.rs b/komorebi/src/workspace_reconciliator.rs new file mode 100644 index 00000000..36842236 --- /dev/null +++ b/komorebi/src/workspace_reconciliator.rs @@ -0,0 +1,108 @@ +#![deny(clippy::unwrap_used, clippy::expect_used)] + +use crate::window_manager::WindowManager; +use crate::window_manager_event::WindowManagerEvent; +use crossbeam_channel::Receiver; +use crossbeam_channel::Sender; +use parking_lot::Mutex; +use std::sync::Arc; +use std::sync::OnceLock; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; + +#[derive(Copy, Clone, Debug)] +pub struct Notification { + pub monitor_idx: usize, + pub workspace_idx: usize, + pub triggered_by: WindowManagerEvent, +} + +static RECONCILIATION_IN_PROGRESS: AtomicBool = AtomicBool::new(false); + +static CHANNEL: OnceLock<(Sender, Receiver)> = OnceLock::new(); + +pub fn channel() -> &'static (Sender, Receiver) { + CHANNEL.get_or_init(|| crossbeam_channel::bounded(1)) +} + +fn event_tx() -> Sender { + channel().0.clone() +} + +fn event_rx() -> Receiver { + channel().1.clone() +} + +pub fn send_notification( + monitor_idx: usize, + workspace_idx: usize, + triggered_by: WindowManagerEvent, +) { + if !RECONCILIATION_IN_PROGRESS.load(Ordering::Relaxed) { + tracing::debug!("sending reconciliation request"); + if event_tx() + .try_send(Notification { + monitor_idx, + workspace_idx, + triggered_by, + }) + .is_err() + { + tracing::warn!("channel is full; dropping notification") + } + } +} + +pub fn listen_for_notifications(wm: Arc>) { + std::thread::spawn(move || { + loop { + match handle_notifications(wm.clone()) { + Ok(()) => { + tracing::warn!("restarting finished thread"); + } + Err(error) => { + if cfg!(debug_assertions) { + tracing::error!("restarting failed thread: {:?}", error) + } else { + tracing::error!("restarting failed thread: {}", error) + } + } + } + } + }); +} +pub fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> { + tracing::info!("listening"); + + let receiver = event_rx(); + + for notification in &receiver { + RECONCILIATION_IN_PROGRESS.store(true, Ordering::Relaxed); + tracing::info!("running reconciliation for notification {notification:?}"); + + let mut wm = wm.lock(); + let focused_monitor_idx = wm.focused_monitor_idx(); + let focused_workspace_idx = + wm.focused_workspace_idx_for_monitor_idx(focused_monitor_idx)?; + + let focused_pair = (focused_monitor_idx, focused_workspace_idx); + let updated_pair = (notification.monitor_idx, notification.workspace_idx); + + if focused_pair != updated_pair { + tracing::info!("reconciliating workspace"); + wm.focus_monitor(notification.monitor_idx)?; + let mouse_follows_focus = wm.mouse_follows_focus; + + if let Some(monitor) = wm.focused_monitor_mut() { + let previous_idx = monitor.focused_workspace_idx(); + monitor.last_focused_workspace = Option::from(previous_idx); + monitor.focus_workspace(notification.workspace_idx)?; + monitor.load_focused_workspace(mouse_follows_focus)?; + } + } + + RECONCILIATION_IN_PROGRESS.store(false, Ordering::Relaxed); + } + + Ok(()) +}