native_window_views.h 12 KB

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