native_window_views.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <string>
  8. #include <vector>
  9. #include "atom/browser/ui/accelerator_util.h"
  10. #include "ui/views/widget/widget_delegate.h"
  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. #include "ui/base/win/accessibility_misc_utils.h"
  17. #include <UIAutomationCoreApi.h>
  18. #endif
  19. namespace views {
  20. class UnhandledKeyboardEventHandler;
  21. }
  22. namespace atom {
  23. class GlobalMenuBarX11;
  24. class MenuBar;
  25. class WindowStateWatcher;
  26. #if defined(OS_WIN)
  27. class AtomDesktopWindowTreeHostWin;
  28. #elif defined(USE_X11)
  29. class EventDisabler;
  30. #endif
  31. class NativeWindowViews : public NativeWindow,
  32. #if defined(OS_WIN)
  33. public MessageHandlerDelegate,
  34. #endif
  35. public views::WidgetDelegateView,
  36. public views::WidgetObserver {
  37. public:
  38. NativeWindowViews(brightray::InspectableWebContents* inspectable_web_contents,
  39. const mate::Dictionary& options,
  40. NativeWindow* parent);
  41. ~NativeWindowViews() override;
  42. // NativeWindow:
  43. void Close() override;
  44. void CloseImmediately() override;
  45. void Focus(bool focus) override;
  46. bool IsFocused() override;
  47. void Show() override;
  48. void ShowInactive() override;
  49. void Hide() override;
  50. bool IsVisible() override;
  51. bool IsEnabled() 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. void SetContentSizeConstraints(
  65. const extensions::SizeConstraints& size_constraints) override;
  66. void SetResizable(bool resizable) override;
  67. bool IsResizable() override;
  68. void SetMovable(bool movable) override;
  69. bool IsMovable() override;
  70. void SetMinimizable(bool minimizable) override;
  71. bool IsMinimizable() override;
  72. void SetMaximizable(bool maximizable) override;
  73. bool IsMaximizable() override;
  74. void SetFullScreenable(bool fullscreenable) override;
  75. bool IsFullScreenable() override;
  76. void SetClosable(bool closable) override;
  77. bool IsClosable() override;
  78. void SetAlwaysOnTop(bool top, const std::string& level,
  79. int relativeLevel, 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 SetKiosk(bool kiosk) override;
  88. bool IsKiosk() override;
  89. void SetBackgroundColor(const std::string& color_name) override;
  90. void SetHasShadow(bool has_shadow) override;
  91. bool HasShadow() override;
  92. void SetIgnoreMouseEvents(bool ignore) override;
  93. void SetContentProtection(bool enable) override;
  94. void SetFocusable(bool focusable) override;
  95. void SetMenu(AtomMenuModel* menu_model) override;
  96. void SetBrowserView(NativeBrowserView* browser_view) override;
  97. void SetParentWindow(NativeWindow* parent) override;
  98. gfx::NativeWindow GetNativeWindow() override;
  99. void SetOverlayIcon(const gfx::Image& overlay,
  100. const std::string& description) override;
  101. void SetProgressBar(double progress, const ProgressState state) override;
  102. void SetAutoHideMenuBar(bool auto_hide) override;
  103. bool IsMenuBarAutoHide() override;
  104. void SetMenuBarVisibility(bool visible) override;
  105. bool IsMenuBarVisible() override;
  106. void SetVisibleOnAllWorkspaces(bool visible) override;
  107. bool IsVisibleOnAllWorkspaces() override;
  108. gfx::AcceleratedWidget GetAcceleratedWidget() override;
  109. #if defined(OS_WIN)
  110. void SetIcon(HICON small_icon, HICON app_icon);
  111. #elif defined(USE_X11)
  112. void SetIcon(const gfx::ImageSkia& icon);
  113. #endif
  114. void SetEnabled(bool enable);
  115. views::Widget* widget() const { return window_.get(); }
  116. #if defined(OS_WIN)
  117. TaskbarHost& taskbar_host() { return taskbar_host_; }
  118. #endif
  119. private:
  120. // views::WidgetObserver:
  121. void OnWidgetActivationChanged(
  122. views::Widget* widget, bool active) override;
  123. void OnWidgetBoundsChanged(
  124. views::Widget* widget, const gfx::Rect& bounds) override;
  125. // views::WidgetDelegate:
  126. void DeleteDelegate() override;
  127. views::View* GetInitiallyFocusedView() override;
  128. bool CanResize() const override;
  129. bool CanMaximize() const override;
  130. bool CanMinimize() const override;
  131. base::string16 GetWindowTitle() const override;
  132. bool ShouldHandleSystemCommands() const override;
  133. views::Widget* GetWidget() override;
  134. const views::Widget* GetWidget() const override;
  135. views::View* GetContentsView() override;
  136. bool ShouldDescendIntoChildForEventHandling(
  137. gfx::NativeView child,
  138. const gfx::Point& location) override;
  139. views::ClientView* CreateClientView(views::Widget* widget) override;
  140. views::NonClientFrameView* CreateNonClientFrameView(
  141. views::Widget* widget) override;
  142. void OnWidgetMove() override;
  143. #if defined(OS_WIN)
  144. bool ExecuteWindowsCommand(int command_id) override;
  145. #endif
  146. #if defined(OS_WIN)
  147. // MessageHandlerDelegate:
  148. bool PreHandleMSG(
  149. UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) override;
  150. void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
  151. #endif
  152. // NativeWindow:
  153. gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) override;
  154. gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) override;
  155. void HandleKeyboardEvent(
  156. content::WebContents*,
  157. const content::NativeWebKeyboardEvent& event) override;
  158. // views::View:
  159. void Layout() override;
  160. gfx::Size GetMinimumSize() override;
  161. gfx::Size GetMaximumSize() override;
  162. bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
  163. // Register accelerators supported by the menu model.
  164. void RegisterAccelerators(AtomMenuModel* menu_model);
  165. // Returns the restore state for the window.
  166. ui::WindowShowState GetRestoredState();
  167. std::unique_ptr<views::Widget> window_;
  168. views::View* web_view_; // Managed by inspectable_web_contents_.
  169. NativeBrowserView* browser_view_;
  170. std::unique_ptr<MenuBar> menu_bar_;
  171. bool menu_bar_autohide_;
  172. bool menu_bar_visible_;
  173. bool menu_bar_alt_pressed_;
  174. #if defined(USE_X11)
  175. std::unique_ptr<GlobalMenuBarX11> global_menu_bar_;
  176. // Handles window state events.
  177. std::unique_ptr<WindowStateWatcher> window_state_watcher_;
  178. // To disable the mouse events.
  179. std::unique_ptr<EventDisabler> event_disabler_;
  180. // The "resizable" flag on Linux is implemented by setting size constraints,
  181. // we need to make sure size constraints are restored when window becomes
  182. // resizable again.
  183. extensions::SizeConstraints old_size_constraints_;
  184. #elif defined(OS_WIN)
  185. // Weak ref.
  186. AtomDesktopWindowTreeHostWin* atom_desktop_window_tree_host_win_;
  187. ui::WindowShowState last_window_state_;
  188. // There's an issue with restore on Windows, that sometimes causes the Window
  189. // to receive the wrong size (#2498). To circumvent that, we keep tabs on the
  190. // size of the window while in the normal state (not maximized, minimized or
  191. // fullscreen), so we restore it correctly.
  192. gfx::Rect last_normal_bounds_;
  193. gfx::Rect last_normal_bounds_before_move_;
  194. // last_normal_bounds_ may or may not require update on WM_MOVE. When a
  195. // window is maximized, it is moved (WM_MOVE) to maximum size first and then
  196. // sized (WM_SIZE). In this case, last_normal_bounds_ should not update. We
  197. // keep last_normal_bounds_candidate_ as a candidate which will become valid
  198. // last_normal_bounds_ if the moves are consecutive with no WM_SIZE event in
  199. // between.
  200. gfx::Rect last_normal_bounds_candidate_;
  201. bool consecutive_moves_;
  202. // In charge of running taskbar related APIs.
  203. TaskbarHost taskbar_host_;
  204. // Memoized version of a11y check
  205. bool checked_for_a11y_support_;
  206. // Whether to show the WS_THICKFRAME style.
  207. bool thick_frame_;
  208. // The bounds of window before maximize/fullscreen.
  209. gfx::Rect restore_bounds_;
  210. // The icons of window and taskbar.
  211. base::win::ScopedHICON window_icon_;
  212. base::win::ScopedHICON app_icon_;
  213. #endif
  214. // Handles unhandled keyboard messages coming back from the renderer process.
  215. std::unique_ptr<views::UnhandledKeyboardEventHandler> keyboard_event_handler_;
  216. // Map from accelerator to menu item's command id.
  217. accelerator_util::AcceleratorTable accelerator_table_;
  218. // How many times the Disable has been called.
  219. int disable_count_;
  220. bool use_content_size_;
  221. bool movable_;
  222. bool resizable_;
  223. bool maximizable_;
  224. bool minimizable_;
  225. bool fullscreenable_;
  226. std::string title_;
  227. gfx::Size widget_size_;
  228. DISALLOW_COPY_AND_ASSIGN(NativeWindowViews);
  229. };
  230. } // namespace atom
  231. #endif // ATOM_BROWSER_NATIVE_WINDOW_VIEWS_H_