atom_api_top_level_window.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (c) 2018 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_API_ATOM_API_TOP_LEVEL_WINDOW_H_
  5. #define ATOM_BROWSER_API_ATOM_API_TOP_LEVEL_WINDOW_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "atom/browser/api/trackable_object.h"
  11. #include "atom/browser/native_window.h"
  12. #include "atom/browser/native_window_observer.h"
  13. #include "atom/common/api/atom_api_native_image.h"
  14. #include "native_mate/handle.h"
  15. namespace atom {
  16. namespace api {
  17. class View;
  18. class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
  19. public NativeWindowObserver {
  20. public:
  21. static mate::WrappableBase* New(mate::Arguments* args);
  22. static void BuildPrototype(v8::Isolate* isolate,
  23. v8::Local<v8::FunctionTemplate> prototype);
  24. base::WeakPtr<TopLevelWindow> GetWeakPtr() {
  25. return weak_factory_.GetWeakPtr();
  26. }
  27. NativeWindow* window() const { return window_.get(); }
  28. protected:
  29. // Common constructor.
  30. TopLevelWindow(v8::Isolate* isolate, const mate::Dictionary& options);
  31. // Creating independent TopLevelWindow instance.
  32. TopLevelWindow(v8::Isolate* isolate,
  33. v8::Local<v8::Object> wrapper,
  34. const mate::Dictionary& options);
  35. ~TopLevelWindow() override;
  36. // TrackableObject:
  37. void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) override;
  38. // NativeWindowObserver:
  39. void WillCloseWindow(bool* prevent_default) override;
  40. void OnWindowClosed() override;
  41. void OnWindowEndSession() override;
  42. void OnWindowBlur() override;
  43. void OnWindowFocus() override;
  44. void OnWindowShow() override;
  45. void OnWindowHide() override;
  46. void OnWindowMaximize() override;
  47. void OnWindowUnmaximize() override;
  48. void OnWindowMinimize() override;
  49. void OnWindowRestore() override;
  50. void OnWindowResize() override;
  51. void OnWindowMove() override;
  52. void OnWindowMoved() override;
  53. void OnWindowScrollTouchBegin() override;
  54. void OnWindowScrollTouchEnd() override;
  55. void OnWindowSwipe(const std::string& direction) override;
  56. void OnWindowSheetBegin() override;
  57. void OnWindowSheetEnd() override;
  58. void OnWindowEnterFullScreen() override;
  59. void OnWindowLeaveFullScreen() override;
  60. void OnWindowEnterHtmlFullScreen() override;
  61. void OnWindowLeaveHtmlFullScreen() override;
  62. void OnExecuteWindowsCommand(const std::string& command_name) override;
  63. void OnTouchBarItemResult(const std::string& item_id,
  64. const base::DictionaryValue& details) override;
  65. void OnNewWindowForTab() override;
  66. #if defined(OS_WIN)
  67. void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) override;
  68. #endif
  69. // Public APIs of NativeWindow.
  70. void SetContentView(mate::Handle<View> view);
  71. void Close();
  72. virtual void Focus();
  73. virtual void Blur();
  74. bool IsFocused();
  75. void Show();
  76. void ShowInactive();
  77. void Hide();
  78. bool IsVisible();
  79. bool IsEnabled();
  80. void SetEnabled(bool enable);
  81. void Maximize();
  82. void Unmaximize();
  83. bool IsMaximized();
  84. void Minimize();
  85. void Restore();
  86. bool IsMinimized();
  87. void SetFullScreen(bool fullscreen);
  88. bool IsFullscreen();
  89. void SetBounds(const gfx::Rect& bounds, mate::Arguments* args);
  90. gfx::Rect GetBounds();
  91. void SetSize(int width, int height, mate::Arguments* args);
  92. std::vector<int> GetSize();
  93. void SetContentSize(int width, int height, mate::Arguments* args);
  94. std::vector<int> GetContentSize();
  95. void SetContentBounds(const gfx::Rect& bounds, mate::Arguments* args);
  96. gfx::Rect GetContentBounds();
  97. void SetMinimumSize(int width, int height);
  98. std::vector<int> GetMinimumSize();
  99. void SetMaximumSize(int width, int height);
  100. std::vector<int> GetMaximumSize();
  101. void SetSheetOffset(double offsetY, mate::Arguments* args);
  102. void SetResizable(bool resizable);
  103. bool IsResizable();
  104. void SetMovable(bool movable);
  105. #if defined(OS_WIN) || defined(OS_MACOSX)
  106. void MoveTop();
  107. #endif
  108. bool IsMovable();
  109. void SetMinimizable(bool minimizable);
  110. bool IsMinimizable();
  111. void SetMaximizable(bool maximizable);
  112. bool IsMaximizable();
  113. void SetFullScreenable(bool fullscreenable);
  114. bool IsFullScreenable();
  115. void SetClosable(bool closable);
  116. bool IsClosable();
  117. void SetAlwaysOnTop(bool top, mate::Arguments* args);
  118. bool IsAlwaysOnTop();
  119. void Center();
  120. void SetPosition(int x, int y, mate::Arguments* args);
  121. std::vector<int> GetPosition();
  122. void SetTitle(const std::string& title);
  123. std::string GetTitle();
  124. void FlashFrame(bool flash);
  125. void SetSkipTaskbar(bool skip);
  126. void SetSimpleFullScreen(bool simple_fullscreen);
  127. bool IsSimpleFullScreen();
  128. void SetKiosk(bool kiosk);
  129. bool IsKiosk();
  130. virtual void SetBackgroundColor(const std::string& color_name);
  131. void SetHasShadow(bool has_shadow);
  132. bool HasShadow();
  133. void SetOpacity(const double opacity);
  134. double GetOpacity();
  135. void SetRepresentedFilename(const std::string& filename);
  136. std::string GetRepresentedFilename();
  137. void SetDocumentEdited(bool edited);
  138. bool IsDocumentEdited();
  139. void SetIgnoreMouseEvents(bool ignore, mate::Arguments* args);
  140. void SetContentProtection(bool enable);
  141. void SetFocusable(bool focusable);
  142. void SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> menu);
  143. void SetParentWindow(v8::Local<v8::Value> value, mate::Arguments* args);
  144. virtual void SetBrowserView(v8::Local<v8::Value> value);
  145. v8::Local<v8::Value> GetNativeWindowHandle();
  146. void SetProgressBar(double progress, mate::Arguments* args);
  147. void SetOverlayIcon(const gfx::Image& overlay,
  148. const std::string& description);
  149. void SetVisibleOnAllWorkspaces(bool visible);
  150. bool IsVisibleOnAllWorkspaces();
  151. void SetAutoHideCursor(bool auto_hide);
  152. virtual void SetVibrancy(mate::Arguments* args);
  153. void SetTouchBar(const std::vector<mate::PersistentDictionary>& items);
  154. void RefreshTouchBarItem(const std::string& item_id);
  155. void SetEscapeTouchBarItem(const mate::PersistentDictionary& item);
  156. void SelectPreviousTab();
  157. void SelectNextTab();
  158. void MergeAllWindows();
  159. void MoveTabToNewWindow();
  160. void ToggleTabBar();
  161. void AddTabbedWindow(NativeWindow* window, mate::Arguments* args);
  162. void SetAutoHideMenuBar(bool auto_hide);
  163. bool IsMenuBarAutoHide();
  164. void SetMenuBarVisibility(bool visible);
  165. bool IsMenuBarVisible();
  166. void SetAspectRatio(double aspect_ratio, mate::Arguments* args);
  167. void PreviewFile(const std::string& path, mate::Arguments* args);
  168. void CloseFilePreview();
  169. // Public getters of NativeWindow.
  170. v8::Local<v8::Value> GetContentView() const;
  171. v8::Local<v8::Value> GetParentWindow() const;
  172. std::vector<v8::Local<v8::Object>> GetChildWindows() const;
  173. v8::Local<v8::Value> GetBrowserView() const;
  174. bool IsModal() const;
  175. // Extra APIs added in JS.
  176. bool SetThumbarButtons(mate::Arguments* args);
  177. #if defined(TOOLKIT_VIEWS)
  178. void SetIcon(mate::Handle<NativeImage> icon);
  179. #endif
  180. #if defined(OS_WIN)
  181. typedef base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>
  182. MessageCallback;
  183. bool HookWindowMessage(UINT message, const MessageCallback& callback);
  184. bool IsWindowMessageHooked(UINT message);
  185. void UnhookWindowMessage(UINT message);
  186. void UnhookAllWindowMessages();
  187. bool SetThumbnailClip(const gfx::Rect& region);
  188. bool SetThumbnailToolTip(const std::string& tooltip);
  189. void SetAppDetails(const mate::Dictionary& options);
  190. #endif
  191. int32_t GetID() const;
  192. // Helpers.
  193. // Remove BrowserView.
  194. void ResetBrowserView();
  195. // Remove this window from parent window's |child_windows_|.
  196. void RemoveFromParentChildWindows();
  197. #if defined(OS_WIN)
  198. typedef std::map<UINT, MessageCallback> MessageCallbackMap;
  199. MessageCallbackMap messages_callback_map_;
  200. #endif
  201. v8::Global<v8::Value> content_view_;
  202. v8::Global<v8::Value> browser_view_;
  203. v8::Global<v8::Value> menu_;
  204. v8::Global<v8::Value> parent_window_;
  205. KeyWeakMap<int> child_windows_;
  206. std::unique_ptr<NativeWindow> window_;
  207. base::WeakPtrFactory<TopLevelWindow> weak_factory_;
  208. };
  209. } // namespace api
  210. } // namespace atom
  211. namespace mate {
  212. template <>
  213. struct Converter<atom::NativeWindow*> {
  214. static bool FromV8(v8::Isolate* isolate,
  215. v8::Local<v8::Value> val,
  216. atom::NativeWindow** out) {
  217. // null would be tranfered to NULL.
  218. if (val->IsNull()) {
  219. *out = NULL;
  220. return true;
  221. }
  222. atom::api::TopLevelWindow* window;
  223. if (!Converter<atom::api::TopLevelWindow*>::FromV8(isolate, val, &window))
  224. return false;
  225. *out = window->window();
  226. return true;
  227. }
  228. };
  229. } // namespace mate
  230. #endif // ATOM_BROWSER_API_ATOM_API_TOP_LEVEL_WINDOW_H_