Subject: bug#3303: delete-frame raises old (invisible) frame

From: David Reitter <[email-masked]>
Date: Mon, 18 May 2009 11:08:39 -0400

On May 18, 2009, at 4:05 AM, Adrian Robert wrote:

>
> On May 18, 2009, at 10:33 AM, Stefan Monnier wrote:
>
>>> There is no behavior built into the NS window manager to  choose  
>>> another
>>> application window to be active after the active one  has been  
>>> removed --
>>> this is left to application code.
>>
>> [ So, nothing gets focus?  Keyboard events are just dropped on the  
>> floor
>>  in such a case?  Sounds odd: it should be easy for Apple to provide
>>  a sensible default behavior without any negative impact.  ]
>
> Yes, KB events only get sent to a focused window, except for menu  
> shortcut invocations.  It might be that in NSDocument-based apps  
> (which Emacs.app isn't, but would be conceptually similar to if 1- 
> buffer=1-frame) the NSDocument architecture would autofocus the most  
> recently available doc window, but I guess NeXT/Apple decided not to  
> make assumptions otherwise about sensible focus-sequencing.

Precisely for this reason is the patch not sufficient.
When there is a hidden frame, and you delete the only other existing  
frame, we end up in a situation where there is no key window to  
receive the event, and all events (including menu items) are simply  
dropped.

I've experimented with NSWindow's makeKeyWindow, which should leave  
the order of the windows so the frame stays hidden.

However, this alone doesn't do the job.  Events still get dropped when  
you do

(progn
   (make-frame-invisible (selected-frame) t)
   (make-frame)
   (delete-frame (selected-frame) t))

Note that I had to add FRAME_SAMPLE_VISIBILITY compared to the last  
patch I sent.

diff --git a/src/frame.c b/src/frame.c
index de857af..144b8ac 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -778,6 +778,9 @@ affects all frames on the same terminal device.  */)
     is dead.

     The value of NORECORD is passed as argument to Fselect_window.  */
+#ifdef HAVE_NS
+extern  void x_make_key_frame (struct frame *f);
+#endif

  Lisp_Object
  do_switch_frame (frame, track, for_deletion, norecord)
@@ -868,8 +871,12 @@ do_switch_frame (frame, track, for_deletion,  
norecord)

  #ifdef NS_IMPL_COCOA
    /* term gets no other notification of this */
+  FRAME_SAMPLE_VISIBILITY (XFRAME (selected_frame));
    if (for_deletion)
-    Fraise_frame(Qnil);
+    if (FRAME_VISIBLE_P (XFRAME (selected_frame)))
+      Fraise_frame(Qnil);
+    else
+      x_make_key_frame(XFRAME (selected_frame));
  #endif

    /* We want to make sure that the next event generates a frame-switch
diff --git a/src/nsterm.m b/src/nsterm.m
index 7e3028a..0994f7d 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -910,6 +910,20 @@ ns_raise_frame (struct frame *f)
  }

+void
+x_make_key_frame (struct frame *f)
+/*  
--------------------------------------------------------------------------
+     Make window active
+    
-------------------------------------------------------------------------- */
+{
+  NSView *view = FRAME_NS_VIEW (f);
+  check_ns ();
+  BLOCK_INPUT;
+  [[view window] makeKeyWindow];
+  UNBLOCK_INPUT;
+}
+
+
  static void
  ns_lower_frame (struct frame *f)
  /*  
--------------------------------------------------------------------------