native_window_mac.h 9.3 KB

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