native_window_mac.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 mate::Dictionary& options, NativeWindow* parent);
  25. ~NativeWindowMac() override;
  26. // NativeWindow:
  27. void SetContentView(views::View* view) override;
  28. void Close() override;
  29. void CloseImmediately() override;
  30. void Focus(bool focus) override;
  31. bool IsFocused() override;
  32. void Show() override;
  33. void ShowInactive() override;
  34. void Hide() override;
  35. bool IsVisible() override;
  36. bool IsEnabled() override;
  37. void SetEnabled(bool enable) override;
  38. void Maximize() override;
  39. void Unmaximize() override;
  40. bool IsMaximized() override;
  41. void Minimize() override;
  42. void Restore() override;
  43. bool IsMinimized() override;
  44. void SetFullScreen(bool fullscreen) override;
  45. bool IsFullscreen() const override;
  46. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  47. gfx::Rect GetBounds() override;
  48. bool IsNormal() override;
  49. gfx::Rect GetNormalBounds() override;
  50. void SetContentSizeConstraints(
  51. const extensions::SizeConstraints& size_constraints) override;
  52. void SetResizable(bool resizable) override;
  53. bool MoveAbove(const std::string& sourceId) override;
  54. void MoveTop() override;
  55. bool IsResizable() override;
  56. void SetMovable(bool movable) override;
  57. void SetAspectRatio(double aspect_ratio,
  58. const gfx::Size& extra_size) override;
  59. void PreviewFile(const std::string& path,
  60. const std::string& display_name) override;
  61. void CloseFilePreview() override;
  62. bool IsMovable() override;
  63. void SetMinimizable(bool minimizable) override;
  64. bool IsMinimizable() override;
  65. void SetMaximizable(bool maximizable) override;
  66. bool IsMaximizable() override;
  67. void SetFullScreenable(bool fullscreenable) override;
  68. bool IsFullScreenable() override;
  69. void SetClosable(bool closable) override;
  70. bool IsClosable() override;
  71. void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  72. const std::string& level,
  73. int relativeLevel) override;
  74. ui::ZOrderLevel GetZOrderLevel() override;
  75. void Center() override;
  76. void Invalidate() override;
  77. void SetTitle(const std::string& title) override;
  78. std::string GetTitle() override;
  79. void FlashFrame(bool flash) override;
  80. void SetSkipTaskbar(bool skip) override;
  81. void SetExcludedFromShownWindowsMenu(bool excluded) override;
  82. bool IsExcludedFromShownWindowsMenu() override;
  83. void SetSimpleFullScreen(bool simple_fullscreen) override;
  84. bool IsSimpleFullScreen() override;
  85. void SetKiosk(bool kiosk) override;
  86. bool IsKiosk() override;
  87. void SetBackgroundColor(SkColor color) 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. void AddBrowserView(NativeBrowserView* browser_view) override;
  100. void RemoveBrowserView(NativeBrowserView* browser_view) override;
  101. void SetParentWindow(NativeWindow* parent) override;
  102. content::DesktopMediaID GetDesktopMediaID() const override;
  103. gfx::NativeView GetNativeView() const override;
  104. gfx::NativeWindow GetNativeWindow() const override;
  105. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  106. NativeWindowHandle GetNativeWindowHandle() const override;
  107. void SetProgressBar(double progress, const ProgressState state) override;
  108. void SetOverlayIcon(const gfx::Image& overlay,
  109. const std::string& description) override;
  110. void SetVisibleOnAllWorkspaces(bool visible,
  111. bool visibleOnFullScreen) override;
  112. bool IsVisibleOnAllWorkspaces() override;
  113. void SetAutoHideCursor(bool auto_hide) override;
  114. void SelectPreviousTab() override;
  115. void SelectNextTab() override;
  116. void MergeAllWindows() override;
  117. void MoveTabToNewWindow() override;
  118. void ToggleTabBar() override;
  119. bool AddTabbedWindow(NativeWindow* window) override;
  120. bool SetWindowButtonVisibility(bool visible) override;
  121. void SetVibrancy(const std::string& type) override;
  122. void SetTouchBar(
  123. const std::vector<mate::PersistentDictionary>& items) override;
  124. void RefreshTouchBarItem(const std::string& item_id) override;
  125. void SetEscapeTouchBarItem(const mate::PersistentDictionary& item) override;
  126. void SetGTKDarkThemeEnabled(bool use_dark_theme) override {}
  127. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  128. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  129. // Use a custom content view instead of Chromium's BridgedContentView.
  130. void OverrideNSWindowContentView();
  131. // Set the attribute of NSWindow while work around a bug of zoom button.
  132. void SetStyleMask(bool on, NSUInteger flag);
  133. void SetCollectionBehavior(bool on, NSUInteger flag);
  134. void SetWindowLevel(int level);
  135. // Custom traffic light positioning
  136. void RepositionTrafficLights() override;
  137. void SetExitingFullScreen(bool flag);
  138. void SetTrafficLightPosition(const gfx::Point& position) override;
  139. gfx::Point GetTrafficLightPosition() const override;
  140. void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
  141. enum class TitleBarStyle {
  142. NORMAL,
  143. HIDDEN,
  144. HIDDEN_INSET,
  145. CUSTOM_BUTTONS_ON_HOVER,
  146. };
  147. TitleBarStyle title_bar_style() const { return title_bar_style_; }
  148. ElectronPreviewItem* preview_item() const { return preview_item_.get(); }
  149. ElectronTouchBar* touch_bar() const { return touch_bar_.get(); }
  150. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  151. bool fullscreen_window_title() const { return fullscreen_window_title_; }
  152. bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
  153. bool exiting_fullscreen() const { return exiting_fullscreen_; }
  154. protected:
  155. // views::WidgetDelegate:
  156. bool CanResize() const override;
  157. views::View* GetContentsView() override;
  158. private:
  159. // Add custom layers to the content view.
  160. void AddContentViewLayers(bool minimizable, bool closable);
  161. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  162. void SetForwardMouseMessages(bool forward);
  163. ElectronNSWindow* window_; // Weak ref, managed by widget_.
  164. base::scoped_nsobject<ElectronNSWindowDelegate> window_delegate_;
  165. base::scoped_nsobject<ElectronPreviewItem> preview_item_;
  166. base::scoped_nsobject<ElectronTouchBar> touch_bar_;
  167. base::scoped_nsobject<CustomWindowButtonView> buttons_view_;
  168. // Event monitor for scroll wheel event.
  169. id wheel_event_monitor_;
  170. // The NSView that used as contentView of window.
  171. //
  172. // For frameless window it would fill the whole window.
  173. base::scoped_nsobject<NSView> container_view_;
  174. // The views::View that fills the client area.
  175. std::unique_ptr<RootViewMac> root_view_;
  176. bool is_kiosk_ = false;
  177. bool was_fullscreen_ = false;
  178. bool zoom_to_page_width_ = false;
  179. bool fullscreen_window_title_ = false;
  180. bool resizable_ = true;
  181. bool exiting_fullscreen_ = false;
  182. gfx::Point traffic_light_position_;
  183. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  184. // The presentation options before entering kiosk mode.
  185. NSApplicationPresentationOptions kiosk_options_;
  186. // The "titleBarStyle" option.
  187. TitleBarStyle title_bar_style_ = TitleBarStyle::NORMAL;
  188. // The visibility mode of window button controls when explicitly set through
  189. // setWindowButtonVisibility().
  190. base::Optional<bool> window_button_visibility_;
  191. // Simple (pre-Lion) Fullscreen Settings
  192. bool always_simple_fullscreen_ = false;
  193. bool is_simple_fullscreen_ = false;
  194. bool was_maximizable_ = false;
  195. bool was_movable_ = false;
  196. NSRect original_frame_;
  197. NSInteger original_level_;
  198. NSUInteger simple_fullscreen_mask_;
  199. base::scoped_nsobject<NSColor> background_color_before_vibrancy_;
  200. bool transparency_before_vibrancy_ = false;
  201. // The presentation options before entering simple fullscreen mode.
  202. NSApplicationPresentationOptions simple_fullscreen_options_;
  203. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  204. };
  205. } // namespace electron
  206. #endif // SHELL_BROWSER_NATIVE_WINDOW_MAC_H_