Reland "Don't allow NSWindow to steal focus from modal window on scrim click"
Moderate
Commit Hash:
03549f10a1c37ca13adbaf1c8d1195ad302cb88a
Commit Time: 2026-04-08 19:30:25
Impact Level: Moderate
Generated By: webview2-upstream-sentry
Upstream Review:
View Upstream Review 🔗
📋 Summary
This commit is a reland of commit c197df3, fixing a BOOL/bool type mismatch that caused an x64 compile failure in the original submission. The functionality is identical to the original:
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 a synchronous mojo call GetHitTestResult to determine if the click landed on the web contents area, and if so with a visible child modal window present, swallows the mouse event and redirects key window focus to the modal window.
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 a synchronous mojo call GetHitTestResult to determine if the click landed on the web contents area, and if so with a visible child modal window present, swallows the mouse event and redirects key window focus to the modal window.
🎯 Impact Analysis
This change has a moderate impact on WebView2Mac and warrants attention. 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. After integration, WebView2Mac's modal dialog focus behavior will benefit — when users click the web content behind a modal dialog, focus will correctly remain on the dialog.
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 does not have this accessor yet; it must be added upon integration.
4. **Edge's custom sendEvent: modifications**: Edge repo has custom NSEventTypeLeftMouseDown double-click handling in sendEvent: (guarded by BUILDFLAG(MICROSOFT_EDGE_BRANDING)), positioned before the right-click handling logic. The upstream modal window focus logic is placed after right-click handling and before the hasViewsMenuActive check. Careful merging of code placement is needed to ensure Edge's double-click handling and upstream's modal window handling don't interfere with each other.
5. **Successful reland**: Unlike the previously reverted c197df3, this version has fixed the compile issue and can be successfully integrated.
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. After integration, WebView2Mac's modal dialog focus behavior will benefit — when users click the web content behind a modal dialog, focus will correctly remain on the dialog.
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 does not have this accessor yet; it must be added upon integration.
4. **Edge's custom sendEvent: modifications**: Edge repo has custom NSEventTypeLeftMouseDown double-click handling in sendEvent: (guarded by BUILDFLAG(MICROSOFT_EDGE_BRANDING)), positioned before the right-click handling logic. The upstream modal window focus logic is placed after right-click handling and before the hasViewsMenuActive check. Careful merging of code placement is needed to ensure Edge's double-click handling and upstream's modal window handling don't interfere with each other.
5. **Successful reland**: Unlike the previously reverted c197df3, this version has fixed the compile issue and can be successfully integrated.
Impacted Classes:
remote_cocoa::NativeWidgetMacNSWindow
remote_cocoa::NativeWidgetNSWindowBridge