native_window.h 19 KB

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