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

From: "Charles A. Roelli" <[email-masked]>
Date: Thu, 27 Apr 2017 21:14:05 +0200

Hi Anders,

As discussed off-list, the copyright assignment is done.

I fixed the patch so that child frames are never constrained (after some 
testing, it seems that a child frame cannot get stuck off screen as long 
as its parent is still visible).  To test this out, evaluate the 
following from emacs -Q,

(progn
   (setq test-frame (make-frame `((parent-frame . ,(selected-frame)))))
   (set-frame-position test-frame 0 500))

and drag the parent frame down until its child is off screen. When the 
parent is moved back up, the child should reappear with it.  (Assuming 
Spaces is off; not sure what would happen in OS X 10.9+ in this case 
when Spaces is on... I guess the child would be constrained before it is 
placed off screen?).

I've also CC'ed Alan since this change is related to the new frame 
parameters.

Cheers,

Charles

>From d2ed9a7bed8872bd89c32d3475607a6e5a271198 Mon Sep 17 00:00:00 2001
From: Charles A. Roelli <[email-masked]>
Date: Thu, 27 Apr 2017 20:44:09 +0200
Subject: [PATCH] Constrain non-child frames to screen area in OS X

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

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

diff --git a/src/nsterm.m b/src/nsterm.m
index 8c5eb06..f0495c4 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -8011,8 +8011,40 @@ - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)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 is visible in at least one
+    // screen.  If it is not, ask the system to reposition it (only
+    // for non-child frames).
+
+    if (!FRAME_PARENT_FRAME (((EmacsView *)[self delegate])->emacsframe))
+    {
+      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,
-- 
1.7.4.4