native_window.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 SHELL_BROWSER_NATIVE_WINDOW_H_
  5. #define SHELL_BROWSER_NATIVE_WINDOW_H_
  6. #include <list>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <tuple>
  11. #include <vector>
  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 "shell/browser/native_window_observer.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 electron {
  35. class AtomMenuModel;
  36. class NativeBrowserView;
  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. virtual void MoveTop() = 0;
  100. virtual bool IsResizable() = 0;
  101. virtual void SetMovable(bool movable) = 0;
  102. virtual bool IsMovable() = 0;
  103. virtual void SetMinimizable(bool minimizable) = 0;
  104. virtual bool IsMinimizable() = 0;
  105. virtual void SetMaximizable(bool maximizable) = 0;
  106. virtual bool IsMaximizable() = 0;
  107. virtual void SetFullScreenable(bool fullscreenable) = 0;
  108. virtual bool IsFullScreenable() = 0;
  109. virtual void SetClosable(bool closable) = 0;
  110. virtual bool IsClosable() = 0;
  111. virtual void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  112. const std::string& level = "floating",
  113. int relativeLevel = 0,
  114. std::string* error = nullptr) = 0;
  115. virtual ui::ZOrderLevel GetZOrderLevel() = 0;
  116. virtual void Center() = 0;
  117. virtual void Invalidate() = 0;
  118. virtual void SetTitle(const std::string& title) = 0;
  119. virtual std::string GetTitle() = 0;
  120. virtual void FlashFrame(bool flash) = 0;
  121. virtual void SetSkipTaskbar(bool skip) = 0;
  122. virtual void SetExcludedFromShownWindowsMenu(bool excluded) = 0;
  123. virtual bool IsExcludedFromShownWindowsMenu() = 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 AddBrowserView(NativeBrowserView* browser_view) = 0;
  143. virtual void RemoveBrowserView(NativeBrowserView* browser_view) = 0;
  144. virtual gfx::NativeView GetNativeView() const = 0;
  145. virtual gfx::NativeWindow GetNativeWindow() const = 0;
  146. virtual gfx::AcceleratedWidget GetAcceleratedWidget() const = 0;
  147. virtual NativeWindowHandle GetNativeWindowHandle() const = 0;
  148. // Taskbar/Dock APIs.
  149. enum class ProgressState {
  150. kNone, // no progress, no marking
  151. kIndeterminate, // progress, indeterminate
  152. kError, // progress, errored (red)
  153. kPaused, // progress, paused (yellow)
  154. kNormal, // progress, not marked (green)
  155. };
  156. virtual void SetProgressBar(double progress, const ProgressState state) = 0;
  157. virtual void SetOverlayIcon(const gfx::Image& overlay,
  158. const std::string& description) = 0;
  159. // Workspace APIs.
  160. virtual void SetVisibleOnAllWorkspaces(bool visible,
  161. bool visibleOnFullScreen = false) = 0;
  162. virtual bool IsVisibleOnAllWorkspaces() = 0;
  163. virtual void SetAutoHideCursor(bool auto_hide);
  164. // Vibrancy API
  165. virtual void SetVibrancy(const std::string& type);
  166. // Touchbar API
  167. virtual void SetTouchBar(
  168. const std::vector<mate::PersistentDictionary>& items);
  169. virtual void RefreshTouchBarItem(const std::string& item_id);
  170. virtual void SetEscapeTouchBarItem(const mate::PersistentDictionary& item);
  171. // Native Tab API
  172. virtual void SelectPreviousTab();
  173. virtual void SelectNextTab();
  174. virtual void MergeAllWindows();
  175. virtual void MoveTabToNewWindow();
  176. virtual void ToggleTabBar();
  177. virtual bool AddTabbedWindow(NativeWindow* window);
  178. // Returns false if unsupported.
  179. virtual bool SetWindowButtonVisibility(bool visible);
  180. // Toggle the menu bar.
  181. virtual void SetAutoHideMenuBar(bool auto_hide);
  182. virtual bool IsMenuBarAutoHide();
  183. virtual void SetMenuBarVisibility(bool visible);
  184. virtual bool IsMenuBarVisible();
  185. // Set the aspect ratio when resizing window.
  186. double GetAspectRatio();
  187. gfx::Size GetAspectRatioExtraSize();
  188. virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
  189. // File preview APIs.
  190. virtual void PreviewFile(const std::string& path,
  191. const std::string& display_name);
  192. virtual void CloseFilePreview();
  193. virtual void SetGTKDarkThemeEnabled(bool use_dark_theme) = 0;
  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 NotifyWindowRotateGesture(float rotation);
  230. void NotifyWindowSheetBegin();
  231. void NotifyWindowSheetEnd();
  232. void NotifyWindowEnterFullScreen();
  233. void NotifyWindowLeaveFullScreen();
  234. void NotifyWindowEnterHtmlFullScreen();
  235. void NotifyWindowLeaveHtmlFullScreen();
  236. void NotifyWindowAlwaysOnTopChanged();
  237. void NotifyWindowExecuteAppCommand(const std::string& command);
  238. void NotifyTouchBarItemInteraction(const std::string& item_id,
  239. const base::DictionaryValue& details);
  240. void NotifyNewWindowForTab();
  241. #if defined(OS_WIN)
  242. void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
  243. #endif
  244. void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); }
  245. void RemoveObserver(NativeWindowObserver* obs) {
  246. observers_.RemoveObserver(obs);
  247. }
  248. views::Widget* widget() const { return widget_.get(); }
  249. views::View* content_view() const { return content_view_; }
  250. bool has_frame() const { return has_frame_; }
  251. void set_has_frame(bool has_frame) { has_frame_ = has_frame; }
  252. bool transparent() const { return transparent_; }
  253. bool enable_larger_than_screen() const { return enable_larger_than_screen_; }
  254. NativeWindow* parent() const { return parent_; }
  255. bool is_modal() const { return is_modal_; }
  256. std::list<NativeBrowserView*> browser_views() const { return browser_views_; }
  257. protected:
  258. NativeWindow(const mate::Dictionary& options, NativeWindow* parent);
  259. // views::WidgetDelegate:
  260. views::Widget* GetWidget() override;
  261. const views::Widget* GetWidget() const override;
  262. void set_content_view(views::View* view) { content_view_ = view; }
  263. void add_browser_view(NativeBrowserView* browser_view) {
  264. browser_views_.push_back(browser_view);
  265. }
  266. void remove_browser_view(NativeBrowserView* browser_view) {
  267. browser_views_.remove_if(
  268. [&browser_view](NativeBrowserView* n) { return (n == browser_view); });
  269. }
  270. private:
  271. std::unique_ptr<views::Widget> widget_;
  272. // The content view, weak ref.
  273. views::View* content_view_ = nullptr;
  274. // Whether window has standard frame.
  275. bool has_frame_ = true;
  276. // Whether window is transparent.
  277. bool transparent_ = false;
  278. // Minimum and maximum size, stored as content size.
  279. extensions::SizeConstraints size_constraints_;
  280. // Whether window can be resized larger than screen.
  281. bool enable_larger_than_screen_ = false;
  282. // The windows has been closed.
  283. bool is_closed_ = false;
  284. // Used to display sheets at the appropriate horizontal and vertical offsets
  285. // on macOS.
  286. double sheet_offset_x_ = 0.0;
  287. double sheet_offset_y_ = 0.0;
  288. // Used to maintain the aspect ratio of a view which is inside of the
  289. // content view.
  290. double aspect_ratio_ = 0.0;
  291. gfx::Size aspect_ratio_extraSize_;
  292. // The parent window, it is guaranteed to be valid during this window's life.
  293. NativeWindow* parent_ = nullptr;
  294. // Is this a modal window.
  295. bool is_modal_ = false;
  296. // The browser view layer.
  297. std::list<NativeBrowserView*> browser_views_;
  298. // Observers of this window.
  299. base::ObserverList<NativeWindowObserver> observers_;
  300. base::WeakPtrFactory<NativeWindow> weak_factory_;
  301. DISALLOW_COPY_AND_ASSIGN(NativeWindow);
  302. };
  303. // This class provides a hook to get a NativeWindow from a WebContents.
  304. class NativeWindowRelay
  305. : public content::WebContentsUserData<NativeWindowRelay> {
  306. public:
  307. static void CreateForWebContents(content::WebContents*,
  308. base::WeakPtr<NativeWindow>);
  309. ~NativeWindowRelay() override;
  310. NativeWindow* GetNativeWindow() const { return native_window_.get(); }
  311. WEB_CONTENTS_USER_DATA_KEY_DECL();
  312. private:
  313. friend class content::WebContentsUserData<NativeWindow>;
  314. explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window);
  315. base::WeakPtr<NativeWindow> native_window_;
  316. };
  317. } // namespace electron
  318. #endif // SHELL_BROWSER_NATIVE_WINDOW_H_