native_window.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_H_
  5. #define ATOM_BROWSER_NATIVE_WINDOW_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "atom/browser/native_window_observer.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "base/supports_user_data.h"
  14. #include "content/public/browser/web_contents_user_data.h"
  15. #include "extensions/browser/app_window/size_constraints.h"
  16. #include "ui/views/widget/widget_delegate.h"
  17. class SkRegion;
  18. namespace content {
  19. struct NativeWebKeyboardEvent;
  20. }
  21. namespace gfx {
  22. class Image;
  23. class Point;
  24. class Rect;
  25. class RectF;
  26. class Size;
  27. } // namespace gfx
  28. namespace mate {
  29. class Dictionary;
  30. class PersistentDictionary;
  31. } // namespace mate
  32. namespace atom {
  33. class AtomMenuModel;
  34. class NativeBrowserView;
  35. struct DraggableRegion;
  36. class NativeWindow : public base::SupportsUserData,
  37. public views::WidgetDelegate {
  38. public:
  39. ~NativeWindow() override;
  40. // Create window with existing WebContents, the caller is responsible for
  41. // managing the window's live.
  42. static NativeWindow* Create(const mate::Dictionary& options,
  43. NativeWindow* parent = nullptr);
  44. void InitFromOptions(const mate::Dictionary& options);
  45. virtual void SetContentView(views::View* view) = 0;
  46. virtual void Close() = 0;
  47. virtual void CloseImmediately() = 0;
  48. virtual bool IsClosed() const;
  49. virtual void Focus(bool focus) = 0;
  50. virtual bool IsFocused() = 0;
  51. virtual void Show() = 0;
  52. virtual void ShowInactive() = 0;
  53. virtual void Hide() = 0;
  54. virtual bool IsVisible() = 0;
  55. virtual bool IsEnabled() = 0;
  56. virtual void SetEnabled(bool enable) = 0;
  57. virtual void Maximize() = 0;
  58. virtual void Unmaximize() = 0;
  59. virtual bool IsMaximized() = 0;
  60. virtual void Minimize() = 0;
  61. virtual void Restore() = 0;
  62. virtual bool IsMinimized() = 0;
  63. virtual void SetFullScreen(bool fullscreen) = 0;
  64. virtual bool IsFullscreen() const = 0;
  65. virtual void SetBounds(const gfx::Rect& bounds, bool animate = false) = 0;
  66. virtual gfx::Rect GetBounds() = 0;
  67. virtual void SetSize(const gfx::Size& size, bool animate = false);
  68. virtual gfx::Size GetSize();
  69. virtual void SetPosition(const gfx::Point& position, bool animate = false);
  70. virtual gfx::Point GetPosition();
  71. virtual void SetContentSize(const gfx::Size& size, bool animate = false);
  72. virtual gfx::Size GetContentSize();
  73. virtual void SetContentBounds(const gfx::Rect& bounds, bool animate = false);
  74. virtual gfx::Rect GetContentBounds();
  75. virtual void SetSizeConstraints(
  76. const extensions::SizeConstraints& size_constraints);
  77. virtual extensions::SizeConstraints GetSizeConstraints() const;
  78. virtual void SetContentSizeConstraints(
  79. const extensions::SizeConstraints& size_constraints);
  80. virtual extensions::SizeConstraints GetContentSizeConstraints() const;
  81. virtual void SetMinimumSize(const gfx::Size& size);
  82. virtual gfx::Size GetMinimumSize() const;
  83. virtual void SetMaximumSize(const gfx::Size& size);
  84. virtual gfx::Size GetMaximumSize() const;
  85. virtual gfx::Size GetContentMinimumSize() const;
  86. virtual gfx::Size GetContentMaximumSize() const;
  87. virtual void SetSheetOffset(const double offsetX, const double offsetY);
  88. virtual double GetSheetOffsetX();
  89. virtual double GetSheetOffsetY();
  90. virtual void SetResizable(bool resizable) = 0;
  91. #if defined(OS_WIN) || defined(OS_MACOSX)
  92. virtual void MoveTop() = 0;
  93. #endif
  94. virtual bool IsResizable() = 0;
  95. virtual void SetMovable(bool movable) = 0;
  96. virtual bool IsMovable() = 0;
  97. virtual void SetMinimizable(bool minimizable) = 0;
  98. virtual bool IsMinimizable() = 0;
  99. virtual void SetMaximizable(bool maximizable) = 0;
  100. virtual bool IsMaximizable() = 0;
  101. virtual void SetFullScreenable(bool fullscreenable) = 0;
  102. virtual bool IsFullScreenable() = 0;
  103. virtual void SetClosable(bool closable) = 0;
  104. virtual bool IsClosable() = 0;
  105. virtual void SetAlwaysOnTop(bool top,
  106. const std::string& level = "floating",
  107. int relativeLevel = 0,
  108. std::string* error = nullptr) = 0;
  109. virtual bool IsAlwaysOnTop() = 0;
  110. virtual void Center() = 0;
  111. virtual void Invalidate() = 0;
  112. virtual void SetTitle(const std::string& title) = 0;
  113. virtual std::string GetTitle() = 0;
  114. virtual void FlashFrame(bool flash) = 0;
  115. virtual void SetSkipTaskbar(bool skip) = 0;
  116. virtual void SetSimpleFullScreen(bool simple_fullscreen) = 0;
  117. virtual bool IsSimpleFullScreen() = 0;
  118. virtual void SetKiosk(bool kiosk) = 0;
  119. virtual bool IsKiosk() = 0;
  120. virtual void SetBackgroundColor(SkColor color) = 0;
  121. virtual void SetHasShadow(bool has_shadow) = 0;
  122. virtual bool HasShadow() = 0;
  123. virtual void SetOpacity(const double opacity) = 0;
  124. virtual double GetOpacity() = 0;
  125. virtual void SetRepresentedFilename(const std::string& filename);
  126. virtual std::string GetRepresentedFilename();
  127. virtual void SetDocumentEdited(bool edited);
  128. virtual bool IsDocumentEdited();
  129. virtual void SetIgnoreMouseEvents(bool ignore, bool forward) = 0;
  130. virtual void SetContentProtection(bool enable) = 0;
  131. virtual void SetFocusable(bool focusable);
  132. virtual void SetMenu(AtomMenuModel* menu);
  133. virtual void SetParentWindow(NativeWindow* parent);
  134. virtual void SetBrowserView(NativeBrowserView* browser_view) = 0;
  135. virtual gfx::NativeView GetNativeView() const = 0;
  136. virtual gfx::NativeWindow GetNativeWindow() const = 0;
  137. virtual gfx::AcceleratedWidget GetAcceleratedWidget() const = 0;
  138. // Taskbar/Dock APIs.
  139. enum ProgressState {
  140. PROGRESS_NONE, // no progress, no marking
  141. PROGRESS_INDETERMINATE, // progress, indeterminate
  142. PROGRESS_ERROR, // progress, errored (red)
  143. PROGRESS_PAUSED, // progress, paused (yellow)
  144. PROGRESS_NORMAL, // progress, not marked (green)
  145. };
  146. virtual void SetProgressBar(double progress, const ProgressState state) = 0;
  147. virtual void SetOverlayIcon(const gfx::Image& overlay,
  148. const std::string& description) = 0;
  149. // Workspace APIs.
  150. virtual void SetVisibleOnAllWorkspaces(bool visible) = 0;
  151. virtual bool IsVisibleOnAllWorkspaces() = 0;
  152. virtual void SetAutoHideCursor(bool auto_hide);
  153. // Vibrancy API
  154. virtual void SetVibrancy(const std::string& type);
  155. // Touchbar API
  156. virtual void SetTouchBar(
  157. const std::vector<mate::PersistentDictionary>& items);
  158. virtual void RefreshTouchBarItem(const std::string& item_id);
  159. virtual void SetEscapeTouchBarItem(const mate::PersistentDictionary& item);
  160. // Native Tab API
  161. virtual void SelectPreviousTab();
  162. virtual void SelectNextTab();
  163. virtual void MergeAllWindows();
  164. virtual void MoveTabToNewWindow();
  165. virtual void ToggleTabBar();
  166. virtual bool AddTabbedWindow(NativeWindow* window);
  167. // Toggle the menu bar.
  168. virtual void SetAutoHideMenuBar(bool auto_hide);
  169. virtual bool IsMenuBarAutoHide();
  170. virtual void SetMenuBarVisibility(bool visible);
  171. virtual bool IsMenuBarVisible();
  172. // Set the aspect ratio when resizing window.
  173. double GetAspectRatio();
  174. gfx::Size GetAspectRatioExtraSize();
  175. virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
  176. // File preview APIs.
  177. virtual void PreviewFile(const std::string& path,
  178. const std::string& display_name);
  179. virtual void CloseFilePreview();
  180. // Converts between content bounds and window bounds.
  181. virtual gfx::Rect ContentBoundsToWindowBounds(
  182. const gfx::Rect& bounds) const = 0;
  183. virtual gfx::Rect WindowBoundsToContentBounds(
  184. const gfx::Rect& bounds) const = 0;
  185. base::WeakPtr<NativeWindow> GetWeakPtr() {
  186. return weak_factory_.GetWeakPtr();
  187. }
  188. // Methods called by the WebContents.
  189. virtual void HandleKeyboardEvent(
  190. content::WebContents*,
  191. const content::NativeWebKeyboardEvent& event) {}
  192. // Public API used by platform-dependent delegates and observers to send UI
  193. // related notifications.
  194. void NotifyWindowRequestPreferredWith(int* width);
  195. void NotifyWindowCloseButtonClicked();
  196. void NotifyWindowClosed();
  197. void NotifyWindowEndSession();
  198. void NotifyWindowBlur();
  199. void NotifyWindowFocus();
  200. void NotifyWindowShow();
  201. void NotifyWindowHide();
  202. void NotifyWindowMaximize();
  203. void NotifyWindowUnmaximize();
  204. void NotifyWindowMinimize();
  205. void NotifyWindowRestore();
  206. void NotifyWindowMove();
  207. void NotifyWindowResize();
  208. void NotifyWindowMoved();
  209. void NotifyWindowScrollTouchBegin();
  210. void NotifyWindowScrollTouchEnd();
  211. void NotifyWindowSwipe(const std::string& direction);
  212. void NotifyWindowSheetBegin();
  213. void NotifyWindowSheetEnd();
  214. void NotifyWindowEnterFullScreen();
  215. void NotifyWindowLeaveFullScreen();
  216. void NotifyWindowEnterHtmlFullScreen();
  217. void NotifyWindowLeaveHtmlFullScreen();
  218. void NotifyWindowExecuteWindowsCommand(const std::string& command);
  219. void NotifyTouchBarItemInteraction(const std::string& item_id,
  220. const base::DictionaryValue& details);
  221. void NotifyNewWindowForTab();
  222. #if defined(OS_WIN)
  223. void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
  224. #endif
  225. void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); }
  226. void RemoveObserver(NativeWindowObserver* obs) {
  227. observers_.RemoveObserver(obs);
  228. }
  229. views::Widget* widget() const { return widget_.get(); }
  230. views::View* content_view() const { return content_view_; }
  231. bool has_frame() const { return has_frame_; }
  232. void set_has_frame(bool has_frame) { has_frame_ = has_frame; }
  233. bool transparent() const { return transparent_; }
  234. bool enable_larger_than_screen() const { return enable_larger_than_screen_; }
  235. NativeBrowserView* browser_view() const { return browser_view_; }
  236. NativeWindow* parent() const { return parent_; }
  237. bool is_modal() const { return is_modal_; }
  238. protected:
  239. NativeWindow(const mate::Dictionary& options, NativeWindow* parent);
  240. // views::WidgetDelegate:
  241. views::Widget* GetWidget() override;
  242. const views::Widget* GetWidget() const override;
  243. void set_content_view(views::View* view) { content_view_ = view; }
  244. void set_browser_view(NativeBrowserView* browser_view) {
  245. browser_view_ = browser_view;
  246. }
  247. private:
  248. std::unique_ptr<views::Widget> widget_;
  249. // The content view, weak ref.
  250. views::View* content_view_ = nullptr;
  251. // Whether window has standard frame.
  252. bool has_frame_ = true;
  253. // Whether window is transparent.
  254. bool transparent_ = false;
  255. // Minimum and maximum size, stored as content size.
  256. extensions::SizeConstraints size_constraints_;
  257. // Whether window can be resized larger than screen.
  258. bool enable_larger_than_screen_ = false;
  259. // The windows has been closed.
  260. bool is_closed_ = false;
  261. // Used to display sheets at the appropriate horizontal and vertical offsets
  262. // on macOS.
  263. double sheet_offset_x_ = 0.0;
  264. double sheet_offset_y_ = 0.0;
  265. // Used to maintain the aspect ratio of a view which is inside of the
  266. // content view.
  267. double aspect_ratio_ = 0.0;
  268. gfx::Size aspect_ratio_extraSize_;
  269. // The parent window, it is guaranteed to be valid during this window's life.
  270. NativeWindow* parent_ = nullptr;
  271. // Is this a modal window.
  272. bool is_modal_ = false;
  273. // The browser view layer.
  274. NativeBrowserView* browser_view_ = nullptr;
  275. // Observers of this window.
  276. base::ObserverList<NativeWindowObserver> observers_;
  277. base::WeakPtrFactory<NativeWindow> weak_factory_;
  278. DISALLOW_COPY_AND_ASSIGN(NativeWindow);
  279. };
  280. // This class provides a hook to get a NativeWindow from a WebContents.
  281. class NativeWindowRelay
  282. : public content::WebContentsUserData<NativeWindowRelay> {
  283. public:
  284. explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window);
  285. ~NativeWindowRelay() override;
  286. static void* UserDataKey() {
  287. return content::WebContentsUserData<NativeWindowRelay>::UserDataKey();
  288. }
  289. void* key;
  290. base::WeakPtr<NativeWindow> window;
  291. private:
  292. friend class content::WebContentsUserData<NativeWindow>;
  293. };
  294. } // namespace atom
  295. #endif // ATOM_BROWSER_NATIVE_WINDOW_H_