native_window_mac.h 7.9 KB

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