native_window.h 12 KB

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