Root cause found via research pipeline: NSAccessibility notifications alone are insufficient for custom NSView. macOS Zoom 'Follow keyboard focus' requires UAZoomChangeFocus() from HIServices/UniversalAccess.h — same as iTerm2 and Chromium. Squashed into single clean patch. 159 insertions, 2 files.
237 lines
8.0 KiB
Diff
237 lines
8.0 KiB
Diff
From 09f293daca75996e7348c89e2b0d31a16c1c2500 Mon Sep 17 00:00:00 2001
|
|
From: Daneel <daneel@sukany.cz>
|
|
Date: Mon, 23 Feb 2026 10:22:47 +0100
|
|
Subject: [PATCH] ns: implement UAZoomChangeFocus + NSAccessibility for macOS
|
|
Zoom
|
|
|
|
Add full cursor tracking support for macOS Zoom 'Follow keyboard focus'
|
|
and other assistive technology tools.
|
|
|
|
ROOT CAUSE: NSAccessibility notifications alone are insufficient for
|
|
custom-drawn views. macOS Zoom requires an explicit call to
|
|
UAZoomChangeFocus() from HIServices/UniversalAccess.h. This is the
|
|
same mechanism used by iTerm2 (PTYTextView.m:refreshAccessibility) and
|
|
Chromium (render_widget_host_view_mac.mm:OnSelectionBoundsChanged).
|
|
|
|
Changes to src/nsterm.h:
|
|
- Add lastAccessibilityCursorRect ivar to EmacsView
|
|
|
|
Changes to src/nsterm.m:
|
|
- ns_draw_window_cursor: store cursor rect, post SelectedTextChanged,
|
|
call UAZoomChangeFocus() with cursor rect in AX screen coordinates
|
|
- windowDidBecomeKey: post FocusedUIElementChangedNotification
|
|
- EmacsView: add accessibilityIsIgnored (NO), isAccessibilityElement (YES),
|
|
accessibilityFocusedUIElement (self), accessibilityRole (TextAreaRole),
|
|
accessibilityAttributeNames, accessibilityAttributeValue: (responds to
|
|
NSAccessibilitySelectedTextRangeAttribute with {0,0}),
|
|
accessibilityBoundsForRange: (new NSAccessibilityProtocol, macOS 10.10+),
|
|
accessibilityParameterizedAttributeNames + old AXBoundsForRange fallback
|
|
|
|
Carbon/Carbon.h (already included in NS_IMPL_COCOA) provides UAZoomEnabled(),
|
|
UAZoomChangeFocus(), and kUAZoomFocusTypeInsertionPoint.
|
|
|
|
See also: https://github.com/nicowillis/Ghostty/issues/4053
|
|
---
|
|
src/nsterm.h | 3 +
|
|
src/nsterm.m | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
2 files changed, 159 insertions(+)
|
|
|
|
diff --git a/src/nsterm.h b/src/nsterm.h
|
|
index 7c1ee4cf535..6c1ff3434a3 100644
|
|
--- a/src/nsterm.h
|
|
+++ b/src/nsterm.h
|
|
@@ -485,6 +485,9 @@ enum ns_return_frame_mode
|
|
struct frame *emacsframe;
|
|
int scrollbarsNeedingUpdate;
|
|
NSRect ns_userRect;
|
|
+#ifdef NS_IMPL_COCOA
|
|
+ NSRect lastAccessibilityCursorRect;
|
|
+#endif
|
|
}
|
|
|
|
/* AppKit-side interface. */
|
|
diff --git a/src/nsterm.m b/src/nsterm.m
|
|
index 932d209f56b..38d946f1d9f 100644
|
|
--- a/src/nsterm.m
|
|
+++ b/src/nsterm.m
|
|
@@ -3232,6 +3232,40 @@ Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
|
|
/* Prevent the cursor from being drawn outside the text area. */
|
|
r = NSIntersectionRect (r, ns_row_rect (w, glyph_row, TEXT_AREA));
|
|
|
|
+#ifdef NS_IMPL_COCOA
|
|
+ /* Update accessibility cursor tracking for macOS Zoom and VoiceOver.
|
|
+ NSAccessibility notifications alone are NOT sufficient for custom-drawn
|
|
+ views -- macOS Zoom "Follow keyboard focus" requires an explicit call to
|
|
+ UAZoomChangeFocus() from HIServices/UniversalAccess.h, which directly
|
|
+ tells Zoom the cursor's screen position. This is the same mechanism used
|
|
+ by iTerm2 (PTYTextView.m:refreshAccessibility) and Chromium. */
|
|
+ {
|
|
+ EmacsView *view = FRAME_NS_VIEW (f);
|
|
+ if (view)
|
|
+ {
|
|
+ view->lastAccessibilityCursorRect = r;
|
|
+
|
|
+ /* Post standard AX notification for VoiceOver and other AT tools. */
|
|
+ NSAccessibilityPostNotification (view,
|
|
+ NSAccessibilitySelectedTextChangedNotification);
|
|
+
|
|
+ /* Tell macOS Zoom exactly where the cursor is. UAZoomChangeFocus()
|
|
+ expects coordinates in the NSAccessibility coordinate system (origin
|
|
+ at top-left of primary screen). Convert: view → window → screen
|
|
+ (Quartz, origin bottom-left) → AX (origin top-left) via
|
|
+ accessibilityConvertScreenRect:. */
|
|
+ if (UAZoomEnabled ())
|
|
+ {
|
|
+ NSRect windowRect = [view convertRect:r toView:nil];
|
|
+ NSRect screenRect = [[view window] convertRectToScreen:windowRect];
|
|
+ CGRect cgRect = [view accessibilityConvertScreenRect:
|
|
+ NSRectToCGRect (screenRect)];
|
|
+ UAZoomChangeFocus (&cgRect, &cgRect, kUAZoomFocusTypeInsertionPoint);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+#endif
|
|
+
|
|
ns_focus (f, NULL, 0);
|
|
|
|
NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
|
|
@@ -8237,6 +8271,14 @@ - (void)windowDidBecomeKey /* for direct calls */
|
|
XSETFRAME (event.frame_or_window, emacsframe);
|
|
kbd_buffer_store_event (&event);
|
|
ns_send_appdefined (-1); // Kick main loop
|
|
+
|
|
+#ifdef NS_IMPL_COCOA
|
|
+ /* Notify accessibility clients (e.g. macOS Zoom) that the focused
|
|
+ UI element changed to this Emacs view. Zoom uses this to activate
|
|
+ keyboard focus tracking when the window gains focus. */
|
|
+ NSAccessibilityPostNotification (self,
|
|
+ NSAccessibilityFocusedUIElementChangedNotification);
|
|
+#endif
|
|
}
|
|
|
|
|
|
@@ -9474,6 +9516,120 @@ - (int) fullscreenState
|
|
return fs_state;
|
|
}
|
|
|
|
+#ifdef NS_IMPL_COCOA
|
|
+/* Accessibility support for macOS Zoom and other assistive tools.
|
|
+ Implements both old (AXBoundsForRange parameterized attribute) and
|
|
+ new (accessibilityBoundsForRange:) APIs so cursor tracking works
|
|
+ on all macOS versions. */
|
|
+
|
|
+- (BOOL)accessibilityIsIgnored
|
|
+{
|
|
+ return NO;
|
|
+}
|
|
+
|
|
+- (BOOL)isAccessibilityElement
|
|
+{
|
|
+ return YES;
|
|
+}
|
|
+
|
|
+- (id)accessibilityFocusedUIElement
|
|
+{
|
|
+ return self;
|
|
+}
|
|
+
|
|
+- (NSString *)accessibilityRole
|
|
+{
|
|
+ return NSAccessibilityTextAreaRole;
|
|
+}
|
|
+
|
|
+- (NSArray *)accessibilityAttributeNames
|
|
+{
|
|
+ NSArray *superAttrs = [super accessibilityAttributeNames];
|
|
+ if (superAttrs == nil)
|
|
+ superAttrs = @[];
|
|
+ return [superAttrs arrayByAddingObjectsFromArray:
|
|
+ @[NSAccessibilityRoleAttribute,
|
|
+ NSAccessibilitySelectedTextRangeAttribute,
|
|
+ NSAccessibilityNumberOfCharactersAttribute]];
|
|
+}
|
|
+
|
|
+- (id)accessibilityAttributeValue:(NSString *)attribute
|
|
+{
|
|
+ if ([attribute isEqualToString:NSAccessibilityRoleAttribute])
|
|
+ return NSAccessibilityTextAreaRole;
|
|
+
|
|
+ /* macOS Zoom queries NSAccessibilitySelectedTextRangeAttribute before
|
|
+ calling AXBoundsForRange / accessibilityBoundsForRange:. We return
|
|
+ {0,0}; our bounds methods ignore the range and always return the
|
|
+ actual cursor rect, so any range value here works. */
|
|
+ if ([attribute isEqualToString:NSAccessibilitySelectedTextRangeAttribute])
|
|
+ return [NSValue valueWithRange:NSMakeRange (0, 0)];
|
|
+
|
|
+ if ([attribute isEqualToString:NSAccessibilityNumberOfCharactersAttribute])
|
|
+ return @(0);
|
|
+
|
|
+ return [super accessibilityAttributeValue:attribute];
|
|
+}
|
|
+
|
|
+/* New NSAccessibilityProtocol (macOS 10.10+) — preferred by Zoom. */
|
|
+- (NSRect)accessibilityBoundsForRange:(NSRange)range
|
|
+{
|
|
+ NSRect viewRect = lastAccessibilityCursorRect;
|
|
+
|
|
+ if (viewRect.size.width < 1)
|
|
+ viewRect.size.width = 1;
|
|
+ if (viewRect.size.height < 1)
|
|
+ viewRect.size.height = 8;
|
|
+
|
|
+ NSWindow *win = [self window];
|
|
+ if (win == nil)
|
|
+ return NSZeroRect;
|
|
+
|
|
+ NSRect windowRect = [self convertRect:viewRect toView:nil];
|
|
+ return [win convertRectToScreen:windowRect];
|
|
+}
|
|
+
|
|
+/* Old parameterized attribute API — fallback for older tools. */
|
|
+- (NSArray *)accessibilityParameterizedAttributeNames
|
|
+{
|
|
+ NSArray *superAttrs = [super accessibilityParameterizedAttributeNames];
|
|
+ if (superAttrs == nil)
|
|
+ superAttrs = @[];
|
|
+ return [superAttrs arrayByAddingObjectsFromArray:
|
|
+ @[NSAccessibilityBoundsForRangeParameterizedAttribute]];
|
|
+}
|
|
+
|
|
+- (id)accessibilityAttributeValue:(NSString *)attribute
|
|
+ forParameter:(id)parameter
|
|
+{
|
|
+ if ([attribute isEqualToString:
|
|
+ NSAccessibilityBoundsForRangeParameterizedAttribute])
|
|
+ {
|
|
+ NSRect viewRect = lastAccessibilityCursorRect;
|
|
+
|
|
+ /* lastAccessibilityCursorRect is in EmacsView coordinates
|
|
+ (flipped: y=0 at top). convertRect:toView:nil handles
|
|
+ the flipped-to-unflipped conversion automatically for
|
|
+ flipped views, so no manual flip is needed. */
|
|
+
|
|
+ if (viewRect.size.width < 1)
|
|
+ viewRect.size.width = 1;
|
|
+ if (viewRect.size.height < 1)
|
|
+ viewRect.size.height = 8;
|
|
+
|
|
+ NSWindow *win = [self window];
|
|
+ if (win == nil)
|
|
+ return [NSValue valueWithRect:NSZeroRect];
|
|
+
|
|
+ NSRect windowRect = [self convertRect:viewRect toView:nil];
|
|
+ NSRect screenRect = [win convertRectToScreen:windowRect];
|
|
+
|
|
+ return [NSValue valueWithRect:screenRect];
|
|
+ }
|
|
+ return [super accessibilityAttributeValue:attribute forParameter:parameter];
|
|
+}
|
|
+#endif /* NS_IMPL_COCOA */
|
|
+
|
|
@end /* EmacsView */
|
|
|
|
|
|
--
|
|
2.43.0
|
|
|