Don't allow NSWindow to steal focus from modal window on scrim click
Moderate
Commit Hash:
c197df3fe6acfc9954f6da5b10984c84047d02c9
Commit Time: 2026-04-03 20:35:01
Impact Level: Moderate
Generated By: webview2-upstream-sentry
Upstream Review:
View Upstream Review 🔗
📋 Summary
This commit fixes an issue where clicking the web contents area behind a tab-modal dialog caused the parent browser window to become the key window, breaking keyboard navigation (Enter, Esc, Tab) of the dialog. Key changes include:
1. Added topmostVisibleChildModalWindow method to NativeWidgetMacNSWindow, which reverse-iterates NativeWidgetNSWindowBridge's child_windows to find the topmost visible modal child window.
2. Added modal_type() public accessor to NativeWidgetNSWindowBridge header, exposing the modal_type_ member.
3. Added NSEventTypeLeftMouseDown handling in sendEvent:: uses hit test to determine if the click landed on the web contents area (via synchronous mojo call GetHitTestResult), and if so with a visible child modal window present, swallows the mouse event and redirects key window focus to the modal window.
**Note: This commit was subsequently reverted by commit 75c644e (compile failure), but may be re-submitted in fixed form.**
1. Added topmostVisibleChildModalWindow method to NativeWidgetMacNSWindow, which reverse-iterates NativeWidgetNSWindowBridge's child_windows to find the topmost visible modal child window.
2. Added modal_type() public accessor to NativeWidgetNSWindowBridge header, exposing the modal_type_ member.
3. Added NSEventTypeLeftMouseDown handling in sendEvent:: uses hit test to determine if the click landed on the web contents area (via synchronous mojo call GetHitTestResult), and if so with a visible child modal window present, swallows the mouse event and redirects key window focus to the modal window.
**Note: This commit was subsequently reverted by commit 75c644e (compile failure), but may be re-submitted in fixed form.**
🎯 Impact Analysis
This change has a moderate impact on WebView2Mac, but since it was reverted, no immediate action is needed. Key analysis:
1. **sendEvent: event interception logic**: The new NSEventTypeLeftMouseDown interception is in NativeWidgetMacNSWindow's sendEvent:. WebView2Mac's embedded WebView doesn't use NativeWidgetMacNSWindow as its main window (it runs within the host app's window), but child windows created by WebView2Mac (JavaScript alert/confirm dialogs, find bar, etc.) use NativeWidgetMacNSWindow. If this fix is re-submitted, WebView2Mac's modal dialog focus behavior will benefit.
2. **Synchronous mojo call GetHitTestResult**: The sendEvent: handler uses a synchronous mojo call to determine click location, blocking the app shim process's main thread waiting for browser process response. For WebView2Mac's cross-process window scenarios, this may introduce additional latency. However, this call only triggers on left-click with a modal child window present, limiting its scope.
3. **modal_type() accessor addition**: NativeWidgetNSWindowBridge's new modal_type() public method exposes the existing modal_type_ member. Edge repo has child_windows() accessor but not modal_type() yet. If this fix is re-submitted and merged into Edge, this accessor must be added.
4. **Edge's custom sendEvent: modifications**: Edge repo has custom NSEventTypeLeftMouseDown double-click handling in sendEvent: (guarded by BUILDFLAG(MICROSOFT_EDGE_BRANDING)), in the same method but handling different event conditions. If this fix is re-submitted, careful merging of code placement is needed.
5. **Already reverted**: Reverted by 75c644e due to compile failure; upstream code currently does not contain this change. Track subsequent fixed versions.
1. **sendEvent: event interception logic**: The new NSEventTypeLeftMouseDown interception is in NativeWidgetMacNSWindow's sendEvent:. WebView2Mac's embedded WebView doesn't use NativeWidgetMacNSWindow as its main window (it runs within the host app's window), but child windows created by WebView2Mac (JavaScript alert/confirm dialogs, find bar, etc.) use NativeWidgetMacNSWindow. If this fix is re-submitted, WebView2Mac's modal dialog focus behavior will benefit.
2. **Synchronous mojo call GetHitTestResult**: The sendEvent: handler uses a synchronous mojo call to determine click location, blocking the app shim process's main thread waiting for browser process response. For WebView2Mac's cross-process window scenarios, this may introduce additional latency. However, this call only triggers on left-click with a modal child window present, limiting its scope.
3. **modal_type() accessor addition**: NativeWidgetNSWindowBridge's new modal_type() public method exposes the existing modal_type_ member. Edge repo has child_windows() accessor but not modal_type() yet. If this fix is re-submitted and merged into Edge, this accessor must be added.
4. **Edge's custom sendEvent: modifications**: Edge repo has custom NSEventTypeLeftMouseDown double-click handling in sendEvent: (guarded by BUILDFLAG(MICROSOFT_EDGE_BRANDING)), in the same method but handling different event conditions. If this fix is re-submitted, careful merging of code placement is needed.
5. **Already reverted**: Reverted by 75c644e due to compile failure; upstream code currently does not contain this change. Track subsequent fixed versions.
Impacted Classes:
remote_cocoa::NativeWidgetMacNSWindow
remote_cocoa::NativeWidgetNSWindowBridge