native_window_views.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 ATOM_BROWSER_NATIVE_WINDOW_VIEWS_H_
  5. #define ATOM_BROWSER_NATIVE_WINDOW_VIEWS_H_
  6. #include "atom/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 "atom/browser/ui/win/message_handler_delegate.h"
  14. #include "atom/browser/ui/win/taskbar_host.h"
  15. #include "base/win/scoped_gdi_object.h"
  16. #endif
  17. namespace views {
  18. class UnhandledKeyboardEventHandler;
  19. }
  20. namespace atom {
  21. class GlobalMenuBarX11;
  22. class RootView;
  23. class WindowStateWatcher;
  24. #if defined(OS_WIN)
  25. class AtomDesktopWindowTreeHostWin;
  26. #elif defined(USE_X11)
  27. class EventDisabler;
  28. #endif
  29. class NativeWindowViews : public NativeWindow,
  30. #if defined(OS_WIN)
  31. public MessageHandlerDelegate,
  32. #endif
  33. public views::WidgetObserver,
  34. public ui::EventHandler {
  35. public:
  36. NativeWindowViews(const mate::Dictionary& options, NativeWindow* parent);
  37. ~NativeWindowViews() override;
  38. // NativeWindow:
  39. void SetContentView(views::View* view) override;
  40. void Close() override;
  41. void CloseImmediately() override;
  42. void Focus(bool focus) override;
  43. bool IsFocused() override;
  44. void Show() override;
  45. void ShowInactive() override;
  46. void Hide() override;
  47. bool IsVisible() override;
  48. bool IsEnabled() override;
  49. void SetEnabled(bool enable) override;
  50. void Maximize() override;
  51. void Unmaximize() override;
  52. bool IsMaximized() override;
  53. void Minimize() override;
  54. void Restore() override;
  55. bool IsMinimized() override;
  56. void SetFullScreen(bool fullscreen) override;
  57. bool IsFullscreen() const override;
  58. void SetBounds(const gfx::Rect& bounds, bool animate) override;
  59. gfx::Rect GetBounds() override;
  60. gfx::Rect GetContentBounds() override;
  61. gfx::Size GetContentSize() override;
  62. gfx::Rect GetNormalBounds() override;
  63. void SetContentSizeConstraints(
  64. const extensions::SizeConstraints& size_constraints) override;
  65. void SetResizable(bool resizable) override;
  66. #if defined(OS_WIN)
  67. void MoveTop() override;
  68. #endif
  69. bool IsResizable() override;
  70. void SetMovable(bool movable) override;
  71. bool IsMovable() override;
  72. void SetMinimizable(bool minimizable) override;
  73. bool IsMinimizable() override;
  74. void SetMaximizable(bool maximizable) override;
  75. bool IsMaximizable() override;
  76. void SetFullScreenable(bool fullscreenable) override;
  77. bool IsFullScreenable() override;
  78. void SetClosable(bool closable) override;
  79. bool IsClosable() override;
  80. void SetAlwaysOnTop(bool top,
  81. const std::string& level,
  82. int relativeLevel,
  83. std::string* error) override;
  84. bool IsAlwaysOnTop() override;
  85. void Center() override;
  86. void Invalidate() override;
  87. void SetTitle(const std::string& title) override;
  88. std::string GetTitle() override;
  89. void FlashFrame(bool flash) override;
  90. void SetSkipTaskbar(bool skip) override;
  91. void SetSimpleFullScreen(bool simple_fullscreen) override;
  92. bool IsSimpleFullScreen() override;
  93. void SetKiosk(bool kiosk) override;
  94. bool IsKiosk() override;
  95. void SetBackgroundColor(SkColor color) override;
  96. void SetHasShadow(bool has_shadow) override;
  97. bool HasShadow() override;
  98. void SetOpacity(const double opacity) override;
  99. double GetOpacity() override;
  100. void SetIgnoreMouseEvents(bool ignore, bool forward) override;
  101. void SetContentProtection(bool enable) override;
  102. void SetFocusable(bool focusable) override;
  103. void SetMenu(AtomMenuModel* menu_model) override;
  104. void AddBrowserView(NativeBrowserView* browser_view) override;
  105. void RemoveBrowserView(NativeBrowserView* browser_view) override;
  106. void SetParentWindow(NativeWindow* parent) override;
  107. gfx::NativeView GetNativeView() const override;
  108. gfx::NativeWindow GetNativeWindow() const override;
  109. void SetOverlayIcon(const gfx::Image& overlay,
  110. const std::string& description) override;
  111. void SetProgressBar(double progress, const ProgressState state) override;
  112. void SetAutoHideMenuBar(bool auto_hide) override;
  113. bool IsMenuBarAutoHide() override;
  114. void SetMenuBarVisibility(bool visible) override;
  115. bool IsMenuBarVisible() override;
  116. void SetVisibleOnAllWorkspaces(bool visible,
  117. bool visibleOnFullScreen) override;
  118. bool IsVisibleOnAllWorkspaces() override;
  119. gfx::AcceleratedWidget GetAcceleratedWidget() const override;
  120. NativeWindowHandle GetNativeWindowHandle() const override;
  121. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
  122. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;
  123. void UpdateDraggableRegions(std::unique_ptr<SkRegion> region);
  124. void IncrementChildModals();
  125. void DecrementChildModals();
  126. #if defined(OS_WIN)
  127. void SetIcon(HICON small_icon, HICON app_icon);
  128. #elif defined(USE_X11)
  129. void SetIcon(const gfx::ImageSkia& icon);
  130. #endif
  131. SkRegion* draggable_region() const { return draggable_region_.get(); }
  132. #if defined(OS_WIN)
  133. TaskbarHost& taskbar_host() { return taskbar_host_; }
  134. #endif
  135. private:
  136. // views::WidgetObserver:
  137. void OnWidgetActivationChanged(views::Widget* widget, bool active) override;
  138. void OnWidgetBoundsChanged(views::Widget* widget,
  139. const gfx::Rect& bounds) override;
  140. void AutoresizeBrowserView(int width_delta,
  141. int height_delta,
  142. NativeBrowserView* browser_view);
  143. void OnWidgetDestroying(views::Widget* widget) override;
  144. // views::WidgetDelegate:
  145. void DeleteDelegate() override;
  146. views::View* GetInitiallyFocusedView() override;
  147. bool CanResize() const override;
  148. bool CanMaximize() const override;
  149. bool CanMinimize() const override;
  150. base::string16 GetWindowTitle() const override;
  151. views::View* GetContentsView() override;
  152. bool ShouldDescendIntoChildForEventHandling(
  153. gfx::NativeView child,
  154. const gfx::Point& location) override;
  155. views::ClientView* CreateClientView(views::Widget* widget) override;
  156. views::NonClientFrameView* CreateNonClientFrameView(
  157. views::Widget* widget) override;
  158. void OnWidgetMove() override;
  159. #if defined(OS_WIN)
  160. bool ExecuteWindowsCommand(int command_id) override;
  161. #endif
  162. #if defined(OS_WIN)
  163. // MessageHandlerDelegate:
  164. bool PreHandleMSG(UINT message,
  165. WPARAM w_param,
  166. LPARAM l_param,
  167. LRESULT* result) override;
  168. void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
  169. void SetForwardMouseMessages(bool forward);
  170. static LRESULT CALLBACK SubclassProc(HWND hwnd,
  171. UINT msg,
  172. WPARAM w_param,
  173. LPARAM l_param,
  174. UINT_PTR subclass_id,
  175. DWORD_PTR ref_data);
  176. static LRESULT CALLBACK MouseHookProc(int n_code,
  177. WPARAM w_param,
  178. LPARAM l_param);
  179. #endif
  180. // Enable/disable:
  181. bool ShouldBeEnabled();
  182. void SetEnabledInternal(bool enabled);
  183. // NativeWindow:
  184. void HandleKeyboardEvent(
  185. content::WebContents*,
  186. const content::NativeWebKeyboardEvent& event) override;
  187. #if defined(OS_LINUX)
  188. // ui::EventHandler:
  189. void OnMouseEvent(ui::MouseEvent* event) override;
  190. #endif
  191. // Returns the restore state for the window.
  192. ui::WindowShowState GetRestoredState();
  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. // Weak ref.
  210. AtomDesktopWindowTreeHostWin* atom_desktop_window_tree_host_win_;
  211. ui::WindowShowState last_window_state_;
  212. // There's an issue with restore on Windows, that sometimes causes the Window
  213. // to receive the wrong size (#2498). To circumvent that, we keep tabs on the
  214. // size of the window while in the normal state (not maximized, minimized or
  215. // fullscreen), so we restore it correctly.
  216. gfx::Rect last_normal_bounds_;
  217. gfx::Rect last_normal_bounds_before_move_;
  218. // last_normal_bounds_ may or may not require update on WM_MOVE. When a
  219. // window is maximized, it is moved (WM_MOVE) to maximum size first and then
  220. // sized (WM_SIZE). In this case, last_normal_bounds_ should not update. We
  221. // keep last_normal_bounds_candidate_ as a candidate which will become valid
  222. // last_normal_bounds_ if the moves are consecutive with no WM_SIZE event in
  223. // between.
  224. gfx::Rect last_normal_bounds_candidate_;
  225. bool consecutive_moves_;
  226. // In charge of running taskbar related APIs.
  227. TaskbarHost taskbar_host_;
  228. // Memoized version of a11y check
  229. bool checked_for_a11y_support_ = false;
  230. // Whether to show the WS_THICKFRAME style.
  231. bool thick_frame_ = true;
  232. // The bounds of window before maximize/fullscreen.
  233. gfx::Rect restore_bounds_;
  234. // The icons of window and taskbar.
  235. base::win::ScopedHICON window_icon_;
  236. base::win::ScopedHICON app_icon_;
  237. // The set of windows currently forwarding mouse messages.
  238. static std::set<NativeWindowViews*> forwarding_windows_;
  239. static HHOOK mouse_hook_;
  240. bool forwarding_mouse_messages_ = false;
  241. HWND legacy_window_ = NULL;
  242. bool layered_ = false;
  243. #endif
  244. // Handles unhandled keyboard messages coming back from the renderer process.
  245. std::unique_ptr<views::UnhandledKeyboardEventHandler> keyboard_event_handler_;
  246. // For custom drag, the whole window is non-draggable and the draggable region
  247. // has to been explicitly provided.
  248. std::unique_ptr<SkRegion> draggable_region_; // used in custom drag.
  249. // Whether the window should be enabled based on user calls to SetEnabled()
  250. bool is_enabled_ = true;
  251. // How many modal children this window has;
  252. // used to determine enabled state
  253. unsigned int num_modal_children_ = 0;
  254. bool use_content_size_ = false;
  255. bool movable_ = true;
  256. bool resizable_ = true;
  257. bool maximizable_ = true;
  258. bool minimizable_ = true;
  259. bool fullscreenable_ = true;
  260. std::string title_;
  261. gfx::Size widget_size_;
  262. double opacity_ = 1.0;
  263. DISALLOW_COPY_AND_ASSIGN(NativeWindowViews);
  264. };
  265. } // namespace atom
  266. #endif // ATOM_BROWSER_NATIVE_WINDOW_VIEWS_H_