native_window_mac.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <tuple>
  11. #include <vector>
  12. #include "base/mac/scoped_nsobject.h"
  13. #include "shell/browser/native_window.h"
  14. #include "ui/display/display_observer.h"
  15. #include "ui/native_theme/native_theme_observer.h"
  16. #include "ui/views/controls/native/native_view_host.h"
  17. @class ElectronNSWindow;
  18. @class ElectronNSWindowDelegate;
  19. @class ElectronPreviewItem;
  20. @class ElectronTouchBar;
  21. @class CustomWindowButtonView;
  22. namespace electron {
  23. class RootViewMac;
  24. class NativeWindowMac : public NativeWindow,
  25. public ui::NativeThemeObserver,
  26. public display::DisplayObserver {
  27. public:
  28. NativeWindowMac(const gin_helper::Dictionary& options, NativeWindow* parent);
  29. ~NativeWindowMac() override;
  30. // Cleanup observers when window is getting closed. Note that the destructor
  31. // can be called much later after window gets closed, so we should not do
  32. // cleanup in destructor.
  33. void Cleanup();
  34. // NativeWindow:
  35. void SetContentView(views::View* view) override;
  36. void Close() override;
  37. void CloseImmediately() override;
  38. void Focus(bool focus) override;
  39. bool IsFocused() override;
  40. void Show() override;
  41. void ShowInactive() override;
  42. void Hide() override;
  43. bool IsVisible() override;
  44. bool IsEnabled() override;
  45. void SetEnabled(bool enable) override;
  46. void Maximize() override;
  47. void Unmaximize() override;
  48. bool IsMaximized() override;
  49. void Minimize() override;
  50. void Restore() override;
  51. bool IsMinimized() override;
  52. void SetFullScreen(bool fullscreen) override;
  53. bool IsFullscreen() const override;
  54. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  55. gfx::Rect GetBounds() override;
  56. bool IsNormal() override;
  57. gfx::Rect GetNormalBounds() override;
  58. void SetContentSizeConstraints(
  59. const extensions::SizeConstraints& size_constraints) override;
  60. void SetResizable(bool resizable) override;
  61. bool MoveAbove(const std::string& sourceId) override;
  62. void MoveTop() override;
  63. bool IsResizable() override;
  64. void SetMovable(bool movable) override;
  65. void SetAspectRatio(double aspect_ratio,
  66. const gfx::Size& extra_size) override;
  67. void PreviewFile(const std::string& path,
  68. const std::string& display_name) override;
  69. void CloseFilePreview() override;
  70. bool IsMovable() override;
  71. void SetMinimizable(bool minimizable) override;
  72. bool IsMinimizable() override;
  73. void SetMaximizable(bool maximizable) override;
  74. bool IsMaximizable() override;
  75. void SetFullScreenable(bool fullscreenable) override;
  76. bool IsFullScreenable() override;
  77. void SetClosable(bool closable) override;
  78. bool IsClosable() override;
  79. void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  80. const std::string& level,
  81. int relativeLevel) override;
  82. std::string GetAlwaysOnTopLevel() override;
  83. ui::ZOrderLevel GetZOrderLevel() override;
  84. void Center() override;
  85. void Invalidate() override;
  86. void SetTitle(const std::string& title) override;
  87. std::string GetTitle() override;
  88. void FlashFrame(bool flash) override;
  89. void SetSkipTaskbar(bool skip) override;
  90. void SetExcludedFromShownWindowsMenu(bool excluded) override;
  91. bool IsExcludedFromShownWindowsMenu() override;
  92. void SetSimpleFullScreen(bool simple_fullscreen) override;
  93. bool IsSimpleFullScreen() override;
  94. void SetKiosk(bool kiosk) override;
  95. bool IsKiosk() override;
  96. void SetBackgroundColor(SkColor color) override;
  97. SkColor GetBackgroundColor() override;
  98. void SetHasShadow(bool has_shadow) override;
  99. bool HasShadow() override;
  100. void SetOpacity(const double opacity) override;
  101. double GetOpacity() override;
  102. void SetRepresentedFilename(const std::string& filename) override;
  103. std::string GetRepresentedFilename() override;
  104. void SetDocumentEdited(bool edited) override;
  105. bool IsDocumentEdited() override;
  106. void SetIgnoreMouseEvents(bool ignore, bool forward) override;
  107. void SetContentProtection(bool enable) override;
  108. void SetFocusable(bool focusable) override;
  109. void AddBrowserView(NativeBrowserView* browser_view) override;
  110. void RemoveBrowserView(NativeBrowserView* browser_view) override;
  111. void SetTopBrowserView(NativeBrowserView* browser_view) override;
  112. void SetParentWindow(NativeWindow* parent) override;
  113. content::DesktopMediaID GetDesktopMediaID() const override;
  114. gfx::NativeView GetNativeView() const override;
  115. gfx::NativeWindow GetNativeWindow() const override;
  116. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  117. NativeWindowHandle GetNativeWindowHandle() const override;
  118. void SetProgressBar(double progress, const ProgressState state) override;
  119. void SetOverlayIcon(const gfx::Image& overlay,
  120. const std::string& description) override;
  121. void SetVisibleOnAllWorkspaces(bool visible,
  122. bool visibleOnFullScreen) override;
  123. bool IsVisibleOnAllWorkspaces() override;
  124. void SetAutoHideCursor(bool auto_hide) override;
  125. void SelectPreviousTab() override;
  126. void SelectNextTab() override;
  127. void MergeAllWindows() override;
  128. void MoveTabToNewWindow() override;
  129. void ToggleTabBar() override;
  130. bool AddTabbedWindow(NativeWindow* window) override;
  131. void SetActive(bool is_key) override;
  132. bool IsActive() const override;
  133. bool SetWindowButtonVisibility(bool visible) override;
  134. void SetVibrancy(const std::string& type) override;
  135. void UpdateFrame() override;
  136. void SetTouchBar(
  137. std::vector<gin_helper::PersistentDictionary> items) override;
  138. void RefreshTouchBarItem(const std::string& item_id) override;
  139. void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item) override;
  140. void SetGTKDarkThemeEnabled(bool use_dark_theme) override {}
  141. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  142. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  143. // Use a custom content view instead of Chromium's BridgedContentView.
  144. void OverrideNSWindowContentView();
  145. // Set the attribute of NSWindow while work around a bug of zoom button.
  146. void SetStyleMask(bool on, NSUInteger flag);
  147. void SetCollectionBehavior(bool on, NSUInteger flag);
  148. void SetWindowLevel(int level);
  149. // Custom traffic light positioning
  150. void RedrawTrafficLights() override;
  151. void SetTrafficLightPosition(const gfx::Point& position) override;
  152. gfx::Point GetTrafficLightPosition() const override;
  153. void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
  154. enum class FullScreenTransitionState { ENTERING, EXITING, NONE };
  155. // Handle fullscreen transitions.
  156. void SetFullScreenTransitionState(FullScreenTransitionState state);
  157. void HandlePendingFullscreenTransitions();
  158. enum class VisualEffectState {
  159. kFollowWindow,
  160. kActive,
  161. kInactive,
  162. };
  163. enum class TitleBarStyle {
  164. kNormal,
  165. kHidden,
  166. kHiddenInset,
  167. kCustomButtonsOnHover,
  168. };
  169. TitleBarStyle title_bar_style() const { return title_bar_style_; }
  170. ElectronPreviewItem* preview_item() const { return preview_item_.get(); }
  171. ElectronTouchBar* touch_bar() const { return touch_bar_.get(); }
  172. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  173. bool fullscreen_window_title() const { return fullscreen_window_title_; }
  174. bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
  175. protected:
  176. // views::WidgetDelegate:
  177. bool CanResize() const override;
  178. views::View* GetContentsView() override;
  179. // display::DisplayObserver:
  180. void OnDisplayMetricsChanged(const display::Display& display,
  181. uint32_t changed_metrics) override;
  182. private:
  183. // Add custom layers to the content view.
  184. void AddContentViewLayers(bool minimizable, bool closable);
  185. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  186. void SetForwardMouseMessages(bool forward);
  187. ElectronNSWindow* window_; // Weak ref, managed by widget_.
  188. base::scoped_nsobject<ElectronNSWindowDelegate> window_delegate_;
  189. base::scoped_nsobject<ElectronPreviewItem> preview_item_;
  190. base::scoped_nsobject<ElectronTouchBar> touch_bar_;
  191. base::scoped_nsobject<CustomWindowButtonView> buttons_view_;
  192. // Event monitor for scroll wheel event.
  193. id wheel_event_monitor_;
  194. // The NSView that used as contentView of window.
  195. //
  196. // For frameless window it would fill the whole window.
  197. base::scoped_nsobject<NSView> container_view_;
  198. // The views::View that fills the client area.
  199. std::unique_ptr<RootViewMac> root_view_;
  200. bool is_kiosk_ = false;
  201. bool zoom_to_page_width_ = false;
  202. bool fullscreen_window_title_ = false;
  203. bool resizable_ = true;
  204. gfx::Point traffic_light_position_;
  205. std::queue<bool> pending_transitions_;
  206. FullScreenTransitionState fullscreen_transition_state() const {
  207. return fullscreen_transition_state_;
  208. }
  209. FullScreenTransitionState fullscreen_transition_state_ =
  210. FullScreenTransitionState::NONE;
  211. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  212. // The presentation options before entering kiosk mode.
  213. NSApplicationPresentationOptions kiosk_options_;
  214. // The "titleBarStyle" option.
  215. TitleBarStyle title_bar_style_ = TitleBarStyle::kNormal;
  216. // The "visualEffectState" option.
  217. VisualEffectState visual_effect_state_ = VisualEffectState::kFollowWindow;
  218. // The visibility mode of window button controls when explicitly set through
  219. // setWindowButtonVisibility().
  220. base::Optional<bool> window_button_visibility_;
  221. // Maximizable window state; necessary for persistence through redraws.
  222. bool maximizable_ = true;
  223. // Simple (pre-Lion) Fullscreen Settings
  224. bool always_simple_fullscreen_ = false;
  225. bool is_simple_fullscreen_ = false;
  226. bool was_maximizable_ = false;
  227. bool was_movable_ = false;
  228. bool is_active_ = false;
  229. NSRect original_frame_;
  230. NSInteger original_level_;
  231. NSUInteger simple_fullscreen_mask_;
  232. base::scoped_nsobject<NSColor> background_color_before_vibrancy_;
  233. bool transparency_before_vibrancy_ = false;
  234. // The presentation options before entering simple fullscreen mode.
  235. NSApplicationPresentationOptions simple_fullscreen_options_;
  236. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  237. };
  238. } // namespace electron
  239. #endif // SHELL_BROWSER_NATIVE_WINDOW_MAC_H_