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.
This commit is contained in:
Cheng Zhao 2018-05-02 21:28:28 +09:00
parent 28fc58067b
commit 5547df6073
5 changed files with 35 additions and 14 deletions

View file

@ -232,7 +232,6 @@ void SetFrameSize(NSView* self, SEL _cmd, NSSize size) {
// For frameless window, resize the view to cover full window. // For frameless window, resize the view to cover full window.
if ([self superview]) if ([self superview])
size = [[self superview] bounds].size; size = [[self superview] bounds].size;
// [super setFrameSize:size];
auto super_impl = reinterpret_cast<decltype(&SetFrameSize)>( auto super_impl = reinterpret_cast<decltype(&SetFrameSize)>(
[[self superclass] instanceMethodForSelector:_cmd]); [[self superclass] instanceMethodForSelector:_cmd]);
super_impl(self, _cmd, size); super_impl(self, _cmd, size);
@ -332,11 +331,10 @@ NativeWindowMac::NativeWindowMac(const mate::Dictionary& options,
params.bounds = bounds; params.bounds = bounds;
params.delegate = this; params.delegate = this;
params.type = views::Widget::InitParams::TYPE_WINDOW; 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); widget()->Init(params);
window_ = static_cast<AtomNSWindow*>(widget()->GetNativeWindow()); window_ = static_cast<AtomNSWindow*>(widget()->GetNativeWindow());
[window_ setShell:this];
[window_ setEnableLargerThanScreen:enable_larger_than_screen()]; [window_ setEnableLargerThanScreen:enable_larger_than_screen()];
window_delegate_.reset([[AtomNSWindowDelegate alloc] initWithShell:this]); window_delegate_.reset([[AtomNSWindowDelegate alloc] initWithShell:this]);
@ -714,7 +712,7 @@ void NativeWindowMac::SetContentSizeConstraints(
// will result in actual content size being larger. // will result in actual content size being larger.
if (!has_frame()) { if (!has_frame()) {
NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
NSRect content = [window_ contentRectForFrameRect:frame]; NSRect content = [window_ originalContentRectForFrameRect:frame];
return content.size; return content.size;
} else { } else {
return NSMakeSize(size.width(), size.height()); return NSMakeSize(size.width(), size.height());

View file

@ -9,9 +9,12 @@
namespace atom { namespace atom {
class NativeWindowMac;
class AtomNativeWidgetMac : public views::NativeWidgetMac { class AtomNativeWidgetMac : public views::NativeWidgetMac {
public: public:
AtomNativeWidgetMac(NSUInteger style_mask, AtomNativeWidgetMac(NativeWindowMac* shell,
NSUInteger style_mask,
views::internal::NativeWidgetDelegate* delegate); views::internal::NativeWidgetDelegate* delegate);
~AtomNativeWidgetMac() override; ~AtomNativeWidgetMac() override;
@ -21,6 +24,7 @@ class AtomNativeWidgetMac : public views::NativeWidgetMac {
const views::Widget::InitParams& params) override; const views::Widget::InitParams& params) override;
private: private:
NativeWindowMac* shell_;
NSUInteger style_mask_; NSUInteger style_mask_;
DISALLOW_COPY_AND_ASSIGN(AtomNativeWidgetMac); DISALLOW_COPY_AND_ASSIGN(AtomNativeWidgetMac);

View file

@ -5,25 +5,23 @@
#include "atom/browser/ui/cocoa/atom_native_widget_mac.h" #include "atom/browser/ui/cocoa/atom_native_widget_mac.h"
#include "atom/browser/ui/cocoa/atom_ns_window.h" #include "atom/browser/ui/cocoa/atom_ns_window.h"
#include "ui/base/cocoa/window_size_constants.h"
namespace atom { namespace atom {
AtomNativeWidgetMac::AtomNativeWidgetMac( AtomNativeWidgetMac::AtomNativeWidgetMac(
NativeWindowMac* shell,
NSUInteger style_mask, NSUInteger style_mask,
views::internal::NativeWidgetDelegate* delegate) views::internal::NativeWidgetDelegate* delegate)
: views::NativeWidgetMac(delegate), : views::NativeWidgetMac(delegate),
shell_(shell),
style_mask_(style_mask) {} style_mask_(style_mask) {}
AtomNativeWidgetMac::~AtomNativeWidgetMac() {} AtomNativeWidgetMac::~AtomNativeWidgetMac() {}
NativeWidgetMacNSWindow* AtomNativeWidgetMac::CreateNSWindow( NativeWidgetMacNSWindow* AtomNativeWidgetMac::CreateNSWindow(
const views::Widget::InitParams& params) { const views::Widget::InitParams& params) {
return [[[AtomNSWindow alloc] return [[[AtomNSWindow alloc] initWithShell:shell_ styleMask:style_mask_]
initWithContentRect:ui::kWindowSizeDeterminedLater autorelease];
styleMask:style_mask_
backing:NSBackingStoreBuffered
defer:YES] autorelease];
} }
} // namespace atom } // namespace atom

View file

@ -37,8 +37,10 @@ class ScopedDisableResize {
@property BOOL disableKeyOrMainWindow; @property BOOL disableKeyOrMainWindow;
@property NSPoint windowButtonsOffset; @property NSPoint windowButtonsOffset;
@property(nonatomic, retain) NSView* vibrantView; @property(nonatomic, retain) NSView* vibrantView;
- (void)setShell:(atom::NativeWindowMac*)shell; - (id)initWithShell:(atom::NativeWindowMac*)shell
styleMask:(NSUInteger)styleMask;
- (atom::NativeWindowMac*)shell; - (atom::NativeWindowMac*)shell;
- (NSRect)originalContentRectForFrameRect:(NSRect)frameRect;
- (void)enableWindowButtonsOffset; - (void)enableWindowButtonsOffset;
- (void)toggleFullScreenMode:(id)sender; - (void)toggleFullScreenMode:(id)sender;
@end @end

View file

@ -7,6 +7,7 @@
#include "atom/browser/native_window_mac.h" #include "atom/browser/native_window_mac.h"
#include "atom/browser/ui/cocoa/atom_preview_item.h" #include "atom/browser/ui/cocoa/atom_preview_item.h"
#include "atom/browser/ui/cocoa/atom_touch_bar.h" #include "atom/browser/ui/cocoa/atom_touch_bar.h"
#include "ui/base/cocoa/window_size_constants.h"
namespace atom { namespace atom {
@ -23,14 +24,25 @@ bool ScopedDisableResize::disable_resize_ = false;
@synthesize windowButtonsOffset; @synthesize windowButtonsOffset;
@synthesize vibrantView; @synthesize vibrantView;
- (void)setShell:(atom::NativeWindowMac*)shell { - (id)initWithShell:(atom::NativeWindowMac*)shell
shell_ = shell; styleMask:(NSUInteger)styleMask {
if ((self = [super initWithContentRect:ui::kWindowSizeDeterminedLater
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:YES])) {
shell_ = shell;
}
return self;
} }
- (atom::NativeWindowMac*)shell { - (atom::NativeWindowMac*)shell {
return shell_; return shell_;
} }
- (NSRect)originalContentRectForFrameRect:(NSRect)frameRect {
return [super contentRectForFrameRect:frameRect];
}
- (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) { - (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) {
if (shell_->touch_bar()) if (shell_->touch_bar())
return [shell_->touch_bar() makeTouchBar]; 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 { - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen {
// Resizing is disabled. // Resizing is disabled.
if (atom::ScopedDisableResize::IsResizeDisabled()) if (atom::ScopedDisableResize::IsResizeDisabled())