Subject: bug#25818: 25.2; frame moved off display does not return (OS X)

From: [email-masked] (Charles A. Roelli)
Date: Sat, 08 Apr 2017 22:40:31 +0200

Hi Anders,

Following your idea, here's an updated patch that works equivalently.
Please let me know what you think.

> Anyway, I think that we're on the right track and I appreciate that you
> have taken time to work on this!

My pleasure!

> PS. I just found another, unrelated, problem, when comparing the NS and X11
> versions. When passing a negative value to `set-frame-position' it is
> supposed to place the frame relative to the right/bottom border of the
> display, but in the NS version it doesn't. (The X11 version gets the Y
> placement wrong with 48 pixels, but that is another story.)

Hm... but what if a second display is in the negative coordinate space?
How would you place a frame on it programmatically?  In any case I'll
try to build the X11 version and see how it does it -- I wasn't aware
that it still works on Mac OS.

Cheers,
Charles

>> A quick test on 10.6:
>>
>> (setq ns-auto-hide-menu-bar t)
>>
>> ;; Places the frame just above the top of the screen.
>> (set-frame-position (selected-frame) 0 -20)
>>
>> ;; Frame would be completely offscreen; so it's constrained and brought
>> ;; back.
>> (set-frame-position (selected-frame) 0 -10000)

>From 2b95b86f8c50665bbbb62b5faae0d8d610019594 Mon Sep 17 00:00:00 2001
From: Charles A. Roelli <[email-masked]>
Date: Sat, 8 Apr 2017 22:15:17 +0200
Subject: [PATCH] Constrain frames to visible area of screens in OS X

* nsterm.m (constrainFrameRect:toScreen:): Constrain
  frames in OS X, if they would otherwise go offscreen.

Fixes: debbugs:25818
---
 src/nsterm.m |   33 ++++++++++++++++++++++++++++++++-
 1 files changed, 32 insertions(+), 1 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index ebe29e4..b1429f4 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -7846,21 +7846,52 @@ - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9
   // If separate spaces is on, it is like each screen is independent.  There is
   // no spanning of frames across screens.
   if ([NSScreen screensHaveSeparateSpaces])
     {
       NSTRACE_MSG ("Screens have separate spaces");
       frameRect = [super constrainFrameRect:frameRect toScreen:screen];
       NSTRACE_RETURN_RECT (frameRect);
       return frameRect;
     }
-#endif
+  else
+#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9 */
+    // Check that the proposed frameRect intersects at least one
+    // screen rectangle.  If it does not, constrain the frame and
+    // return it.  See bug #25818.
+    {
+      NSArray *screens = [NSScreen screens];
+      NSUInteger nr_screens = [screens count];
+
+      int i;
+      BOOL frame_on_screen = NO;
+
+      for (i = 0; i < nr_screens; ++i)
+	{
+	  NSScreen *s = [screens objectAtIndex: i];
+	  NSRect scrRect = [s frame];
+
+	  if (NSIntersectsRect(frameRect, scrRect))
+	    {
+	      frame_on_screen = YES;
+	      break;
+	    }
+	}
+
+      if (!frame_on_screen)
+	{
+	  NSTRACE_MSG ("Frame outside screens; constraining");
+	  frameRect = [super constrainFrameRect:frameRect toScreen:screen];
+	  NSTRACE_RETURN_RECT (frameRect);
+	  return frameRect;
+	}
+    }
 #endif
 
   return constrain_frame_rect(frameRect,
                               [(EmacsView *)[self delegate] isFullscreen]);
 }

 - (void)performZoom:(id)sender
 {
   NSTRACE ("[EmacsWindow performZoom:]");
-- 
1.7.4.4