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

From: [email-masked] (Charles A. Roelli)
Date: Sun, 02 Apr 2017 16:14:29 +0200

Hi Anders,

Does the attached patch work for you?  It's still a bit rough around the
edges, but seems to do the right thing.

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)

Cheers,
Charles

>From 60db513f5283525c3adacb29f1bc6ad4461a11b0 Mon Sep 17 00:00:00 2001
From: Charles A. Roelli <[email-masked]>
Date: Sun, 2 Apr 2017 15:51:24 +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..b87b9fa 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 */
+    {
+      NSArray *screens = [NSScreen screens];
+      NSUInteger nr_screens = [screens count];
+
+      int i;
+      // A rectangle covering all the screen the frame is displayed in.
+      // Duplicated from constrain_frame_rect.
+      NSRect multiscreenRect = NSMakeRect(0, 0, 0, 0);
+      for (i = 0; i < nr_screens; ++i )
+	{
+	  NSScreen *s = [screens objectAtIndex: i];
+	  NSRect scrRect = [s frame];
+
+	  NSTRACE_MSG ("Screen %d: " NSTRACE_FMT_RECT,
+		       i, NSTRACE_ARG_RECT (scrRect));
+
+	  multiscreenRect = NSUnionRect (multiscreenRect, scrRect);
+	}
+
+      // Check that the proposed frameRect intersects the
+      // multiscreenRect.  If it does not, constrain the frame and
+      // return it.  See bug #25818.
+      if (!NSIntersectsRect(multiscreenRect, frameRect))
+	{
+	  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