native_window_mac.h 7.9 KB

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