Center child window relative to parent instead of screen

提交: 00d09a69 | 2025-12-05 02:30:01+00:00

← 返回列表

Center child window relative to parent instead of screen

Moderate
提交哈希: 00d09a69b7da3b3be1be4403687a985c04b3919c
提交时间: 2025-12-05 02:30:01+00:00
影响等级: Moderate
生成工具: webview2-upstream-sentry
上游审核链接: 查看上游审核 🔗

📋 摘要

This commit modifies the NativeWidgetNSWindowBridge::SetSizeAndCenter() method to implement a long-standing TODO: center child windows relative to their parent window instead of the screen when a parent window exists. Key changes include:
1. Added parent window detection logic in SetSizeAndCenter()
2. When no parent window exists, maintains original behavior by calling [window_ center] to center on screen
3. When a parent window exists, calculates center position based on the parent window's frame
4. Clamps the calculated position to the visible area of the screen where the parent window resides, avoiding obstruction by menu bar, dock, or screen edges
5. Uses the parent window's screen to properly support multi-monitor setups
6. Added unit test CenterWindowWithParent to verify the new functionality

🎯 影响分析

The impact of this change on WebView2Mac is **Moderate**. Although it modifies a core window management class NativeWidgetNSWindowBridge, the impact is relatively controlled:

**Direct Impact:**
1. WebView2Mac inherits NativeWidgetNSWindowBridge through HostingNSWindowBridge, so it will automatically inherit this behavior change
2. If WebView2Mac uses the SetSizeAndCenter() method to position dialogs or child windows with a parent window, these windows' centering behavior will change
3. HostingNSWindowBridge does not override SetSizeAndCenter(), so it will directly use the modified implementation

**Potential Impact:**
1. **Dialog Positioning**: If the WebView2Mac host application displays modal dialogs or child windows using this method, dialogs will now be centered relative to the parent WebView2 window instead of the screen
2. **Improved Multi-monitor Support**: In multi-monitor setups, child windows will now be positioned more intelligently on the same display as the parent window
3. **Boundary Handling**: The newly added screen boundary clamping logic may affect window positioning behavior at screen edges

**Low Risk Factors:**
1. This is a behavior improvement rather than a breaking change, aligning with user intuition for child window positioning
2. Only affects code paths that call SetSizeAndCenter(), not other window positioning methods
3. The original no-parent-window scenario remains unchanged

**Recommended Actions:**
- Test all scenarios in WebView2Mac that use dialogs and child windows
- Verify window positioning in multi-monitor environments
- Check behavior when displaying windows at screen edges

受影响的类:

remote_cocoa::NativeWidgetNSWindowBridge NativeWidgetNSWindowBridge::SetSizeAndCenter