Reland "Use property isKeyWindow for key window condition check"
Moderate
Commit Hash:
20ea6a5784da7b5d9d21a3a7083ea1932643635c
Commit Time: 2025-11-21 17:15:24+00:00
Impact Level: Moderate
Generated By: webview2-upstream-sentry
Upstream Review:
View Upstream Review 🔗
📋 Summary
This commit relands a previously reverted fix that changes the key window status check in the windowDidResignKey method from
[NSApp keyWindow] == [self window] to [self window].isKeyWindow. This fix addresses a focus state synchronization issue in multi-window applications: when a user clicks a notification, at the moment the window receives the windowDidResignKey notification, [NSApp keyWindow] may still temporarily point to that window, causing an incorrect early return and leaving WebContents and Blink layers thinking they still have focus, making JavaScript document.hasFocus() incorrectly return true. Using the window.isKeyWindow property correctly detects when the window has actually resigned key status, as this property is updated before windowDidResignKey is called. This fix was previously reverted for breaking 🎯 Impact Analysis
This change has **Moderate impact** on WebView2Mac. The fix improves the accuracy of window focus state detection, which is critical for proper focus management in WebView2Mac embedding scenarios.
**Positive Impacts:**
1. **Focus State Synchronization Improvement**: Using the isKeyWindow property provides more accurate detection of when a window resigns key status, avoiding timing issues that may exist with [NSApp keyWindow]
2. **JavaScript API Accuracy**: Ensures that document.hasFocus() correctly returns false after the host window loses focus, which is crucial for web applications that depend on focus state
3. **Multi-Window Scenario Optimization**: For WebView2Mac host applications with multiple windows, this fix more reliably handles focus transitions between windows
**Potential Risks:**
1. **Behavior Change**: Although the original revert was due to a developer configuration issue, it's still necessary to verify that context menus, dropdown menus, and other UI elements in WebView2Mac behave correctly during window focus changes
2. **Event Timing Dependencies**: If the WebView2Mac host application or custom implementations rely on the timing of the old detection logic, adjustments may be needed
**Recommended Test Scenarios:**
- Verify focus transitions between multiple WebView2Mac windows
- Test scenarios where notifications, system dialogs, etc. cause the window to lose focus
- Verify the behavior of select dropdowns, context menus, etc. during window focus changes
- Test the return value of JavaScript document.hasFocus() API in various focus change scenarios
**Positive Impacts:**
1. **Focus State Synchronization Improvement**: Using the isKeyWindow property provides more accurate detection of when a window resigns key status, avoiding timing issues that may exist with [NSApp keyWindow]
2. **JavaScript API Accuracy**: Ensures that document.hasFocus() correctly returns false after the host window loses focus, which is crucial for web applications that depend on focus state
3. **Multi-Window Scenario Optimization**: For WebView2Mac host applications with multiple windows, this fix more reliably handles focus transitions between windows
**Potential Risks:**
1. **Behavior Change**: Although the original revert was due to a developer configuration issue, it's still necessary to verify that context menus, dropdown menus, and other UI elements in WebView2Mac behave correctly during window focus changes
2. **Event Timing Dependencies**: If the WebView2Mac host application or custom implementations rely on the timing of the old detection logic, adjustments may be needed
**Recommended Test Scenarios:**
- Verify focus transitions between multiple WebView2Mac windows
- Test scenarios where notifications, system dialogs, etc. cause the window to lose focus
- Verify the behavior of select dropdowns, context menus, etc. during window focus changes
- Test the return value of JavaScript document.hasFocus() API in various focus change scenarios
Impacted Classes:
RenderWidgetHostViewCocoa
remote_cocoa::mojom::RenderWidgetHostNSViewHost