159 lines
4.7 KiB
Diff
159 lines
4.7 KiB
Diff
From 23fd2004cb00908a37ad44d0c41a1ca223eb7c55 Mon Sep 17 00:00:00 2001
|
|
From: Daneel <daneel@sukany.cz>
|
|
Date: Sun, 22 Feb 2026 23:21:32 +0100
|
|
Subject: [PATCH] ns: implement AXBoundsForRange for macOS Zoom cursor tracking
|
|
|
|
Add NSAccessibilityBoundsForRangeParameterizedAttribute support to
|
|
EmacsView so that macOS Zoom and other accessibility tools can track
|
|
the text cursor position.
|
|
|
|
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 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 | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
2 files changed, 89 insertions(+)
|
|
|
|
diff --git a/src/nsterm.h b/src/nsterm.h
|
|
index 7c1ee4c..6c1ff34 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 932d209..c900d71 100644
|
|
--- a/src/nsterm.m
|
|
+++ b/src/nsterm.m
|
|
@@ -3232,6 +3232,15 @@ 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
|
|
+ /* Store cursor rect for accessibility (AXBoundsForRange). */
|
|
+ {
|
|
+ EmacsView *view = FRAME_NS_VIEW (f);
|
|
+ if (view)
|
|
+ view->lastAccessibilityCursorRect = r;
|
|
+ }
|
|
+#endif
|
|
+
|
|
ns_focus (f, NULL, 0);
|
|
|
|
NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
|
|
@@ -9474,6 +9483,91 @@ - (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;
|
|
+}
|
|
+
|
|
+/* 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
|