native_window_mac.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <string>
  8. #include <vector>
  9. #include "atom/browser/native_window.h"
  10. #include "base/mac/scoped_nsobject.h"
  11. #include "ui/views/controls/native/native_view_host.h"
  12. @class AtomNSWindow;
  13. @class AtomNSWindowDelegate;
  14. @class AtomPreviewItem;
  15. @class AtomTouchBar;
  16. @class CustomWindowButtonView;
  17. @class FullSizeContentView;
  18. namespace atom {
  19. class RootViewMac;
  20. class NativeWindowMac : public NativeWindow {
  21. public:
  22. NativeWindowMac(const mate::Dictionary& options, NativeWindow* parent);
  23. ~NativeWindowMac() override;
  24. // NativeWindow:
  25. void SetContentView(views::View* view) override;
  26. void Close() override;
  27. void CloseImmediately() override;
  28. void Focus(bool focus) override;
  29. bool IsFocused() override;
  30. void Show() override;
  31. void ShowInactive() override;
  32. void Hide() override;
  33. bool IsVisible() override;
  34. bool IsEnabled() override;
  35. void SetEnabled(bool enable) override;
  36. void Maximize() override;
  37. void Unmaximize() override;
  38. bool IsMaximized() override;
  39. void Minimize() override;
  40. void Restore() override;
  41. bool IsMinimized() override;
  42. void SetFullScreen(bool fullscreen) override;
  43. bool IsFullscreen() const override;
  44. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  45. gfx::Rect GetBounds() override;
  46. void SetContentSizeConstraints(
  47. const extensions::SizeConstraints& size_constraints) override;
  48. void SetResizable(bool resizable) override;
  49. void MoveTop() override;
  50. bool IsResizable() override;
  51. void SetMovable(bool movable) override;
  52. void SetAspectRatio(double aspect_ratio,
  53. const gfx::Size& extra_size) override;
  54. void PreviewFile(const std::string& path,
  55. const std::string& display_name) override;
  56. void CloseFilePreview() override;
  57. bool IsMovable() override;
  58. void SetMinimizable(bool minimizable) override;
  59. bool IsMinimizable() override;
  60. void SetMaximizable(bool maximizable) override;
  61. bool IsMaximizable() override;
  62. void SetFullScreenable(bool fullscreenable) override;
  63. bool IsFullScreenable() override;
  64. void SetClosable(bool closable) override;
  65. bool IsClosable() override;
  66. void SetAlwaysOnTop(bool top,
  67. const std::string& level,
  68. int relativeLevel,
  69. std::string* error) override;
  70. bool IsAlwaysOnTop() override;
  71. void Center() override;
  72. void Invalidate() override;
  73. void SetTitle(const std::string& title) override;
  74. std::string GetTitle() override;
  75. void FlashFrame(bool flash) override;
  76. void SetSkipTaskbar(bool skip) override;
  77. void SetSimpleFullScreen(bool simple_fullscreen) override;
  78. bool IsSimpleFullScreen() override;
  79. void SetKiosk(bool kiosk) override;
  80. bool IsKiosk() override;
  81. void SetBackgroundColor(SkColor color) override;
  82. void SetHasShadow(bool has_shadow) override;
  83. bool HasShadow() override;
  84. void SetOpacity(const double opacity) override;
  85. double GetOpacity() override;
  86. void SetRepresentedFilename(const std::string& filename) override;
  87. std::string GetRepresentedFilename() override;
  88. void SetDocumentEdited(bool edited) override;
  89. bool IsDocumentEdited() override;
  90. void SetIgnoreMouseEvents(bool ignore, bool forward) override;
  91. void SetContentProtection(bool enable) override;
  92. void SetBrowserView(NativeBrowserView* browser_view) override;
  93. void SetParentWindow(NativeWindow* parent) override;
  94. gfx::NativeView GetNativeView() const override;
  95. gfx::NativeWindow GetNativeWindow() const override;
  96. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  97. void SetProgressBar(double progress, const ProgressState state) override;
  98. void SetOverlayIcon(const gfx::Image& overlay,
  99. const std::string& description) override;
  100. void SetVisibleOnAllWorkspaces(bool visible) override;
  101. bool IsVisibleOnAllWorkspaces() override;
  102. void SetAutoHideCursor(bool auto_hide) override;
  103. void SelectPreviousTab() override;
  104. void SelectNextTab() override;
  105. void MergeAllWindows() override;
  106. void MoveTabToNewWindow() override;
  107. void ToggleTabBar() override;
  108. bool AddTabbedWindow(NativeWindow* window) override;
  109. void SetVibrancy(const std::string& type) override;
  110. void SetTouchBar(
  111. const std::vector<mate::PersistentDictionary>& items) override;
  112. void RefreshTouchBarItem(const std::string& item_id) override;
  113. void SetEscapeTouchBarItem(const mate::PersistentDictionary& item) override;
  114. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  115. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  116. // Use a custom content view instead of Chromium's BridgedContentView.
  117. void OverrideNSWindowContentView();
  118. // Set the attribute of NSWindow while work around a bug of zoom button.
  119. void SetStyleMask(bool on, NSUInteger flag);
  120. void SetCollectionBehavior(bool on, NSUInteger flag);
  121. enum TitleBarStyle {
  122. NORMAL,
  123. HIDDEN,
  124. HIDDEN_INSET,
  125. CUSTOM_BUTTONS_ON_HOVER,
  126. };
  127. TitleBarStyle title_bar_style() const { return title_bar_style_; }
  128. AtomPreviewItem* preview_item() const { return preview_item_.get(); }
  129. AtomTouchBar* touch_bar() const { return touch_bar_.get(); }
  130. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  131. bool fullscreen_window_title() const { return fullscreen_window_title_; }
  132. bool simple_fullscreen() const { return always_simple_fullscreen_; }
  133. protected:
  134. // views::WidgetDelegate:
  135. bool CanResize() const override;
  136. views::View* GetContentsView() override;
  137. private:
  138. // Add custom layers to the content view.
  139. void AddContentViewLayers();
  140. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  141. void ShowWindowButton(NSWindowButton button);
  142. void SetForwardMouseMessages(bool forward);
  143. AtomNSWindow* window_; // Weak ref, managed by widget_.
  144. base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_;
  145. base::scoped_nsobject<AtomPreviewItem> preview_item_;
  146. base::scoped_nsobject<AtomTouchBar> touch_bar_;
  147. base::scoped_nsobject<CustomWindowButtonView> buttons_view_;
  148. // Event monitor for scroll wheel event.
  149. id wheel_event_monitor_;
  150. // The view that will fill the whole frameless window.
  151. base::scoped_nsobject<FullSizeContentView> container_view_;
  152. // The view that fills the client area.
  153. std::unique_ptr<RootViewMac> root_view_;
  154. bool is_kiosk_ = false;
  155. bool was_fullscreen_ = false;
  156. bool zoom_to_page_width_ = false;
  157. bool fullscreen_window_title_ = false;
  158. bool resizable_ = true;
  159. NSInteger attention_request_id_ = 0; // identifier from requestUserAttention
  160. // The presentation options before entering kiosk mode.
  161. NSApplicationPresentationOptions kiosk_options_;
  162. // The "titleBarStyle" option.
  163. TitleBarStyle title_bar_style_ = NORMAL;
  164. // Simple (pre-Lion) Fullscreen Settings
  165. bool always_simple_fullscreen_ = false;
  166. bool is_simple_fullscreen_ = false;
  167. bool was_maximizable_ = false;
  168. bool was_movable_ = false;
  169. NSRect original_frame_;
  170. NSInteger original_level_;
  171. NSUInteger simple_fullscreen_mask_;
  172. base::scoped_nsobject<NSColor> background_color_before_vibrancy_;
  173. bool transparency_before_vibrancy_ = false;
  174. // The presentation options before entering simple fullscreen mode.
  175. NSApplicationPresentationOptions simple_fullscreen_options_;
  176. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  177. };
  178. } // namespace atom
  179. #endif // ATOM_BROWSER_NATIVE_WINDOW_MAC_H_