native_window_views.h 10 KB

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