native_window.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 ATOM_BROWSER_NATIVE_WINDOW_H_
  5. #define ATOM_BROWSER_NATIVE_WINDOW_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "atom/browser/native_window_observer.h"
  11. #include "atom/browser/ui/accelerator_util.h"
  12. #include "atom/browser/ui/atom_menu_model.h"
  13. #include "base/cancelable_callback.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/observer_list.h"
  16. #include "base/supports_user_data.h"
  17. #include "content/public/browser/readback_types.h"
  18. #include "content/public/browser/web_contents_observer.h"
  19. #include "content/public/browser/web_contents_user_data.h"
  20. #include "extensions/browser/app_window/size_constraints.h"
  21. #include "native_mate/persistent_dictionary.h"
  22. #include "ui/gfx/image/image.h"
  23. #include "ui/gfx/image/image_skia.h"
  24. class SkRegion;
  25. namespace brightray {
  26. class InspectableWebContents;
  27. }
  28. namespace content {
  29. struct NativeWebKeyboardEvent;
  30. }
  31. namespace gfx {
  32. class Point;
  33. class Rect;
  34. class Size;
  35. }
  36. namespace mate {
  37. class Dictionary;
  38. }
  39. namespace atom {
  40. class NativeBrowserView;
  41. struct DraggableRegion;
  42. class NativeWindow : public base::SupportsUserData,
  43. public content::WebContentsObserver {
  44. public:
  45. ~NativeWindow() override;
  46. // Create window with existing WebContents, the caller is responsible for
  47. // managing the window's live.
  48. static NativeWindow* Create(
  49. brightray::InspectableWebContents* inspectable_web_contents,
  50. const mate::Dictionary& options,
  51. NativeWindow* parent = nullptr);
  52. // Find a window from its WebContents
  53. static NativeWindow* FromWebContents(content::WebContents* web_contents);
  54. void InitFromOptions(const mate::Dictionary& options);
  55. virtual void Close() = 0;
  56. virtual void CloseImmediately() = 0;
  57. virtual bool IsClosed() const { return is_closed_; }
  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 Maximize() = 0;
  66. virtual void Unmaximize() = 0;
  67. virtual bool IsMaximized() = 0;
  68. virtual void Minimize() = 0;
  69. virtual void Restore() = 0;
  70. virtual bool IsMinimized() = 0;
  71. virtual void SetFullScreen(bool fullscreen) = 0;
  72. virtual bool IsFullscreen() const = 0;
  73. virtual void SetBounds(const gfx::Rect& bounds, bool animate = false) = 0;
  74. virtual gfx::Rect GetBounds() = 0;
  75. virtual void SetSize(const gfx::Size& size, bool animate = false);
  76. virtual gfx::Size GetSize();
  77. virtual void SetPosition(const gfx::Point& position, bool animate = false);
  78. virtual gfx::Point GetPosition();
  79. virtual void SetContentSize(const gfx::Size& size, bool animate = false);
  80. virtual gfx::Size GetContentSize();
  81. virtual void SetContentBounds(const gfx::Rect& bounds, bool animate = false);
  82. virtual gfx::Rect GetContentBounds();
  83. virtual void SetSizeConstraints(
  84. const extensions::SizeConstraints& size_constraints);
  85. virtual extensions::SizeConstraints GetSizeConstraints();
  86. virtual void SetContentSizeConstraints(
  87. const extensions::SizeConstraints& size_constraints);
  88. virtual extensions::SizeConstraints GetContentSizeConstraints();
  89. virtual void SetMinimumSize(const gfx::Size& size);
  90. virtual gfx::Size GetMinimumSize();
  91. virtual void SetMaximumSize(const gfx::Size& size);
  92. virtual gfx::Size GetMaximumSize();
  93. virtual void SetSheetOffset(const double offsetX, const double offsetY);
  94. virtual double GetSheetOffsetX();
  95. virtual double GetSheetOffsetY();
  96. virtual void SetResizable(bool resizable) = 0;
  97. virtual bool IsResizable() = 0;
  98. virtual void SetMovable(bool movable) = 0;
  99. virtual bool IsMovable() = 0;
  100. virtual void SetMinimizable(bool minimizable) = 0;
  101. virtual bool IsMinimizable() = 0;
  102. virtual void SetMaximizable(bool maximizable) = 0;
  103. virtual bool IsMaximizable() = 0;
  104. virtual void SetFullScreenable(bool fullscreenable) = 0;
  105. virtual bool IsFullScreenable() = 0;
  106. virtual void SetClosable(bool closable) = 0;
  107. virtual bool IsClosable() = 0;
  108. virtual void SetAlwaysOnTop(bool top,
  109. const std::string& level = "floating",
  110. int relativeLevel = 0,
  111. std::string* error = nullptr) = 0;
  112. virtual bool IsAlwaysOnTop() = 0;
  113. virtual void Center() = 0;
  114. virtual void Invalidate() = 0;
  115. virtual void SetTitle(const std::string& title) = 0;
  116. virtual std::string GetTitle() = 0;
  117. virtual void FlashFrame(bool flash) = 0;
  118. virtual void SetSkipTaskbar(bool skip) = 0;
  119. virtual void SetKiosk(bool kiosk) = 0;
  120. virtual bool IsKiosk() = 0;
  121. virtual void SetBackgroundColor(const std::string& color_name) = 0;
  122. virtual void SetHasShadow(bool has_shadow) = 0;
  123. virtual bool HasShadow() = 0;
  124. virtual void SetRepresentedFilename(const std::string& filename);
  125. virtual std::string GetRepresentedFilename();
  126. virtual void SetDocumentEdited(bool edited);
  127. virtual bool IsDocumentEdited();
  128. virtual void SetIgnoreMouseEvents(bool ignore) = 0;
  129. virtual void SetContentProtection(bool enable) = 0;
  130. virtual void SetFocusable(bool focusable);
  131. virtual void SetMenu(AtomMenuModel* menu);
  132. virtual void SetParentWindow(NativeWindow* parent);
  133. virtual void SetBrowserView(NativeBrowserView* browser_view) = 0;
  134. virtual gfx::NativeWindow GetNativeWindow() = 0;
  135. virtual gfx::AcceleratedWidget GetAcceleratedWidget() = 0;
  136. // Taskbar/Dock APIs.
  137. enum ProgressState {
  138. PROGRESS_NONE, // no progress, no marking
  139. PROGRESS_INDETERMINATE, // progress, indeterminate
  140. PROGRESS_ERROR, // progress, errored (red)
  141. PROGRESS_PAUSED, // progress, paused (yellow)
  142. PROGRESS_NORMAL, // progress, not marked (green)
  143. };
  144. virtual void SetProgressBar(double progress,
  145. const ProgressState state) = 0;
  146. virtual void SetOverlayIcon(const gfx::Image& overlay,
  147. const std::string& description) = 0;
  148. // Workspace APIs.
  149. virtual void SetVisibleOnAllWorkspaces(bool visible) = 0;
  150. virtual bool IsVisibleOnAllWorkspaces() = 0;
  151. virtual void SetAutoHideCursor(bool auto_hide);
  152. // Vibrancy API
  153. virtual void SetVibrancy(const std::string& type);
  154. // Touchbar API
  155. virtual void SetTouchBar(
  156. const std::vector<mate::PersistentDictionary>& items);
  157. virtual void RefreshTouchBarItem(const std::string& item_id);
  158. virtual void SetEscapeTouchBarItem(const mate::PersistentDictionary& item);
  159. // Webview APIs.
  160. virtual void FocusOnWebView();
  161. virtual void BlurWebView();
  162. virtual bool IsWebViewFocused();
  163. // Toggle the menu bar.
  164. virtual void SetAutoHideMenuBar(bool auto_hide);
  165. virtual bool IsMenuBarAutoHide();
  166. virtual void SetMenuBarVisibility(bool visible);
  167. virtual bool IsMenuBarVisible();
  168. // Set the aspect ratio when resizing window.
  169. double GetAspectRatio();
  170. gfx::Size GetAspectRatioExtraSize();
  171. virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size);
  172. // File preview APIs.
  173. virtual void PreviewFile(const std::string& path,
  174. const std::string& display_name);
  175. virtual void CloseFilePreview();
  176. base::WeakPtr<NativeWindow> GetWeakPtr() {
  177. return weak_factory_.GetWeakPtr();
  178. }
  179. // Requests the WebContents to close, can be cancelled by the page.
  180. virtual void RequestToClosePage();
  181. // Methods called by the WebContents.
  182. virtual void CloseContents(content::WebContents* source);
  183. virtual void RendererUnresponsive(content::WebContents* source);
  184. virtual void RendererResponsive(content::WebContents* source);
  185. virtual void HandleKeyboardEvent(
  186. content::WebContents*,
  187. const content::NativeWebKeyboardEvent& event) {}
  188. // Public API used by platform-dependent delegates and observers to send UI
  189. // related notifications.
  190. void NotifyWindowClosed();
  191. void NotifyWindowEndSession();
  192. void NotifyWindowBlur();
  193. void NotifyWindowFocus();
  194. void NotifyWindowShow();
  195. void NotifyWindowHide();
  196. void NotifyWindowMaximize();
  197. void NotifyWindowUnmaximize();
  198. void NotifyWindowMinimize();
  199. void NotifyWindowRestore();
  200. void NotifyWindowMove();
  201. void NotifyWindowResize();
  202. void NotifyWindowMoved();
  203. void NotifyWindowScrollTouchBegin();
  204. void NotifyWindowScrollTouchEnd();
  205. void NotifyWindowScrollTouchEdge();
  206. void NotifyWindowSwipe(const std::string& direction);
  207. void NotifyWindowSheetBegin();
  208. void NotifyWindowSheetEnd();
  209. void NotifyWindowEnterFullScreen();
  210. void NotifyWindowLeaveFullScreen();
  211. void NotifyWindowEnterHtmlFullScreen();
  212. void NotifyWindowLeaveHtmlFullScreen();
  213. void NotifyWindowExecuteWindowsCommand(const std::string& command);
  214. void NotifyTouchBarItemInteraction(const std::string& item_id,
  215. const base::DictionaryValue& details);
  216. #if defined(OS_WIN)
  217. void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
  218. #endif
  219. void AddObserver(NativeWindowObserver* obs) {
  220. observers_.AddObserver(obs);
  221. }
  222. void RemoveObserver(NativeWindowObserver* obs) {
  223. observers_.RemoveObserver(obs);
  224. }
  225. brightray::InspectableWebContents* inspectable_web_contents() const {
  226. return inspectable_web_contents_;
  227. }
  228. bool has_frame() const { return has_frame_; }
  229. void set_has_frame(bool has_frame) { has_frame_ = has_frame; }
  230. bool transparent() const { return transparent_; }
  231. SkRegion* draggable_region() const { return draggable_region_.get(); }
  232. bool enable_larger_than_screen() const { return enable_larger_than_screen_; }
  233. NativeWindow* parent() const { return parent_; }
  234. bool is_modal() const { return is_modal_; }
  235. protected:
  236. NativeWindow(brightray::InspectableWebContents* inspectable_web_contents,
  237. const mate::Dictionary& options,
  238. NativeWindow* parent);
  239. // Convert draggable regions in raw format to SkRegion format. Caller is
  240. // responsible for deleting the returned SkRegion instance.
  241. std::unique_ptr<SkRegion> DraggableRegionsToSkRegion(
  242. const std::vector<DraggableRegion>& regions);
  243. // Converts between content bounds and window bounds.
  244. virtual gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) = 0;
  245. virtual gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) = 0;
  246. // Called when the window needs to update its draggable region.
  247. virtual void UpdateDraggableRegions(
  248. const std::vector<DraggableRegion>& regions);
  249. // content::WebContentsObserver:
  250. void RenderViewCreated(content::RenderViewHost* render_view_host) override;
  251. void BeforeUnloadDialogCancelled() override;
  252. void DidFirstVisuallyNonEmptyPaint() override;
  253. bool OnMessageReceived(const IPC::Message& message) override;
  254. private:
  255. // Schedule a notification unresponsive event.
  256. void ScheduleUnresponsiveEvent(int ms);
  257. // Dispatch unresponsive event to observers.
  258. void NotifyWindowUnresponsive();
  259. // Dispatch ReadyToShow event to observers.
  260. void NotifyReadyToShow();
  261. // Whether window has standard frame.
  262. bool has_frame_;
  263. // Whether window is transparent.
  264. bool transparent_;
  265. // For custom drag, the whole window is non-draggable and the draggable region
  266. // has to been explicitly provided.
  267. std::unique_ptr<SkRegion> draggable_region_; // used in custom drag.
  268. // Minimum and maximum size, stored as content size.
  269. extensions::SizeConstraints size_constraints_;
  270. // Whether window can be resized larger than screen.
  271. bool enable_larger_than_screen_;
  272. // The windows has been closed.
  273. bool is_closed_;
  274. // Closure that would be called when window is unresponsive when closing,
  275. // it should be cancelled when we can prove that the window is responsive.
  276. base::CancelableClosure window_unresposive_closure_;
  277. // Used to display sheets at the appropriate horizontal and vertical offsets
  278. // on macOS.
  279. double sheet_offset_x_;
  280. double sheet_offset_y_;
  281. // Used to maintain the aspect ratio of a view which is inside of the
  282. // content view.
  283. double aspect_ratio_;
  284. gfx::Size aspect_ratio_extraSize_;
  285. // The parent window, it is guaranteed to be valid during this window's life.
  286. NativeWindow* parent_;
  287. // Is this a modal window.
  288. bool is_modal_;
  289. // The page this window is viewing.
  290. brightray::InspectableWebContents* inspectable_web_contents_;
  291. // Observers of this window.
  292. base::ObserverList<NativeWindowObserver> observers_;
  293. base::WeakPtrFactory<NativeWindow> weak_factory_;
  294. DISALLOW_COPY_AND_ASSIGN(NativeWindow);
  295. };
  296. // This class provides a hook to get a NativeWindow from a WebContents.
  297. class NativeWindowRelay :
  298. public content::WebContentsUserData<NativeWindowRelay> {
  299. public:
  300. explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window)
  301. : key(UserDataKey()), window(window) {}
  302. void* key;
  303. base::WeakPtr<NativeWindow> window;
  304. private:
  305. friend class content::WebContentsUserData<NativeWindow>;
  306. };
  307. } // namespace atom
  308. #endif // ATOM_BROWSER_NATIVE_WINDOW_H_