patches: auto-detect Zoom/VoiceOver; single variable gates both

Changes:
- EmacsApp gets ns_update_accessibility_state and
  ns_accessibility_did_change: methods (patch 0005)
- At startup: UAZoomEnabled() + AXIsProcessTrustedWithOptions()
  determine initial ns_accessibility_enabled state
- com.apple.accessibility.api distributed notification updates it
  whenever any AT connects or disconnects
- All Zoom call sites (UAZoomChangeFocus) now gated by
  ns_accessibility_enabled in addition to ns_zoom_enabled_p()
- ns-accessibility-enabled docstring updated to describe auto-detect

Result: zero config needed; zero overhead when no AT is active;
single variable overrides auto-detection when needed.
This commit is contained in:
2026-03-01 06:39:37 +01:00
parent cc7b288e99
commit 636545c2a5
9 changed files with 163 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
From 7b4c8fc93674296aac802377ad223db50f79f33d Mon Sep 17 00:00:00 2001
From 61a0ae5344e6f52cb21ae299133b39b2ede010d0 Mon Sep 17 00:00:00 2001
From: Martin Sukany <martin@sukany.cz>
Date: Sat, 28 Feb 2026 12:58:11 +0100
Subject: [PATCH 6/9] ns: integrate accessibility with EmacsView and redisplay
@@ -23,8 +23,8 @@ block cursor, org-mode folded headings, indirect buffers.
Known limitations documented in patch 6 Texinfo node.
---
etc/NEWS | 13 ++
src/nsterm.m | 359 ++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 369 insertions(+), 3 deletions(-)
src/nsterm.m | 430 +++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 431 insertions(+), 12 deletions(-)
diff --git a/etc/NEWS b/etc/NEWS
index 80661a9..2b1f9e6 100644
@@ -51,10 +51,29 @@ index 80661a9..2b1f9e6 100644
** Re-introduced dictation, lost in Emacs v30 (macOS).
We lost macOS dictation in v30 when migrating to NSTextInputClient.
diff --git a/src/nsterm.m b/src/nsterm.m
index 5eaf480..1577338 100644
index 5eaf480..4ad451f 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1405,6 +1405,9 @@ so the visual offset is (ov_line + 1) * line_h from
@@ -1258,7 +1258,7 @@ If a completion candidate is selected (overlay or child frame),
static void
ns_zoom_track_completion (struct frame *f, EmacsView *view)
{
- if (!ns_zoom_enabled_p ())
+ if (!ns_accessibility_enabled || !ns_zoom_enabled_p ())
return;
if (!WINDOWP (f->selected_window))
return;
@@ -1378,7 +1378,8 @@ so the visual offset is (ov_line + 1) * line_h from
(zoomCursorUpdated is NO). */
#if defined (MAC_OS_X_VERSION_MIN_REQUIRED) \
&& MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
- if (view && !view->zoomCursorUpdated && ns_zoom_enabled_p ()
+ if (ns_accessibility_enabled && view && !view->zoomCursorUpdated
+ && ns_zoom_enabled_p ()
&& !NSIsEmptyRect (view->lastCursorRect))
{
NSRect r = view->lastCursorRect;
@@ -1405,6 +1406,9 @@ so the visual offset is (ov_line + 1) * line_h from
if (view)
ns_zoom_track_completion (f, view);
#endif /* NS_IMPL_COCOA */
@@ -64,7 +83,73 @@ index 5eaf480..1577338 100644
}
static void
@@ -7620,7 +7623,6 @@ - (id)accessibilityTopLevelUIElement
@@ -3552,7 +3556,7 @@ EmacsView pixels (AppKit, flipped, top-left origin)
#if defined (MAC_OS_X_VERSION_MIN_REQUIRED) \
&& MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
- if (ns_zoom_enabled_p ())
+ if (ns_accessibility_enabled && ns_zoom_enabled_p ())
{
NSRect windowRect = [view convertRect:r toView:nil];
NSRect screenRect
@@ -6717,9 +6721,56 @@ - (void)applicationDidFinishLaunching: (NSNotification *)notification
}
#endif
+#ifdef NS_IMPL_COCOA
+ /* Auto-detect Zoom and VoiceOver at startup and whenever their state
+ changes. The "com.apple.accessibility.api" distributed notification
+ fires when any assistive technology connects or disconnects.
+ Both code paths set ns_accessibility_enabled so that one variable
+ gates all our accessibility overhead. */
+ [self ns_update_accessibility_state];
+ [[NSDistributedNotificationCenter defaultCenter]
+ addObserver: self
+ selector: @selector(ns_accessibility_did_change:)
+ name: @"com.apple.accessibility.api"
+ object: nil
+suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
+#endif
+
ns_send_appdefined (-2);
}
+#ifdef NS_IMPL_COCOA
+/* Set ns_accessibility_enabled based on current AT state.
+ Called at startup and from the "com.apple.accessibility.api"
+ distributed notification handler. Checks both UAZoomEnabled()
+ (Zoom) and AXIsProcessTrustedWithOptions() (VoiceOver and other
+ ATs that have connected to this process). */
+- (void) ns_update_accessibility_state
+{
+ NSTRACE ("[EmacsApp ns_update_accessibility_state]");
+ BOOL zoom_on = UAZoomEnabled ();
+ NSDictionary *opts = @{(__bridge id) kAXTrustedCheckOptionPrompt: @NO};
+ BOOL at_on = AXIsProcessTrustedWithOptions ((__bridge CFDictionaryRef) opts);
+ BOOL new_state = zoom_on || at_on;
+ if ((BOOL) ns_accessibility_enabled != new_state)
+ {
+ ns_accessibility_enabled = new_state;
+ /* Reset the UAZoomEnabled cache so ns_zoom_enabled_p() reflects
+ the new Zoom state on its next call. */
+ ns_zoom_cache_time = 0;
+ }
+}
+
+/* Handler for the "com.apple.accessibility.api" distributed notification,
+ posted by macOS when any AT (VoiceOver, Switch Control, etc.) starts
+ or stops. */
+- (void) ns_accessibility_did_change: (NSNotification *) notification
+{
+ NSTRACE ("[EmacsApp ns_accessibility_did_change:]");
+ [self ns_update_accessibility_state];
+}
+#endif
+
- (void)antialiasThresholdDidChange:(NSNotification *)notification
{
#ifdef NS_IMPL_COCOA
@@ -7620,7 +7671,6 @@ - (id)accessibilityTopLevelUIElement
@@ -72,7 +157,7 @@ index 5eaf480..1577338 100644
static BOOL
ns_ax_find_completion_overlay_range (struct buffer *b, ptrdiff_t point,
ptrdiff_t *out_start,
@@ -8722,7 +8724,6 @@ - (NSRect)accessibilityFrame
@@ -8722,7 +8772,6 @@ - (NSRect)accessibilityFrame
@end
@@ -80,7 +165,7 @@ index 5eaf480..1577338 100644
/* ===================================================================
EmacsAccessibilityBuffer (Notifications) — AX event dispatch
@@ -9267,7 +9268,6 @@ - (NSRect)accessibilityFrame
@@ -9267,7 +9316,6 @@ - (NSRect)accessibilityFrame
@end
@@ -88,7 +173,7 @@ index 5eaf480..1577338 100644
/* ===================================================================
EmacsAccessibilityInteractiveSpan — helpers and implementation
=================================================================== */
@@ -9597,6 +9597,7 @@ - (void)dealloc
@@ -9597,6 +9645,7 @@ - (void)dealloc
[layer release];
#endif
@@ -96,7 +181,7 @@ index 5eaf480..1577338 100644
[[self menu] release];
[super dealloc];
}
@@ -10945,6 +10946,32 @@ - (void)windowDidBecomeKey /* for direct calls */
@@ -10945,6 +10994,32 @@ - (void)windowDidBecomeKey /* for direct calls */
XSETFRAME (event.frame_or_window, emacsframe);
kbd_buffer_store_event (&event);
ns_send_appdefined (-1); // Kick main loop
@@ -129,7 +214,7 @@ index 5eaf480..1577338 100644
}
@@ -12182,6 +12209,332 @@ - (int) fullscreenState
@@ -12182,6 +12257,332 @@ - (int) fullscreenState
return fs_state;
}
@@ -462,6 +547,30 @@ index 5eaf480..1577338 100644
@end /* EmacsView */
@@ -14182,12 +14583,17 @@ Nil means use fullscreen the old (< 10.7) way. The old way works better with
ns_use_srgb_colorspace = YES;
DEFVAR_BOOL ("ns-accessibility-enabled", ns_accessibility_enabled,
- doc: /* Non-nil means expose buffer content to the macOS accessibility
-subsystem (VoiceOver, Zoom, and other assistive technology).
-When nil, the accessibility virtual element tree is not built and no
-notifications are posted, eliminating the associated overhead.
-Requires the Cocoa (NS) build on macOS; ignored on GNUstep.
-Default is nil. Set to t to enable VoiceOver support. */);
+ doc: /* Non-nil enables Zoom cursor tracking and VoiceOver support.
+Emacs sets this automatically at startup when macOS Zoom is active or
+any assistive technology (VoiceOver, Switch Control, etc.) is connected,
+and updates it whenever that state changes. You can override manually:
+
+ (setq ns-accessibility-enabled t) ; always on
+ (setq ns-accessibility-enabled nil) ; always off
+
+When nil, no AX tree is built and no notifications are posted,
+giving zero per-redisplay overhead.
+Requires the Cocoa (NS) build on macOS; ignored on GNUstep. */);
ns_accessibility_enabled = NO;
DEFVAR_BOOL ("ns-use-mwheel-acceleration",
--
2.43.0