From 5547df60731addfcbb62b9d414d6171ae3b4804f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 2 May 2018 21:28:28 +0900 Subject: [PATCH] report correct content size in AtomNSWindow The views framework relies on NSWindow to return content size of window, since we don't use the borderless window, the original result would include titlebar. We have to override the function to return correct result for frameless window. --- atom/browser/native_window_mac.mm | 6 ++--- .../browser/ui/cocoa/atom_native_widget_mac.h | 6 ++++- .../ui/cocoa/atom_native_widget_mac.mm | 10 ++++---- atom/browser/ui/cocoa/atom_ns_window.h | 4 +++- atom/browser/ui/cocoa/atom_ns_window.mm | 23 +++++++++++++++++-- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 56813877d7..4a8a6fba01 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -232,7 +232,6 @@ void SetFrameSize(NSView* self, SEL _cmd, NSSize 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); @@ -332,11 +331,10 @@ NativeWindowMac::NativeWindowMac(const mate::Dictionary& options, params.bounds = bounds; params.delegate = this; params.type = views::Widget::InitParams::TYPE_WINDOW; - params.native_widget = new AtomNativeWidgetMac(styleMask, widget()); + params.native_widget = new AtomNativeWidgetMac(this, styleMask, widget()); widget()->Init(params); window_ = static_cast(widget()->GetNativeWindow()); - [window_ setShell:this]; [window_ setEnableLargerThanScreen:enable_larger_than_screen()]; window_delegate_.reset([[AtomNSWindowDelegate alloc] initWithShell:this]); @@ -714,7 +712,7 @@ void NativeWindowMac::SetContentSizeConstraints( // will result in actual content size being larger. if (!has_frame()) { NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); - NSRect content = [window_ contentRectForFrameRect:frame]; + NSRect content = [window_ originalContentRectForFrameRect:frame]; return content.size; } else { return NSMakeSize(size.width(), size.height()); diff --git a/atom/browser/ui/cocoa/atom_native_widget_mac.h b/atom/browser/ui/cocoa/atom_native_widget_mac.h index e5f711e80b..c41d102fcd 100644 --- a/atom/browser/ui/cocoa/atom_native_widget_mac.h +++ b/atom/browser/ui/cocoa/atom_native_widget_mac.h @@ -9,9 +9,12 @@ namespace atom { +class NativeWindowMac; + class AtomNativeWidgetMac : public views::NativeWidgetMac { public: - AtomNativeWidgetMac(NSUInteger style_mask, + AtomNativeWidgetMac(NativeWindowMac* shell, + NSUInteger style_mask, views::internal::NativeWidgetDelegate* delegate); ~AtomNativeWidgetMac() override; @@ -21,6 +24,7 @@ class AtomNativeWidgetMac : public views::NativeWidgetMac { const views::Widget::InitParams& params) override; private: + NativeWindowMac* shell_; NSUInteger style_mask_; DISALLOW_COPY_AND_ASSIGN(AtomNativeWidgetMac); diff --git a/atom/browser/ui/cocoa/atom_native_widget_mac.mm b/atom/browser/ui/cocoa/atom_native_widget_mac.mm index 9cb5da48bc..0eccfb093e 100644 --- a/atom/browser/ui/cocoa/atom_native_widget_mac.mm +++ b/atom/browser/ui/cocoa/atom_native_widget_mac.mm @@ -5,25 +5,23 @@ #include "atom/browser/ui/cocoa/atom_native_widget_mac.h" #include "atom/browser/ui/cocoa/atom_ns_window.h" -#include "ui/base/cocoa/window_size_constants.h" namespace atom { AtomNativeWidgetMac::AtomNativeWidgetMac( + NativeWindowMac* shell, NSUInteger style_mask, views::internal::NativeWidgetDelegate* delegate) : views::NativeWidgetMac(delegate), + shell_(shell), style_mask_(style_mask) {} AtomNativeWidgetMac::~AtomNativeWidgetMac() {} NativeWidgetMacNSWindow* AtomNativeWidgetMac::CreateNSWindow( const views::Widget::InitParams& params) { - return [[[AtomNSWindow alloc] - initWithContentRect:ui::kWindowSizeDeterminedLater - styleMask:style_mask_ - backing:NSBackingStoreBuffered - defer:YES] autorelease]; + return [[[AtomNSWindow alloc] initWithShell:shell_ styleMask:style_mask_] + autorelease]; } } // namespace atom diff --git a/atom/browser/ui/cocoa/atom_ns_window.h b/atom/browser/ui/cocoa/atom_ns_window.h index 19e6bfdf80..7fd8f2a9a2 100644 --- a/atom/browser/ui/cocoa/atom_ns_window.h +++ b/atom/browser/ui/cocoa/atom_ns_window.h @@ -37,8 +37,10 @@ class ScopedDisableResize { @property BOOL disableKeyOrMainWindow; @property NSPoint windowButtonsOffset; @property(nonatomic, retain) NSView* vibrantView; -- (void)setShell:(atom::NativeWindowMac*)shell; +- (id)initWithShell:(atom::NativeWindowMac*)shell + styleMask:(NSUInteger)styleMask; - (atom::NativeWindowMac*)shell; +- (NSRect)originalContentRectForFrameRect:(NSRect)frameRect; - (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 0042eb6ada..56ed5dc231 100644 --- a/atom/browser/ui/cocoa/atom_ns_window.mm +++ b/atom/browser/ui/cocoa/atom_ns_window.mm @@ -7,6 +7,7 @@ #include "atom/browser/native_window_mac.h" #include "atom/browser/ui/cocoa/atom_preview_item.h" #include "atom/browser/ui/cocoa/atom_touch_bar.h" +#include "ui/base/cocoa/window_size_constants.h" namespace atom { @@ -23,14 +24,25 @@ bool ScopedDisableResize::disable_resize_ = false; @synthesize windowButtonsOffset; @synthesize vibrantView; -- (void)setShell:(atom::NativeWindowMac*)shell { - shell_ = shell; +- (id)initWithShell:(atom::NativeWindowMac*)shell + styleMask:(NSUInteger)styleMask { + if ((self = [super initWithContentRect:ui::kWindowSizeDeterminedLater + styleMask:styleMask + backing:NSBackingStoreBuffered + defer:YES])) { + shell_ = shell; + } + return self; } - (atom::NativeWindowMac*)shell { return shell_; } +- (NSRect)originalContentRectForFrameRect:(NSRect)frameRect { + return [super contentRectForFrameRect:frameRect]; +} + - (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) { if (shell_->touch_bar()) return [shell_->touch_bar() makeTouchBar]; @@ -52,6 +64,13 @@ bool ScopedDisableResize::disable_resize_ = false; } } +- (NSRect)contentRectForFrameRect:(NSRect)frameRect { + if (shell_->has_frame()) + return [super contentRectForFrameRect:frameRect]; + else + return frameRect; +} + - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen { // Resizing is disabled. if (atom::ScopedDisableResize::IsResizeDisabled())