native_window_mac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_MAC_H_
  5. #define ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_MAC_H_
  6. #import <Cocoa/Cocoa.h>
  7. #include <memory>
  8. #include <queue>
  9. #include <string>
  10. #include <vector>
  11. #include "base/mac/scoped_nsobject.h"
  12. #include "shell/browser/native_window.h"
  13. #include "ui/display/display_observer.h"
  14. #include "ui/native_theme/native_theme_observer.h"
  15. #include "ui/views/controls/native/native_view_host.h"
  16. @class ElectronNSWindow;
  17. @class ElectronNSWindowDelegate;
  18. @class ElectronPreviewItem;
  19. @class ElectronTouchBar;
  20. @class WindowButtonsProxy;
  21. namespace electron {
  22. class RootViewMac;
  23. class NativeWindowMac : public NativeWindow,
  24. public ui::NativeThemeObserver,
  25. public display::DisplayObserver {
  26. public:
  27. NativeWindowMac(const gin_helper::Dictionary& options, NativeWindow* parent);
  28. ~NativeWindowMac() override;
  29. // NativeWindow:
  30. void SetContentView(views::View* view) override;
  31. void Close() override;
  32. void CloseImmediately() override;
  33. void Focus(bool focus) override;
  34. bool IsFocused() override;
  35. void Show() override;
  36. void ShowInactive() override;
  37. void Hide() override;
  38. bool IsVisible() override;
  39. bool IsEnabled() override;
  40. void SetEnabled(bool enable) override;
  41. void Maximize() override;
  42. void Unmaximize() override;
  43. bool IsMaximized() override;
  44. void Minimize() override;
  45. void Restore() override;
  46. bool IsMinimized() override;
  47. void SetFullScreen(bool fullscreen) override;
  48. bool IsFullscreen() const override;
  49. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  50. gfx::Rect GetBounds() override;
  51. bool IsNormal() override;
  52. gfx::Rect GetNormalBounds() override;
  53. void SetContentSizeConstraints(
  54. const extensions::SizeConstraints& size_constraints) override;
  55. void SetResizable(bool resizable) override;
  56. bool MoveAbove(const std::string& sourceId) override;
  57. void MoveTop() override;
  58. bool IsResizable() override;
  59. void SetMovable(bool movable) override;
  60. bool IsMovable() override;
  61. void SetMinimizable(bool minimizable) override;
  62. bool IsMinimizable() override;
  63. void SetMaximizable(bool maximizable) override;
  64. bool IsMaximizable() override;
  65. void SetFullScreenable(bool fullscreenable) override;
  66. bool IsFullScreenable() override;
  67. void SetClosable(bool closable) override;
  68. bool IsClosable() override;
  69. void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  70. const std::string& level,
  71. int relative_level) override;
  72. std::string GetAlwaysOnTopLevel() override;
  73. ui::ZOrderLevel GetZOrderLevel() override;
  74. void Center() override;
  75. void Invalidate() override;
  76. void SetTitle(const std::string& title) override;
  77. std::string GetTitle() override;
  78. void FlashFrame(bool flash) override;
  79. void SetSkipTaskbar(bool skip) override;
  80. void SetExcludedFromShownWindowsMenu(bool excluded) override;
  81. bool IsExcludedFromShownWindowsMenu() override;
  82. void SetSimpleFullScreen(bool simple_fullscreen) override;
  83. bool IsSimpleFullScreen() override;
  84. void SetKiosk(bool kiosk) override;
  85. bool IsKiosk() override;
  86. void SetBackgroundColor(SkColor color) override;
  87. SkColor GetBackgroundColor() override;
  88. void SetHasShadow(bool has_shadow) override;
  89. bool HasShadow() override;
  90. void SetOpacity(const double opacity) override;
  91. double GetOpacity() override;
  92. void SetRepresentedFilename(const std::string& filename) override;
  93. std::string GetRepresentedFilename() override;
  94. void SetDocumentEdited(bool edited) override;
  95. bool IsDocumentEdited() override;
  96. void SetIgnoreMouseEvents(bool ignore, bool forward) override;
  97. void SetContentProtection(bool enable) override;
  98. void SetFocusable(bool focusable) override;
  99. bool IsFocusable() override;
  100. void AddBrowserView(NativeBrowserView* browser_view) override;
  101. void RemoveBrowserView(NativeBrowserView* browser_view) override;
  102. void SetTopBrowserView(NativeBrowserView* browser_view) override;
  103. void SetParentWindow(NativeWindow* parent) override;
  104. content::DesktopMediaID GetDesktopMediaID() const override;
  105. gfx::NativeView GetNativeView() const override;
  106. gfx::NativeWindow GetNativeWindow() const override;
  107. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  108. NativeWindowHandle GetNativeWindowHandle() const override;
  109. void SetProgressBar(double progress, const ProgressState state) override;
  110. void SetOverlayIcon(const gfx::Image& overlay,
  111. const std::string& description) override;
  112. void SetVisibleOnAllWorkspaces(bool visible,
  113. bool visibleOnFullScreen,
  114. bool skipTransformProcessType) override;
  115. bool IsVisibleOnAllWorkspaces() override;
  116. void SetAutoHideCursor(bool auto_hide) override;
  117. void SetVibrancy(const std::string& type) override;
  118. void SetWindowButtonVisibility(bool visible) override;
  119. bool GetWindowButtonVisibility() const override;
  120. void SetTrafficLightPosition(absl::optional<gfx::Point> position) override;
  121. absl::optional<gfx::Point> GetTrafficLightPosition() const override;
  122. void RedrawTrafficLights() override;
  123. void UpdateFrame() override;
  124. void SetTouchBar(
  125. std::vector<gin_helper::PersistentDictionary> items) override;
  126. void RefreshTouchBarItem(const std::string& item_id) override;
  127. void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item) override;
  128. void SelectPreviousTab() override;
  129. void SelectNextTab() override;
  130. void MergeAllWindows() override;
  131. void MoveTabToNewWindow() override;
  132. void ToggleTabBar() override;
  133. bool AddTabbedWindow(NativeWindow* window) override;
  134. void SetAspectRatio(double aspect_ratio,
  135. const gfx::Size& extra_size) override;
  136. void PreviewFile(const std::string& path,
  137. const std::string& display_name) override;
  138. void CloseFilePreview() override;
  139. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  140. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  141. gfx::Rect GetWindowControlsOverlayRect() override;
  142. void NotifyWindowEnterFullScreen() override;
  143. void NotifyWindowLeaveFullScreen() override;
  144. void SetActive(bool is_key) override;
  145. bool IsActive() const override;
  146. void NotifyWindowWillEnterFullScreen();
  147. void NotifyWindowWillLeaveFullScreen();
  148. // Cleanup observers when window is getting closed. Note that the destructor
  149. // can be called much later after window gets closed, so we should not do
  150. // cleanup in destructor.
  151. void Cleanup();
  152. // Use a custom content view instead of Chromium's BridgedContentView.
  153. void OverrideNSWindowContentView();
  154. void UpdateVibrancyRadii(bool fullscreen);
  155. // Set the attribute of NSWindow while work around a bug of zoom button.
  156. void SetStyleMask(bool on, NSUInteger flag);
  157. void SetCollectionBehavior(bool on, NSUInteger flag);
  158. void SetWindowLevel(int level);
  159. enum class FullScreenTransitionState { ENTERING, EXITING, NONE };
  160. // Handle fullscreen transitions.
  161. void SetFullScreenTransitionState(FullScreenTransitionState state);
  162. void HandlePendingFullscreenTransitions();
  163. bool HandleDeferredClose();
  164. void SetHasDeferredWindowClose(bool defer_close) {
  165. has_deferred_window_close_ = defer_close;
  166. }
  167. enum class VisualEffectState {
  168. kFollowWindow,
  169. kActive,
  170. kInactive,
  171. };
  172. ElectronPreviewItem* preview_item() const { return preview_item_.get(); }
  173. ElectronTouchBar* touch_bar() const { return touch_bar_.get(); }
  174. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  175. bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
  176. // We need to save the result of windowWillUseStandardFrame:defaultFrame
  177. // because macOS calls it with what it refers to as the "best fit" frame for a
  178. // zoom. This means that even if an aspect ratio is set, macOS might adjust it
  179. // to better fit the screen.
  180. //
  181. // Thus, we can't just calculate the maximized aspect ratio'd sizing from
  182. // the current visible screen and compare that to the current window's frame
  183. // to determine whether a window is maximized.
  184. NSRect default_frame_for_zoom() const { return default_frame_for_zoom_; }
  185. void set_default_frame_for_zoom(NSRect frame) {
  186. default_frame_for_zoom_ = frame;
  187. }
  188. protected:
  189. // views::WidgetDelegate:
  190. views::View* GetContentsView() override;
  191. bool CanMaximize() const override;
  192. // ui::NativeThemeObserver:
  193. void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
  194. // display::DisplayObserver:
  195. void OnDisplayMetricsChanged(const display::Display& display,
  196. uint32_t changed_metrics) override;
  197. private:
  198. // Add custom layers to the content view.
  199. void AddContentViewLayers();
  200. void InternalSetWindowButtonVisibility(bool visible);
  201. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  202. void SetForwardMouseMessages(bool forward);
  203. ElectronNSWindow* window_; // Weak ref, managed by widget_.
  204. base::scoped_nsobject<ElectronNSWindowDelegate> window_delegate_;
  205. base::scoped_nsobject<ElectronPreviewItem> preview_item_;
  206. base::scoped_nsobject<ElectronTouchBar> touch_bar_;
  207. // Event monitor for scroll wheel event.
  208. id wheel_event_monitor_;
  209. // The NSView that used as contentView of window.
  210. //
  211. // For frameless window it would fill the whole window.
  212. base::scoped_nsobject<NSView> container_view_;
  213. // The views::View that fills the client area.
  214. std::unique_ptr<RootViewMac> root_view_;
  215. bool is_kiosk_ = false;
  216. bool zoom_to_page_width_ = false;
  217. absl::optional<gfx::Point> traffic_light_position_;
  218. std::queue<bool> pending_transitions_;
  219. FullScreenTransitionState fullscreen_transition_state() const {
  220. return fullscreen_transition_state_;
  221. }
  222. FullScreenTransitionState fullscreen_transition_state_ =
  223. FullScreenTransitionState::NONE;
  224. // Trying to close an NSWindow during a fullscreen transition will cause the
  225. // window to lock up. Use this to track if CloseWindow was called during a
  226. // fullscreen transition, to defer the -[NSWindow close] call until the
  227. // transition is complete.
  228. bool has_deferred_window_close_ = false;
  229. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  230. // The presentation options before entering kiosk mode.
  231. NSApplicationPresentationOptions kiosk_options_;
  232. // The "visualEffectState" option.
  233. VisualEffectState visual_effect_state_ = VisualEffectState::kFollowWindow;
  234. // The visibility mode of window button controls when explicitly set through
  235. // setWindowButtonVisibility().
  236. absl::optional<bool> window_button_visibility_;
  237. // Controls the position and visibility of window buttons.
  238. base::scoped_nsobject<WindowButtonsProxy> buttons_proxy_;
  239. // Maximizable window state; necessary for persistence through redraws.
  240. bool maximizable_ = true;
  241. bool user_set_bounds_maximized_ = false;
  242. // Simple (pre-Lion) Fullscreen Settings
  243. bool always_simple_fullscreen_ = false;
  244. bool is_simple_fullscreen_ = false;
  245. bool was_maximizable_ = false;
  246. bool was_movable_ = false;
  247. bool is_active_ = false;
  248. NSRect original_frame_;
  249. NSInteger original_level_;
  250. NSUInteger simple_fullscreen_mask_;
  251. NSRect default_frame_for_zoom_;
  252. std::string vibrancy_type_;
  253. // The presentation options before entering simple fullscreen mode.
  254. NSApplicationPresentationOptions simple_fullscreen_options_;
  255. };
  256. } // namespace electron
  257. #endif // ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_MAC_H_