native_window.h 13 KB

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