native_window_mac.h 7.9 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 <string>
  8. #include <vector>
  9. #include "atom/browser/native_window.h"
  10. #include "base/mac/scoped_nsobject.h"
  11. #include "content/public/browser/render_widget_host.h"
  12. @class AtomNSWindow;
  13. @class AtomNSWindowDelegate;
  14. @class FullSizeContentView;
  15. namespace atom {
  16. class NativeWindowMac : public NativeWindow,
  17. public content::RenderWidgetHost::InputEventObserver {
  18. public:
  19. NativeWindowMac(brightray::InspectableWebContents* inspectable_web_contents,
  20. const mate::Dictionary& options,
  21. NativeWindow* parent);
  22. ~NativeWindowMac() override;
  23. // NativeWindow:
  24. void Close() override;
  25. void CloseImmediately() override;
  26. void Focus(bool focus) override;
  27. bool IsFocused() override;
  28. void Show() override;
  29. void ShowInactive() override;
  30. void Hide() override;
  31. bool IsVisible() override;
  32. bool IsEnabled() override;
  33. void SetEnabled(bool enable) override;
  34. void Maximize() override;
  35. void Unmaximize() override;
  36. bool IsMaximized() override;
  37. void Minimize() override;
  38. void Restore() override;
  39. bool IsMinimized() override;
  40. void SetFullScreen(bool fullscreen) override;
  41. bool IsFullscreen() const override;
  42. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  43. gfx::Rect GetBounds() override;
  44. void SetContentSizeConstraints(
  45. const extensions::SizeConstraints& size_constraints) override;
  46. void SetResizable(bool resizable) override;
  47. bool IsResizable() override;
  48. void SetMovable(bool movable) override;
  49. void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size)
  50. override;
  51. void PreviewFile(const std::string& path, const std::string& display_name)
  52. override;
  53. void CloseFilePreview() override;
  54. bool IsMovable() override;
  55. void SetMinimizable(bool minimizable) override;
  56. bool IsMinimizable() override;
  57. void SetMaximizable(bool maximizable) override;
  58. bool IsMaximizable() override;
  59. void SetFullScreenable(bool fullscreenable) override;
  60. bool IsFullScreenable() override;
  61. void SetClosable(bool closable) override;
  62. bool IsClosable() override;
  63. void SetAlwaysOnTop(bool top, const std::string& level,
  64. int relativeLevel, std::string* error) override;
  65. bool IsAlwaysOnTop() override;
  66. void Center() override;
  67. void Invalidate() override;
  68. void SetTitle(const std::string& title) override;
  69. std::string GetTitle() override;
  70. void FlashFrame(bool flash) override;
  71. void SetSkipTaskbar(bool skip) override;
  72. void SetSimpleFullScreen(bool simple_fullscreen) override;
  73. bool IsSimpleFullScreen() override;
  74. void SetKiosk(bool kiosk) override;
  75. bool IsKiosk() override;
  76. void SetBackgroundColor(const std::string& color_name) override;
  77. void SetHasShadow(bool has_shadow) override;
  78. bool HasShadow() override;
  79. void SetOpacity(const double opacity) override;
  80. double GetOpacity() override;
  81. void SetRepresentedFilename(const std::string& filename) override;
  82. std::string GetRepresentedFilename() override;
  83. void SetDocumentEdited(bool edited) override;
  84. bool IsDocumentEdited() override;
  85. void SetIgnoreMouseEvents(bool ignore, bool) override;
  86. void SetContentProtection(bool enable) override;
  87. void SetBrowserView(NativeBrowserView* browser_view) override;
  88. void SetParentWindow(NativeWindow* parent) override;
  89. gfx::NativeView GetNativeView() const override;
  90. gfx::NativeWindow GetNativeWindow() const override;
  91. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  92. void SetProgressBar(double progress, const ProgressState state) override;
  93. void SetOverlayIcon(const gfx::Image& overlay,
  94. const std::string& description) override;
  95. void SetVisibleOnAllWorkspaces(bool visible) override;
  96. bool IsVisibleOnAllWorkspaces() override;
  97. void SetAutoHideCursor(bool auto_hide) override;
  98. void SelectPreviousTab() override;
  99. void SelectNextTab() override;
  100. void MergeAllWindows() override;
  101. void MoveTabToNewWindow() override;
  102. void ToggleTabBar() override;
  103. bool AddTabbedWindow(NativeWindow* window) override;
  104. void SetVibrancy(const std::string& type) override;
  105. void SetTouchBar(
  106. const std::vector<mate::PersistentDictionary>& items) override;
  107. void RefreshTouchBarItem(const std::string& item_id) override;
  108. void SetEscapeTouchBarItem(const mate::PersistentDictionary& item) override;
  109. // content::RenderWidgetHost::InputEventObserver:
  110. void OnInputEvent(const blink::WebInputEvent& event) override;
  111. // content::WebContentsObserver:
  112. void RenderViewHostChanged(content::RenderViewHost* old_host,
  113. content::RenderViewHost* new_host) override;
  114. // Refresh the DraggableRegion views.
  115. void UpdateDraggableRegionViews() override {
  116. UpdateDraggableRegionViews(draggable_regions_);
  117. }
  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. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  129. bool fullscreen_window_title() const { return fullscreen_window_title_; }
  130. bool simple_fullscreen() const { return always_simple_fullscreen_; }
  131. protected:
  132. // Return a vector of non-draggable regions that fill a window of size
  133. // |width| by |height|, but leave gaps where the window should be draggable.
  134. std::vector<gfx::Rect> CalculateNonDraggableRegions(
  135. const std::vector<DraggableRegion>& regions, int width, int height);
  136. private:
  137. // NativeWindow:
  138. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const;
  139. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const;
  140. void UpdateDraggableRegions(
  141. content::RenderFrameHost* rfh,
  142. const std::vector<DraggableRegion>& regions) override;
  143. void InternalSetParentWindow(NativeWindow* parent, bool attach);
  144. void ShowWindowButton(NSWindowButton button);
  145. void InstallView();
  146. void UninstallView();
  147. // Install the drag view, which will cover the whole window and decides
  148. // whether we can drag.
  149. void UpdateDraggableRegionViews(const std::vector<DraggableRegion>& regions);
  150. void RegisterInputEventObserver(content::RenderViewHost* host);
  151. void UnregisterInputEventObserver(content::RenderViewHost* host);
  152. void SetRenderWidgetHostOpaque(bool opaque);
  153. base::scoped_nsobject<AtomNSWindow> window_;
  154. base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_;
  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> content_view_;
  159. NativeBrowserView* browser_view_;
  160. std::vector<DraggableRegion> draggable_regions_;
  161. bool is_kiosk_;
  162. bool was_fullscreen_;
  163. bool zoom_to_page_width_;
  164. bool fullscreen_window_title_;
  165. NSInteger attention_request_id_; // identifier from requestUserAttention
  166. // The presentation options before entering kiosk mode.
  167. NSApplicationPresentationOptions kiosk_options_;
  168. // The "titleBarStyle" option.
  169. TitleBarStyle title_bar_style_;
  170. // Simple (pre-Lion) Fullscreen Settings
  171. bool always_simple_fullscreen_;
  172. bool is_simple_fullscreen_;
  173. bool was_maximizable_;
  174. bool was_movable_;
  175. NSRect original_frame_;
  176. NSInteger original_level_;
  177. NSUInteger simple_fullscreen_mask_;
  178. base::scoped_nsobject<NSColor> background_color_before_vibrancy_;
  179. bool transparency_before_vibrancy_;
  180. // The presentation options before entering simple fullscreen mode.
  181. NSApplicationPresentationOptions simple_fullscreen_options_;
  182. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  183. };
  184. } // namespace atom
  185. #endif // ATOM_BROWSER_NATIVE_WINDOW_MAC_H_