Truly independent web browser
UI/Qt: Don't move the keyboard focus to a page when it loads on macOS
Andreas Klingede07ddd61ec
diff --git a/Documentation/ScreenReaderSupport.md b/Documentation/ScreenReaderSupport.mdindex 9ba7663b2d9..93864609186 100644--- a/Documentation/ScreenReaderSupport.md+++ b/Documentation/ScreenReaderSupport.md@@ -276,8 +276,8 @@ Until that first request, WebContent reports nothing: `Page::accessibility_inter`WebContentView` (`UI/Qt/WebContentView.cpp`) owns the `AccessibilityTreeManager`, and `WebContentViewAccessibility.mm` hosts the wrapper objects: its `WebContentAccessibilityView` overlay acts as the scroll-area container, with a single child, the document root `LadybirdAccessibilityElement`. The wiring:- 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 take the first responder.-- `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, it makes the widget’s NSView the window’s first responder, so AppKit resolves `accessibilityFocusedUIElement` through it (and thus into the `AXWebArea`); posts `@"AXLoadComplete"` (matching what Chromium and Firefox do); and calls `NSAccessibilityHandleFocusChanged()` — so AppKit refreshes its cached focused-UI-element. 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.+- 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_live_region_changed` → post `NSAccessibilityAnnouncementRequestedNotification` (`post_accessibility_announcement()`)@@ -741,7 +741,7 @@ Key files: `platforms/macos/src/node.rs` (role mapping, attributes, actions), `p5. **Element lifecycle.** HashMap cache with on-demand creation, proper cleanup, and `UIElementDestroyedNotification` on drop.-6. **Focus forwarding.** Patches NSWindow to forward `accessibilityFocusedUIElement` to the content view — solving the same initial-focus problem we solved with `makeFirstResponder`.+6. **Focus forwarding.** Patches NSWindow to forward `accessibilityFocusedUIElement` to the content view, solving the same initial-focus problem we solve with the `accessibilityFocusedUIElement` swizzle on `QNSView`.7. **Cross-platform.** A single tree representation serves macOS, Linux (AT-SPI2), and Windows (UIA). The Linux adapter would give us AT-SPI2 support without relying on Qt’s bridge.</details>diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex 1113a45b083..fa34c1fb091 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -194,7 +194,7 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTrave// Reset the document-root focus gate so that *each* page navigation posts a fresh accessibility focus event on// the newly-loaded document. Otherwise, without this reset, only the *first* page load (often an empty default// page) would trigger the gate — and subsequent user-initiated navigations (type URL, Enter) would never let- // Orca know that a new document is ready for browse-mode navigation. macOS gates its initial setFocus() on the+ // Orca know that a new document is ready for browse-mode navigation. macOS gates its load announcement on the// same flag, so it needs the per-navigation reset too.m_posted_initial_accessibility_focus = false;m_report_accessibility_focus_with_next_tree = false;@@ -213,22 +213,21 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTravem_accessibility_manager->update_tree(AK::move(nodes));#if defined(Q_OS_MACOS)- // Only the first tree for a page may take focus. WebContent rebuilds the tree on DOM mutations too, so- // doing this on every update would yank focus out of the address bar mid-keystroke. Same reasoning, and- // the same gate, as the AT-SPI focus post in the branch below.- auto const take_initial_focus = !m_posted_initial_accessibility_focus && m_accessibility_manager && !m_accessibility_manager->is_empty();- if (take_initial_focus)+ // Only the first tree for a page announces the load. WebContent rebuilds the tree on DOM mutations too, and+ // announcing each of those would move VoiceOver's cursor back to the top of the page while the user is reading+ // it. Same gate as the AT-SPI focus post in the branch below.+ // NB: A page load never moves the keyboard focus, with or without an assistive technology. For example, a new+ // tab's location editor keeps the focus while the new tab page loads.+ auto const announce_load = !m_posted_initial_accessibility_focus && m_accessibility_manager && !m_accessibility_manager->is_empty();+ if (announce_load)m_posted_initial_accessibility_focus = true;// An assistive technology that turned up mid-page got no focused element from its first queries: The first tree// has AppKit look the focus up again — if the page has the keyboard focus (update_accessibility_tree() checks).auto const report_focus = m_report_accessibility_focus_with_next_tree && m_accessibility_manager && !m_accessibility_manager->is_empty();if (report_focus)m_report_accessibility_focus_with_next_tree = false;- QTimer::singleShot(100, this, [this, take_initial_focus, report_focus] {- auto const focus_now = take_initial_focus && isVisible();- if (focus_now)- setFocus(Qt::OtherFocusReason);- update_accessibility_tree(this, focus_now, report_focus);+ QTimer::singleShot(100, this, [this, announce_load, report_focus] {+ update_accessibility_tree(this, announce_load, report_focus);});#elif !defined(Q_OS_WIN)// Prune interfaces for nodes no longer in the tree.@@ -1745,9 +1744,10 @@ void WebContentView::accessibilityActiveChanged(bool active){if (!active || m_requested_accessibility_tree)return;- // An assistive technology turned up after this page's load finished, or during the load: Fetch the tree without the- // focus grab and the load announcement that only a page load gets. A page that's still loading gets both when its- // load finishes, as usual. For a page that's already loaded, its first tree reports where the focus is instead.+ // An assistive technology turned up after this page's load finished, or during the load: Fetch the tree without+ // what only a page load gets, the document-root focus event on Linux and the load announcement on macOS. A page+ // that's still loading gets it when its load finishes, as usual. For a page that's already loaded, its first tree+ // reports where the focus is instead.m_posted_initial_accessibility_focus = true;m_report_accessibility_focus_with_next_tree = !is_loading();fetch_accessibility_tree();diff --git a/UI/Qt/WebContentViewAccessibility.h b/UI/Qt/WebContentViewAccessibility.hindex 82be3cffbad..568c64be025 100644--- a/UI/Qt/WebContentViewAccessibility.h+++ b/UI/Qt/WebContentViewAccessibility.h@@ -18,10 +18,10 @@ class WebContentView;// Attaches LadybirdAccessibilityElement objects to the QWidget's underlying NSView — bypassing Qt's Cocoa accessibility// bridge entirely.void install_accessibility(WebContentView* view);-// take_initial_focus: a page's first tree makes the view the window's first responder and announces the load; the-// trees DOM mutations bring leave both alone. report_focus: the first tree an assistive technology that arrived-// mid-page gets has AppKit look the focus up again, if the view holds it.-void update_accessibility_tree(WebContentView* view, bool take_initial_focus, bool report_focus);+// announce_load: a page's first tree announces the load and has AppKit look the focus up again; the trees DOM mutations+// bring do neither. report_focus: the first tree an assistive technology that arrived mid-page gets has AppKit look the+// focus up again. Both only while the view holds the keyboard focus in the key window.+void update_accessibility_tree(WebContentView* view, bool announce_load, bool report_focus);void post_accessibility_focus_changed(WebContentView* view, i64 node_id);void post_accessibility_announcement(String const& text, String const& live_value);diff --git a/UI/Qt/WebContentViewAccessibility.mm b/UI/Qt/WebContentViewAccessibility.mmindex 5d6039142d0..3cbe6f8ecf4 100644--- a/UI/Qt/WebContentViewAccessibility.mm+++ b/UI/Qt/WebContentViewAccessibility.mm@@ -434,7 +434,7 @@ void install_accessibility(WebContentView* view)[ns_view addSubview:overlay];}-void update_accessibility_tree(WebContentView* view, bool take_initial_focus, bool report_focus)+void update_accessibility_tree(WebContentView* view, bool announce_load, bool report_focus){auto* overlay = get_overlay(view);if (!overlay)@@ -460,27 +460,28 @@ void update_accessibility_tree(WebContentView* view, bool take_initial_focus, bo// back to the AXWebArea inside it, announcing a "frame" — and by setting AXFocused there, which takes the focus// from whatever element in the page had it.- // Only a page's first tree takes the window's first responder and announces the load — the gate WebContentView- // puts on its setFocus(), for the same reason: WebContent rebuilds the tree on DOM mutations too, and doing this on- // each of those would yank keyboard focus out of the address bar mid-keystroke, and VoiceOver's cursor back to the- // top of the page while the user is reading it.- if (take_initial_focus) {- if (ns_view && ns_view.window)- [ns_view.window makeFirstResponder:ns_view];- NSAccessibilityPostNotification(NSAccessibilityUnignoredAncestor(ns_view ?: (NSView*)overlay),- @"AXLoadComplete");- NSAccessibilityHandleFocusChanged();+ // The rest is for a page's first tree only, the gate WebContentView puts on it: WebContent rebuilds the tree on DOM+ // mutations too, and announcing each of those would move VoiceOver's cursor back to the top of the page while the+ // user is reading it.+ if (!announce_load && !report_focus)return;- }- // An assistive technology that arrived mid-page got no focused element from its first queries: Have AppKit look the- // focus up again now that there's a tree to find it in — as a focus change does. But only while the view holds the- // keyboard focus in the key window, which is when Chrome fires focus events too (RenderWidgetHostViewMac::- // AccessibilityHasFocus()); otherwise, 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.+ // 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 (report_focus && window && window.isKeyWindow && view->hasFocus())- NSAccessibilityHandleFocusChanged();+ if (!window || !window.isKeyWindow || !view->hasFocus())+ 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+ // has a new focused element, and an assistive technology that arrived mid-page got no focused element from its+ // first queries.+ if (announce_load)+ NSAccessibilityPostNotification(NSAccessibilityUnignoredAncestor(ns_view ?: (NSView*)overlay),+ @"AXLoadComplete");+ NSAccessibilityHandleFocusChanged();}void post_accessibility_focus_changed(WebContentView* view, i64 node_id)