diff --git a/komorebi/src/notification_center_listener.rs b/komorebi/src/notification_center_listener.rs index 9f947c7a..317ee939 100644 --- a/komorebi/src/notification_center_listener.rs +++ b/komorebi/src/notification_center_listener.rs @@ -35,6 +35,7 @@ define_class! { let mut process_id = None; let mut window_id = None; let mut valid_keys = vec![]; + let mut application = None; if let Some(user_info) = notif.userInfo() { let all_keys = user_info.allKeys(); @@ -46,19 +47,20 @@ define_class! { if let Some(application_key) = user_info.valueForKey(&NSString::from_str("NSWorkspaceApplicationKey")) - && let Some(application) = application_key.downcast_ref::() + && let Some(running_application) = application_key.downcast_ref::() { - process_id = Some(application.processIdentifier()); - if let Ok(application) = Application::new(application.processIdentifier()) { - window_id = application.main_window_id(); + process_id = Some(running_application.processIdentifier()); + if let Ok(app) = Application::new(running_application.processIdentifier()) { + window_id = app.main_window_id(); + application = Some(app); } } } tracing::trace!("received {} with keys {}", notif.name(), valid_keys.join(", ")); - match process_id { - None => { + match process_id { + None => { if let Ok(notification) = AppKitWorkspaceNotification::from_str(¬if.name().to_string()) { if matches!(notification, AppKitWorkspaceNotification::NSWorkspaceActiveSpaceDidChangeNotification) { @@ -74,16 +76,38 @@ define_class! { } Some(process_id) => { tracing::debug!("notification: {}, process: {process_id}", notif.name()); - if let Ok(notification) = - AppKitWorkspaceNotification::from_str(¬if.name().to_string()) - && let Some(event) = WindowManagerEvent::from_system_notification( - SystemNotification::AppKitWorkspace(notification), - process_id, - window_id, - ) - { - window_manager_event_listener::send_notification(event); - }; + match AppKitWorkspaceNotification::from_str(¬if.name().to_string()) { + Ok(AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification) => { + if application.is_some_and(|app| app.name().unwrap_or_default() == "loginwindow") { + window_manager_event_listener::send_notification( + WindowManagerEvent::ScreenLock( + SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification), + process_id + ) + ); + } + } + Ok(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification) => { + if application.is_some_and(|app| app.name().unwrap_or_default() == "loginwindow") { + window_manager_event_listener::send_notification( + WindowManagerEvent::ScreenUnlock( + SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification), + process_id + ) + ); + } + } + Ok(notification) => { + if let Some(event) = WindowManagerEvent::from_system_notification( + SystemNotification::AppKitWorkspace(notification), + process_id, + window_id, + ) { + window_manager_event_listener::send_notification(event); + } + } + _ => {} + } } } } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index a2f9f5fa..dbfd859c 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -68,6 +68,22 @@ pub fn listen_for_events(wm: Arc>) { impl WindowManager { #[instrument(skip_all)] pub fn process_event(&mut self, event: WindowManagerEvent) -> eyre::Result<()> { + if matches!(event, WindowManagerEvent::ScreenLock(_, _)) { + let application = self.application(event.process_id())?; + if application.name().unwrap_or_default() == "loginwindow" { + tracing::debug!("pausing while screen is locked"); + self.is_paused = true; + } + } + + if matches!(event, WindowManagerEvent::ScreenUnlock(_, _)) { + let application = self.application(event.process_id())?; + if application.name().unwrap_or_default() == "loginwindow" { + tracing::debug!("unpausing on screen unlock"); + self.is_paused = false; + } + } + if self.is_paused { tracing::trace!("ignoring while paused"); return Ok(()); @@ -942,7 +958,10 @@ impl WindowManager { } } } - WindowManagerEvent::SpaceChange(_, _) => {} + // handled before this match + WindowManagerEvent::SpaceChange(_, _) + | WindowManagerEvent::ScreenLock(_, _) + | WindowManagerEvent::ScreenUnlock(_, _) => {} } self.update_known_window_ids(); diff --git a/komorebi/src/reaper.rs b/komorebi/src/reaper.rs index 8f0f9d96..4ef8723f 100644 --- a/komorebi/src/reaper.rs +++ b/komorebi/src/reaper.rs @@ -57,8 +57,12 @@ fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> let receiver = event_rx(); - for notification in receiver { + 'notification: for notification in receiver { let mut wm = wm.lock(); + if wm.is_paused { + tracing::debug!("skipping reaper notification while wm is paused"); + continue 'notification; + } match notification { ReaperNotification::InvalidWindow(window_id) => { diff --git a/komorebi/src/window_manager_event.rs b/komorebi/src/window_manager_event.rs index 75fb60f1..a13abe10 100644 --- a/komorebi/src/window_manager_event.rs +++ b/komorebi/src/window_manager_event.rs @@ -43,6 +43,8 @@ pub enum WindowManagerEvent { ResizeStart(SystemNotification, i32, u32), ResizeEnd(SystemNotification, i32, u32), SpaceChange(SystemNotification, i32), + ScreenLock(SystemNotification, i32), + ScreenUnlock(SystemNotification, i32), } impl WindowManagerEvent { @@ -143,6 +145,8 @@ impl WindowManagerEvent { | WindowManagerEvent::ResizeStart(_, process_id, _) | WindowManagerEvent::ResizeEnd(_, process_id, _) | WindowManagerEvent::SpaceChange(_, process_id) + | WindowManagerEvent::ScreenLock(_, process_id) + | WindowManagerEvent::ScreenUnlock(_, process_id) | WindowManagerEvent::Restore(_, process_id, _) => *process_id, } } @@ -160,6 +164,8 @@ impl WindowManagerEvent { | WindowManagerEvent::ResizeEnd(n, _, _) | WindowManagerEvent::Unmanage(n, _, _) | WindowManagerEvent::SpaceChange(n, _) + | WindowManagerEvent::ScreenLock(n, _) + | WindowManagerEvent::ScreenUnlock(n, _) | WindowManagerEvent::Restore(n, _, _) => match n { SystemNotification::Accessibility(a) => a.to_string(), SystemNotification::AppKitWorkspace(a) => a.to_string(), @@ -173,6 +179,8 @@ impl WindowManagerEvent { WindowManagerEvent::FocusChange(_, _, window_id) => *window_id, WindowManagerEvent::Show(_, _) | WindowManagerEvent::Destroy(_, _) + | WindowManagerEvent::ScreenLock(_, _) + | WindowManagerEvent::ScreenUnlock(_, _) | WindowManagerEvent::SpaceChange(_, _) => None, WindowManagerEvent::Minimize(_, _, window_id) | WindowManagerEvent::Manage(_, _, window_id)