native_window.h 14 KB

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