native_window.h 19 KB

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