native_window_mac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 SHELL_BROWSER_NATIVE_WINDOW_MAC_H_
  5. #define 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. enum class VisualEffectState {
  164. kFollowWindow,
  165. kActive,
  166. kInactive,
  167. };
  168. ElectronPreviewItem* preview_item() const { return preview_item_.get(); }
  169. ElectronTouchBar* touch_bar() const { return touch_bar_.get(); }
  170. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  171. bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
  172. // We need to save the result of windowWillUseStandardFrame:defaultFrame
  173. // because macOS calls it with what it refers to as the "best fit" frame for a
  174. // zoom. This means that even if an aspect ratio is set, macOS might adjust it
  175. // to better fit the screen.
  176. //
  177. // Thus, we can't just calculate the maximized aspect ratio'd sizing from
  178. // the current visible screen and compare that to the current window's frame
  179. // to determine whether a window is maximized.
  180. NSRect default_frame_for_zoom() const { return default_frame_for_zoom_; }
  181. void set_default_frame_for_zoom(NSRect frame) {
  182. default_frame_for_zoom_ = frame;
  183. }
  184. protected:
  185. // views::WidgetDelegate:
  186. views::View* GetContentsView() override;
  187. bool CanMaximize() const override;
  188. // ui::NativeThemeObserver:
  189. void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
  190. // display::DisplayObserver:
  191. void OnDisplayMetricsChanged(const display::Display& display,
  192. uint32_t changed_metrics) override;
  193. private:
  194. // Add custom layers to the content view.
  195. void AddContentViewLayers();
  196. void InternalSetWindowButtonVisibility(bool visible);
  197. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  198. void SetForwardMouseMessages(bool forward);
  199. ElectronNSWindow* window_; // Weak ref, managed by widget_.
  200. base::scoped_nsobject<ElectronNSWindowDelegate> window_delegate_;
  201. base::scoped_nsobject<ElectronPreviewItem> preview_item_;
  202. base::scoped_nsobject<ElectronTouchBar> touch_bar_;
  203. // Event monitor for scroll wheel event.
  204. id wheel_event_monitor_;
  205. // The NSView that used as contentView of window.
  206. //
  207. // For frameless window it would fill the whole window.
  208. base::scoped_nsobject<NSView> container_view_;
  209. // The views::View that fills the client area.
  210. std::unique_ptr<RootViewMac> root_view_;
  211. bool is_kiosk_ = false;
  212. bool zoom_to_page_width_ = false;
  213. absl::optional<gfx::Point> traffic_light_position_;
  214. std::queue<bool> pending_transitions_;
  215. FullScreenTransitionState fullscreen_transition_state() const {
  216. return fullscreen_transition_state_;
  217. }
  218. FullScreenTransitionState fullscreen_transition_state_ =
  219. FullScreenTransitionState::NONE;
  220. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  221. // The presentation options before entering kiosk mode.
  222. NSApplicationPresentationOptions kiosk_options_;
  223. // The "visualEffectState" option.
  224. VisualEffectState visual_effect_state_ = VisualEffectState::kFollowWindow;
  225. // The visibility mode of window button controls when explicitly set through
  226. // setWindowButtonVisibility().
  227. absl::optional<bool> window_button_visibility_;
  228. // Controls the position and visibility of window buttons.
  229. base::scoped_nsobject<WindowButtonsProxy> buttons_proxy_;
  230. // Maximizable window state; necessary for persistence through redraws.
  231. bool maximizable_ = true;
  232. bool user_set_bounds_maximized_ = false;
  233. // Simple (pre-Lion) Fullscreen Settings
  234. bool always_simple_fullscreen_ = false;
  235. bool is_simple_fullscreen_ = false;
  236. bool was_maximizable_ = false;
  237. bool was_movable_ = false;
  238. bool is_active_ = false;
  239. NSRect original_frame_;
  240. NSInteger original_level_;
  241. NSUInteger simple_fullscreen_mask_;
  242. NSRect default_frame_for_zoom_;
  243. std::string vibrancy_type_;
  244. // The presentation options before entering simple fullscreen mode.
  245. NSApplicationPresentationOptions simple_fullscreen_options_;
  246. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  247. };
  248. } // namespace electron
  249. #endif // SHELL_BROWSER_NATIVE_WINDOW_MAC_H_