native_window_views.h 12 KB

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