native_window.h 16 KB

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