Subject: 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: Thu, 25 Jul 2024 09:39:16 +0200

Gerd Möllmann <[email-masked]> writes:

> Jones Stephen <[email-masked]> writes:
>
>> Dear Emacs Developers,
>>
>> I am encountering an issue with window-absolute-pixel-position on macOS that leads to incorrect Y-coordinate values under specific conditions. This
>> bug appears to manifest in two scenarios: after the system resumes from sleep, and when Emacs is in fullscreen mode.
>>
>> Environment:
>>
>> Emacs Version: (30.0.50)
>> Operating System: macOS (After 14.00)
>> Issue Description:
>>
>> After Sleep: When macOS resumes from sleep, the Y-coordinate value returned by window-absolute-pixel-position is off by approximately one-fourth
>> of the screen height.
>> During Fullscreen Mode: In fullscreen mode, the Y-coordinate is incorrectly offset by the height of the navbar.
>> Steps to Reproduce:
>>
>> Open Emacs and position a window at a known pixel position.
>> Either put the system to sleep and wake it, or switch Emacs to fullscreen mode.
>> Evaluate (window-absolute-pixel-position) and note the Y-coordinate.
>> Expected Behavior:
>> The function should return accurate window positions regardless of sleep mode or fullscreen state, without any unexpected offsets.
>>
>> Actual Behavior:
>> The Y-coordinate is inaccurately calculated, showing a significant offset that seems to be influenced by system sleep and fullscreen transitions.
>>
>> This issue can be particularly disruptive in workflows that rely on precise window positioning, such as automated GUI testing or window management
>> scripts. Any insights or fixes would be greatly appreciated!
>>
>> Thank you for your attention to this matter.
>>
>> Best regards,
>> happyo
>
> I can reproduce this (only tried the fullscreen case), but I don't know
> how to fix it. In a fullscreen frame, eval
>
>   (ns-frame-edges (selected-frame) 'native-edges)
>   -> (566 39 2290 1107)
>
> That is, both left and top are bogus values. Also,
>
>   (frame-parameter (selected-frame) 'fullscreen)
>   -> fullboth
>   (frame-parameter (selected-frame) 'top)
>   -> 39
>   (frame-parameter (selected-frame) 'left)
>   -> 566
>
> which is why ns-frame-edges in its current implementation returns these
> values for left and top. Don't know what is expected here. Should top
> and left frame parameters have been set to 0 somewhere? Or is the
> implementation of ns-frame-edges wrong in using top_pos and left_pos of
> the frame?

The following patch for master addresses this at least partially. Part
of the problem is, it appears to me, is that windowDidMove is not called
when entering/exiting fullscreen mode, so that a frame's left_pos and
top_pos are not adjusted. The patch tries to fix that. I haven't found
out what to do with the frame parameters top and left.

(Alan Third CC'd)

>From 78f81f9089b51c2de06ca0ad62f4c42c37303c4c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gerd=20M=C3=B6llmann?= <[email-masked]>
Date: Thu, 25 Jul 2024 09:20:04 +0200
Subject: [PATCH] macOS: adjust frame position when entering/exiting fullscreen

* src/nsterm.h ([EmacsView adjustEmacsRectRect]): Declare.
* src/nsterm.m ([EmacsView windowDidEnterFullScreen]): New method.
([EmacsView windowDidEnterFullScreen]): Call it.
([EmacsView windowDidExitFullScreen]): Call it.
---
 src/nsterm.h |  1 +
 src/nsterm.m | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/src/nsterm.h b/src/nsterm.h
index 3a713f8e8c9..e3f55c4e41c 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -522,6 +522,7 @@ #define NSTRACE_UNSILENCE()
 - (void)copyRect:(NSRect)srcRect to:(NSPoint)dest;
 
 /* Non-notification versions of NSView methods. Used for direct calls.  */
+- (void)adjustEmacsFrameRect;
 - (void)windowWillEnterFullScreen;
 - (void)windowDidEnterFullScreen;
 - (void)windowWillExitFullScreen;
diff --git a/src/nsterm.m b/src/nsterm.m
index 2aadada2df6..10e06b07962 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -8318,6 +8318,15 @@ - (void)windowDidEnterFullScreen:(NSNotification *)notification
   [self windowDidEnterFullScreen];
 }
 
+- (void)adjustEmacsFrameRect
+{
+  struct frame *f = emacsframe;
+  NSWindow *frame_window = [FRAME_NS_VIEW (f) window];
+  NSRect r = [frame_window frame];
+  f->left_pos = NSMinX (r) - NS_PARENT_WINDOW_LEFT_POS (f);
+  f->top_pos = NS_PARENT_WINDOW_TOP_POS (f) - NSMaxY (r);
+}
+
 - (void)windowDidEnterFullScreen /* provided for direct calls */
 {
   NSTRACE ("[EmacsView windowDidEnterFullScreen]");
@@ -8347,6 +8356,10 @@ - (void)windowDidEnterFullScreen /* provided for direct calls */
         }
 #endif
     }
+
+  /* Do what windowDidMove does which isn't called when entering/exiting
+     fullscreen mode.  */
+  [self adjustEmacsFrameRect];
 }
 
 - (void)windowWillExitFullScreen:(NSNotification *)notification
@@ -8389,6 +8402,10 @@ - (void)windowDidExitFullScreen /* provided for direct calls */
 
   if (next_maximized != -1)
     [[self window] performZoom:self];
+
+  /* Do what windowDidMove does which isn't called when entering/exiting
+     fullscreen mode.  */
+  [self adjustEmacsFrameRect];
 }
 
 - (BOOL)fsIsNative
-- 
2.45.2