native_window_views.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. void MoveTop() override;
  61. bool IsResizable() override;
  62. void SetMovable(bool movable) override;
  63. bool IsMovable() override;
  64. void SetMinimizable(bool minimizable) override;
  65. bool IsMinimizable() override;
  66. void SetMaximizable(bool maximizable) override;
  67. bool IsMaximizable() override;
  68. void SetFullScreenable(bool fullscreenable) override;
  69. bool IsFullScreenable() override;
  70. void SetClosable(bool closable) override;
  71. bool IsClosable() override;
  72. void SetAlwaysOnTop(ui::ZOrderLevel z_order,
  73. const std::string& level,
  74. int relativeLevel,
  75. std::string* error) 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(AtomMenuModel* 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. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  115. NativeWindowHandle GetNativeWindowHandle() const override;
  116. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  117. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  118. void UpdateDraggableRegions(std::unique_ptr<SkRegion> region);
  119. void IncrementChildModals();
  120. void DecrementChildModals();
  121. #if defined(OS_WIN)
  122. // Catch-all message handling and filtering. Called before
  123. // HWNDMessageHandler's built-in handling, which may pre-empt some
  124. // expectations in Views/Aura if messages are consumed. Returns true if the
  125. // message was consumed by the delegate and should not be processed further
  126. // by the HWNDMessageHandler. In this case, |result| is returned. |result| is
  127. // not modified otherwise.
  128. bool PreHandleMSG(UINT message,
  129. WPARAM w_param,
  130. LPARAM l_param,
  131. LRESULT* result);
  132. void SetIcon(HICON small_icon, HICON app_icon);
  133. #elif defined(USE_X11)
  134. void SetIcon(const gfx::ImageSkia& icon);
  135. #endif
  136. SkRegion* draggable_region() const { return draggable_region_.get(); }
  137. #if defined(OS_WIN)
  138. TaskbarHost& taskbar_host() { return taskbar_host_; }
  139. #endif
  140. private:
  141. // views::WidgetObserver:
  142. void OnWidgetActivationChanged(views::Widget* widget, bool active) override;
  143. void OnWidgetBoundsChanged(views::Widget* widget,
  144. const gfx::Rect& bounds) override;
  145. void OnWidgetDestroying(views::Widget* widget) override;
  146. // views::WidgetDelegate:
  147. void DeleteDelegate() override;
  148. views::View* GetInitiallyFocusedView() override;
  149. bool CanResize() const override;
  150. bool CanMaximize() const override;
  151. bool CanMinimize() const override;
  152. base::string16 GetWindowTitle() const override;
  153. views::View* GetContentsView() override;
  154. bool ShouldDescendIntoChildForEventHandling(
  155. gfx::NativeView child,
  156. const gfx::Point& location) override;
  157. views::ClientView* CreateClientView(views::Widget* widget) override;
  158. views::NonClientFrameView* CreateNonClientFrameView(
  159. views::Widget* widget) override;
  160. void OnWidgetMove() override;
  161. #if defined(OS_WIN)
  162. bool ExecuteWindowsCommand(int command_id) override;
  163. #endif
  164. #if defined(OS_WIN)
  165. void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
  166. void SetForwardMouseMessages(bool forward);
  167. static LRESULT CALLBACK SubclassProc(HWND hwnd,
  168. UINT msg,
  169. WPARAM w_param,
  170. LPARAM l_param,
  171. UINT_PTR subclass_id,
  172. DWORD_PTR ref_data);
  173. static LRESULT CALLBACK MouseHookProc(int n_code,
  174. WPARAM w_param,
  175. LPARAM l_param);
  176. #endif
  177. // Enable/disable:
  178. bool ShouldBeEnabled();
  179. void SetEnabledInternal(bool enabled);
  180. // NativeWindow:
  181. void HandleKeyboardEvent(
  182. content::WebContents*,
  183. const content::NativeWebKeyboardEvent& event) override;
  184. #if defined(OS_LINUX)
  185. // ui::EventHandler:
  186. void OnMouseEvent(ui::MouseEvent* event) override;
  187. #endif
  188. // Returns the restore state for the window.
  189. ui::WindowShowState GetRestoredState();
  190. // Maintain window placement.
  191. void MoveBehindTaskBarIfNeeded();
  192. std::unique_ptr<RootView> root_view_;
  193. // The view should be focused by default.
  194. views::View* focused_view_ = nullptr;
  195. // The "resizable" flag on Linux is implemented by setting size constraints,
  196. // we need to make sure size constraints are restored when window becomes
  197. // resizable again. This is also used on Windows, to keep taskbar resize
  198. // events from resizing the window.
  199. extensions::SizeConstraints old_size_constraints_;
  200. #if defined(USE_X11)
  201. std::unique_ptr<GlobalMenuBarX11> global_menu_bar_;
  202. // Handles window state events.
  203. std::unique_ptr<WindowStateWatcher> window_state_watcher_;
  204. // To disable the mouse events.
  205. std::unique_ptr<EventDisabler> event_disabler_;
  206. #endif
  207. #if defined(OS_WIN)
  208. ui::WindowShowState last_window_state_;
  209. gfx::Rect last_normal_placement_bounds_;
  210. // In charge of running taskbar related APIs.
  211. TaskbarHost taskbar_host_;
  212. // Memoized version of a11y check
  213. bool checked_for_a11y_support_ = false;
  214. // Whether to show the WS_THICKFRAME style.
  215. bool thick_frame_ = true;
  216. // The bounds of window before maximize/fullscreen.
  217. gfx::Rect restore_bounds_;
  218. // The icons of window and taskbar.
  219. base::win::ScopedHICON window_icon_;
  220. base::win::ScopedHICON app_icon_;
  221. // The set of windows currently forwarding mouse messages.
  222. static std::set<NativeWindowViews*> forwarding_windows_;
  223. static HHOOK mouse_hook_;
  224. bool forwarding_mouse_messages_ = false;
  225. HWND legacy_window_ = NULL;
  226. bool layered_ = false;
  227. // Set to true if the window is always on top and behind the task bar.
  228. bool behind_task_bar_ = false;
  229. // Whether to block Chromium from handling window messages.
  230. bool block_chromium_message_handler_ = false;
  231. #endif
  232. // Handles unhandled keyboard messages coming back from the renderer process.
  233. std::unique_ptr<views::UnhandledKeyboardEventHandler> keyboard_event_handler_;
  234. // For custom drag, the whole window is non-draggable and the draggable region
  235. // has to been explicitly provided.
  236. std::unique_ptr<SkRegion> draggable_region_; // used in custom drag.
  237. // Whether the window should be enabled based on user calls to SetEnabled()
  238. bool is_enabled_ = true;
  239. // How many modal children this window has;
  240. // used to determine enabled state
  241. unsigned int num_modal_children_ = 0;
  242. bool use_content_size_ = false;
  243. bool movable_ = true;
  244. bool resizable_ = true;
  245. bool maximizable_ = true;
  246. bool minimizable_ = true;
  247. bool fullscreenable_ = true;
  248. std::string title_;
  249. gfx::Size widget_size_;
  250. double opacity_ = 1.0;
  251. DISALLOW_COPY_AND_ASSIGN(NativeWindowViews);
  252. };
  253. } // namespace electron
  254. #endif // SHELL_BROWSER_NATIVE_WINDOW_VIEWS_H_