Truly independent web browser
UI/Qt: Keep VoiceOver's focus in step with the keyboard focus on macOS
Andreas Kling3980cd2db031
diff --git a/Documentation/ScreenReaderSupport.md b/Documentation/ScreenReaderSupport.mdindex 93864609186..e4930a0c782 100644--- a/Documentation/ScreenReaderSupport.md+++ b/Documentation/ScreenReaderSupport.md@@ -226,7 +226,7 @@ The search-predicate implementation does a pre-order depth-first traversal. Two### Actions-`accessibilityPerformAction:` handles `NSAccessibilityPressAction` by sending a `perform_accessibility_action` IPC with action `press`. On the WebContent side, `HTMLElement::click()` is called on the target node, looked up by `Node::from_unique_id(UniqueNodeID)`. `accessibilitySetValue:forAttribute:` handles the focused attribute by sending action `focus`, which calls `HTML::run_focusing_steps()` on the target element.+`accessibilityPerformAction:` handles `NSAccessibilityPressAction` by sending a `perform_accessibility_action` IPC with action `press`. On the WebContent side, `HTMLElement::click()` is called on the target node, looked up by `Node::from_unique_id(UniqueNodeID)`. `accessibilitySetValue:forAttribute:` handles the focused attribute by sending action `focus`, which calls `HTML::run_focusing_steps()` on the target element. Before it sends that, the view takes the keyboard focus, unless the view or a child of it already has it, so keys go where VoiceOver says the focus is. VoiceOver sets the focus on what its cursor moves to when the keyboard focus follows that cursor, so the keyboard focus enters the page with the VoiceOver cursor, as with Chromium’s `BrowserAccessibilityManager::SetFocus()`.### Table navigation@@ -278,7 +278,7 @@ Until that first request, WebContent reports nothing: `Page::accessibility_inter- Load finish (a navigation listener) → `request_accessibility_tree()`, but only while an assistive technology is present, which Qt reports as `QAccessible::isActive()`: on macOS, `-[QNSView activateQtAccessibility]` turns that on at the first accessibility query into any Qt window. Without one, the view never requests the tree — so WebContent never sends it tree updates or focus changes (see _Live DOM-mutation updates_ above).- An assistive technology that turns up later → `accessibilityActiveChanged()` (the view is a `QAccessible::ActivationObserver`) → `request_accessibility_tree()`, if the view hasn’t requested the tree yet. That tree doesn’t announce a load.- `on_accessibility_tree_received` → update manager; then `update_accessibility_tree()` drops the cached elements of the nodes that left the tree (invalidating them first) and keeps the rest, so an assistive technology’s handles survive a DOM mutation. For the first tree after each load only, and only while the view holds the keyboard focus in the key window, it posts `@"AXLoadComplete"` (matching what Chromium and Firefox do) and calls `NSAccessibilityHandleFocusChanged()`, so AppKit refreshes its cached focused-UI-element. A page load never moves the keyboard focus. VoiceOver moves into the web content when it gets `AXLoadComplete`, so the focus check keeps it in the location editor of a new tab, for example, and out of windows the user isn’t in. Any other tree posts no notification at all, and no tree posts `NSAccessibilityLayoutChangedNotification`: VoiceOver answers that one by moving its cursor back to the `AXWebArea`, announcing “frame”, and by setting `AXFocused` there, which takes the focus from whatever element in the page had it.-- `on_accessibility_focus_changed` → update manager, post `NSAccessibilityFocusedUIElementChangedNotification` on the node’s element (`post_accessibility_focus_changed()`)+- `on_accessibility_focus_changed` → update manager; then, only while the view holds the keyboard focus in the key window, post `NSAccessibilityFocusedUIElementChangedNotification` on the node’s element (`post_accessibility_focus_changed()`). A page that calls `focus()` while the keyboard focus is elsewhere can’t pull VoiceOver into the page. Once the view gets the keyboard focus, Qt’s focus change has AppKit look up the page’s focused element.- `on_live_region_changed` → post `NSAccessibilityAnnouncementRequestedNotification` (`post_accessibility_announcement()`)`accessibilityFocusedUIElement` returns the focused DOM element when one is focused (e.g., an `<input>` the user clicked into) — and otherwise returns the `AXWebArea` (document root). That matches what Safari and Chrome also do. Returning a leaf (heading, link, etc.) instead causes VoiceOver to interpret the focus as “user already navigated to this element” — which makes it unexpectedly skip its read-from-document-beginning behavior.diff --git a/UI/Qt/WebContentViewAccessibility.mm b/UI/Qt/WebContentViewAccessibility.mmindex 3cbe6f8ecf4..5bf12015589 100644--- a/UI/Qt/WebContentViewAccessibility.mm+++ b/UI/Qt/WebContentViewAccessibility.mm@@ -8,6 +8,7 @@#include "WebContentView.h"#include <LibWebView/AccessibilityTreeManager.h>+#include <UI/Qt/MacWindow.h>#import <AppKit/AppKit.h>#import <objc/message.h>@@ -110,6 +111,19 @@ extern "C" void NSAccessibilityHandleFocusChanged();- (void)performAccessibilityAction:(NSString*)action forNodeID:(int64_t)nodeID{+ // An assistive technology that moves the focus into the page moves the keyboard focus there too, as a click does.+ // Otherwise, keys would go to the location editor, for example, while VoiceOver reports a text field in the page as+ // focused. VoiceOver also sets the focus on what its cursor moves to when the keyboard focus follows that cursor,+ // the AXWebArea first as the cursor enters the page. Leave the focus alone if the view or a child of it, such as+ // the crash overlay's buttons, already has it.+ auto& view = *_webContentView;+ auto* focus_widget = view.window()->focusWidget();+ auto focus_is_in_view = focus_widget && (focus_widget == &view || view.isAncestorOf(focus_widget));+ if ([action isEqualToString:@"focus"] && view.isVisible() && !focus_is_in_view) {+ Ladybird::make_appkit_window_first_responder(view);+ view.setFocus(Qt::OtherFocusReason);+ }+auto action_string = MUST(String::from_utf8(StringView { [action UTF8String], strlen([action UTF8String]) }));_webContentView->perform_accessibility_action(nodeID, AK::move(action_string));}@@ -434,6 +448,17 @@ void install_accessibility(WebContentView* view)[ns_view addSubview:overlay];}+// Whether the view holds the keyboard focus in the key window, which is when Chrome fires focus events too+// (RenderWidgetHostViewMac::AccessibilityHasFocus()). Only then may the page tell VoiceOver about its load or its+// focus: VoiceOver moves into the web content when it gets either, which would otherwise pull it out of the location+// editor of a new tab, or into a tab or window the user isn't in. And AppKit would re-post the focus of whatever else+// holds it. Qt's focus is the one to ask: the window's first responder is the top-level QNSView, whichever widget has+// the focus.+static bool view_holds_keyboard_focus(WebContentView* view, WebContentAccessibilityView* overlay)+{+ return overlay.window.isKeyWindow && view->hasFocus();+}+void update_accessibility_tree(WebContentView* view, bool announce_load, bool report_focus){auto* overlay = get_overlay(view);@@ -465,14 +490,7 @@ void update_accessibility_tree(WebContentView* view, bool announce_load, bool re// user is reading it.if (!announce_load && !report_focus)return;-- // And only while the view holds the keyboard focus in the key window, which is when Chrome fires focus events too- // (RenderWidgetHostViewMac::AccessibilityHasFocus()). VoiceOver moves into the web content when it receives- // AXLoadComplete, so posting it otherwise would pull VoiceOver out of the location editor of a new tab, or into a- // window the user isn't in. And AppKit would re-post the focus of whatever else holds it. Qt's focus is the one to- // ask: the window's first responder is the top-level QNSView, whichever widget has the focus.- NSWindow* window = ns_view ? ns_view.window : overlay.window;- if (!window || !window.isKeyWindow || !view->hasFocus())+ if (!view_holds_keyboard_focus(view, overlay))return;// Have AppKit look the focus up again now that there's a tree to find it in, as a focus change does: A loaded page@@ -489,6 +507,11 @@ void post_accessibility_focus_changed(WebContentView* view, i64 node_id)auto* overlay = get_overlay(view);if (!overlay)return;+ // A page can call focus() at any time, from a timer for example. Once the view gets the keyboard focus, Qt's focus+ // change has AppKit look up the page's focused element. An assistive technology's focus action gives the view the+ // keyboard focus first (see performAccessibilityAction:forNodeID:), so the focus change it causes gets posted.+ if (!view_holds_keyboard_focus(view, overlay))+ return;id element = [overlay accessibilityElementForNodeID:node_id];if (!element)