Skip to content

Commit db13956

Browse files
zcbenzMarshallOfSound
authored andcommitted
fix: notify views of content view size change (electron#19888)
1 parent 5e33a5e commit db13956

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

atom/browser/native_window_mac.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
@class AtomPreviewItem;
2222
@class AtomTouchBar;
2323
@class CustomWindowButtonView;
24-
@class FullSizeContentView;
2524

2625
namespace atom {
2726

@@ -182,10 +181,12 @@ class NativeWindowMac : public NativeWindow {
182181
// Event monitor for scroll wheel event.
183182
id wheel_event_monitor_;
184183

185-
// The view that will fill the whole frameless window.
186-
base::scoped_nsobject<FullSizeContentView> container_view_;
184+
// The NSView that used as contentView of window.
185+
//
186+
// For frameless window it would fill the whole window.
187+
base::scoped_nsobject<NSView> container_view_;
187188

188-
// The view that fills the client area.
189+
// The views::View that fills the client area.
189190
std::unique_ptr<RootViewMac> root_view_;
190191

191192
bool is_kiosk_ = false;

atom/browser/native_window_mac.mm

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,59 @@
3131
#include "ui/gfx/skia_util.h"
3232
#include "ui/gl/gpu_switching_manager.h"
3333
#include "ui/views/background.h"
34+
#include "ui/views/cocoa/bridged_native_widget_host_impl.h"
3435
#include "ui/views/widget/widget.h"
3536

37+
// This view would inform Chromium to resize the hosted views::View.
38+
//
39+
// The overrided methods should behave the same with BridgedContentView.
40+
@interface ElectronAdapatedContentView : NSView {
41+
@private
42+
views::BridgedNativeWidgetHostImpl* bridge_host_;
43+
}
44+
@end
45+
46+
@implementation ElectronAdapatedContentView
47+
48+
- (id)initWithShell:(atom::NativeWindowMac*)shell {
49+
if ((self = [self init])) {
50+
bridge_host_ = views::BridgedNativeWidgetHostImpl::GetFromNativeWindow(
51+
shell->GetNativeWindow());
52+
}
53+
return self;
54+
}
55+
56+
- (void)viewDidMoveToWindow {
57+
// When this view is added to a window, AppKit calls setFrameSize before it is
58+
// added to the window, so the behavior in setFrameSize is not triggered.
59+
NSWindow* window = [self window];
60+
if (window)
61+
[self setFrameSize:NSZeroSize];
62+
}
63+
64+
- (void)setFrameSize:(NSSize)newSize {
65+
// The size passed in here does not always use
66+
// -[NSWindow contentRectForFrameRect]. The following ensures that the
67+
// contentView for a frameless window can extend over the titlebar of the new
68+
// window containing it, since AppKit requires a titlebar to give frameless
69+
// windows correct shadows and rounded corners.
70+
NSWindow* window = [self window];
71+
if (window && [window contentView] == self) {
72+
newSize = [window contentRectForFrameRect:[window frame]].size;
73+
// Ensure that the window geometry be updated on the host side before the
74+
// view size is updated.
75+
bridge_host_->bridge_impl()->UpdateWindowGeometry();
76+
}
77+
78+
[super setFrameSize:newSize];
79+
80+
// The OnViewSizeChanged is marked private in derived class.
81+
static_cast<remote_cocoa::mojom::BridgedNativeWidgetHost*>(bridge_host_)
82+
->OnViewSizeChanged(gfx::Size(newSize.width, newSize.height));
83+
}
84+
85+
@end
86+
3687
// This view always takes the size of its superview. It is intended to be used
3788
// as a NSWindow's contentView. It is needed because NSWindow's implementation
3889
// explicitly resizes the contentView at inopportune times.
@@ -1496,10 +1547,15 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
14961547
// `BridgedContentView` as content view, which does not support draggable
14971548
// regions. In order to make draggable regions work, we have to replace the
14981549
// content view with a simple NSView.
1499-
container_view_.reset([[FullSizeContentView alloc] init]);
1550+
if (has_frame()) {
1551+
container_view_.reset(
1552+
[[ElectronAdapatedContentView alloc] initWithShell:this]);
1553+
} else {
1554+
container_view_.reset([[FullSizeContentView alloc] init]);
1555+
[container_view_ setFrame:[[[window_ contentView] superview] bounds]];
1556+
}
15001557
[container_view_
15011558
setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
1502-
[container_view_ setFrame:[[[window_ contentView] superview] bounds]];
15031559
[window_ setContentView:container_view_];
15041560
AddContentViewLayers(IsMinimizable(), IsClosable());
15051561
}

0 commit comments

Comments
 (0)