Browse Source

Move AtomNSWindow to a new file

Cheng Zhao 7 years ago
parent
commit
c44279bb10

+ 1 - 243
atom/browser/native_window_mac.mm

@@ -9,6 +9,7 @@
 #include <string>
 
 #include "atom/browser/native_browser_view_mac.h"
+#include "atom/browser/ui/cocoa/atom_ns_window.h"
 #include "atom/browser/ui/cocoa/atom_ns_window_delegate.h"
 #include "atom/browser/ui/cocoa/atom_preview_item.h"
 #include "atom/browser/ui/cocoa/atom_touch_bar.h"
@@ -19,7 +20,6 @@
 #include "base/strings/sys_string_conversions.h"
 #include "brightray/browser/inspectable_web_contents.h"
 #include "brightray/browser/inspectable_web_contents_view.h"
-#include "brightray/browser/mac/event_dispatching_window.h"
 #include "content/public/browser/browser_accessibility_state.h"
 #include "native_mate/dictionary.h"
 #include "skia/ext/skia_utils_mac.h"
@@ -27,24 +27,6 @@
 #include "ui/gl/gpu_switching_manager.h"
 #include "ui/views/widget/widget.h"
 
-namespace {
-
-// Prevents window from resizing during the scope.
-class ScopedDisableResize {
- public:
-  ScopedDisableResize() { disable_resize_ = true; }
-  ~ScopedDisableResize() { disable_resize_ = false; }
-
-  static bool IsResizeDisabled() { return disable_resize_; }
-
- private:
-  static bool disable_resize_;
-};
-
-bool ScopedDisableResize::disable_resize_ = false;
-
-}  // namespace
-
 // Custom Quit, Minimize and Full Screen button container for frameless
 // windows.
 @interface CustomWindowButtonView : NSView {
@@ -185,230 +167,6 @@ enum {
 
 #endif
 
-@interface AtomNSWindow : EventDispatchingWindow {
- @private
-  atom::NativeWindowMac* shell_;
-  bool enable_larger_than_screen_;
-  CGFloat windowButtonsInterButtonSpacing_;
-}
-@property BOOL acceptsFirstMouse;
-@property BOOL disableAutoHideCursor;
-@property BOOL disableKeyOrMainWindow;
-@property NSPoint windowButtonsOffset;
-@property (nonatomic, retain) NSView* vibrantView;
-
-- (void)setShell:(atom::NativeWindowMac*)shell;
-- (void)setEnableLargerThanScreen:(bool)enable;
-- (void)enableWindowButtonsOffset;
-
-@end
-
-@implementation AtomNSWindow
-
-@synthesize acceptsFirstMouse;
-@synthesize disableAutoHideCursor;
-@synthesize disableKeyOrMainWindow;
-@synthesize windowButtonsOffset;
-@synthesize quickLookItem;
-@synthesize vibrantView;
-
-- (void)setShell:(atom::NativeWindowMac*)shell {
-  shell_ = shell;
-}
-
-- (void)setEnableLargerThanScreen:(bool)enable {
-  enable_larger_than_screen_ = enable;
-}
-
-- (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) {
-  if (shell_->touch_bar())
-    return [shell_->touch_bar() makeTouchBar];
-  else
-    return nil;
-}
-
-// NSWindow overrides.
-
-- (void)swipeWithEvent:(NSEvent *)event {
-  if (event.deltaY == 1.0) {
-    shell_->NotifyWindowSwipe("up");
-  } else if (event.deltaX == -1.0) {
-    shell_->NotifyWindowSwipe("right");
-  } else if (event.deltaY == -1.0) {
-    shell_->NotifyWindowSwipe("down");
-  } else if (event.deltaX == 1.0) {
-    shell_->NotifyWindowSwipe("left");
-  }
-}
-
-- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen {
-  // Resizing is disabled.
-  if (ScopedDisableResize::IsResizeDisabled())
-    return [self frame];
-
-  // Enable the window to be larger than screen.
-  if (enable_larger_than_screen_)
-    return frameRect;
-  else
-    return [super constrainFrameRect:frameRect toScreen:screen];
-}
-
-- (void)setFrame:(NSRect)windowFrame display:(BOOL)displayViews {
-  // constrainFrameRect is not called on hidden windows so disable adjusting
-  // the frame directly when resize is disabled
-  if (!ScopedDisableResize::IsResizeDisabled())
-    [super setFrame:windowFrame display:displayViews];
-}
-
-- (id)accessibilityAttributeValue:(NSString*)attribute {
-  if (![attribute isEqualToString:@"AXChildren"])
-    return [super accessibilityAttributeValue:attribute];
-
-  // Filter out objects that aren't the title bar buttons. This has the effect
-  // of removing the window title, which VoiceOver already sees.
-  // * when VoiceOver is disabled, this causes Cmd+C to be used for TTS but
-  //   still leaves the buttons available in the accessibility tree.
-  // * when VoiceOver is enabled, the full accessibility tree is used.
-  // Without removing the title and with VO disabled, the TTS would always read
-  // the window title instead of using Cmd+C to get the selected text.
-  NSPredicate *predicate = [NSPredicate predicateWithFormat:
-      @"(self isKindOfClass: %@) OR (self.className == %@)",
-      [NSButtonCell class],
-      @"RenderWidgetHostViewCocoa"];
-
-  NSArray *children = [super accessibilityAttributeValue:attribute];
-  return [children filteredArrayUsingPredicate:predicate];
-}
-
-- (BOOL)canBecomeMainWindow {
-  return !self.disableKeyOrMainWindow;
-}
-
-- (BOOL)canBecomeKeyWindow {
-  return !self.disableKeyOrMainWindow;
-}
-
-- (void)enableWindowButtonsOffset {
-  auto closeButton = [self standardWindowButton:NSWindowCloseButton];
-  auto miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
-  auto zoomButton = [self standardWindowButton:NSWindowZoomButton];
-
-  [closeButton setPostsFrameChangedNotifications:YES];
-  [miniaturizeButton setPostsFrameChangedNotifications:YES];
-  [zoomButton setPostsFrameChangedNotifications:YES];
-
-  windowButtonsInterButtonSpacing_ =
-    NSMinX([miniaturizeButton frame]) - NSMaxX([closeButton frame]);
-
-  auto center = [NSNotificationCenter defaultCenter];
-
-  [center addObserver:self
-             selector:@selector(adjustCloseButton:)
-                 name:NSViewFrameDidChangeNotification
-               object:closeButton];
-
-  [center addObserver:self
-             selector:@selector(adjustMiniaturizeButton:)
-                 name:NSViewFrameDidChangeNotification
-               object:miniaturizeButton];
-
-  [center addObserver:self
-             selector:@selector(adjustZoomButton:)
-                 name:NSViewFrameDidChangeNotification
-               object:zoomButton];
-}
-
-- (void)adjustCloseButton:(NSNotification*)notification {
-  [self adjustButton:[notification object]
-              ofKind:NSWindowCloseButton];
-}
-
-- (void)adjustMiniaturizeButton:(NSNotification*)notification {
-  [self adjustButton:[notification object]
-              ofKind:NSWindowMiniaturizeButton];
-}
-
-- (void)adjustZoomButton:(NSNotification*)notification {
-  [self adjustButton:[notification object]
-              ofKind:NSWindowZoomButton];
-}
-
-- (void)adjustButton:(NSButton*)button
-              ofKind:(NSWindowButton)kind {
-  NSRect buttonFrame = [button frame];
-  NSRect frameViewBounds = [[self frameView] bounds];
-  NSPoint offset = self.windowButtonsOffset;
-
-  buttonFrame.origin = NSMakePoint(
-    offset.x,
-    (NSHeight(frameViewBounds) - NSHeight(buttonFrame) - offset.y));
-
-  switch (kind) {
-    case NSWindowZoomButton:
-      buttonFrame.origin.x += NSWidth(
-        [[self standardWindowButton:NSWindowMiniaturizeButton] frame]);
-      buttonFrame.origin.x += windowButtonsInterButtonSpacing_;
-      // fallthrough
-    case NSWindowMiniaturizeButton:
-      buttonFrame.origin.x += NSWidth(
-        [[self standardWindowButton:NSWindowCloseButton] frame]);
-      buttonFrame.origin.x += windowButtonsInterButtonSpacing_;
-      // fallthrough
-    default:
-      break;
-  }
-
-  BOOL didPost = [button postsBoundsChangedNotifications];
-  [button setPostsFrameChangedNotifications:NO];
-  [button setFrame:buttonFrame];
-  [button setPostsFrameChangedNotifications:didPost];
-}
-
-- (NSView*)frameView {
-  return [[self contentView] superview];
-}
-
-// Quicklook methods
-
-- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel*)panel {
-  return YES;
-}
-
-- (void)beginPreviewPanelControl:(QLPreviewPanel*)panel {
-  panel.delegate = [self delegate];
-  panel.dataSource = static_cast<id<QLPreviewPanelDataSource>>([self delegate]);
-}
-
-- (void)endPreviewPanelControl:(QLPreviewPanel*)panel {
-  panel.delegate = nil;
-  panel.dataSource = nil;
-}
-
-// Custom window button methods
-
-- (void)performClose:(id)sender {
-  if (shell_->title_bar_style() == atom::NativeWindowMac::CUSTOM_BUTTONS_ON_HOVER)
-    [[self delegate] windowShouldClose:self];
-  else
-    [super performClose:sender];
-}
-
-- (void)toggleFullScreenMode:(id)sender {
-  if (shell_->simple_fullscreen())
-    shell_->SetSimpleFullScreen(!shell_->IsSimpleFullScreen());
-  else
-   [super toggleFullScreen:sender];
-}
-
-- (void)performMiniaturize:(id)sender {
-  if (shell_->title_bar_style() == atom::NativeWindowMac::CUSTOM_BUTTONS_ON_HOVER)
-    [self miniaturize:self];
-  else
-    [super performMiniaturize:sender];
-}
-
-@end
-
 @interface AtomProgressBar : NSProgressIndicator
 @end
 

+ 45 - 0
atom/browser/ui/cocoa/atom_ns_window.h

@@ -0,0 +1,45 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_UI_COCOA_ATOM_NS_WINDOW_H_
+#define ATOM_BROWSER_UI_COCOA_ATOM_NS_WINDOW_H_
+
+#include "brightray/browser/mac/event_dispatching_window.h"
+#include "ui/views/cocoa/native_widget_mac_nswindow.h"
+
+namespace atom {
+
+class NativeWindowMac;
+
+// Prevents window from resizing during the scope.
+class ScopedDisableResize {
+ public:
+  ScopedDisableResize() { disable_resize_ = true; }
+  ~ScopedDisableResize() { disable_resize_ = false; }
+
+  static bool IsResizeDisabled() { return disable_resize_; }
+
+ private:
+  static bool disable_resize_;
+};
+
+}  // namespace atom
+
+@interface AtomNSWindow : EventDispatchingWindow {
+ @private
+  atom::NativeWindowMac* shell_;
+  CGFloat windowButtonsInterButtonSpacing_;
+}
+@property BOOL acceptsFirstMouse;
+@property BOOL enableLargerThanScreen;
+@property BOOL disableAutoHideCursor;
+@property BOOL disableKeyOrMainWindow;
+@property NSPoint windowButtonsOffset;
+@property (nonatomic, retain) NSView* vibrantView;
+- (void)setShell:(atom::NativeWindowMac*)shell;
+- (void)enableWindowButtonsOffset;
+- (void)toggleFullScreenMode:(id)sender;
+@end
+
+#endif  // ATOM_BROWSER_UI_COCOA_ATOM_NS_WINDOW_H_

+ 217 - 0
atom/browser/ui/cocoa/atom_ns_window.mm

@@ -0,0 +1,217 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/ui/cocoa/atom_ns_window.h"
+
+#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"
+
+namespace atom {
+
+bool ScopedDisableResize::disable_resize_ = false;
+
+}  // namespace atom
+
+@implementation AtomNSWindow
+
+@synthesize acceptsFirstMouse;
+@synthesize enableLargerThanScreen;
+@synthesize disableAutoHideCursor;
+@synthesize disableKeyOrMainWindow;
+@synthesize windowButtonsOffset;
+@synthesize vibrantView;
+
+- (void)setShell:(atom::NativeWindowMac*)shell {
+  shell_ = shell;
+}
+
+- (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) {
+  if (shell_->touch_bar())
+    return [shell_->touch_bar() makeTouchBar];
+  else
+    return nil;
+}
+
+// NSWindow overrides.
+
+- (void)swipeWithEvent:(NSEvent *)event {
+  if (event.deltaY == 1.0) {
+    shell_->NotifyWindowSwipe("up");
+  } else if (event.deltaX == -1.0) {
+    shell_->NotifyWindowSwipe("right");
+  } else if (event.deltaY == -1.0) {
+    shell_->NotifyWindowSwipe("down");
+  } else if (event.deltaX == 1.0) {
+    shell_->NotifyWindowSwipe("left");
+  }
+}
+
+- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen {
+  // Resizing is disabled.
+  if (atom::ScopedDisableResize::IsResizeDisabled())
+    return [self frame];
+
+  // Enable the window to be larger than screen.
+  if ([self enableLargerThanScreen])
+    return frameRect;
+  else
+    return [super constrainFrameRect:frameRect toScreen:screen];
+}
+
+- (void)setFrame:(NSRect)windowFrame display:(BOOL)displayViews {
+  // constrainFrameRect is not called on hidden windows so disable adjusting
+  // the frame directly when resize is disabled
+  if (!atom::ScopedDisableResize::IsResizeDisabled())
+    [super setFrame:windowFrame display:displayViews];
+}
+
+- (id)accessibilityAttributeValue:(NSString*)attribute {
+  if (![attribute isEqualToString:@"AXChildren"])
+    return [super accessibilityAttributeValue:attribute];
+
+  // Filter out objects that aren't the title bar buttons. This has the effect
+  // of removing the window title, which VoiceOver already sees.
+  // * when VoiceOver is disabled, this causes Cmd+C to be used for TTS but
+  //   still leaves the buttons available in the accessibility tree.
+  // * when VoiceOver is enabled, the full accessibility tree is used.
+  // Without removing the title and with VO disabled, the TTS would always read
+  // the window title instead of using Cmd+C to get the selected text.
+  NSPredicate *predicate = [NSPredicate predicateWithFormat:
+      @"(self isKindOfClass: %@) OR (self.className == %@)",
+      [NSButtonCell class],
+      @"RenderWidgetHostViewCocoa"];
+
+  NSArray *children = [super accessibilityAttributeValue:attribute];
+  return [children filteredArrayUsingPredicate:predicate];
+}
+
+- (BOOL)canBecomeMainWindow {
+  return !self.disableKeyOrMainWindow;
+}
+
+- (BOOL)canBecomeKeyWindow {
+  return !self.disableKeyOrMainWindow;
+}
+
+- (void)enableWindowButtonsOffset {
+  auto closeButton = [self standardWindowButton:NSWindowCloseButton];
+  auto miniaturizeButton = [self standardWindowButton:NSWindowMiniaturizeButton];
+  auto zoomButton = [self standardWindowButton:NSWindowZoomButton];
+
+  [closeButton setPostsFrameChangedNotifications:YES];
+  [miniaturizeButton setPostsFrameChangedNotifications:YES];
+  [zoomButton setPostsFrameChangedNotifications:YES];
+
+  windowButtonsInterButtonSpacing_ =
+    NSMinX([miniaturizeButton frame]) - NSMaxX([closeButton frame]);
+
+  auto center = [NSNotificationCenter defaultCenter];
+
+  [center addObserver:self
+             selector:@selector(adjustCloseButton:)
+                 name:NSViewFrameDidChangeNotification
+               object:closeButton];
+
+  [center addObserver:self
+             selector:@selector(adjustMiniaturizeButton:)
+                 name:NSViewFrameDidChangeNotification
+               object:miniaturizeButton];
+
+  [center addObserver:self
+             selector:@selector(adjustZoomButton:)
+                 name:NSViewFrameDidChangeNotification
+               object:zoomButton];
+}
+
+- (void)adjustCloseButton:(NSNotification*)notification {
+  [self adjustButton:[notification object]
+              ofKind:NSWindowCloseButton];
+}
+
+- (void)adjustMiniaturizeButton:(NSNotification*)notification {
+  [self adjustButton:[notification object]
+              ofKind:NSWindowMiniaturizeButton];
+}
+
+- (void)adjustZoomButton:(NSNotification*)notification {
+  [self adjustButton:[notification object]
+              ofKind:NSWindowZoomButton];
+}
+
+- (void)adjustButton:(NSButton*)button
+              ofKind:(NSWindowButton)kind {
+  NSRect buttonFrame = [button frame];
+  NSRect frameViewBounds = [[self frameView] bounds];
+  NSPoint offset = self.windowButtonsOffset;
+
+  buttonFrame.origin = NSMakePoint(
+    offset.x,
+    (NSHeight(frameViewBounds) - NSHeight(buttonFrame) - offset.y));
+
+  switch (kind) {
+    case NSWindowZoomButton:
+      buttonFrame.origin.x += NSWidth(
+        [[self standardWindowButton:NSWindowMiniaturizeButton] frame]);
+      buttonFrame.origin.x += windowButtonsInterButtonSpacing_;
+      // fallthrough
+    case NSWindowMiniaturizeButton:
+      buttonFrame.origin.x += NSWidth(
+        [[self standardWindowButton:NSWindowCloseButton] frame]);
+      buttonFrame.origin.x += windowButtonsInterButtonSpacing_;
+      // fallthrough
+    default:
+      break;
+  }
+
+  BOOL didPost = [button postsBoundsChangedNotifications];
+  [button setPostsFrameChangedNotifications:NO];
+  [button setFrame:buttonFrame];
+  [button setPostsFrameChangedNotifications:didPost];
+}
+
+- (NSView*)frameView {
+  return [[self contentView] superview];
+}
+
+// Quicklook methods
+
+- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel*)panel {
+  return YES;
+}
+
+- (void)beginPreviewPanelControl:(QLPreviewPanel*)panel {
+  panel.delegate = [self delegate];
+  panel.dataSource = static_cast<id<QLPreviewPanelDataSource>>([self delegate]);
+}
+
+- (void)endPreviewPanelControl:(QLPreviewPanel*)panel {
+  panel.delegate = nil;
+  panel.dataSource = nil;
+}
+
+// Custom window button methods
+
+- (void)performClose:(id)sender {
+  if (shell_->title_bar_style() == atom::NativeWindowMac::CUSTOM_BUTTONS_ON_HOVER)
+    [[self delegate] windowShouldClose:self];
+  else
+    [super performClose:sender];
+}
+
+- (void)toggleFullScreenMode:(id)sender {
+  if (shell_->simple_fullscreen())
+    shell_->SetSimpleFullScreen(!shell_->IsSimpleFullScreen());
+  else
+   [super toggleFullScreen:sender];
+}
+
+- (void)performMiniaturize:(id)sender {
+  if (shell_->title_bar_style() == atom::NativeWindowMac::CUSTOM_BUTTONS_ON_HOVER)
+    [self miniaturize:self];
+  else
+    [super performMiniaturize:sender];
+}
+
+@end

