native_window_mac.h 12 KB

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