ui_controls_factory_ash.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/shell.h"
  5. #include "ash/wm/window_properties.h"
  6. #include "ash/wm/window_util.h"
  7. #include "base/callback.h"
  8. #include "ui/aura/client/capture_client.h"
  9. #include "ui/aura/client/screen_position_client.h"
  10. #include "ui/aura/env.h"
  11. #include "ui/aura/test/ui_controls_factory_aura.h"
  12. #include "ui/aura/window_tree_host.h"
  13. #include "ui/base/class_property.h"
  14. #include "ui/base/test/ui_controls.h"
  15. #include "ui/base/test/ui_controls_aura.h"
  16. #include "ui/display/screen.h"
  17. DEFINE_UI_CLASS_PROPERTY_TYPE(ui_controls::UIControlsAura*)
  18. namespace ash {
  19. namespace test {
  20. namespace {
  21. using ui_controls::UIControlsAura;
  22. using ui_controls::MouseButton;
  23. DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(UIControlsAura, kUIControlsKey, NULL)
  24. // Returns the UIControls object for RootWindow.
  25. // kUIControlsKey is owned property and UIControls object
  26. // will be deleted when the root window is deleted.
  27. UIControlsAura* GetUIControlsForRootWindow(aura::Window* root_window) {
  28. UIControlsAura* native_ui_control = root_window->GetProperty(kUIControlsKey);
  29. if (!native_ui_control) {
  30. native_ui_control =
  31. aura::test::CreateUIControlsAura(root_window->GetHost());
  32. // Pass the ownership to the |root_window|.
  33. root_window->SetProperty(kUIControlsKey, native_ui_control);
  34. }
  35. return native_ui_control;
  36. }
  37. // Returns the UIControls object for the RootWindow at |point_in_screen|.
  38. UIControlsAura* GetUIControlsAt(const gfx::Point& point_in_screen) {
  39. // TODO(mazda): Support the case passive grab is taken.
  40. return GetUIControlsForRootWindow(
  41. window_util::GetRootWindowAt(point_in_screen));
  42. }
  43. } // namespace
  44. class UIControlsAsh : public UIControlsAura {
  45. public:
  46. UIControlsAsh() = default;
  47. UIControlsAsh(const UIControlsAsh&) = delete;
  48. UIControlsAsh& operator=(const UIControlsAsh&) = delete;
  49. ~UIControlsAsh() override = default;
  50. // UIControslAura overrides:
  51. bool SendKeyPress(gfx::NativeWindow window,
  52. ui::KeyboardCode key,
  53. bool control,
  54. bool shift,
  55. bool alt,
  56. bool command) override {
  57. return SendKeyPressNotifyWhenDone(window, key, control, shift, alt, command,
  58. base::OnceClosure());
  59. }
  60. bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
  61. ui::KeyboardCode key,
  62. bool control,
  63. bool shift,
  64. bool alt,
  65. bool command,
  66. base::OnceClosure closure) override {
  67. aura::Window* root = window ? window->GetRootWindow()
  68. : ash::Shell::GetRootWindowForNewWindows();
  69. UIControlsAura* ui_controls = GetUIControlsForRootWindow(root);
  70. return ui_controls &&
  71. ui_controls->SendKeyPressNotifyWhenDone(
  72. window, key, control, shift, alt, command, std::move(closure));
  73. }
  74. bool SendMouseMove(int x, int y) override {
  75. gfx::Point p(x, y);
  76. UIControlsAura* ui_controls = GetUIControlsAt(p);
  77. return ui_controls && ui_controls->SendMouseMove(p.x(), p.y());
  78. }
  79. bool SendMouseMoveNotifyWhenDone(int x,
  80. int y,
  81. base::OnceClosure closure) override {
  82. gfx::Point p(x, y);
  83. UIControlsAura* ui_controls = GetUIControlsAt(p);
  84. return ui_controls && ui_controls->SendMouseMoveNotifyWhenDone(
  85. p.x(), p.y(), std::move(closure));
  86. }
  87. bool SendMouseEvents(MouseButton type,
  88. int button_state,
  89. int accelerator_state) override {
  90. gfx::Point p(display::Screen::GetScreen()->GetCursorScreenPoint());
  91. UIControlsAura* ui_controls = GetUIControlsAt(p);
  92. return ui_controls &&
  93. ui_controls->SendMouseEvents(type, button_state, accelerator_state);
  94. }
  95. bool SendMouseEventsNotifyWhenDone(MouseButton type,
  96. int button_state,
  97. base::OnceClosure closure,
  98. int accelerator_state) override {
  99. gfx::Point p(aura::Env::GetInstance()->last_mouse_location());
  100. UIControlsAura* ui_controls = GetUIControlsAt(p);
  101. return ui_controls &&
  102. ui_controls->SendMouseEventsNotifyWhenDone(
  103. type, button_state, std::move(closure), accelerator_state);
  104. }
  105. bool SendMouseClick(MouseButton type) override {
  106. gfx::Point p(display::Screen::GetScreen()->GetCursorScreenPoint());
  107. UIControlsAura* ui_controls = GetUIControlsAt(p);
  108. return ui_controls && ui_controls->SendMouseClick(type);
  109. }
  110. bool SendTouchEvents(int action, int id, int x, int y) override {
  111. UIControlsAura* ui_controls = GetUIControlsAt(gfx::Point(x, y));
  112. return ui_controls && ui_controls->SendTouchEvents(action, id, x, y);
  113. }
  114. bool SendTouchEventsNotifyWhenDone(int action,
  115. int id,
  116. int x,
  117. int y,
  118. base::OnceClosure task) override {
  119. UIControlsAura* ui_controls = GetUIControlsAt(gfx::Point(x, y));
  120. return ui_controls && ui_controls->SendTouchEventsNotifyWhenDone(
  121. action, id, x, y, std::move(task));
  122. }
  123. };
  124. ui_controls::UIControlsAura* CreateAshUIControls() {
  125. return new ash::test::UIControlsAsh();
  126. }
  127. } // namespace test
  128. } // namespace ash