+ 2 - 0
filenames.gypi

@@ -318,6 +318,8 @@
       'atom/browser/ui/cocoa/atom_menu_controller.mm',
       'atom/browser/ui/cocoa/atom_native_widget_mac.h',
       'atom/browser/ui/cocoa/atom_native_widget_mac.mm',
+      'atom/browser/ui/cocoa/atom_ns_window.h',
+      'atom/browser/ui/cocoa/atom_ns_window.mm',
       'atom/browser/ui/cocoa/atom_ns_window_delegate.h',
       'atom/browser/ui/cocoa/atom_ns_window_delegate.mm',
       'atom/browser/ui/cocoa/atom_preview_item.h',

+ 1 - 0
script/cpplint.py

@@ -12,6 +12,7 @@ IGNORE_FILES = set(os.path.join(*components) for components in [
   ['atom', 'browser', 'mac', 'atom_application_delegate.h'],
   ['atom', 'browser', 'resources', 'win', 'resource.h'],
   ['atom', 'browser', 'ui', 'cocoa', 'atom_menu_controller.h'],
+  ['atom', 'browser', 'ui', 'cocoa', 'atom_ns_window.h'],
   ['atom', 'browser', 'ui', 'cocoa', 'atom_ns_window_delegate.h'],
   ['atom', 'browser', 'ui', 'cocoa', 'atom_preview_item.h'],
   ['atom', 'browser', 'ui', 'cocoa', 'atom_touch_bar.h'],