Subject: [Gerd Möllmann] Re: bug#71912: 30.0.50; Inaccurate window-absolute-pixel-position Values on macOS After Sleep and During Fullscreen Mode

From: Gerd Möllmann <[email-masked]>
Date: Sun, 11 Aug 2024 08:49:13 +0200

Forwarded to bug list to keep it informed.

-------------------- Start of forwarded message --------------------
From: Gerd Möllmann <[email-masked]>
To: Jones Stephen <[email-masked]>
Subject: Re: bug#71912: 30.0.50; Inaccurate window-absolute-pixel-position
 Values on macOS After Sleep and During Fullscreen Mode
Date: Sun, 11 Aug 2024 08:41:49 +0200

Jones Stephen <[email-masked]> writes:

> Thank you for the recent patch. I wanted to let you know that after
> using the latest version of Emacs, I found that the fullscreen issue
> has been resolved, which is great news—thank you for that!
>
> However, I’m still encountering an issue with the frame position after
> the system wakes from sleep. Here’s how to reproduce it:
>
> 1. Open Emacs.
> 2. Use a window management tool like Rectangle to position Emacs on one side of the screen.
> 3. Put macOS to sleep.
> 4. Wait for a while, approximately 30 minutes.
> 5. Wake up macOS, enter the login password, and then focus back on Emacs.
>
> At this point, the frame position seems to be incorrect.

I can reproduce this here.

> Interestingly, when I use Rectangle to move Emacs to the other side of
> the screen, the position issue is resolved.
>
> I believe this could be related to missing a system wake notification.
> Perhaps adding something like:
>
> ```swift
> NotificationCenter.default.addObserver(
>     self,
>     selector: #selector(systemDidWake),
>     name: NSWorkspace.didWakeNotification,
>     object: nil
> )
> ```
>
> and calling `adjustEmacsFrameRect` in the callback could address this
> issue. Alternatively, calling `adjustEmacsFrameRect` whenever
> `windowDidBecomeKey` is triggered might also work.
>

Indeed, that seems to work, although I find it strage that something
like that is necessary. But what do I know.

Could you please try the attached patch?

> From 1e095fd07bf91cdccf4fe1ecdbcdd9855773e787 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gerd=20M=C3=B6llmann?= <[email-masked]>
Date: Sun, 11 Aug 2024 08:35:23 +0200
Subject: [PATCH] macOS: Wrong frame rectangle after wake (bug#71912)

* src/nsterm.h ([EmacsView workspaceDidWake:]: New method.
* src/nsterm.m ([EmacsView dealloc])
([EmacsView initFrameFromEmacs:]): Register and deregister view as
observer for NSWorkspaceDidWakeNotification.
---
 src/nsterm.h |  1 +
 src/nsterm.m | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/src/nsterm.h b/src/nsterm.h
index 6c67653705e..0833745616e 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -523,6 +523,7 @@ #define NSTRACE_UNSILENCE()

 /* Non-notification versions of NSView methods. Used for direct calls.  */
 - (void)adjustEmacsFrameRect;
+- (void)workspaceDidWake: (NSNotification *) notification;
 - (void)windowWillEnterFullScreen;
 - (void)windowDidEnterFullScreen;
 - (void)windowWillExitFullScreen;
diff --git a/src/nsterm.m b/src/nsterm.m
index 905707dace4..b60c38b742a 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -6689,6 +6689,13 @@ - (void)dealloc
               name:NSViewFrameDidChangeNotification
             object:nil];

+#ifdef NS_IMPL_COCOA
+  [[[NSWorkspace sharedWorkspace] notificationCenter]
+    removeObserver: self
+	      name: NSWorkspaceDidWakeNotification
+	    object: nil];
+#endif
+
   if (fs_state == FULLSCREEN_BOTH)
     [nonfs_window release];
   [super dealloc];
@@ -8039,6 +8046,11 @@ - (BOOL)isOpaque
   return NO;
 }

+- (void)workspaceDidWake: (NSNotification *) notification
+{
+  if (emacsframe)
+    [self adjustEmacsFrameRect];
+}

 - (instancetype) initFrameFromEmacs: (struct frame *)f
 {
@@ -8051,6 +8063,15 @@ - (instancetype) initFrameFromEmacs: (struct frame *)f
   fs_state = FULLSCREEN_NONE;
   fs_before_fs = next_maximized = -1;

+#ifdef NS_IMPL_COCOA
+  /* Wrong frame rectangle after wake (bug#71912).  */
+  [[[NSWorkspace sharedWorkspace] notificationCenter]
+    addObserver: self
+       selector: @selector (workspaceDidWake:)
+	   name: NSWorkspaceDidWakeNotification
+	 object: nil];
+#endif
+
   fs_is_native = NO;
 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
--
2.46.0

-------------------- End of forwarded message --------------------