fix(wm): preserve state on screen lock and unlock

Screen locking and unlocking is a bit weird in macOS because it's
implemented as a separate app which is activated and deactivated, so
unique events for screen lock and unlock are never emitted by the
system.

This commit encapsulates the logic to detect the relevant events on the
loginwindow into the creation of ScreenLock and ScreenUnlock
WindowManagerEvent variants.
This commit is contained in:
LGUG2Z committed 2025-10-09 19:19:51 -07:00
1 parent acee420bd3
commit 141eedfd60
4 files changed
+73 -18

No files matched your search

+40 -16
View File
@@ -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::<NSRunningApplication>()
&& let Some(running_application) = application_key.downcast_ref::<NSRunningApplication>()
{
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(&notif.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(&notif.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(&notif.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);
}
}
_ => {}
}
}
}
}
+20 -1
View File
@@ -68,6 +68,22 @@ pub fn listen_for_events(wm: Arc<Mutex<WindowManager>>) {
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();
+5 -1
View File
@@ -57,8 +57,12 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> 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) => {
+8
View File
@@ -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)