native_window.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. // whether windows should be ignored by mission control
  197. #if BUILDFLAG(IS_MAC)
  198. virtual bool IsHiddenInMissionControl() = 0;
  199. virtual void SetHiddenInMissionControl(bool hidden) = 0;
  200. #endif
  201. // Touchbar API
  202. virtual void SetTouchBar(std::vector<gin_helper::PersistentDictionary> items);
  203. virtual void RefreshTouchBarItem(const std::string& item_id);
  204. virtual void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item);
  205. // Native Tab API
  206. virtual void SelectPreviousTab();
  207. virtual void SelectNextTab();
  208. virtual void MergeAllWindows();
  209. virtual void MoveTabToNewWindow();
  210. virtual void ToggleTabBar();
  211. virtual bool AddTabbedWindow(NativeWindow* window);
  212. // Toggle the menu bar.
  213. virtual void SetAutoHideMenuBar(bool auto_hide);
  214. virtual bool IsMenuBarAutoHide();
  215. virtual void SetMenuBarVisibility(bool visible);
  216. virtual bool IsMenuBarVisible();
  217. // Set the aspect ratio when resizing window.
  218. double GetAspectRatio();
  219. gfx::Size GetAspectRatioExtraSize();
  220. virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
  221. // File preview APIs.
  222. virtual void PreviewFile(const std::string& path,
  223. const std::string& display_name);
  224. virtual void CloseFilePreview();
  225. virtual void SetGTKDarkThemeEnabled(bool use_dark_theme) {}
  226. // Converts between content bounds and window bounds.
  227. virtual gfx::Rect ContentBoundsToWindowBounds(
  228. const gfx::Rect& bounds) const = 0;
  229. virtual gfx::Rect WindowBoundsToContentBounds(
  230. const gfx::Rect& bounds) const = 0;
  231. base::WeakPtr<NativeWindow> GetWeakPtr() {
  232. return weak_factory_.GetWeakPtr();
  233. }
  234. virtual gfx::Rect GetWindowControlsOverlayRect();
  235. virtual void SetWindowControlsOverlayRect(const gfx::Rect& overlay_rect);
  236. // Methods called by the WebContents.
  237. virtual void HandleKeyboardEvent(
  238. content::WebContents*,
  239. const content::NativeWebKeyboardEvent& event) {}
  240. // Public API used by platform-dependent delegates and observers to send UI
  241. // related notifications.
  242. void NotifyWindowRequestPreferredWidth(int* width);
  243. void NotifyWindowCloseButtonClicked();
  244. void NotifyWindowClosed();
  245. void NotifyWindowEndSession();
  246. void NotifyWindowBlur();
  247. void NotifyWindowFocus();
  248. void NotifyWindowShow();
  249. void NotifyWindowIsKeyChanged(bool is_key);
  250. void NotifyWindowHide();
  251. void NotifyWindowMaximize();
  252. void NotifyWindowUnmaximize();
  253. void NotifyWindowMinimize();
  254. void NotifyWindowRestore();
  255. void NotifyWindowMove();
  256. void NotifyWindowWillResize(const gfx::Rect& new_bounds,
  257. const gfx::ResizeEdge& edge,
  258. bool* prevent_default);
  259. void NotifyWindowResize();
  260. void NotifyWindowResized();
  261. void NotifyWindowWillMove(const gfx::Rect& new_bounds, bool* prevent_default);
  262. void NotifyWindowMoved();
  263. void NotifyWindowSwipe(const std::string& direction);
  264. void NotifyWindowRotateGesture(float rotation);
  265. void NotifyWindowSheetBegin();
  266. void NotifyWindowSheetEnd();
  267. virtual void NotifyWindowEnterFullScreen();
  268. virtual void NotifyWindowLeaveFullScreen();
  269. void NotifyWindowEnterHtmlFullScreen();
  270. void NotifyWindowLeaveHtmlFullScreen();
  271. void NotifyWindowAlwaysOnTopChanged();
  272. void NotifyWindowExecuteAppCommand(const std::string& command);
  273. void NotifyTouchBarItemInteraction(const std::string& item_id,
  274. base::Value::Dict details);
  275. void NotifyNewWindowForTab();
  276. void NotifyWindowSystemContextMenu(int x, int y, bool* prevent_default);
  277. void NotifyLayoutWindowControlsOverlay();
  278. #if BUILDFLAG(IS_WIN)
  279. void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
  280. #endif
  281. void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); }
  282. void RemoveObserver(NativeWindowObserver* obs) {
  283. observers_.RemoveObserver(obs);
  284. }
  285. // Handle fullscreen transitions.
  286. void HandlePendingFullscreenTransitions();
  287. enum class FullScreenTransitionState { ENTERING, EXITING, NONE };
  288. void set_fullscreen_transition_state(FullScreenTransitionState state) {
  289. fullscreen_transition_state_ = state;
  290. }
  291. FullScreenTransitionState fullscreen_transition_state() const {
  292. return fullscreen_transition_state_;
  293. }
  294. enum class FullScreenTransitionType { HTML, NATIVE, NONE };
  295. void set_fullscreen_transition_type(FullScreenTransitionType type) {
  296. fullscreen_transition_type_ = type;
  297. }
  298. FullScreenTransitionType fullscreen_transition_type() const {
  299. return fullscreen_transition_type_;
  300. }
  301. views::Widget* widget() const { return widget_.get(); }
  302. views::View* content_view() const { return content_view_; }
  303. enum class TitleBarStyle {
  304. kNormal,
  305. kHidden,
  306. kHiddenInset,
  307. kCustomButtonsOnHover,
  308. };
  309. TitleBarStyle title_bar_style() const { return title_bar_style_; }
  310. int titlebar_overlay_height() const { return titlebar_overlay_height_; }
  311. void set_titlebar_overlay_height(int height) {
  312. titlebar_overlay_height_ = height;
  313. }
  314. bool titlebar_overlay_enabled() const { return titlebar_overlay_; }
  315. bool has_frame() const { return has_frame_; }
  316. void set_has_frame(bool has_frame) { has_frame_ = has_frame; }
  317. bool has_client_frame() const { return has_client_frame_; }
  318. bool transparent() const { return transparent_; }
  319. bool enable_larger_than_screen() const { return enable_larger_than_screen_; }
  320. NativeWindow* parent() const { return parent_; }
  321. bool is_modal() const { return is_modal_; }
  322. std::list<NativeBrowserView*> browser_views() const { return browser_views_; }
  323. int32_t window_id() const { return next_id_; }
  324. protected:
  325. NativeWindow(const gin_helper::Dictionary& options, NativeWindow* parent);
  326. // views::WidgetDelegate:
  327. views::Widget* GetWidget() override;
  328. const views::Widget* GetWidget() const override;
  329. std::u16string GetAccessibleWindowTitle() const override;
  330. void set_content_view(views::View* view) { content_view_ = view; }
  331. void add_browser_view(NativeBrowserView* browser_view) {
  332. browser_views_.push_back(browser_view);
  333. }
  334. void remove_browser_view(NativeBrowserView* browser_view) {
  335. browser_views_.remove_if(
  336. [&browser_view](NativeBrowserView* n) { return (n == browser_view); });
  337. }
  338. // The boolean parsing of the "titleBarOverlay" option
  339. bool titlebar_overlay_ = false;
  340. // The custom height parsed from the "height" option in a Object
  341. // "titleBarOverlay"
  342. int titlebar_overlay_height_ = 0;
  343. // The "titleBarStyle" option.
  344. TitleBarStyle title_bar_style_ = TitleBarStyle::kNormal;
  345. std::queue<bool> pending_transitions_;
  346. FullScreenTransitionState fullscreen_transition_state_ =
  347. FullScreenTransitionState::NONE;
  348. FullScreenTransitionType fullscreen_transition_type_ =
  349. FullScreenTransitionType::NONE;
  350. private:
  351. std::unique_ptr<views::Widget> widget_;
  352. static int32_t next_id_;
  353. // The content view, weak ref.
  354. views::View* content_view_ = nullptr;
  355. // Whether window has standard frame.
  356. bool has_frame_ = true;
  357. // Whether window has standard frame, but it's drawn by Electron (the client
  358. // application) instead of the OS. Currently only has meaning on Linux for
  359. // Wayland hosts.
  360. bool has_client_frame_ = false;
  361. // Whether window is transparent.
  362. bool transparent_ = false;
  363. // Minimum and maximum size, stored as content size.
  364. extensions::SizeConstraints size_constraints_;
  365. // Whether window can be resized larger than screen.
  366. bool enable_larger_than_screen_ = false;
  367. // The windows has been closed.
  368. bool is_closed_ = false;
  369. // Used to display sheets at the appropriate horizontal and vertical offsets
  370. // on macOS.
  371. double sheet_offset_x_ = 0.0;
  372. double sheet_offset_y_ = 0.0;
  373. // Used to maintain the aspect ratio of a view which is inside of the
  374. // content view.
  375. double aspect_ratio_ = 0.0;
  376. gfx::Size aspect_ratio_extraSize_;
  377. // The parent window, it is guaranteed to be valid during this window's life.
  378. NativeWindow* parent_ = nullptr;
  379. // Is this a modal window.
  380. bool is_modal_ = false;
  381. // The browser view layer.
  382. std::list<NativeBrowserView*> browser_views_;
  383. // Observers of this window.
  384. base::ObserverList<NativeWindowObserver> observers_;
  385. // Accessible title.
  386. std::u16string accessible_title_;
  387. gfx::Rect overlay_rect_;
  388. base::WeakPtrFactory<NativeWindow> weak_factory_{this};
  389. };
  390. // This class provides a hook to get a NativeWindow from a WebContents.
  391. class NativeWindowRelay
  392. : public content::WebContentsUserData<NativeWindowRelay> {
  393. public:
  394. static void CreateForWebContents(content::WebContents*,
  395. base::WeakPtr<NativeWindow>);
  396. ~NativeWindowRelay() override;
  397. NativeWindow* GetNativeWindow() const { return native_window_.get(); }
  398. WEB_CONTENTS_USER_DATA_KEY_DECL();
  399. private:
  400. friend class content::WebContentsUserData<NativeWindow>;
  401. explicit NativeWindowRelay(content::WebContents* web_contents,
  402. base::WeakPtr<NativeWindow> window);
  403. base::WeakPtr<NativeWindow> native_window_;
  404. };
  405. } // namespace electron
  406. #endif // ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_H_