native_window.h 17 KB

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