Change WebContentsViewMac bounds from window coordinates to superview coordinates
Moderate
Commit Hash:
5c0d21896b354329f40f0ca41c5c31169fb05c5b
Commit Time: 2025-11-14 09:31:53+00:00
Impact Level: Moderate
Generated By: webview2-upstream-sentry
Upstream Review:
View Upstream Review 🔗
📋 Summary
This commit modifies the WebContentsViewMac::ViewsHostableSetBounds method to accept coordinates relative to the superview rather than the window (NSWindow). The change includes the following core modifications:
1. **Interface Change**: Renamed the ViewsHostableView::ViewsHostableSetBounds method parameter from bounds_in_window to bounds_in_superview
2. **Mojo Interface Update**: Modified the remote_cocoa::mojom::WebContentsNSView::SetBounds interface, changing the parameter from bounds_in_window to bounds_in_superview, and updated the documentation comments
3. **Implementation Logic Refactoring**: In WebContentsNSViewBridge::SetBounds, coordinates are no longer calculated through NSWindow's contentRectForFrameRect; instead, the superview's coordinate system is used directly, with Y-axis handling based on the superview's isFlipped property
4. **Namespace Correction**: Corrected the namespace declaration in web_contents_ns_view_bridge.h from content to remote_cocoa
This change fixes an incorrect assumption: the previous implementation assumed that the superview of the WebContents's NSView was the root view of the window, but this is not always the case and could lead to incorrect layout.
1. **Interface Change**: Renamed the ViewsHostableView::ViewsHostableSetBounds method parameter from bounds_in_window to bounds_in_superview
2. **Mojo Interface Update**: Modified the remote_cocoa::mojom::WebContentsNSView::SetBounds interface, changing the parameter from bounds_in_window to bounds_in_superview, and updated the documentation comments
3. **Implementation Logic Refactoring**: In WebContentsNSViewBridge::SetBounds, coordinates are no longer calculated through NSWindow's contentRectForFrameRect; instead, the superview's coordinate system is used directly, with Y-axis handling based on the superview's isFlipped property
4. **Namespace Correction**: Corrected the namespace declaration in web_contents_ns_view_bridge.h from content to remote_cocoa
This change fixes an incorrect assumption: the previous implementation assumed that the superview of the WebContents's NSView was the root view of the window, but this is not always the case and could lead to incorrect layout.
🎯 Impact Analysis
This change has a **moderate** impact on WebView2Mac. While the change is an important correction to the coordinate system, the impact on downstream code is relatively manageable:
**Direct Impact:**
1. **HostingWebContentsNSViewBridge Inheritance**: WebView2Mac's HostingWebContentsNSViewBridge class inherits from remote_cocoa::WebContentsNSViewBridge. This change modifies the parameter semantics of the base class SetBounds method. Although the parameter type remains unchanged (still gfx::Rect), its meaning has shifted from window coordinates to superview coordinates, which may affect any WebView2Mac code that overrides or calls this method
2. **Coordinate Conversion Logic**: If WebView2Mac performs its own window-to-superview coordinate conversion when calling SetBounds, this conversion now becomes redundant and could lead to layout errors
3. **Mojo Interface Compatibility**: The semantic change to the remote_cocoa::mojom::WebContentsNSView::SetBounds interface may affect cross-process communication if WebView2Mac uses the remote Cocoa architecture
**Potential Benefits:**
1. **Layout Bug Fix**: This change fixes layout errors when the WebContents NSView's superview is not the window's root view, which may resolve certain layout issues in WebView2Mac
2. **Simplified Coordinate Calculation**: The new implementation uses the superview coordinate system more directly, reducing the complexity of indirect calculations through the window
3. **Flipped Coordinate Handling**: The new implementation correctly handles the isFlipped property, making layout more reliable across different coordinate systems
**Areas of Concern:**
1. If WebView2Mac overrides the ViewsHostableSetBounds method, ensure that the passed coordinates are already relative to the superview
2. Check if any code performs window-to-superview coordinate conversion before calling SetBounds; if so, this conversion should be removed
3. Verify that layout is correct in various view hierarchies, especially when WebView2 is embedded in a non-root view
**Direct Impact:**
1. **HostingWebContentsNSViewBridge Inheritance**: WebView2Mac's HostingWebContentsNSViewBridge class inherits from remote_cocoa::WebContentsNSViewBridge. This change modifies the parameter semantics of the base class SetBounds method. Although the parameter type remains unchanged (still gfx::Rect), its meaning has shifted from window coordinates to superview coordinates, which may affect any WebView2Mac code that overrides or calls this method
2. **Coordinate Conversion Logic**: If WebView2Mac performs its own window-to-superview coordinate conversion when calling SetBounds, this conversion now becomes redundant and could lead to layout errors
3. **Mojo Interface Compatibility**: The semantic change to the remote_cocoa::mojom::WebContentsNSView::SetBounds interface may affect cross-process communication if WebView2Mac uses the remote Cocoa architecture
**Potential Benefits:**
1. **Layout Bug Fix**: This change fixes layout errors when the WebContents NSView's superview is not the window's root view, which may resolve certain layout issues in WebView2Mac
2. **Simplified Coordinate Calculation**: The new implementation uses the superview coordinate system more directly, reducing the complexity of indirect calculations through the window
3. **Flipped Coordinate Handling**: The new implementation correctly handles the isFlipped property, making layout more reliable across different coordinate systems
**Areas of Concern:**
1. If WebView2Mac overrides the ViewsHostableSetBounds method, ensure that the passed coordinates are already relative to the superview
2. Check if any code performs window-to-superview coordinate conversion before calling SetBounds; if so, this conversion should be removed
3. Verify that layout is correct in various view hierarchies, especially when WebView2 is embedded in a non-root view
Impacted Classes:
remote_cocoa::WebContentsNSViewBridge
remote_cocoa::mojom::WebContentsNSView
remote_cocoa::mojom::WebContentsNSViewHost
ui::ViewsHostableView
content::WebContentsViewMac