native_window.h 19 KB

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