native_window_mac.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/views/controls/native/native_view_host.h"
  14. @class AtomNSWindow;
  15. @class AtomNSWindowDelegate;
  16. @class AtomPreviewItem;
  17. @class AtomTouchBar;
  18. @class CustomWindowButtonView;
  19. namespace electron {
  20. class RootViewMac;
  21. class NativeWindowMac : public NativeWindow {
  22. public:
  23. NativeWindowMac(const mate::Dictionary& options, NativeWindow* parent);
  24. ~NativeWindowMac() override;
  25. // NativeWindow:
  26. void SetContentView(views::View* view) override;
  27. void Close() override;
  28. void CloseImmediately() override;
  29. void Focus(bool focus) override;
  30. bool IsFocused() override;
  31. void Show() override;
  32. void ShowInactive() override;
  33. void Hide() override;
  34. bool IsVisible() override;
  35. bool IsEnabled() override;
  36. void SetEnabled(bool enable) override;
  37. void Maximize() override;
  38. void Unmaximize() override;
  39. bool IsMaximized() override;
  40. void Minimize() override;
  41. void Restore() override;
  42. bool IsMinimized() override;
  43. void SetFullScreen(bool fullscreen) override;
  44. bool IsFullscreen() const override;
  45. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  46. gfx::Rect GetBounds() override;
  47. bool IsNormal() override;
  48. gfx::Rect GetNormalBounds() override;
  49. void SetContentSizeConstraints(
  50. const extensions::SizeConstraints& size_constraints) override;
  51. void SetResizable(bool resizable) override;
  52. void MoveTop() override;
  53. bool IsResizable() override;
  54. void SetMovable(bool movable) override;
  55. void SetAspectRatio(double aspect_ratio,
  56. const gfx::Size& extra_size) override;
  57. void PreviewFile(const std::string& path,
  58. const std::string& display_name) override;
  59. void CloseFilePreview() 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 relativeLevel,
  72. std::string* error) 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. void SetHasShadow(bool has_shadow) override;
  88. bool HasShadow() override;
  89. void SetOpacity(const double opacity) override;
  90. double GetOpacity() override;
  91. void SetRepresentedFilename(const std::string& filename) override;
  92. std::string GetRepresentedFilename() override;
  93. void SetDocumentEdited(bool edited) override;
  94. bool IsDocumentEdited() override;
  95. void SetIgnoreMouseEvents(bool ignore, bool forward) override;
  96. void SetContentProtection(bool enable) override;
  97. void SetFocusable(bool focusable) override;
  98. void AddBrowserView(NativeBrowserView* browser_view) override;
  99. void RemoveBrowserView(NativeBrowserView* browser_view) override;
  100. void SetParentWindow(NativeWindow* parent) override;
  101. gfx::NativeView GetNativeView() const override;
  102. gfx::NativeWindow GetNativeWindow() const override;
  103. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  104. NativeWindowHandle GetNativeWindowHandle() const override;
  105. void SetProgressBar(double progress, const ProgressState state) override;
  106. void SetOverlayIcon(const gfx::Image& overlay,
  107. const std::string& description) override;
  108. void SetVisibleOnAllWorkspaces(bool visible,
  109. bool visibleOnFullScreen) override;
  110. bool IsVisibleOnAllWorkspaces() override;
  111. void SetAutoHideCursor(bool auto_hide) override;
  112. void SelectPreviousTab() override;
  113. void SelectNextTab() override;
  114. void MergeAllWindows() override;
  115. void MoveTabToNewWindow() override;
  116. void ToggleTabBar() override;
  117. bool AddTabbedWindow(NativeWindow* window) override;
  118. bool SetWindowButtonVisibility(bool visible) override;
  119. void SetVibrancy(const std::string& type) override;
  120. void SetTouchBar(
  121. const std::vector<mate::PersistentDictionary>& items) override;
  122. void RefreshTouchBarItem(const std::string& item_id) override;
  123. void SetEscapeTouchBarItem(const mate::PersistentDictionary& item) override;
  124. void SetGTKDarkThemeEnabled(bool use_dark_theme) override {}
  125. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  126. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  127. // Use a custom content view instead of Chromium's BridgedContentView.
  128. void OverrideNSWindowContentView();
  129. // Set the attribute of NSWindow while work around a bug of zoom button.
  130. void SetStyleMask(bool on, NSUInteger flag);
  131. void SetCollectionBehavior(bool on, NSUInteger flag);
  132. enum class TitleBarStyle {
  133. NORMAL,
  134. HIDDEN,
  135. HIDDEN_INSET,
  136. CUSTOM_BUTTONS_ON_HOVER,
  137. };
  138. TitleBarStyle title_bar_style() const { return title_bar_style_; }
  139. AtomPreviewItem* preview_item() const { return preview_item_.get(); }
  140. AtomTouchBar* touch_bar() const { return touch_bar_.get(); }
  141. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  142. bool fullscreen_window_title() const { return fullscreen_window_title_; }
  143. bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
  144. protected:
  145. // views::WidgetDelegate:
  146. bool CanResize() const override;
  147. views::View* GetContentsView() override;
  148. private:
  149. // Add custom layers to the content view.
  150. void AddContentViewLayers(bool minimizable, bool closable);
  151. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  152. void SetForwardMouseMessages(bool forward);
  153. AtomNSWindow* window_; // Weak ref, managed by widget_.
  154. base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_;
  155. base::scoped_nsobject<AtomPreviewItem> preview_item_;
  156. base::scoped_nsobject<AtomTouchBar> touch_bar_;
  157. base::scoped_nsobject<CustomWindowButtonView> buttons_view_;
  158. // Event monitor for scroll wheel event.
  159. id wheel_event_monitor_;
  160. // The NSView that used as contentView of window.
  161. //
  162. // For frameless window it would fill the whole window.
  163. base::scoped_nsobject<NSView> container_view_;
  164. // The views::View that fills the client area.
  165. std::unique_ptr<RootViewMac> root_view_;
  166. bool is_kiosk_ = false;
  167. bool was_fullscreen_ = false;
  168. bool zoom_to_page_width_ = false;
  169. bool fullscreen_window_title_ = false;
  170. bool resizable_ = true;
  171. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  172. // The presentation options before entering kiosk mode.
  173. NSApplicationPresentationOptions kiosk_options_;
  174. // The "titleBarStyle" option.
  175. TitleBarStyle title_bar_style_ = TitleBarStyle::NORMAL;
  176. // The visibility mode of window button controls when explicitly set through
  177. // setWindowButtonVisibility().
  178. base::Optional<bool> window_button_visibility_;
  179. // Simple (pre-Lion) Fullscreen Settings
  180. bool always_simple_fullscreen_ = false;
  181. bool is_simple_fullscreen_ = false;
  182. bool was_maximizable_ = false;
  183. bool was_movable_ = false;
  184. NSRect original_frame_;
  185. NSInteger original_level_;
  186. NSUInteger simple_fullscreen_mask_;
  187. base::scoped_nsobject<NSColor> background_color_before_vibrancy_;
  188. bool transparency_before_vibrancy_ = false;
  189. // The presentation options before entering simple fullscreen mode.
  190. NSApplicationPresentationOptions simple_fullscreen_options_;
  191. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  192. };
  193. } // namespace electron
  194. #endif // SHELL_BROWSER_NATIVE_WINDOW_MAC_H_