native_window.h 18 KB

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