native_window_views.h 9.8 KB

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