native_window_mac.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <optional>
  9. #include <string>
  10. #include <vector>
  11. #include "electron/shell/common/api/api.mojom.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() const override;
  35. void Show() override;
  36. void ShowInactive() override;
  37. void Hide() override;
  38. bool IsOccluded() const override;
  39. bool IsVisible() const override;
  40. bool IsEnabled() const override;
  41. void SetEnabled(bool enable) override;
  42. void Maximize() override;
  43. void Unmaximize() override;
  44. bool IsMaximized() const override;
  45. void Minimize() override;
  46. void Restore() override;
  47. bool IsMinimized() const override;
  48. void SetFullScreen(bool fullscreen) override;
  49. bool IsFullscreen() const override;
  50. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  51. gfx::Rect GetBounds() const override;
  52. bool IsNormal() const override;
  53. gfx::Rect GetNormalBounds() const override;
  54. void SetSizeConstraints(
  55. const extensions::SizeConstraints& window_constraints) override;
  56. void SetContentSizeConstraints(
  57. const extensions::SizeConstraints& size_constraints) override;
  58. void SetResizable(bool resizable) override;
  59. bool MoveAbove(const std::string& sourceId) override;
  60. void MoveTop() override;
  61. bool IsResizable() const override;
  62. void SetMovable(bool movable) override;
  63. bool IsMovable() const override;
  64. void SetMinimizable(bool minimizable) override;
  65. bool IsMinimizable() const override;
  66. void SetMaximizable(bool maximizable) override;
  67. bool IsMaximizable() const override;
  68. void SetFullScreenable(bool fullscreenable) override;
  69. bool IsFullScreenable() const override;
  70. void SetClosable(bool closable) override;
  71. bool IsClosable() const override;
  72. void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  73. const std::string& level,
  74. int relative_level) override;
  75. std::string GetAlwaysOnTopLevel() const override;
  76. ui::ZOrderLevel GetZOrderLevel() const override;
  77. void Center() override;
  78. void Invalidate() override;
  79. void SetTitle(const std::string& title) override;
  80. std::string GetTitle() const override;
  81. void FlashFrame(bool flash) override;
  82. void SetSkipTaskbar(bool skip) override;
  83. void SetExcludedFromShownWindowsMenu(bool excluded) override;
  84. bool IsExcludedFromShownWindowsMenu() const override;
  85. void SetSimpleFullScreen(bool simple_fullscreen) override;
  86. bool IsSimpleFullScreen() const override;
  87. void SetKiosk(bool kiosk) override;
  88. bool IsKiosk() const override;
  89. void SetBackgroundColor(SkColor color) override;
  90. SkColor GetBackgroundColor() const override;
  91. void InvalidateShadow() override;
  92. void SetHasShadow(bool has_shadow) override;
  93. bool HasShadow() const override;
  94. void SetOpacity(const double opacity) override;
  95. double GetOpacity() const override;
  96. void SetRepresentedFilename(const std::string& filename) override;
  97. std::string GetRepresentedFilename() const override;
  98. void SetDocumentEdited(bool edited) override;
  99. bool IsDocumentEdited() const override;
  100. void SetIgnoreMouseEvents(bool ignore, bool forward) override;
  101. bool IsHiddenInMissionControl() const override;
  102. void SetHiddenInMissionControl(bool hidden) override;
  103. void SetContentProtection(bool enable) override;
  104. void SetFocusable(bool focusable) override;
  105. bool IsFocusable() const override;
  106. void SetParentWindow(NativeWindow* parent) override;
  107. content::DesktopMediaID GetDesktopMediaID() const override;
  108. gfx::NativeView GetNativeView() const override;
  109. gfx::NativeWindow GetNativeWindow() const override;
  110. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  111. NativeWindowHandle GetNativeWindowHandle() const override;
  112. void SetProgressBar(double progress, const ProgressState state) override;
  113. void SetOverlayIcon(const gfx::Image& overlay,
  114. const std::string& description) override;
  115. void SetVisibleOnAllWorkspaces(bool visible,
  116. bool visibleOnFullScreen,
  117. bool skipTransformProcessType) override;
  118. bool IsVisibleOnAllWorkspaces() const override;
  119. void SetAutoHideCursor(bool auto_hide) override;
  120. void SetVibrancy(const std::string& type) override;
  121. void SetWindowButtonVisibility(bool visible) override;
  122. bool GetWindowButtonVisibility() const override;
  123. void SetWindowButtonPosition(std::optional<gfx::Point> position) override;
  124. std::optional<gfx::Point> GetWindowButtonPosition() const override;
  125. void RedrawTrafficLights() override;
  126. void UpdateFrame() override;
  127. void SetTouchBar(
  128. std::vector<gin_helper::PersistentDictionary> items) override;
  129. void RefreshTouchBarItem(const std::string& item_id) override;
  130. void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item) override;
  131. void SelectPreviousTab() override;
  132. void SelectNextTab() override;
  133. void ShowAllTabs() override;
  134. void MergeAllWindows() override;
  135. void MoveTabToNewWindow() override;
  136. void ToggleTabBar() override;
  137. bool AddTabbedWindow(NativeWindow* window) override;
  138. std::optional<std::string> GetTabbingIdentifier() const override;
  139. void SetAspectRatio(double aspect_ratio,
  140. const gfx::Size& extra_size) override;
  141. void PreviewFile(const std::string& path,
  142. const std::string& display_name) override;
  143. void CloseFilePreview() override;
  144. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  145. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  146. std::optional<gfx::Rect> GetWindowControlsOverlayRect() override;
  147. void NotifyWindowEnterFullScreen() override;
  148. void NotifyWindowLeaveFullScreen() override;
  149. void SetActive(bool is_key) override;
  150. bool IsActive() const override;
  151. // Remove the specified child window without closing it.
  152. void RemoveChildWindow(NativeWindow* child) override;
  153. void RemoveChildFromParentWindow() override;
  154. // Attach child windows, if the window is visible.
  155. void AttachChildren() override;
  156. // Detach window from parent without destroying it.
  157. void DetachChildren() override;
  158. void NotifyWindowWillEnterFullScreen();
  159. void NotifyWindowWillLeaveFullScreen();
  160. // Cleanup observers when window is getting closed. Note that the destructor
  161. // can be called much later after window gets closed, so we should not do
  162. // cleanup in destructor.
  163. void Cleanup();
  164. void UpdateVibrancyRadii(bool fullscreen);
  165. void UpdateWindowOriginalFrame();
  166. // Set the attribute of NSWindow while work around a bug of zoom button.
  167. bool HasStyleMask(NSUInteger flag) const;
  168. void SetStyleMask(bool on, NSUInteger flag);
  169. void SetCollectionBehavior(bool on, NSUInteger flag);
  170. void SetWindowLevel(int level);
  171. bool HandleDeferredClose();
  172. void SetHasDeferredWindowClose(bool defer_close) {
  173. has_deferred_window_close_ = defer_close;
  174. }
  175. void set_wants_to_be_visible(bool visible) { wants_to_be_visible_ = visible; }
  176. bool wants_to_be_visible() const { return wants_to_be_visible_; }
  177. enum class VisualEffectState {
  178. kFollowWindow,
  179. kActive,
  180. kInactive,
  181. };
  182. ElectronPreviewItem* preview_item() const { return preview_item_; }
  183. ElectronTouchBar* touch_bar() const { return touch_bar_; }
  184. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  185. bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
  186. // We need to save the result of windowWillUseStandardFrame:defaultFrame
  187. // because macOS calls it with what it refers to as the "best fit" frame for a
  188. // zoom. This means that even if an aspect ratio is set, macOS might adjust it
  189. // to better fit the screen.
  190. //
  191. // Thus, we can't just calculate the maximized aspect ratio'd sizing from
  192. // the current visible screen and compare that to the current window's frame
  193. // to determine whether a window is maximized.
  194. NSRect default_frame_for_zoom() const { return default_frame_for_zoom_; }
  195. void set_default_frame_for_zoom(NSRect frame) {
  196. default_frame_for_zoom_ = frame;
  197. }
  198. protected:
  199. // views::WidgetDelegate:
  200. views::View* GetContentsView() override;
  201. bool CanMaximize() const override;
  202. std::unique_ptr<views::NonClientFrameView> CreateNonClientFrameView(
  203. views::Widget* widget) override;
  204. // ui::NativeThemeObserver:
  205. void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
  206. // display::DisplayObserver:
  207. void OnDisplayMetricsChanged(const display::Display& display,
  208. uint32_t changed_metrics) override;
  209. private:
  210. // Add custom layers to the content view.
  211. void AddContentViewLayers();
  212. void InternalSetWindowButtonVisibility(bool visible);
  213. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  214. void SetForwardMouseMessages(bool forward);
  215. void UpdateZoomButton();
  216. ElectronNSWindow* window_; // Weak ref, managed by widget_.
  217. ElectronNSWindowDelegate* __strong window_delegate_;
  218. ElectronPreviewItem* __strong preview_item_;
  219. ElectronTouchBar* __strong touch_bar_;
  220. // The views::View that fills the client area.
  221. std::unique_ptr<RootViewMac> root_view_;
  222. bool fullscreen_before_kiosk_ = false;
  223. bool is_kiosk_ = false;
  224. bool zoom_to_page_width_ = false;
  225. std::optional<gfx::Point> traffic_light_position_;
  226. // Trying to close an NSWindow during a fullscreen transition will cause the
  227. // window to lock up. Use this to track if CloseWindow was called during a
  228. // fullscreen transition, to defer the -[NSWindow close] call until the
  229. // transition is complete.
  230. bool has_deferred_window_close_ = false;
  231. // If true, the window is either visible, or wants to be visible but is
  232. // currently hidden due to having a hidden parent.
  233. bool wants_to_be_visible_ = false;
  234. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  235. // The presentation options before entering kiosk mode.
  236. NSApplicationPresentationOptions kiosk_options_;
  237. // The "visualEffectState" option.
  238. VisualEffectState visual_effect_state_ = VisualEffectState::kFollowWindow;
  239. // The visibility mode of window button controls when explicitly set through
  240. // setWindowButtonVisibility().
  241. std::optional<bool> window_button_visibility_;
  242. // Controls the position and visibility of window buttons.
  243. WindowButtonsProxy* __strong buttons_proxy_;
  244. std::unique_ptr<SkRegion> draggable_region_;
  245. // Maximizable window state; necessary for persistence through redraws.
  246. bool maximizable_ = true;
  247. bool user_set_bounds_maximized_ = false;
  248. // Simple (pre-Lion) Fullscreen Settings
  249. bool always_simple_fullscreen_ = false;
  250. bool is_simple_fullscreen_ = false;
  251. bool was_maximizable_ = false;
  252. bool was_movable_ = false;
  253. bool is_active_ = false;
  254. NSRect original_frame_;
  255. NSInteger original_level_;
  256. NSUInteger simple_fullscreen_mask_;
  257. NSRect default_frame_for_zoom_;
  258. std::string vibrancy_type_;
  259. // The presentation options before entering simple fullscreen mode.
  260. NSApplicationPresentationOptions simple_fullscreen_options_;
  261. };
  262. } // namespace electron
  263. #endif // ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_MAC_H_