Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

fix: supress display events during sleep/wake on macOS (#1331)

Lars Berger6e5242eaecc4
diff --git a/packages/wm-platform/src/platform_impl/macos/display_listener.rs b/packages/wm-platform/src/platform_impl/macos/display_listener.rs
index 2b537b5e..eb6c9daf 100644
--- a/packages/wm-platform/src/platform_impl/macos/display_listener.rs
+++ b/packages/wm-platform/src/platform_impl/macos/display_listener.rs
@@ -1,9 +1,12 @@
+use std::time::{Duration, Instant};
+
use objc2::rc::Retained;
use tokio::sync::mpsc;
use crate::{
platform_impl::{
- NotificationCenter, NotificationName, NotificationObserver,
+ NotificationCenter, NotificationEvent, NotificationName,
+ NotificationObserver,
},
Dispatcher, ThreadBound,
};
@@ -22,7 +25,7 @@ impl DisplayListener {
) -> crate::Result<Self> {
let dispatcher_clone = dispatcher.clone();
let observer = dispatcher.dispatch_sync(move || {
- Self::add_observer(event_tx, dispatcher_clone)
+ Self::add_observers(event_tx, dispatcher_clone)
})?;
Ok(Self {
@@ -43,13 +46,14 @@ impl DisplayListener {
Ok(())
}
- /// Registers the notification observer on the main thread.
- fn add_observer(
+ /// Registers notification observers on the main thread.
+ fn add_observers(
event_tx: mpsc::UnboundedSender<()>,
dispatcher: Dispatcher,
) -> ThreadBound<Retained<NotificationObserver>> {
let (observer, mut events_rx) = NotificationObserver::new();
let mut default_center = NotificationCenter::default_center();
+ let mut workspace_center = NotificationCenter::workspace_center();
// Add observer which will fire when displays are connected and
// disconnected, resolution changes, or arrangement changes.
@@ -61,12 +65,77 @@ impl DisplayListener {
);
}
+ // Add observers for system sleep and wake events.
+ unsafe {
+ workspace_center.add_observer(
+ NotificationName::WorkspaceWillSleep,
+ &observer,
+ None,
+ );
+ workspace_center.add_observer(
+ NotificationName::WorkspaceDidWake,
+ &observer,
+ None,
+ );
+ }
+
std::thread::spawn(move || {
+ // Duration to suppress display change events after wake. macOS fires
+ // several notifications after waking from sleep, and displays can
+ // take 1-2 seconds to be reported as online.
+ const WAKE_COALESCE_DURATION: Duration = Duration::from_secs(5);
+
+ let mut is_asleep = false;
+ let mut wake_time: Option<Instant> = None;
+
// Loop exits when the sender is dropped in `Self::terminate`.
- while events_rx.blocking_recv().is_some() {
- if let Err(err) = event_tx.send(()) {
- tracing::warn!("Failed to send display change event: {}", err);
- break;
+ while let Some(event) = events_rx.blocking_recv() {
+ match event {
+ NotificationEvent::WorkspaceWillSleep => {
+ is_asleep = true;
+ }
+ NotificationEvent::WorkspaceDidWake => {
+ is_asleep = false;
+ wake_time = Some(Instant::now());
+
+ // Send a single display change event after the coalesce
+ // duration to pick up any changes that occurred during wake.
+ let event_tx = event_tx.clone();
+ std::thread::spawn(move || {
+ std::thread::sleep(WAKE_COALESCE_DURATION);
+
+ if let Err(err) = event_tx.send(()) {
+ tracing::warn!(
+ "Failed to send display change event: {}",
+ err
+ );
+ }
+ });
+ }
+ NotificationEvent::ApplicationDidChangeScreenParameters => {
+ // Ignore display change events while asleep or within the
+ // coalesce duration after wake.
+ if is_asleep
+ || wake_time
+ .is_some_and(|t| t.elapsed() < WAKE_COALESCE_DURATION)
+ {
+ continue;
+ }
+
+ // Coalesce duration has passed; clear it.
+ if wake_time.is_some() {
+ wake_time = None;
+ }
+
+ if let Err(err) = event_tx.send(()) {
+ tracing::warn!(
+ "Failed to send display change event: {}",
+ err
+ );
+ break;
+ }
+ }
+ _ => {}
}
}
diff --git a/packages/wm-platform/src/platform_impl/macos/notification_center.rs b/packages/wm-platform/src/platform_impl/macos/notification_center.rs
index 4ea735e1..3665b158 100644
--- a/packages/wm-platform/src/platform_impl/macos/notification_center.rs
+++ b/packages/wm-platform/src/platform_impl/macos/notification_center.rs
@@ -11,6 +11,7 @@ use objc2_app_kit::{
NSWorkspaceDidLaunchApplicationNotification,
NSWorkspaceDidTerminateApplicationNotification,
NSWorkspaceDidUnhideApplicationNotification,
+ NSWorkspaceDidWakeNotification, NSWorkspaceWillSleepNotification,
};
use objc2_foundation::{
ns_string, NSNotification, NSNotificationCenter, NSNotificationName,
@@ -27,6 +28,8 @@ pub(crate) enum NotificationName {
WorkspaceDidTerminateApplication,
WorkspaceDidHideApplication,
WorkspaceDidUnhideApplication,
+ WorkspaceDidWake,
+ WorkspaceWillSleep,
ApplicationDidChangeScreenParameters,
}
@@ -53,6 +56,10 @@ impl From<&NSNotificationName> for NotificationName {
== unsafe { NSWorkspaceDidUnhideApplicationNotification }
{
Self::WorkspaceDidUnhideApplication
+ } else if name == unsafe { NSWorkspaceDidWakeNotification } {
+ Self::WorkspaceDidWake
+ } else if name == unsafe { NSWorkspaceWillSleepNotification } {
+ Self::WorkspaceWillSleep
} else if name
== unsafe { NSApplicationDidChangeScreenParametersNotification }
{
@@ -84,6 +91,12 @@ impl From<NotificationName> for &NSString {
NotificationName::WorkspaceDidUnhideApplication => unsafe {
NSWorkspaceDidUnhideApplicationNotification
},
+ NotificationName::WorkspaceDidWake => unsafe {
+ NSWorkspaceDidWakeNotification
+ },
+ NotificationName::WorkspaceWillSleep => unsafe {
+ NSWorkspaceWillSleepNotification
+ },
NotificationName::ApplicationDidChangeScreenParameters => unsafe {
NSApplicationDidChangeScreenParametersNotification
},
@@ -100,6 +113,8 @@ pub(crate) enum NotificationEvent {
WorkspaceDidTerminateApplication(Retained<NSRunningApplication>),
WorkspaceDidHideApplication(Retained<NSRunningApplication>),
WorkspaceDidUnhideApplication(Retained<NSRunningApplication>),
+ WorkspaceWillSleep,
+ WorkspaceDidWake,
ApplicationDidChangeScreenParameters,
}
@@ -192,6 +207,12 @@ impl NotificationObserver {
);
}
}
+ NotificationName::WorkspaceDidWake => {
+ self.emit_event(NotificationEvent::WorkspaceDidWake);
+ }
+ NotificationName::WorkspaceWillSleep => {
+ self.emit_event(NotificationEvent::WorkspaceWillSleep);
+ }
NotificationName::ApplicationDidChangeScreenParameters => {
self.emit_event(
NotificationEvent::ApplicationDidChangeScreenParameters,