native_window_views.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <vector>
  11. #include "shell/common/api/api.mojom.h"
  12. #include "ui/views/widget/widget_observer.h"
  13. #if defined(USE_OZONE)
  14. #include "ui/ozone/buildflags.h"
  15. #if BUILDFLAG(OZONE_PLATFORM_X11)
  16. #define USE_OZONE_PLATFORM_X11
  17. #endif
  18. #endif
  19. #if defined(OS_WIN)
  20. #include "base/win/scoped_gdi_object.h"
  21. #include "shell/browser/ui/win/taskbar_host.h"
  22. #endif
  23. namespace views {
  24. class UnhandledKeyboardEventHandler;
  25. }
  26. namespace electron {
  27. class GlobalMenuBarX11;
  28. class RootView;
  29. class WindowStateWatcher;
  30. #if defined(USE_OZONE_PLATFORM_X11)
  31. class EventDisabler;
  32. #endif
  33. #if defined(OS_WIN)
  34. gfx::Rect ScreenToDIPRect(HWND hwnd, const gfx::Rect& pixel_bounds);
  35. #endif
  36. class NativeWindowViews : public NativeWindow,
  37. public views::WidgetObserver,
  38. public ui::EventHandler {
  39. public:
  40. NativeWindowViews(const gin_helper::Dictionary& options,
  41. NativeWindow* parent);
  42. ~NativeWindowViews() override;
  43. // NativeWindow:
  44. void SetContentView(views::View* view) override;
  45. void Close() override;
  46. void CloseImmediately() override;
  47. void Focus(bool focus) override;
  48. bool IsFocused() override;
  49. void Show() override;
  50. void ShowInactive() override;
  51. void Hide() override;
  52. bool IsVisible() override;
  53. bool IsEnabled() override;
  54. void SetEnabled(bool enable) override;
  55. void Maximize() override;
  56. void Unmaximize() override;
  57. bool IsMaximized() override;
  58. void Minimize() override;
  59. void Restore() override;
  60. bool IsMinimized() override;
  61. void SetFullScreen(bool fullscreen) override;
  62. bool IsFullscreen() const override;
  63. void SetBounds(const gfx::Rect& bounds, bool animate) override;
  64. gfx::Rect GetBounds() override;
  65. gfx::Rect GetContentBounds() override;
  66. gfx::Size GetContentSize() override;
  67. gfx::Rect GetNormalBounds() override;
  68. SkColor GetBackgroundColor() override;
  69. void SetContentSizeConstraints(
  70. const extensions::SizeConstraints& size_constraints) override;
  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 SetVisibleOnAllWorkspaces(bool visible,
  128. bool visibleOnFullScreen,
  129. bool skipTransformProcessType) override;
  130. bool IsVisibleOnAllWorkspaces() override;
  131. void SetGTKDarkThemeEnabled(bool use_dark_theme) override;
  132. content::DesktopMediaID GetDesktopMediaID() const override;
  133. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  134. NativeWindowHandle GetNativeWindowHandle() const override;
  135. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  136. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  137. void UpdateDraggableRegions(
  138. const std::vector<mojom::DraggableRegionPtr>& regions);
  139. void IncrementChildModals();
  140. void DecrementChildModals();
  141. #if defined(OS_WIN)
  142. // Catch-all message handling and filtering. Called before
  143. // HWNDMessageHandler's built-in handling, which may pre-empt some
  144. // expectations in Views/Aura if messages are consumed. Returns true if the
  145. // message was consumed by the delegate and should not be processed further
  146. // by the HWNDMessageHandler. In this case, |result| is returned. |result| is
  147. // not modified otherwise.
  148. bool PreHandleMSG(UINT message,
  149. WPARAM w_param,
  150. LPARAM l_param,
  151. LRESULT* result);
  152. void SetIcon(HICON small_icon, HICON app_icon);
  153. #elif defined(OS_LINUX)
  154. void SetIcon(const gfx::ImageSkia& icon);
  155. #endif
  156. SkRegion* draggable_region() const { return draggable_region_.get(); }
  157. #if defined(OS_WIN)
  158. TaskbarHost& taskbar_host() { return taskbar_host_; }
  159. #endif
  160. #if defined(OS_WIN)
  161. bool IsWindowControlsOverlayEnabled() const {
  162. return (title_bar_style_ == NativeWindowViews::TitleBarStyle::kHidden) &&
  163. titlebar_overlay_;
  164. }
  165. SkColor overlay_button_color() const { return overlay_button_color_; }
  166. void set_overlay_button_color(SkColor color) {
  167. overlay_button_color_ = color;
  168. }
  169. SkColor overlay_symbol_color() const { return overlay_symbol_color_; }
  170. void set_overlay_symbol_color(SkColor color) {
  171. overlay_symbol_color_ = color;
  172. }
  173. #endif
  174. private:
  175. // views::WidgetObserver:
  176. void OnWidgetActivationChanged(views::Widget* widget, bool active) override;
  177. void OnWidgetBoundsChanged(views::Widget* widget,
  178. const gfx::Rect& bounds) override;
  179. void OnWidgetDestroying(views::Widget* widget) override;
  180. void OnWidgetDestroyed(views::Widget* widget) override;
  181. // views::WidgetDelegate:
  182. views::View* GetInitiallyFocusedView() override;
  183. bool CanMaximize() const override;
  184. bool CanMinimize() const override;
  185. std::u16string GetWindowTitle() const override;
  186. views::View* GetContentsView() override;
  187. bool ShouldDescendIntoChildForEventHandling(
  188. gfx::NativeView child,
  189. const gfx::Point& location) override;
  190. views::ClientView* CreateClientView(views::Widget* widget) override;
  191. std::unique_ptr<views::NonClientFrameView> CreateNonClientFrameView(
  192. views::Widget* widget) override;
  193. void OnWidgetMove() override;
  194. #if defined(OS_WIN)
  195. bool ExecuteWindowsCommand(int command_id) override;
  196. #endif
  197. #if defined(OS_WIN)
  198. void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
  199. void SetForwardMouseMessages(bool forward);
  200. static LRESULT CALLBACK SubclassProc(HWND hwnd,
  201. UINT msg,
  202. WPARAM w_param,
  203. LPARAM l_param,
  204. UINT_PTR subclass_id,
  205. DWORD_PTR ref_data);
  206. static LRESULT CALLBACK MouseHookProc(int n_code,
  207. WPARAM w_param,
  208. LPARAM l_param);
  209. #endif
  210. // Enable/disable:
  211. bool ShouldBeEnabled();
  212. void SetEnabledInternal(bool enabled);
  213. // NativeWindow:
  214. void HandleKeyboardEvent(
  215. content::WebContents*,
  216. const content::NativeWebKeyboardEvent& event) override;
  217. // ui::EventHandler:
  218. void OnMouseEvent(ui::MouseEvent* event) override;
  219. // Returns the restore state for the window.
  220. ui::WindowShowState GetRestoredState();
  221. // Maintain window placement.
  222. void MoveBehindTaskBarIfNeeded();
  223. std::unique_ptr<RootView> root_view_;
  224. // The view should be focused by default.
  225. views::View* focused_view_ = nullptr;
  226. // The "resizable" flag on Linux is implemented by setting size constraints,
  227. // we need to make sure size constraints are restored when window becomes
  228. // resizable again. This is also used on Windows, to keep taskbar resize
  229. // events from resizing the window.
  230. extensions::SizeConstraints old_size_constraints_;
  231. #if defined(USE_OZONE)
  232. std::unique_ptr<GlobalMenuBarX11> global_menu_bar_;
  233. #endif
  234. #if defined(USE_OZONE_PLATFORM_X11)
  235. // To disable the mouse events.
  236. std::unique_ptr<EventDisabler> event_disabler_;
  237. #endif
  238. #if defined(OS_WIN)
  239. ui::WindowShowState last_window_state_;
  240. gfx::Rect last_normal_placement_bounds_;
  241. // In charge of running taskbar related APIs.
  242. TaskbarHost taskbar_host_;
  243. // Memoized version of a11y check
  244. bool checked_for_a11y_support_ = false;
  245. // Whether to show the WS_THICKFRAME style.
  246. bool thick_frame_ = true;
  247. // The bounds of window before maximize/fullscreen.
  248. gfx::Rect restore_bounds_;
  249. // The icons of window and taskbar.
  250. base::win::ScopedHICON window_icon_;
  251. base::win::ScopedHICON app_icon_;
  252. // The set of windows currently forwarding mouse messages.
  253. static std::set<NativeWindowViews*> forwarding_windows_;
  254. static HHOOK mouse_hook_;
  255. bool forwarding_mouse_messages_ = false;
  256. HWND legacy_window_ = NULL;
  257. bool layered_ = false;
  258. // Set to true if the window is always on top and behind the task bar.
  259. bool behind_task_bar_ = false;
  260. // Whether we want to set window placement without side effect.
  261. bool is_setting_window_placement_ = false;
  262. // Whether the window is currently being resized.
  263. bool is_resizing_ = false;
  264. // Whether the window is currently being moved.
  265. bool is_moving_ = false;
  266. absl::optional<gfx::Rect> pending_bounds_change_;
  267. // The color to use as the theme and symbol colors respectively for Window
  268. // Controls Overlay if enabled on Windows.
  269. SkColor overlay_button_color_;
  270. SkColor overlay_symbol_color_;
  271. #endif
  272. // Handles unhandled keyboard messages coming back from the renderer process.
  273. std::unique_ptr<views::UnhandledKeyboardEventHandler> keyboard_event_handler_;
  274. // For custom drag, the whole window is non-draggable and the draggable region
  275. // has to been explicitly provided.
  276. std::unique_ptr<SkRegion> draggable_region_; // used in custom drag.
  277. // Whether the window should be enabled based on user calls to SetEnabled()
  278. bool is_enabled_ = true;
  279. // How many modal children this window has;
  280. // used to determine enabled state
  281. unsigned int num_modal_children_ = 0;
  282. bool use_content_size_ = false;
  283. bool movable_ = true;
  284. bool resizable_ = true;
  285. bool maximizable_ = true;
  286. bool minimizable_ = true;
  287. bool fullscreenable_ = true;
  288. std::string title_;
  289. gfx::Size widget_size_;
  290. double opacity_ = 1.0;
  291. bool widget_destroyed_ = false;
  292. };
  293. } // namespace electron
  294. #endif // ELECTRON_SHELL_BROWSER_NATIVE_WINDOW_VIEWS_H_