native_window_mac.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Maximize() override;
  34. void Unmaximize() override;
  35. bool IsMaximized() override;
  36. void Minimize() override;
  37. void Restore() override;
  38. bool IsMinimized() override;
  39. void SetFullScreen(bool fullscreen) override;
  40. bool IsFullscreen() const override;
  41. void SetBounds(const gfx::Rect& bounds, bool animate = false) override;
  42. gfx::Rect GetBounds() override;
  43. void SetContentSizeConstraints(
  44. const extensions::SizeConstraints& size_constraints) override;
  45. void SetResizable(bool resizable) override;
  46. bool IsResizable() override;
  47. void SetMovable(bool movable) override;
  48. void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size)
  49. override;
  50. void PreviewFile(const std::string& path, const std::string& display_name)
  51. override;
  52. void CloseFilePreview() override;
  53. bool IsMovable() override;
  54. void SetMinimizable(bool minimizable) override;
  55. bool IsMinimizable() override;
  56. void SetMaximizable(bool maximizable) override;
  57. bool IsMaximizable() override;
  58. void SetFullScreenable(bool fullscreenable) override;
  59. bool IsFullScreenable() override;
  60. void SetClosable(bool closable) override;
  61. bool IsClosable() override;
  62. void SetAlwaysOnTop(bool top, const std::string& level,
  63. int relativeLevel, std::string* error) override;
  64. bool IsAlwaysOnTop() override;
  65. void Center() override;
  66. void Invalidate() override;
  67. void SetTitle(const std::string& title) override;
  68. std::string GetTitle() override;
  69. void FlashFrame(bool flash) override;
  70. void SetSkipTaskbar(bool skip) override;
  71. void SetKiosk(bool kiosk) override;
  72. bool IsKiosk() override;
  73. void SetBackgroundColor(const std::string& color_name) override;
  74. void SetHasShadow(bool has_shadow) override;
  75. bool HasShadow() override;
  76. void SetRepresentedFilename(const std::string& filename) override;
  77. std::string GetRepresentedFilename() override;
  78. void SetDocumentEdited(bool edited) override;
  79. bool IsDocumentEdited() override;
  80. void SetIgnoreMouseEvents(bool ignore) override;
  81. void SetContentProtection(bool enable) override;
  82. void SetBrowserView(NativeBrowserView* browser_view) override;
  83. void SetParentWindow(NativeWindow* parent) override;
  84. gfx::NativeWindow GetNativeWindow() override;
  85. gfx::AcceleratedWidget GetAcceleratedWidget() override;
  86. void SetProgressBar(double progress, const ProgressState state) override;
  87. void SetOverlayIcon(const gfx::Image& overlay,
  88. const std::string& description) override;
  89. void SetVisibleOnAllWorkspaces(bool visible) override;
  90. bool IsVisibleOnAllWorkspaces() override;
  91. void SetAutoHideCursor(bool auto_hide) override;
  92. void SetVibrancy(const std::string& type) override;
  93. void SetTouchBar(
  94. const std::vector<mate::PersistentDictionary>& items) override;
  95. void RefreshTouchBarItem(const std::string& item_id) override;
  96. void SetEscapeTouchBarItem(const mate::PersistentDictionary& item) override;
  97. // content::RenderWidgetHost::InputEventObserver:
  98. void OnInputEvent(const blink::WebInputEvent& event) override;
  99. // content::WebContentsObserver:
  100. void RenderViewHostChanged(content::RenderViewHost* old_host,
  101. content::RenderViewHost* new_host) override;
  102. // Refresh the DraggableRegion views.
  103. void UpdateDraggableRegionViews() {
  104. UpdateDraggableRegionViews(draggable_regions_);
  105. }
  106. // Set the attribute of NSWindow while work around a bug of zoom button.
  107. void SetStyleMask(bool on, NSUInteger flag);
  108. void SetCollectionBehavior(bool on, NSUInteger flag);
  109. enum TitleBarStyle {
  110. NORMAL,
  111. HIDDEN,
  112. HIDDEN_INSET,
  113. };
  114. TitleBarStyle title_bar_style() const { return title_bar_style_; }
  115. bool zoom_to_page_width() const { return zoom_to_page_width_; }
  116. protected:
  117. // Return a vector of non-draggable regions that fill a window of size
  118. // |width| by |height|, but leave gaps where the window should be draggable.
  119. std::vector<gfx::Rect> CalculateNonDraggableRegions(
  120. const std::vector<DraggableRegion>& regions, int width, int height);
  121. private:
  122. // NativeWindow:
  123. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds);
  124. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds);
  125. void UpdateDraggableRegions(
  126. const std::vector<DraggableRegion>& regions) override;
  127. void ShowWindowButton(NSWindowButton button);
  128. void InstallView();
  129. void UninstallView();
  130. // Install the drag view, which will cover the whole window and decides
  131. // whehter we can drag.
  132. void UpdateDraggableRegionViews(const std::vector<DraggableRegion>& regions);
  133. void RegisterInputEventObserver(content::RenderViewHost* host);
  134. void UnregisterInputEventObserver(content::RenderViewHost* host);
  135. base::scoped_nsobject<AtomNSWindow> window_;
  136. base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_;
  137. // Event monitor for scroll wheel event.
  138. id wheel_event_monitor_;
  139. // The view that will fill the whole frameless window.
  140. base::scoped_nsobject<FullSizeContentView> content_view_;
  141. NativeBrowserView* browser_view_;
  142. std::vector<DraggableRegion> draggable_regions_;
  143. bool is_kiosk_;
  144. bool was_fullscreen_;
  145. bool zoom_to_page_width_;
  146. NSInteger attention_request_id_; // identifier from requestUserAttention
  147. // The presentation options before entering kiosk mode.
  148. NSApplicationPresentationOptions kiosk_options_;
  149. // The "titleBarStyle" option.
  150. TitleBarStyle title_bar_style_;
  151. DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
  152. };
  153. } // namespace atom
  154. #endif // ATOM_BROWSER_NATIVE_WINDOW_MAC_H_