Browse Source

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.
Cheng Zhao 7 years ago
parent
commit
5547df6073

+ 2 - 4
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<decltype(&SetFrameSize)>(
       [[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<AtomNSWindow*>(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());

+ 5 - 1
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);

+ 4 - 6
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

+ 3 - 1
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

+ 21 - 2
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())