views_nswindow_delegate.mm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "components/remote_cocoa/app_shim/views_nswindow_delegate.h"
  5. #include "base/bind.h"
  6. #include "base/check.h"
  7. #include "base/mac/mac_util.h"
  8. #include "base/threading/thread_task_runner_handle.h"
  9. #import "components/remote_cocoa/app_shim/bridged_content_view.h"
  10. #import "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
  11. #include "components/remote_cocoa/app_shim/native_widget_ns_window_fullscreen_controller.h"
  12. #include "components/remote_cocoa/app_shim/native_widget_ns_window_host_helper.h"
  13. #include "components/remote_cocoa/common/native_widget_ns_window_host.mojom.h"
  14. #include "ui/gfx/geometry/resize_utils.h"
  15. @implementation ViewsNSWindowDelegate
  16. - (instancetype)initWithBridgedNativeWidget:
  17. (remote_cocoa::NativeWidgetNSWindowBridge*)parent {
  18. DCHECK(parent);
  19. if ((self = [super init])) {
  20. _parent = parent;
  21. }
  22. return self;
  23. }
  24. - (NSCursor*)cursor {
  25. return _cursor.get();
  26. }
  27. - (void)setCursor:(NSCursor*)newCursor {
  28. if (_cursor.get() == newCursor)
  29. return;
  30. _cursor.reset([newCursor retain]);
  31. // The window has a tracking rect that was installed in -[BridgedContentView
  32. // initWithView:] that uses the NSTrackingCursorUpdate option. In the case
  33. // where the window is the key window, that tracking rect will cause
  34. // -cursorUpdate: to be sent up the responder chain, which will cause the
  35. // cursor to be set when the message gets to the NativeWidgetMacNSWindow.
  36. NSWindow* window = _parent->ns_window();
  37. [window resetCursorRects];
  38. // However, if this window isn't the key window, that tracking area will have
  39. // no effect. This is good if this window is just some top-level window that
  40. // isn't key, but isn't so good if this window isn't key but is a child window
  41. // of a window that is key. To handle that case, the case where the
  42. // -cursorUpdate: message will never be sent, just set the cursor here.
  43. //
  44. // Only do this for non-key windows so that there will be no flickering
  45. // between cursors set here and set elsewhere.
  46. //
  47. // (This is a known issue; see https://stackoverflow.com/questions/45712066/.)
  48. if (![window isKeyWindow]) {
  49. NSWindow* currentWindow = window;
  50. // Walk up the window chain. If there is a key window in the window parent
  51. // chain, then work around the issue and set the cursor.
  52. while (true) {
  53. NSWindow* parentWindow = [currentWindow parentWindow];
  54. if (!parentWindow)
  55. break;
  56. currentWindow = parentWindow;
  57. if ([currentWindow isKeyWindow]) {
  58. [(newCursor ? newCursor : [NSCursor arrowCursor]) set];
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. - (void)onWindowOrderChanged:(NSNotification*)notification {
  65. _parent->OnVisibilityChanged();
  66. }
  67. - (void)onSystemControlTintChanged:(NSNotification*)notification {
  68. _parent->OnSystemControlTintChanged();
  69. }
  70. - (void)sheetDidEnd:(NSWindow*)sheet
  71. returnCode:(NSInteger)returnCode
  72. contextInfo:(void*)contextInfo {
  73. [sheet orderOut:nil];
  74. _parent->OnWindowWillClose();
  75. }
  76. // NSWindowDelegate implementation.
  77. - (void)windowDidFailToEnterFullScreen:(NSWindow*)window {
  78. // Cocoa should already have sent an (unexpected) windowDidExitFullScreen:
  79. // notification, and the attempt to get back into fullscreen should fail.
  80. // Nothing to do except verify |parent_| is no longer trying to fullscreen.
  81. DCHECK(!_parent->target_fullscreen_state());
  82. }
  83. - (void)windowDidFailToExitFullScreen:(NSWindow*)window {
  84. // Unlike entering fullscreen, windowDidFailToExitFullScreen: is sent *before*
  85. // windowDidExitFullScreen:. Also, failing to exit fullscreen just dumps the
  86. // window out of fullscreen without an animation; still sending the expected,
  87. // windowDidExitFullScreen: notification. So, again, nothing to do here.
  88. DCHECK(!_parent->target_fullscreen_state());
  89. }
  90. - (void)setAspectRatio:(float)aspectRatio {
  91. _aspectRatio = aspectRatio;
  92. }
  93. - (NSSize)windowWillResize:(NSWindow*)window toSize:(NSSize)size {
  94. if (!_aspectRatio)
  95. return size;
  96. if (!_resizingHorizontally) {
  97. const auto widthDelta = size.width - [window frame].size.width;
  98. const auto heightDelta = size.height - [window frame].size.height;
  99. _resizingHorizontally = std::abs(widthDelta) > std::abs(heightDelta);
  100. }
  101. gfx::Rect resizedWindowRect(gfx::Point([window frame].origin),
  102. gfx::Size(size));
  103. absl::optional<gfx::Size> maxSizeParam;
  104. gfx::Size maxSize([window maxSize]);
  105. if (!maxSize.IsEmpty())
  106. maxSizeParam = maxSize;
  107. gfx::SizeRectToAspectRatio(*_resizingHorizontally ? gfx::ResizeEdge::kRight
  108. : gfx::ResizeEdge::kBottom,
  109. *_aspectRatio, gfx::Size([window minSize]),
  110. maxSizeParam, &resizedWindowRect);
  111. // Discard any updates to |resizedWindowRect| origin as Cocoa takes care of
  112. // that.
  113. return resizedWindowRect.size().ToCGSize();
  114. }
  115. - (void)windowDidEndLiveResize:(NSNotification*)notification {
  116. _resizingHorizontally.reset();
  117. }
  118. - (void)windowDidResize:(NSNotification*)notification {
  119. _parent->OnSizeChanged();
  120. }
  121. - (void)windowDidMove:(NSNotification*)notification {
  122. // Note: windowDidMove: is sent only once at the end of a window drag. There
  123. // is also windowWillMove: sent at the start, also once. When the window is
  124. // being moved by the WindowServer live updates are not provided.
  125. _parent->OnPositionChanged();
  126. }
  127. - (void)windowDidBecomeKey:(NSNotification*)notification {
  128. _parent->OnWindowKeyStatusChangedTo(true);
  129. }
  130. - (void)windowDidResignKey:(NSNotification*)notification {
  131. // If our app is still active and we're still the key window, ignore this
  132. // message, since it just means that a menu extra (on the "system status bar")
  133. // was activated; we'll get another |-windowDidResignKey| if we ever really
  134. // lose key window status.
  135. if ([NSApp isActive] && ([NSApp keyWindow] == notification.object))
  136. return;
  137. _parent->OnWindowKeyStatusChangedTo(false);
  138. }
  139. - (BOOL)windowShouldClose:(id)sender {
  140. bool canWindowClose = true;
  141. _parent->host()->OnWindowCloseRequested(&canWindowClose);
  142. return canWindowClose;
  143. }
  144. - (void)windowWillClose:(NSNotification*)notification {
  145. NSWindow* window = _parent->ns_window();
  146. if (NSWindow* sheetParent = [window sheetParent]) {
  147. // On no! Something called -[NSWindow close] on a sheet rather than calling
  148. // -[NSWindow endSheet:] on its parent. If the modal session is not ended
  149. // then the parent will never be able to show another sheet. But calling
  150. // -endSheet: here will block the thread with an animation, so post a task.
  151. // Use a block: The argument to -endSheet: must be retained, since it's the
  152. // window that is closing and -performSelector: won't retain the argument
  153. // (putting |window| on the stack above causes this block to retain it).
  154. base::ThreadTaskRunnerHandle::Get()->PostTask(
  155. FROM_HERE, base::BindOnce(base::RetainBlock(^{
  156. [sheetParent endSheet:window];
  157. })));
  158. }
  159. DCHECK([window isEqual:[notification object]]);
  160. _parent->OnWindowWillClose();
  161. // |self| may be deleted here (it's NSObject, so who really knows).
  162. // |parent_| _will_ be deleted for sure.
  163. // Note OnWindowWillClose() will clear the NSWindow delegate. That is, |self|.
  164. // That guarantees that the task possibly-posted above will never call into
  165. // our -sheetDidEnd:. (The task's purpose is just to unblock the modal session
  166. // on the parent window.)
  167. DCHECK(![window delegate]);
  168. }
  169. - (void)windowDidMiniaturize:(NSNotification*)notification {
  170. _parent->host()->OnWindowMiniaturizedChanged(true);
  171. _parent->OnVisibilityChanged();
  172. }
  173. - (void)windowDidDeminiaturize:(NSNotification*)notification {
  174. _parent->host()->OnWindowMiniaturizedChanged(false);
  175. _parent->OnVisibilityChanged();
  176. }
  177. - (void)windowDidChangeBackingProperties:(NSNotification*)notification {
  178. _parent->OnBackingPropertiesChanged();
  179. }
  180. - (void)windowWillEnterFullScreen:(NSNotification*)notification {
  181. _parent->fullscreen_controller().OnWindowWillEnterFullscreen();
  182. }
  183. - (void)windowDidEnterFullScreen:(NSNotification*)notification {
  184. _parent->fullscreen_controller().OnWindowDidEnterFullscreen();
  185. }
  186. - (void)windowWillExitFullScreen:(NSNotification*)notification {
  187. _parent->fullscreen_controller().OnWindowWillExitFullscreen();
  188. }
  189. - (void)windowDidExitFullScreen:(NSNotification*)notification {
  190. _parent->fullscreen_controller().OnWindowDidExitFullscreen();
  191. }
  192. // Allow non-resizable windows (without NSWindowStyleMaskResizable) to fill the
  193. // screen in fullscreen mode. This only happens when
  194. // -[NSWindow toggleFullscreen:] is called since non-resizable windows have no
  195. // fullscreen button. Without this they would only enter fullscreen at their
  196. // current size.
  197. - (NSSize)window:(NSWindow*)window
  198. willUseFullScreenContentSize:(NSSize)proposedSize {
  199. return proposedSize;
  200. }
  201. // Override to correctly position modal dialogs.
  202. - (NSRect)window:(NSWindow*)window
  203. willPositionSheet:(NSWindow*)sheet
  204. usingRect:(NSRect)defaultSheetLocation {
  205. int32_t sheetPositionY = 0;
  206. _parent->host()->GetSheetOffsetY(&sheetPositionY);
  207. NSView* view = [window contentView];
  208. NSPoint pointInView = NSMakePoint(0, NSMaxY([view bounds]) - sheetPositionY);
  209. NSPoint pointInWindow = [view convertPoint:pointInView toView:nil];
  210. // As per NSWindowDelegate documentation, the origin indicates the top left
  211. // point of the host frame in window coordinates. The width changes the
  212. // animation from vertical to trapezoid if it is smaller than the width of the
  213. // dialog. The height is ignored but should be set to zero.
  214. return NSMakeRect(0, pointInWindow.y, NSWidth(defaultSheetLocation), 0);
  215. }
  216. @end