fix: compensate for title bar height when setting bounds on BrowserView (#34713)

fix: compensate for title bar height when setting bounds
This commit is contained in:
Shelley Vohr 2022-08-29 17:53:03 +02:00 committed by GitHub
parent d0e220cbce
commit 75f9573e53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 9 deletions

View file

@ -262,9 +262,21 @@ void NativeBrowserViewMac::SetBounds(const gfx::Rect& bounds) {
auto* view = iwc_view->GetNativeView().GetNativeNSView(); auto* view = iwc_view->GetNativeView().GetNativeNSView();
auto* superview = view.superview; auto* superview = view.superview;
const auto superview_height = superview ? superview.frame.size.height : 0; const auto superview_height = superview ? superview.frame.size.height : 0;
// We need to use the content rect to calculate the titlebar height if the
// superview is an framed NSWindow, otherwise it will be offset incorrectly by
// the height of the titlebar.
auto titlebar_height = 0;
if (auto* win = [superview window]) {
const auto content_rect_height =
[win contentRectForFrameRect:superview.frame].size.height;
titlebar_height = superview_height - content_rect_height;
}
auto new_height =
superview_height - bounds.y() - bounds.height() + titlebar_height;
view.frame = view.frame =
NSMakeRect(bounds.x(), superview_height - bounds.y() - bounds.height(), NSMakeRect(bounds.x(), new_height, bounds.width(), bounds.height());
bounds.width(), bounds.height());
// Ensure draggable regions are properly updated to reflect new bounds. // Ensure draggable regions are properly updated to reflect new bounds.
UpdateDraggableRegions(draggable_regions_); UpdateDraggableRegions(draggable_regions_);
@ -275,12 +287,23 @@ gfx::Rect NativeBrowserViewMac::GetBounds() {
if (!iwc_view) if (!iwc_view)
return gfx::Rect(); return gfx::Rect();
NSView* view = iwc_view->GetNativeView().GetNativeNSView(); NSView* view = iwc_view->GetNativeView().GetNativeNSView();
const int superview_height = auto* superview = view.superview;
(view.superview) ? view.superview.frame.size.height : 0; const int superview_height = superview ? superview.frame.size.height : 0;
return gfx::Rect(
view.frame.origin.x, // We need to use the content rect to calculate the titlebar height if the
superview_height - view.frame.origin.y - view.frame.size.height, // superview is an framed NSWindow, otherwise it will be offset incorrectly by
view.frame.size.width, view.frame.size.height); // the height of the titlebar.
auto titlebar_height = 0;
if (auto* win = [superview window]) {
const auto content_rect_height =
[win contentRectForFrameRect:superview.frame].size.height;
titlebar_height = superview_height - content_rect_height;
}
auto new_height = superview_height - view.frame.origin.y -
view.frame.size.height + titlebar_height;
return gfx::Rect(view.frame.origin.x, new_height, view.frame.size.width,
view.frame.size.height);
} }
void NativeBrowserViewMac::SetBackgroundColor(SkColor color) { void NativeBrowserViewMac::SetBackgroundColor(SkColor color) {

View file

@ -146,7 +146,14 @@ describe('BrowserView module', () => {
}); });
describe('BrowserView.getBounds()', () => { describe('BrowserView.getBounds()', () => {
it('returns the current bounds', () => { it('returns correct bounds on a framed window', () => {
view = new BrowserView();
const bounds = { x: 10, y: 20, width: 30, height: 40 };
view.setBounds(bounds);
expect(view.getBounds()).to.deep.equal(bounds);
});
it('returns correct bounds on a frameless window', () => {
view = new BrowserView(); view = new BrowserView();
const bounds = { x: 10, y: 20, width: 30, height: 40 }; const bounds = { x: 10, y: 20, width: 30, height: 40 };
view.setBounds(bounds); view.setBounds(bounds);