Subject: bug#31324: 26.0.91; Wrong AXSubrole of childframe on macOS

From: Alan Third <[email-masked]>
Date: Sun, 13 May 2018 11:14:02 +0100

Please keep the bug tracker Cc’d in.

On Fri, May 11, 2018 at 09:31:14PM +0800, Fu Yuan wrote:
> I first ran the lisp code and nothing changed. Then I patched source
> file and compiled, then ran the lisp code on the compiled emacs.
> Nothing different on that Emacs. The UIElementInspecter shows
> AXStandardWindow. Of course both time I used “emacs" command with
> “-Q" flag.
> 
> Is there anything else I can do?

OK, I looked into this a bit further and it seems this stuff is in the
accessibility API. I’ve attached a couple of patches, please apply
‘fix redefinition of child frames on NS’, then ‘Set accessibility
subroles for child frame’ and see if it does what you expect.

This will only work on macOS >= 10.10. I think there must be another
way of doing it on older versions but I haven’t found it yet. I’ll see
if this works before digging any more.
-- 
Alan Third
>From a33d6248a7369fc18111e42b6157a6bf84c5578a Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Sun, 13 May 2018 10:33:44 +0100
Subject: [PATCH] Fix redefinition of child frames on NS

* src/nsterm.m (x_set_parent_frame): If the NSWindow has an existing
parent frame, remove it.
---
 src/nsterm.m | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index c8ae31abc0..df883346de 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1958,12 +1958,20 @@ so some key presses (TAB) are swallowed by the system. */
 
   if (p != FRAME_PARENT_FRAME (f))
     {
-      parent = [FRAME_NS_VIEW (p) window];
+      block_input ();
       child = [FRAME_NS_VIEW (f) window];
 
-      block_input ();
-      [parent addChildWindow: child
-                     ordered: NSWindowAbove];
+      if ([child parentWindow] != nil)
+        [[child parentWindow] removeChildWindow:child];
+
+      if (!NILP (new_value))
+        {
+          parent = [FRAME_NS_VIEW (p) window];
+
+          [parent addChildWindow: child
+                         ordered: NSWindowAbove];
+        }
+
       unblock_input ();
 
       fset_parent_frame (f, new_value);
-- 
2.16.1

>From 8f1502d98ed40ba6c750bd6ab48fb7812d3c937e Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Sun, 13 May 2018 11:02:00 +0100
Subject: [PATCH] Set accessibility subroles for child frame (bug#31324)

; Depends on patch in bug#31440.

* src/nsterm.m (x_set_parent_frame): Set subrole depending on whether
frame is a child or not.
---
 src/nsterm.m | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index df883346de..97682f7a47 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1962,7 +1962,15 @@ so some key presses (TAB) are swallowed by the system. */
       child = [FRAME_NS_VIEW (f) window];
 
       if ([child parentWindow] != nil)
-        [[child parentWindow] removeChildWindow:child];
+        {
+          [[child parentWindow] removeChildWindow:child];
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 101000
+          if ([child respondsToSelector:@selector(setAccessibilitySubrole:)]
+#endif
+              [child setAccessibilitySubrole:NSAccessibilityStandardWindowSubrole];
+#endif
+        }
 
       if (!NILP (new_value))
         {
@@ -1970,6 +1978,12 @@ so some key presses (TAB) are swallowed by the system. */
 
           [parent addChildWindow: child
                          ordered: NSWindowAbove];
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 101000
+          if ([child respondsToSelector:@selector(setAccessibilitySubrole:)]
+#endif
+          [child setAccessibilitySubrole:NSAccessibilityFloatingWindowSubrole];
+#endif
         }
 
       unblock_input ();
-- 
2.16.1