native_window_views.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright (c) 2014 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_VIEWS_H_
  5. #define SHELL_BROWSER_NATIVE_WINDOW_VIEWS_H_
  6. #include "shell/browser/native_window.h"
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <tuple>
  11. #include <vector>
  12. #include "shell/common/api/api.mojom.h"
  13. #include "ui/views/widget/widget_observer.h"
  14. #if defined(OS_WIN)
  15. #include "base/win/scoped_gdi_object.h"
  16. #include "shell/browser/ui/win/taskbar_host.h"
  17. #endif
  18. namespace views {
  19. class UnhandledKeyboardEventHandler;
  20. }
  21. namespace electron {
  22. class GlobalMenuBarX11;
  23. class RootView;
  24. class WindowStateWatcher;
  25. #if defined(USE_X11)
  26. class EventDisabler;
  27. #endif
  28. class NativeWindowViews : public NativeWindow,
  29. public views::WidgetObserver,
  30. public ui::EventHandler {
  31. public:
  32. NativeWindowViews(const gin_helper::Dictionary& options,
  33. NativeWindow* parent);
  34. ~NativeWindowViews() override;
  35. // NativeWindow:
  36. void SetContentView(views::View* view) override;
  37. void Close() override;
  38. void CloseImmediately() override;
  39. void Focus(bool focus) override;
  40. bool IsFocused() override;
  41. void Show() override;
  42. void ShowInactive() override;
  43. void Hide() override;
  44. bool IsVisible() override;
  45. bool IsEnabled() override;
  46. void SetEnabled(bool enable) override;
  47. void Maximize() override;
  48. void Unmaximize() override;
  49. bool IsMaximized() override;
  50. void Minimize() override;
  51. void Restore() override;
  52. bool IsMinimized() override;
  53. void SetFullScreen(bool fullscreen) override;
  54. bool IsFullscreen() const override;
  55. void SetBounds(const gfx::Rect& bounds, bool animate) override;
  56. gfx::Rect GetBounds() override;
  57. gfx::Rect GetContentBounds() override;
  58. gfx::Size GetContentSize() override;
  59. gfx::Rect GetNormalBounds() override;
  60. SkColor GetBackgroundColor() override;
  61. void SetContentSizeConstraints(
  62. const extensions::SizeConstraints& size_constraints) override;
  63. void SetResizable(bool resizable) override;
  64. bool MoveAbove(const std::string& sourceId) override;
  65. void MoveTop() override;
  66. bool IsResizable() override;
  67. void SetAspectRatio(double aspect_ratio,
  68. const gfx::Size& extra_size) override;
  69. void SetMovable(bool movable) override;
  70. bool IsMovable() override;
  71. void SetMinimizable(bool minimizable) override;
  72. bool IsMinimizable() override;
  73. void SetMaximizable(bool maximizable) override;
  74. bool IsMaximizable() override;
  75. void SetFullScreenable(bool fullscreenable) override;
  76. bool IsFullScreenable() override;
  77. void SetClosable(bool closable) override;
  78. bool IsClosable() override;
  79. void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  80. const std::string& level,
  81. int relativeLevel) override;
  82. ui::ZOrderLevel GetZOrderLevel() override;
  83. void Center() override;
  84. void Invalidate() override;
  85. void SetTitle(const std::string& title) override;
  86. std::string GetTitle() override;
  87. void FlashFrame(bool flash) override;
  88. void SetSkipTaskbar(bool skip) override;
  89. void SetExcludedFromShownWindowsMenu(bool excluded) override;
  90. bool IsExcludedFromShownWindowsMenu() override;
  91. void SetSimpleFullScreen(bool simple_fullscreen) override;
  92. bool IsSimpleFullScreen() override;
  93. void SetKiosk(bool kiosk) override;
  94. bool IsKiosk() override;
  95. void SetBackgroundColor(SkColor color) override;
  96. void SetHasShadow(bool has_shadow) override;
  97. bool HasShadow() override;
  98. void SetOpacity(const double opacity) override;
  99. double GetOpacity() override;
  100. void SetIgnoreMouseEvents(bool ignore, bool forward) override;
  101. void SetContentProtection(bool enable) override;
  102. void SetFocusable(bool focusable) override;
  103. void SetMenu(ElectronMenuModel* menu_model) override;
  104. void AddBrowserView(NativeBrowserView* browser_view) override;
  105. void RemoveBrowserView(NativeBrowserView* browser_view) override;
  106. void SetParentWindow(NativeWindow* parent) override;
  107. gfx::NativeView GetNativeView() const override;
  108. gfx::NativeWindow GetNativeWindow() const override;
  109. void SetOverlayIcon(const gfx::Image& overlay,
  110. const std::string& description) override;
  111. void SetProgressBar(double progress, const ProgressState state) override;
  112. void SetAutoHideMenuBar(bool auto_hide) override;
  113. bool IsMenuBarAutoHide() override;
  114. void SetMenuBarVisibility(bool visible) override;
  115. bool IsMenuBarVisible() override;
  116. void SetVisibleOnAllWorkspaces(bool visible,
  117. bool visibleOnFullScreen) override;
  118. bool IsVisibleOnAllWorkspaces() override;
  119. void SetGTKDarkThemeEnabled(bool use_dark_theme) override;
  120. content::DesktopMediaID GetDesktopMediaID() const override;
  121. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  122. NativeWindowHandle GetNativeWindowHandle() const override;
  123. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  124. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  125. void UpdateDraggableRegions(
  126. const std::vector<mojom::DraggableRegionPtr>& regions);
  127. void IncrementChildModals();
  128. void DecrementChildModals();
  129. #if defined(OS_WIN)
  130. // Catch-all message handling and filtering. Called before
  131. // HWNDMessageHandler's built-in handling, which may pre-empt some
  132. // expectations in Views/Aura if messages are consumed. Returns true if the
  133. // message was consumed by the delegate and should not be processed further
  134. // by the HWNDMessageHandler. In this case, |result| is returned. |result| is
  135. // not modified otherwise.
  136. bool PreHandleMSG(UINT message,
  137. WPARAM w_param,
  138. LPARAM l_param,
  139. LRESULT* result);
  140. void SetIcon(HICON small_icon, HICON app_icon);
  141. #elif defined(USE_X11)
  142. void SetIcon(const gfx::ImageSkia& icon);
  143. #endif
  144. SkRegion* draggable_region() const { return draggable_region_.get(); }
  145. #if defined(OS_WIN)
  146. TaskbarHost& taskbar_host() { return taskbar_host_; }
  147. #endif
  148. private:
  149. // views::WidgetObserver:
  150. void OnWidgetActivationChanged(views::Widget* widget, bool active) override;
  151. void OnWidgetBoundsChanged(views::Widget* widget,
  152. const gfx::Rect& bounds) override;
  153. void OnWidgetDestroying(views::Widget* widget) override;
  154. // views::WidgetDelegate:
  155. void DeleteDelegate() override;
  156. views::View* GetInitiallyFocusedView() override;
  157. bool CanResize() const override;
  158. bool CanMaximize() const override;
  159. bool CanMinimize() const override;
  160. base::string16 GetWindowTitle() const override;
  161. views::View* GetContentsView() override;
  162. bool ShouldDescendIntoChildForEventHandling(
  163. gfx::NativeView child,
  164. const gfx::Point& location) override;
  165. views::ClientView* CreateClientView(views::Widget* widget) override;
  166. views::NonClientFrameView* CreateNonClientFrameView(
  167. views::Widget* widget) override;
  168. void OnWidgetMove() override;
  169. #if defined(OS_WIN)
  170. bool ExecuteWindowsCommand(int command_id) override;
  171. #endif
  172. #if defined(OS_WIN)
  173. void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
  174. void SetForwardMouseMessages(bool forward);
  175. static LRESULT CALLBACK SubclassProc(HWND hwnd,
  176. UINT msg,
  177. WPARAM w_param,
  178. LPARAM l_param,
  179. UINT_PTR subclass_id,
  180. DWORD_PTR ref_data);
  181. static LRESULT CALLBACK MouseHookProc(int n_code,
  182. WPARAM w_param,
  183. LPARAM l_param);
  184. #endif
  185. // Enable/disable:
  186. bool ShouldBeEnabled();
  187. void SetEnabledInternal(bool enabled);
  188. // NativeWindow:
  189. void HandleKeyboardEvent(
  190. content::WebContents*,
  191. const content::NativeWebKeyboardEvent& event) override;
  192. #if defined(OS_LINUX)
  193. // ui::EventHandler:
  194. void OnMouseEvent(ui::MouseEvent* event) override;
  195. #endif
  196. // Returns the restore state for the window.
  197. ui::WindowShowState GetRestoredState();
  198. // Maintain window placement.
  199. void MoveBehindTaskBarIfNeeded();
  200. std::unique_ptr<RootView> root_view_;
  201. // The view should be focused by default.
  202. views::View* focused_view_ = nullptr;
  203. // The "resizable" flag on Linux is implemented by setting size constraints,
  204. // we need to make sure size constraints are restored when window becomes
  205. // resizable again. This is also used on Windows, to keep taskbar resize
  206. // events from resizing the window.
  207. extensions::SizeConstraints old_size_constraints_;
  208. #if defined(USE_X11)
  209. std::unique_ptr<GlobalMenuBarX11> global_menu_bar_;
  210. // Handles window state events.
  211. std::unique_ptr<WindowStateWatcher> window_state_watcher_;
  212. // To disable the mouse events.
  213. std::unique_ptr<EventDisabler> event_disabler_;
  214. #endif
  215. #if defined(OS_WIN)
  216. ui::WindowShowState last_window_state_;
  217. gfx::Rect last_normal_placement_bounds_;
  218. // In charge of running taskbar related APIs.
  219. TaskbarHost taskbar_host_;
  220. // Memoized version of a11y check
  221. bool checked_for_a11y_support_ = false;
  222. // Whether to show the WS_THICKFRAME style.
  223. bool thick_frame_ = true;
  224. // The bounds of window before maximize/fullscreen.
  225. gfx::Rect restore_bounds_;
  226. // The icons of window and taskbar.
  227. base::win::ScopedHICON window_icon_;
  228. base::win::ScopedHICON app_icon_;
  229. // The set of windows currently forwarding mouse messages.
  230. static std::set<NativeWindowViews*> forwarding_windows_;
  231. static HHOOK mouse_hook_;
  232. bool forwarding_mouse_messages_ = false;
  233. HWND legacy_window_ = NULL;
  234. bool layered_ = false;
  235. // Set to true if the window is always on top and behind the task bar.
  236. bool behind_task_bar_ = false;
  237. // Whether we want to set window placement without side effect.
  238. bool is_setting_window_placement_ = false;
  239. #endif
  240. // Handles unhandled keyboard messages coming back from the renderer process.
  241. std::unique_ptr<views::UnhandledKeyboardEventHandler> keyboard_event_handler_;
  242. // For custom drag, the whole window is non-draggable and the draggable region
  243. // has to been explicitly provided.
  244. std::unique_ptr<SkRegion> draggable_region_; // used in custom drag.
  245. // Whether the window should be enabled based on user calls to SetEnabled()
  246. bool is_enabled_ = true;
  247. // How many modal children this window has;
  248. // used to determine enabled state
  249. unsigned int num_modal_children_ = 0;
  250. bool use_content_size_ = false;
  251. bool movable_ = true;
  252. bool resizable_ = true;
  253. bool maximizable_ = true;
  254. bool minimizable_ = true;
  255. bool fullscreenable_ = true;
  256. std::string title_;
  257. gfx::Size widget_size_;
  258. double opacity_ = 1.0;
  259. DISALLOW_COPY_AND_ASSIGN(NativeWindowViews);
  260. };
  261. } // namespace electron
  262. #endif // SHELL_BROWSER_NATIVE_WINDOW_VIEWS_H_