native_window_mac.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 ATOM_BROWSER_NATIVE_WINDOW_MAC_H_
  5. #define ATOM_BROWSER_NATIVE_WINDOW_MAC_H_
  6. #import <Cocoa/Cocoa.h>
  7. #include <memory>
  8. #include <string>
  9. #include <tuple>
  10. #include <vector>
  11. #include "atom/browser/native_window.h"
  12. #include "base/mac/scoped_nsobject.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 atom {
  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(bool top,
  70. const std::string& level,
  71. int relativeLevel,
  72. std::string* error) override;
  73. bool IsAlwaysOnTop() 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 AddBrowserView(NativeBrowserView* browser_view) override;
  98. void RemoveBrowserView(NativeBrowserView* browser_view) override;
  99. void SetParentWindow(NativeWindow* parent) override;
  100. gfx::NativeView GetNativeView() const override;
  101. gfx::NativeWindow GetNativeWindow() const override;
  102. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  103. NativeWindowHandle GetNativeWindowHandle() const override;
  104. void SetProgressBar(double progress, const ProgressState state) override;
  105. void SetOverlayIcon(const gfx::Image& overlay,
  106. const std::string& description) override;
  107. void SetVisibleOnAllWorkspaces(bool visible,
  108. bool visibleOnFullScreen) override;
  109. bool IsVisibleOnAllWorkspaces() override;
  110. void SetAutoHideCursor(bool auto_hide) override;
  111. void SelectPreviousTab() override;
  112. void SelectNextTab() override;
  113. void MergeAllWindows() override;
  114. void MoveTabToNewWindow() override;
  115. void ToggleTabBar() override;
  116. bool AddTabbedWindow(NativeWindow* window) override;
  117. bool SetWindowButtonVisibility(bool visible) override;
  118. void SetVibrancy(const std::string& type) override;
  119. void SetTouchBar(
  120. const std::vector<mate::PersistentDictionary>& items) override;
  121. void RefreshTouchBarItem(const std::string& item_id) override;
  122. void SetEscapeTouchBarItem(const mate::PersistentDictionary& item) override;
  123. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  124. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  125. // Use a custom content view instead of Chromium's BridgedContentView.
  126. void OverrideNSWindowContentView();
  127. // Set the attribute of NSWindow while work around a bug of zoom button.
  128. void SetStyleMask(bool on, NSUInteger flag);
  129. void SetCollectionBehavior(bool on, NSUInteger flag);
  130. enum TitleBarStyle {
  131. NORMAL,
  132. HIDDEN,
  133. HIDDEN_INSET,
  134. CUSTOM_BUTTONS_ON_HOVER,
  135. };
  136. TitleBarStyle title_bar_style() const { return title_bar_style_; }
  137. AtomPreviewItem* preview_item() const { return preview_item_.get(); }
  138. AtomTouchBar* touch_bar() const { return touch_bar_.get(); }
  139. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  140. bool fullscreen_window_title() const { return fullscreen_window_title_; }
  141. bool always_simple_fullscreen() const { return always_simple_fullscreen_; }
  142. protected:
  143. // views::WidgetDelegate:
  144. bool CanResize() const override;
  145. views::View* GetContentsView() override;
  146. private:
  147. // Add custom layers to the content view.
  148. void AddContentViewLayers(bool minimizable, bool closable);
  149. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  150. void SetForwardMouseMessages(bool forward);
  151. AtomNSWindow* window_; // Weak ref, managed by widget_.
  152. base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_;
  153. base::scoped_nsobject<AtomPreviewItem> preview_item_;
  154. base::scoped_nsobject<AtomTouchBar> touch_bar_;
  155. base::scoped_nsobject<CustomWindowButtonView> buttons_view_;
  156. // Event monitor for scroll wheel event.
  157. id wheel_event_monitor_;
  158. // The NSView that used as contentView of window.
  159. //
  160. // For frameless window it would fill the whole window.
  161. base::scoped_nsobject<NSView> container_view_;
  162. // The views::View that fills the client area.
  163. std::unique_ptr<RootViewMac> root_view_;
  164. bool is_kiosk_ = false;
  165. bool was_fullscreen_ = false;
  166. bool zoom_to_page_width_ = false;
  167. bool fullscreen_window_title_ = false;
  168. bool resizable_ = true;
  169. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  170. // The presentation options before entering kiosk mode.
  171. NSApplicationPresentationOptions kiosk_options_;
  172. // The "titleBarStyle" option.
  173. TitleBarStyle title_bar_style_ = NORMAL;
  174. // The visibility mode of window button controls when explicitly set through
  175. // setWindowButtonVisibility().
  176. base::Optional<bool> window_button_visibility_;
  177. // Simple (pre-Lion) Fullscreen Settings
  178. bool always_simple_fullscreen_ = false;
  179. bool is_simple_fullscreen_ = false;
  180. bool was_maximizable_ = false;
  181. bool was_movable_ = false;
  182. NSRect original_frame_;
  183. NSInteger original_level_;
  184. NSUInteger simple_fullscreen_mask_;
  185. base::scoped_nsobject<NSColor> background_color_before_vibrancy_;
  186. bool transparency_before_vibrancy_ = false;
  187. // The presentation options before entering simple fullscreen mode.
  188. NSApplicationPresentationOptions simple_fullscreen_options_;
  189. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  190. };
  191. } // namespace atom
  192. #endif // ATOM_BROWSER_NATIVE_WINDOW_MAC_H_