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!
This commit is contained in:
LGUG2Z committed 2025-10-09 16:54:14 -07:00
1 parent 7bbad6539d
commit acee420bd3
5 files changed
+122 -14

No files matched your search

+1
View File
@@ -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 = {
+2
View File
@@ -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());
+10 -13
View File
@@ -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)?;
+1 -1
View File
@@ -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,
+108
View File
@@ -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<Notification>, Receiver<Notification>)> = OnceLock::new();
pub fn channel() -> &'static (Sender<Notification>, Receiver<Notification>) {
CHANNEL.get_or_init(|| crossbeam_channel::bounded(1))
}
fn event_tx() -> Sender<Notification> {
channel().0.clone()
}
fn event_rx() -> Receiver<Notification> {
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<Mutex<WindowManager>>) {
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<Mutex<WindowManager>>) -> 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(())
}