From 28fc58067bc05f679cd00d2710ea1e825e6b1a0c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 2 May 2018 20:43:45 +0900 Subject: [PATCH] remove usage of FullSizeContentView --- atom/browser/native_window_mac.mm | 82 ++++++++++++++++--------- atom/browser/ui/cocoa/atom_ns_window.h | 3 +- atom/browser/ui/cocoa/atom_ns_window.mm | 4 ++ 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 8b055d29200b..56813877d7c6 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -5,6 +5,7 @@ #include "atom/browser/native_window_mac.h" #include +#include #include @@ -127,31 +128,6 @@ @end -// This view always takes the size of its superview. It is intended to be used -// as a NSWindow's contentView. It is needed because NSWindow's implementation -// explicitly resizes the contentView at inopportune times. -@interface FullSizeContentView : NSView -@end - -@implementation FullSizeContentView - -// This method is directly called by NSWindow during a window resize on OSX -// 10.10.0, beta 2. We must override it to prevent the content view from -// shrinking. -- (void)setFrameSize:(NSSize)size { - if ([self superview]) - size = [[self superview] bounds].size; - [super setFrameSize:size]; -} - -// The contentView gets moved around during certain full-screen operations. -// This is less than ideal, and should eventually be removed. -- (void)viewDidMoveToSuperview { - [self setFrame:[[self superview] bounds]]; -} - -@end - #if !defined(AVAILABLE_MAC_OS_X_VERSION_10_12_AND_LATER) enum { NSWindowTabbingModeDisallowed = 2 }; @@ -234,6 +210,48 @@ struct Converter { namespace atom { +namespace { + +bool IsFramelessWindow(NSView* view) { + NativeWindow* window = [static_cast([view window]) shell]; + return window && !window->has_frame(); +} + +IMP original_set_frame_size = nullptr; +IMP original_view_did_move_to_superview = nullptr; + +// This method is directly called by NSWindow during a window resize on OSX +// 10.10.0, beta 2. We must override it to prevent the content view from +// shrinking. +void SetFrameSize(NSView* self, SEL _cmd, NSSize size) { + if (!IsFramelessWindow(self)) { + auto original = + reinterpret_cast(original_set_frame_size); + return original(self, _cmd, size); + } + // For frameless window, resize the view to cover full window. + if ([self superview]) + size = [[self superview] bounds].size; + // [super setFrameSize:size]; + auto super_impl = reinterpret_cast( + [[self superclass] instanceMethodForSelector:_cmd]); + super_impl(self, _cmd, size); +} + +// The contentView gets moved around during certain full-screen operations. +// This is less than ideal, and should eventually be removed. +void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { + if (!IsFramelessWindow(self)) { + // [BridgedContentView viewDidMoveToSuperview]; + auto original = reinterpret_cast( + original_view_did_move_to_superview); + return original(self, _cmd); + } + [self setFrame:[[self superview] bounds]]; +} + +} // namespace + NativeWindowMac::NativeWindowMac(const mate::Dictionary& options, NativeWindow* parent) : NativeWindow(options, parent), @@ -450,11 +468,15 @@ NativeWindowMac::NativeWindowMac(const mate::Dictionary& options, // produces warnings. To eliminate the warnings, we resize the contentView // to fill the window, and add subviews to that. // http://crbug.com/380412 - container_view_.reset([[FullSizeContentView alloc] init]); - [container_view_ - setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; - [container_view_ setFrame:[[[window_ contentView] superview] bounds]]; - [window_ setContentView:container_view_]; + if (!original_set_frame_size) { + Class cl = [[window_ contentView] class]; + original_set_frame_size = class_replaceMethod( + cl, @selector(setFrameSize:), (IMP)SetFrameSize, "v@:{_NSSize=ff}"); + original_view_did_move_to_superview = + class_replaceMethod(cl, @selector(viewDidMoveToSuperview), + (IMP)ViewDidMoveToSuperview, "v@:"); + [[window_ contentView] viewDidMoveToWindow]; + } // The fullscreen button should always be hidden for frameless window. [[window_ standardWindowButton:NSWindowFullScreenButton] setHidden:YES]; diff --git a/atom/browser/ui/cocoa/atom_ns_window.h b/atom/browser/ui/cocoa/atom_ns_window.h index 885e19c6dcc4..19e6bfdf8090 100644 --- a/atom/browser/ui/cocoa/atom_ns_window.h +++ b/atom/browser/ui/cocoa/atom_ns_window.h @@ -36,8 +36,9 @@ class ScopedDisableResize { @property BOOL disableAutoHideCursor; @property BOOL disableKeyOrMainWindow; @property NSPoint windowButtonsOffset; -@property (nonatomic, retain) NSView* vibrantView; +@property(nonatomic, retain) NSView* vibrantView; - (void)setShell:(atom::NativeWindowMac*)shell; +- (atom::NativeWindowMac*)shell; - (void)enableWindowButtonsOffset; - (void)toggleFullScreenMode:(id)sender; @end diff --git a/atom/browser/ui/cocoa/atom_ns_window.mm b/atom/browser/ui/cocoa/atom_ns_window.mm index 386156f988a0..0042eb6adaeb 100644 --- a/atom/browser/ui/cocoa/atom_ns_window.mm +++ b/atom/browser/ui/cocoa/atom_ns_window.mm @@ -27,6 +27,10 @@ bool ScopedDisableResize::disable_resize_ = false; shell_ = shell; } +- (atom::NativeWindowMac*)shell { + return shell_; +} + - (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) { if (shell_->touch_bar()) return [shell_->touch_bar() makeTouchBar];