Fix window focus issue by deferring windowDidResignKey processing

Commit: c9401b77 | 2025-12-03 01:50:39+00:00

← Back to List

Fix window focus issue by deferring windowDidResignKey processing

Moderate
Commit Hash: c9401b77185b239d12ceb6cdb5edb1b8f6fe5437
Commit Time: 2025-12-03 01:50:39+00:00
Impact Level: Moderate
Generated By: webview2-upstream-sentry
Upstream Review: View Upstream Review 🔗

📋 Summary

This commit introduces a deferred notification mechanism in render_widget_host_view_cocoa.mm to handle edge cases in window focus state changes. The implementation includes: adding a class-level weak reference property deferredResignKeyWindow to track windows that receive windowDidResignKey notification while still being the key window; modifying the windowDidResignKey: method to defer processing when the window is still key and the application is active; adding logic in applicationDidResignActive: and windowDidBecomeKey: methods to ensure deferred resign key events are processed at appropriate times. This mechanism fixes an issue where web content may not maintain correct focus state when switching windows via clicking notification items.

🎯 Impact Analysis

This change has a moderate impact on WebView2Mac. Although the modification is to the underlying focus handling logic in the remote_cocoa layer, WebView2Mac uses the functionality of RenderWidgetHostViewCocoa through HostingWebContentsNSViewBridge and HostingNSWindowBridge inheritance, and will therefore be affected by this change.

Positive impacts:
1. Improved window focus management: Fixes incorrect web content focus state in specific scenarios (switching windows via notifications), which will improve the focus behavior of WebView2Mac controls within host applications.
2. More precise event handling: The deferral mechanism ensures that related events are triggered only when the window truly loses focus, reducing the possibility of false triggers.
3. Better user experience: Especially in multi-window scenarios, web content embedded in WebView2 can more accurately reflect the actual focus state.

Potential risks:
1. Focus event timing changes: Deferring windowDidResignKey event processing may alter the timing of certain focus-related events. If WebView2Mac host applications rely on specific event sequences, they may need to adapt to this change.
2. Edge case compatibility: In some complex window management scenarios (such as multiple WebView2 windows existing simultaneously), the deferral mechanism may introduce new edge cases.
3. IME and text input: Changes in focus state affect Input Method Editor (IME) behavior, particularly for composed text handling. It's necessary to verify that input methods for Chinese, Japanese, etc., behave correctly during window switches.

Recommended test scenarios:
- Test focus behavior during window switching in multi-window WebView2Mac applications
- Test focus state when switching windows via system notifications
- Verify IME state transitions when windows lose and gain focus
- Test whether the closeOnDeactivate feature still works properly

Impacted Classes:

RenderWidgetHostViewCocoa remote_cocoa::WebContentsNSViewBridge (间接影响/indirectly impacted)