atom_ns_window.mm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright (c) 2018 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/cocoa/atom_ns_window.h"
  5. #include "atom/browser/native_window_mac.h"
  6. #include "atom/browser/ui/cocoa/atom_preview_item.h"
  7. #include "atom/browser/ui/cocoa/atom_touch_bar.h"
  8. #include "ui/base/cocoa/window_size_constants.h"
  9. namespace atom {
  10. bool ScopedDisableResize::disable_resize_ = false;
  11. } // namespace atom
  12. @implementation AtomNSWindow
  13. @synthesize acceptsFirstMouse;
  14. @synthesize enableLargerThanScreen;
  15. @synthesize disableAutoHideCursor;
  16. @synthesize disableKeyOrMainWindow;
  17. @synthesize windowButtonsOffset;
  18. @synthesize vibrantView;
  19. - (id)initWithShell:(atom::NativeWindowMac*)shell
  20. styleMask:(NSUInteger)styleMask {
  21. if ((self = [super initWithContentRect:ui::kWindowSizeDeterminedLater
  22. styleMask:styleMask
  23. backing:NSBackingStoreBuffered
  24. defer:YES])) {
  25. shell_ = shell;
  26. }
  27. return self;
  28. }
  29. - (atom::NativeWindowMac*)shell {
  30. return shell_;
  31. }
  32. - (NSRect)originalContentRectForFrameRect:(NSRect)frameRect {
  33. return [super contentRectForFrameRect:frameRect];
  34. }
  35. - (NSTouchBar*)makeTouchBar API_AVAILABLE(macosx(10.12.2)) {
  36. if (shell_->touch_bar())
  37. return [shell_->touch_bar() makeTouchBar];
  38. else
  39. return nil;
  40. }
  41. // NSWindow overrides.
  42. - (void)swipeWithEvent:(NSEvent*)event {
  43. if (event.deltaY == 1.0) {
  44. shell_->NotifyWindowSwipe("up");
  45. } else if (event.deltaX == -1.0) {
  46. shell_->NotifyWindowSwipe("right");
  47. } else if (event.deltaY == -1.0) {
  48. shell_->NotifyWindowSwipe("down");
  49. } else if (event.deltaX == 1.0) {
  50. shell_->NotifyWindowSwipe("left");
  51. }
  52. }
  53. - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
  54. if (shell_->has_frame())
  55. return [super contentRectForFrameRect:frameRect];
  56. else
  57. return frameRect;
  58. }
  59. - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen*)screen {
  60. // Resizing is disabled.
  61. if (atom::ScopedDisableResize::IsResizeDisabled())
  62. return [self frame];
  63. // Enable the window to be larger than screen.
  64. if ([self enableLargerThanScreen])
  65. return frameRect;
  66. else
  67. return [super constrainFrameRect:frameRect toScreen:screen];
  68. }
  69. - (void)setFrame:(NSRect)windowFrame display:(BOOL)displayViews {
  70. // constrainFrameRect is not called on hidden windows so disable adjusting
  71. // the frame directly when resize is disabled
  72. if (!atom::ScopedDisableResize::IsResizeDisabled())
  73. [super setFrame:windowFrame display:displayViews];
  74. }
  75. - (id)accessibilityAttributeValue:(NSString*)attribute {
  76. if (![attribute isEqualToString:@"AXChildren"])
  77. return [super accessibilityAttributeValue:attribute];
  78. // Filter out objects that aren't the title bar buttons. This has the effect
  79. // of removing the window title, which VoiceOver already sees.
  80. // * when VoiceOver is disabled, this causes Cmd+C to be used for TTS but
  81. // still leaves the buttons available in the accessibility tree.
  82. // * when VoiceOver is enabled, the full accessibility tree is used.
  83. // Without removing the title and with VO disabled, the TTS would always read
  84. // the window title instead of using Cmd+C to get the selected text.
  85. NSPredicate* predicate = [NSPredicate
  86. predicateWithFormat:@"(self isKindOfClass: %@) OR (self.className == %@)",
  87. [NSButtonCell class], @"RenderWidgetHostViewCocoa"];
  88. NSArray* children = [super accessibilityAttributeValue:attribute];
  89. return [children filteredArrayUsingPredicate:predicate];
  90. }
  91. - (BOOL)canBecomeMainWindow {
  92. return !self.disableKeyOrMainWindow;
  93. }
  94. - (BOOL)canBecomeKeyWindow {
  95. return !self.disableKeyOrMainWindow;
  96. }
  97. - (void)enableWindowButtonsOffset {
  98. auto closeButton = [self standardWindowButton:NSWindowCloseButton];
  99. auto miniaturizeButton =
  100. [self standardWindowButton:NSWindowMiniaturizeButton];
  101. auto zoomButton = [self standardWindowButton:NSWindowZoomButton];
  102. [closeButton setPostsFrameChangedNotifications:YES];
  103. [miniaturizeButton setPostsFrameChangedNotifications:YES];
  104. [zoomButton setPostsFrameChangedNotifications:YES];
  105. windowButtonsInterButtonSpacing_ =
  106. NSMinX([miniaturizeButton frame]) - NSMaxX([closeButton frame]);
  107. auto center = [NSNotificationCenter defaultCenter];
  108. [center addObserver:self
  109. selector:@selector(adjustCloseButton:)
  110. name:NSViewFrameDidChangeNotification
  111. object:closeButton];
  112. [center addObserver:self
  113. selector:@selector(adjustMiniaturizeButton:)
  114. name:NSViewFrameDidChangeNotification
  115. object:miniaturizeButton];
  116. [center addObserver:self
  117. selector:@selector(adjustZoomButton:)
  118. name:NSViewFrameDidChangeNotification
  119. object:zoomButton];
  120. }
  121. - (void)adjustCloseButton:(NSNotification*)notification {
  122. [self adjustButton:[notification object] ofKind:NSWindowCloseButton];
  123. }
  124. - (void)adjustMiniaturizeButton:(NSNotification*)notification {
  125. [self adjustButton:[notification object] ofKind:NSWindowMiniaturizeButton];
  126. }
  127. - (void)adjustZoomButton:(NSNotification*)notification {
  128. [self adjustButton:[notification object] ofKind:NSWindowZoomButton];
  129. }
  130. - (void)adjustButton:(NSButton*)button ofKind:(NSWindowButton)kind {
  131. NSRect buttonFrame = [button frame];
  132. NSRect frameViewBounds = [[self frameView] bounds];
  133. NSPoint offset = self.windowButtonsOffset;
  134. buttonFrame.origin = NSMakePoint(
  135. offset.x, (NSHeight(frameViewBounds) - NSHeight(buttonFrame) - offset.y));
  136. switch (kind) {
  137. case NSWindowZoomButton:
  138. buttonFrame.origin.x += NSWidth(
  139. [[self standardWindowButton:NSWindowMiniaturizeButton] frame]);
  140. buttonFrame.origin.x += windowButtonsInterButtonSpacing_;
  141. // fallthrough
  142. case NSWindowMiniaturizeButton:
  143. buttonFrame.origin.x +=
  144. NSWidth([[self standardWindowButton:NSWindowCloseButton] frame]);
  145. buttonFrame.origin.x += windowButtonsInterButtonSpacing_;
  146. // fallthrough
  147. default:
  148. break;
  149. }
  150. BOOL didPost = [button postsBoundsChangedNotifications];
  151. [button setPostsFrameChangedNotifications:NO];
  152. [button setFrame:buttonFrame];
  153. [button setPostsFrameChangedNotifications:didPost];
  154. }
  155. - (NSView*)frameView {
  156. return [[self contentView] superview];
  157. }
  158. // Quicklook methods
  159. - (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel*)panel {
  160. return YES;
  161. }
  162. - (void)beginPreviewPanelControl:(QLPreviewPanel*)panel {
  163. panel.delegate = [self delegate];
  164. panel.dataSource = static_cast<id<QLPreviewPanelDataSource>>([self delegate]);
  165. }
  166. - (void)endPreviewPanelControl:(QLPreviewPanel*)panel {
  167. panel.delegate = nil;
  168. panel.dataSource = nil;
  169. }
  170. // Custom window button methods
  171. - (void)performClose:(id)sender {
  172. if (shell_->title_bar_style() ==
  173. atom::NativeWindowMac::CUSTOM_BUTTONS_ON_HOVER)
  174. [[self delegate] windowShouldClose:self];
  175. else
  176. [super performClose:sender];
  177. }
  178. - (void)toggleFullScreenMode:(id)sender {
  179. if (shell_->simple_fullscreen())
  180. shell_->SetSimpleFullScreen(!shell_->IsSimpleFullScreen());
  181. else
  182. [super toggleFullScreen:sender];
  183. }
  184. - (void)performMiniaturize:(id)sender {
  185. if (shell_->title_bar_style() ==
  186. atom::NativeWindowMac::CUSTOM_BUTTONS_ON_HOVER)
  187. [self miniaturize:self];
  188. else
  189. [super performMiniaturize:sender];
  190. }
  191. @end