fix: add accessibilityBoundsForRange: (new NSAccessibilityProtocol) + isAccessibilityElement

Old parameterized attribute API alone insufficient — macOS Zoom prefers
the new NSAccessibilityProtocol method accessibilityBoundsForRange:.
Also adds isAccessibilityElement returning YES.

Both APIs now implemented for compatibility across macOS versions.
This commit is contained in:
2026-02-23 09:19:51 +01:00
parent 78ee89512e
commit ce46e5b7eb

View File

@@ -7,23 +7,26 @@ Add NSAccessibilityBoundsForRangeParameterizedAttribute support to
EmacsView so that macOS Zoom and other accessibility tools can track
the text cursor position.
This implements accessibilityAttributeValue:forParameter: on EmacsView,
storing the cursor rect in ns_draw_window_cursor and converting it to
screen coordinates on demand.
Implements both the old parameterized attribute API
(accessibilityAttributeValue:forParameter:) and the new
NSAccessibilityProtocol (accessibilityBoundsForRange:) so that Zoom
works on both older and newer macOS versions.
Changes:
- Add lastAccessibilityCursorRect ivar to EmacsView (nsterm.h)
- Store cursor rect in ns_draw_window_cursor (nsterm.m)
- Implement accessibilityParameterizedAttributeNames
- Implement accessibilityAttributeValue:forParameter: for BoundsForRange
- Implement accessibilityIsIgnored, accessibilityFocusedUIElement, accessibilityRole
- EmacsView reports as NSAccessibilityTextAreaRole
- Implement accessibilityIsIgnored, accessibilityFocusedUIElement
- Implement accessibilityRole (NSAccessibilityTextAreaRole)
- Implement isAccessibilityElement (new API, returns YES)
- Implement accessibilityParameterizedAttributeNames (old API)
- Implement accessibilityAttributeValue:forParameter: for BoundsForRange (old API)
- Implement accessibilityBoundsForRange: (new NSAccessibilityProtocol)
See also: https://github.com/nicowillis/Ghostty/issues/4053
---
src/nsterm.h | 3 +++
src/nsterm.m | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 71 insertions(+)
src/nsterm.m | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 89 insertions(+)
diff --git a/src/nsterm.h b/src/nsterm.h
index 7c1ee4c..6c1ff34 100644
@@ -59,19 +62,26 @@ index 932d209..c900d71 100644
ns_focus (f, NULL, 0);
NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
@@ -9474,6 +9483,65 @@ - (int) fullscreenState
@@ -9474,6 +9483,83 @@ - (int) fullscreenState
return fs_state;
}
+#ifdef NS_IMPL_COCOA
+/* Accessibility support for macOS Zoom and other assistive tools.
+ Implements AXBoundsForRange so that cursor tracking works. */
+ 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;
@@ -82,6 +92,25 @@ index 932d209..c900d71 100644
+ return NSAccessibilityTextAreaRole;
+}
+
+/* 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];
@@ -127,4 +156,3 @@ index 932d209..c900d71 100644
--
2.43.0