native_window.h 13 KB

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