native_window.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 SHELL_BROWSER_NATIVE_WINDOW_H_
  5. #define SHELL_BROWSER_NATIVE_WINDOW_H_
  6. #include <list>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <tuple>
  11. #include <vector>
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/observer_list.h"
  14. #include "base/strings/string16.h"
  15. #include "base/supports_user_data.h"
  16. #include "base/values.h"
  17. #include "content/public/browser/desktop_media_id.h"
  18. #include "content/public/browser/web_contents_user_data.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. namespace content {
  24. struct NativeWebKeyboardEvent;
  25. }
  26. namespace gfx {
  27. class Image;
  28. class Point;
  29. class Rect;
  30. class RectF;
  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 defined(OS_MACOSX)
  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. // Create window with existing WebContents, the caller is responsible for
  50. // managing the window's live.
  51. static NativeWindow* Create(const gin_helper::Dictionary& options,
  52. NativeWindow* parent = nullptr);
  53. void InitFromOptions(const gin_helper::Dictionary& options);
  54. virtual void SetContentView(views::View* view) = 0;
  55. virtual void Close() = 0;
  56. virtual void CloseImmediately() = 0;
  57. virtual bool IsClosed() const;
  58. virtual void Focus(bool focus) = 0;
  59. virtual bool IsFocused() = 0;
  60. virtual void Show() = 0;
  61. virtual void ShowInactive() = 0;
  62. virtual void Hide() = 0;
  63. virtual bool IsVisible() = 0;
  64. virtual bool IsEnabled() = 0;
  65. virtual void SetEnabled(bool enable) = 0;
  66. virtual void Maximize() = 0;
  67. virtual void Unmaximize() = 0;
  68. virtual bool IsMaximized() = 0;
  69. virtual void Minimize() = 0;
  70. virtual void Restore() = 0;
  71. virtual bool IsMinimized() = 0;
  72. virtual void SetFullScreen(bool fullscreen) = 0;
  73. virtual bool IsFullscreen() const = 0;
  74. virtual void SetBounds(const gfx::Rect& bounds, bool animate = false) = 0;
  75. virtual gfx::Rect GetBounds() = 0;
  76. virtual void SetSize(const gfx::Size& size, bool animate = false);
  77. virtual gfx::Size GetSize();
  78. virtual void SetPosition(const gfx::Point& position, bool animate = false);
  79. virtual gfx::Point GetPosition();
  80. virtual void SetContentSize(const gfx::Size& size, bool animate = false);
  81. virtual gfx::Size GetContentSize();
  82. virtual void SetContentBounds(const gfx::Rect& bounds, bool animate = false);
  83. virtual gfx::Rect GetContentBounds();
  84. virtual bool IsNormal();
  85. virtual gfx::Rect GetNormalBounds() = 0;
  86. virtual void SetSizeConstraints(
  87. const extensions::SizeConstraints& size_constraints);
  88. virtual extensions::SizeConstraints GetSizeConstraints() const;
  89. virtual void SetContentSizeConstraints(
  90. const extensions::SizeConstraints& size_constraints);
  91. virtual extensions::SizeConstraints GetContentSizeConstraints() const;
  92. virtual void SetMinimumSize(const gfx::Size& size);
  93. virtual gfx::Size GetMinimumSize() const;
  94. virtual void SetMaximumSize(const gfx::Size& size);
  95. virtual gfx::Size GetMaximumSize() const;
  96. virtual gfx::Size GetContentMinimumSize() const;
  97. virtual gfx::Size GetContentMaximumSize() const;
  98. virtual void SetSheetOffset(const double offsetX, const double offsetY);
  99. virtual double GetSheetOffsetX();
  100. virtual double GetSheetOffsetY();
  101. virtual void SetResizable(bool resizable) = 0;
  102. virtual bool MoveAbove(const std::string& sourceId) = 0;
  103. virtual void MoveTop() = 0;
  104. virtual bool IsResizable() = 0;
  105. virtual void SetMovable(bool movable) = 0;
  106. virtual bool IsMovable() = 0;
  107. virtual void SetMinimizable(bool minimizable) = 0;
  108. virtual bool IsMinimizable() = 0;
  109. virtual void SetMaximizable(bool maximizable) = 0;
  110. virtual bool IsMaximizable() = 0;
  111. virtual void SetFullScreenable(bool fullscreenable) = 0;
  112. virtual bool IsFullScreenable() = 0;
  113. virtual void SetClosable(bool closable) = 0;
  114. virtual bool IsClosable() = 0;
  115. virtual void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  116. const std::string& level = "floating",
  117. int relativeLevel = 0) = 0;
  118. virtual ui::ZOrderLevel GetZOrderLevel() = 0;
  119. virtual void Center() = 0;
  120. virtual void Invalidate() = 0;
  121. virtual void SetTitle(const std::string& title) = 0;
  122. virtual std::string GetTitle() = 0;
  123. // Ability to augment the window title for the screen readers.
  124. void SetAccessibleTitle(const std::string& title);
  125. std::string GetAccessibleTitle();
  126. virtual void FlashFrame(bool flash) = 0;
  127. virtual void SetSkipTaskbar(bool skip) = 0;
  128. virtual void SetExcludedFromShownWindowsMenu(bool excluded) = 0;
  129. virtual bool IsExcludedFromShownWindowsMenu() = 0;
  130. virtual void SetSimpleFullScreen(bool simple_fullscreen) = 0;
  131. virtual bool IsSimpleFullScreen() = 0;
  132. virtual void SetKiosk(bool kiosk) = 0;
  133. virtual bool IsKiosk() = 0;
  134. virtual void SetBackgroundColor(SkColor color) = 0;
  135. virtual SkColor GetBackgroundColor() = 0;
  136. virtual void SetHasShadow(bool has_shadow) = 0;
  137. virtual bool HasShadow() = 0;
  138. virtual void SetOpacity(const double opacity) = 0;
  139. virtual double GetOpacity() = 0;
  140. virtual void SetRepresentedFilename(const std::string& filename);
  141. virtual std::string GetRepresentedFilename();
  142. virtual void SetDocumentEdited(bool edited);
  143. virtual bool IsDocumentEdited();
  144. virtual void SetIgnoreMouseEvents(bool ignore, bool forward) = 0;
  145. virtual void SetContentProtection(bool enable) = 0;
  146. virtual void SetFocusable(bool focusable);
  147. virtual void SetMenu(ElectronMenuModel* menu);
  148. virtual void SetParentWindow(NativeWindow* parent);
  149. virtual void AddBrowserView(NativeBrowserView* browser_view) = 0;
  150. virtual void RemoveBrowserView(NativeBrowserView* browser_view) = 0;
  151. virtual content::DesktopMediaID GetDesktopMediaID() const = 0;
  152. virtual gfx::NativeView GetNativeView() const = 0;
  153. virtual gfx::NativeWindow GetNativeWindow() const = 0;
  154. virtual gfx::AcceleratedWidget GetAcceleratedWidget() const = 0;
  155. virtual NativeWindowHandle GetNativeWindowHandle() const = 0;
  156. // Taskbar/Dock APIs.
  157. enum class ProgressState {
  158. kNone, // no progress, no marking
  159. kIndeterminate, // progress, indeterminate
  160. kError, // progress, errored (red)
  161. kPaused, // progress, paused (yellow)
  162. kNormal, // progress, not marked (green)
  163. };
  164. virtual void SetProgressBar(double progress, const ProgressState state) = 0;
  165. virtual void SetOverlayIcon(const gfx::Image& overlay,
  166. const std::string& description) = 0;
  167. // Workspace APIs.
  168. virtual void SetVisibleOnAllWorkspaces(bool visible,
  169. bool visibleOnFullScreen = false) = 0;
  170. virtual bool IsVisibleOnAllWorkspaces() = 0;
  171. virtual void SetAutoHideCursor(bool auto_hide);
  172. // Vibrancy API
  173. virtual void SetVibrancy(const std::string& type);
  174. // Traffic Light API
  175. #if defined(OS_MACOSX)
  176. virtual void SetTrafficLightPosition(const gfx::Point& position) = 0;
  177. virtual gfx::Point GetTrafficLightPosition() const = 0;
  178. virtual void RedrawTrafficLights() = 0;
  179. #endif
  180. // Touchbar API
  181. virtual void SetTouchBar(std::vector<gin_helper::PersistentDictionary> items);
  182. virtual void RefreshTouchBarItem(const std::string& item_id);
  183. virtual void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item);
  184. // Native Tab API
  185. virtual void SelectPreviousTab();
  186. virtual void SelectNextTab();
  187. virtual void MergeAllWindows();
  188. virtual void MoveTabToNewWindow();
  189. virtual void ToggleTabBar();
  190. virtual bool AddTabbedWindow(NativeWindow* window);
  191. // Returns false if unsupported.
  192. virtual bool SetWindowButtonVisibility(bool visible);
  193. // Toggle the menu bar.
  194. virtual void SetAutoHideMenuBar(bool auto_hide);
  195. virtual bool IsMenuBarAutoHide();
  196. virtual void SetMenuBarVisibility(bool visible);
  197. virtual bool IsMenuBarVisible();
  198. // Set the aspect ratio when resizing window.
  199. double GetAspectRatio();
  200. gfx::Size GetAspectRatioExtraSize();
  201. virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
  202. // File preview APIs.
  203. virtual void PreviewFile(const std::string& path,
  204. const std::string& display_name);
  205. virtual void CloseFilePreview();
  206. virtual void SetGTKDarkThemeEnabled(bool use_dark_theme) = 0;
  207. // Converts between content bounds and window bounds.
  208. virtual gfx::Rect ContentBoundsToWindowBounds(
  209. const gfx::Rect& bounds) const = 0;
  210. virtual gfx::Rect WindowBoundsToContentBounds(
  211. const gfx::Rect& bounds) const = 0;
  212. base::WeakPtr<NativeWindow> GetWeakPtr() {
  213. return weak_factory_.GetWeakPtr();
  214. }
  215. // Methods called by the WebContents.
  216. virtual void HandleKeyboardEvent(
  217. content::WebContents*,
  218. const content::NativeWebKeyboardEvent& event) {}
  219. // Public API used by platform-dependent delegates and observers to send UI
  220. // related notifications.
  221. void NotifyWindowRequestPreferredWith(int* width);
  222. void NotifyWindowCloseButtonClicked();
  223. void NotifyWindowClosed();
  224. void NotifyWindowEndSession();
  225. void NotifyWindowBlur();
  226. void NotifyWindowFocus();
  227. void NotifyWindowShow();
  228. void NotifyWindowIsKeyChanged(bool is_key);
  229. void NotifyWindowHide();
  230. void NotifyWindowMaximize();
  231. void NotifyWindowUnmaximize();
  232. void NotifyWindowMinimize();
  233. void NotifyWindowRestore();
  234. void NotifyWindowMove();
  235. void NotifyWindowWillResize(const gfx::Rect& new_bounds,
  236. bool* prevent_default);
  237. void NotifyWindowResize();
  238. void NotifyWindowWillMove(const gfx::Rect& new_bounds, bool* prevent_default);
  239. void NotifyWindowMoved();
  240. void NotifyWindowScrollTouchBegin();
  241. void NotifyWindowScrollTouchEnd();
  242. void NotifyWindowSwipe(const std::string& direction);
  243. void NotifyWindowRotateGesture(float rotation);
  244. void NotifyWindowSheetBegin();
  245. void NotifyWindowSheetEnd();
  246. void NotifyWindowEnterFullScreen();
  247. void NotifyWindowLeaveFullScreen();
  248. void NotifyWindowEnterHtmlFullScreen();
  249. void NotifyWindowLeaveHtmlFullScreen();
  250. void NotifyWindowAlwaysOnTopChanged();
  251. void NotifyWindowExecuteAppCommand(const std::string& command);
  252. void NotifyTouchBarItemInteraction(const std::string& item_id,
  253. const base::DictionaryValue& details);
  254. void NotifyNewWindowForTab();
  255. #if defined(OS_WIN)
  256. void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
  257. #endif
  258. void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); }
  259. void RemoveObserver(NativeWindowObserver* obs) {
  260. observers_.RemoveObserver(obs);
  261. }
  262. views::Widget* widget() const { return widget_.get(); }
  263. views::View* content_view() const { return content_view_; }
  264. bool has_frame() const { return has_frame_; }
  265. void set_has_frame(bool has_frame) { has_frame_ = has_frame; }
  266. bool transparent() const { return transparent_; }
  267. bool enable_larger_than_screen() const { return enable_larger_than_screen_; }
  268. NativeWindow* parent() const { return parent_; }
  269. bool is_modal() const { return is_modal_; }
  270. std::list<NativeBrowserView*> browser_views() const { return browser_views_; }
  271. protected:
  272. NativeWindow(const gin_helper::Dictionary& options, NativeWindow* parent);
  273. // views::WidgetDelegate:
  274. views::Widget* GetWidget() override;
  275. const views::Widget* GetWidget() const override;
  276. base::string16 GetAccessibleWindowTitle() const override;
  277. void set_content_view(views::View* view) { content_view_ = view; }
  278. void add_browser_view(NativeBrowserView* browser_view) {
  279. browser_views_.push_back(browser_view);
  280. }
  281. void remove_browser_view(NativeBrowserView* browser_view) {
  282. browser_views_.remove_if(
  283. [&browser_view](NativeBrowserView* n) { return (n == browser_view); });
  284. }
  285. private:
  286. std::unique_ptr<views::Widget> widget_;
  287. // The content view, weak ref.
  288. views::View* content_view_ = nullptr;
  289. // Whether window has standard frame.
  290. bool has_frame_ = true;
  291. // Whether window is transparent.
  292. bool transparent_ = false;
  293. // Minimum and maximum size, stored as content size.
  294. extensions::SizeConstraints size_constraints_;
  295. // Whether window can be resized larger than screen.
  296. bool enable_larger_than_screen_ = false;
  297. // The windows has been closed.
  298. bool is_closed_ = false;
  299. // Used to display sheets at the appropriate horizontal and vertical offsets
  300. // on macOS.
  301. double sheet_offset_x_ = 0.0;
  302. double sheet_offset_y_ = 0.0;
  303. // Used to maintain the aspect ratio of a view which is inside of the
  304. // content view.
  305. double aspect_ratio_ = 0.0;
  306. gfx::Size aspect_ratio_extraSize_;
  307. // The parent window, it is guaranteed to be valid during this window's life.
  308. NativeWindow* parent_ = nullptr;
  309. // Is this a modal window.
  310. bool is_modal_ = false;
  311. // The browser view layer.
  312. std::list<NativeBrowserView*> browser_views_;
  313. // Observers of this window.
  314. base::ObserverList<NativeWindowObserver> observers_;
  315. // Accessible title.
  316. base::string16 accessible_title_;
  317. base::WeakPtrFactory<NativeWindow> weak_factory_;
  318. DISALLOW_COPY_AND_ASSIGN(NativeWindow);
  319. };
  320. // This class provides a hook to get a NativeWindow from a WebContents.
  321. class NativeWindowRelay
  322. : public content::WebContentsUserData<NativeWindowRelay> {
  323. public:
  324. static void CreateForWebContents(content::WebContents*,
  325. base::WeakPtr<NativeWindow>);
  326. ~NativeWindowRelay() override;
  327. NativeWindow* GetNativeWindow() const { return native_window_.get(); }
  328. WEB_CONTENTS_USER_DATA_KEY_DECL();
  329. private:
  330. friend class content::WebContentsUserData<NativeWindow>;
  331. explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window);
  332. base::WeakPtr<NativeWindow> native_window_;
  333. };
  334. } // namespace electron
  335. #endif // SHELL_BROWSER_NATIVE_WINDOW_H_