window_cycle_event_filter.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // Copyright 2016 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/wm/window_cycle/window_cycle_event_filter.h"
  5. #include "ash/accelerators/debug_commands.h"
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/display/screen_ash.h"
  8. #include "ash/session/session_controller_impl.h"
  9. #include "ash/shell.h"
  10. #include "ash/wm/window_cycle/window_cycle_controller.h"
  11. #include "ash/wm/window_cycle/window_cycle_list.h"
  12. #include "ash/wm/window_state.h"
  13. #include "base/bind.h"
  14. #include "components/prefs/pref_service.h"
  15. #include "ui/events/event.h"
  16. #include "ui/events/types/event_type.h"
  17. #include "ui/wm/core/coordinate_conversion.h"
  18. namespace ash {
  19. // The distance a user has to move their mouse from |initial_mouse_location_|
  20. // before this stops filtering mouse events.
  21. constexpr int kMouseMovementThreshold = 5;
  22. // Is the reverse scrolling for touchpad on.
  23. bool IsNaturalScrollOn() {
  24. PrefService* pref =
  25. Shell::Get()->session_controller()->GetActivePrefService();
  26. return pref->GetBoolean(prefs::kTouchpadEnabled) &&
  27. pref->GetBoolean(prefs::kNaturalScroll);
  28. }
  29. // Is reverse scrolling for mouse wheel on.
  30. bool IsReverseScrollOn() {
  31. PrefService* pref =
  32. Shell::Get()->session_controller()->GetActivePrefService();
  33. return pref->GetBoolean(prefs::kMouseReverseScroll);
  34. }
  35. WindowCycleEventFilter::WindowCycleEventFilter()
  36. : initial_mouse_location_(
  37. display::Screen::GetScreen()->GetCursorScreenPoint()) {
  38. Shell::Get()->AddPreTargetHandler(this);
  39. // Handling release of "Alt" must come before other pretarget handlers
  40. // (specifically, the partial screenshot handler). See crbug.com/651939
  41. // We can't do all key event handling that early though because it prevents
  42. // other accelerators (like triggering a partial screenshot) from working.
  43. Shell::Get()->AddPreTargetHandler(&alt_release_handler_,
  44. ui::EventTarget::Priority::kSystem);
  45. }
  46. WindowCycleEventFilter::~WindowCycleEventFilter() {
  47. Shell::Get()->RemovePreTargetHandler(this);
  48. Shell::Get()->RemovePreTargetHandler(&alt_release_handler_);
  49. }
  50. void WindowCycleEventFilter::OnKeyEvent(ui::KeyEvent* event) {
  51. // Until the alt key is released, all key events except the trigger key press
  52. // (which is handled by the accelerator controller to call Step) are handled
  53. // by this window cycle controller: https://crbug.com/340339. When the window
  54. // cycle list exists, right + left arrow keys are considered trigger keys and
  55. // those two are handled by this.
  56. const bool is_trigger_key = IsTriggerKey(event);
  57. const bool is_exit_key = IsExitKey(event);
  58. if (!is_trigger_key || event->type() != ui::ET_KEY_PRESSED)
  59. event->StopPropagation();
  60. if (is_trigger_key)
  61. HandleTriggerKey(event);
  62. else if (is_exit_key)
  63. Shell::Get()->window_cycle_controller()->CompleteCycling();
  64. else if (event->key_code() == ui::VKEY_ESCAPE)
  65. Shell::Get()->window_cycle_controller()->CancelCycling();
  66. }
  67. void WindowCycleEventFilter::OnMouseEvent(ui::MouseEvent* event) {
  68. if (!has_user_used_mouse_)
  69. SetHasUserUsedMouse(event);
  70. if (has_user_used_mouse_) {
  71. WindowCycleController* window_cycle_controller =
  72. Shell::Get()->window_cycle_controller();
  73. const bool cycle_list_is_visible =
  74. window_cycle_controller->IsWindowListVisible();
  75. if (cycle_list_is_visible)
  76. ProcessMouseEvent(event);
  77. if (window_cycle_controller->IsEventInCycleView(event) ||
  78. !cycle_list_is_visible) {
  79. return;
  80. }
  81. }
  82. // Prevent mouse clicks from doing anything while the Alt+Tab UI is active
  83. // <crbug.com/641171> but don't interfere with drag and drop operations
  84. // <crbug.com/660945>.
  85. if (event->type() != ui::ET_MOUSE_DRAGGED &&
  86. event->type() != ui::ET_MOUSE_RELEASED) {
  87. event->StopPropagation();
  88. }
  89. }
  90. void WindowCycleEventFilter::OnScrollEvent(ui::ScrollEvent* event) {
  91. // ET_SCROLL_FLING_CANCEL means a touchpad swipe has started.
  92. if (event->type() == ui::ET_SCROLL_FLING_CANCEL) {
  93. scroll_data_ = ScrollData();
  94. return;
  95. }
  96. // ET_SCROLL_FLING_START means a touchpad swipe has ended.
  97. if (event->type() == ui::ET_SCROLL_FLING_START) {
  98. scroll_data_.reset();
  99. return;
  100. }
  101. DCHECK_EQ(ui::ET_SCROLL, event->type());
  102. if (ProcessEventImpl(event->finger_count(), event->x_offset(),
  103. event->y_offset())) {
  104. event->SetHandled();
  105. event->StopPropagation();
  106. }
  107. }
  108. void WindowCycleEventFilter::OnGestureEvent(ui::GestureEvent* event) {
  109. if (Shell::Get()->window_cycle_controller()->IsEventInTabSliderContainer(
  110. event)) {
  111. // Return immediately if the event is on the tab slider container. Pass
  112. // the event to the tab slider buttons to handle it.
  113. return;
  114. }
  115. ProcessGestureEvent(event);
  116. }
  117. void WindowCycleEventFilter::HandleTriggerKey(ui::KeyEvent* event) {
  118. const ui::KeyboardCode key_code = event->key_code();
  119. if (event->type() == ui::ET_KEY_RELEASED) {
  120. repeat_timer_.Stop();
  121. } else if (ShouldRepeatKey(event)) {
  122. repeat_timer_.Start(
  123. FROM_HERE, base::Milliseconds(180),
  124. base::BindRepeating(
  125. &WindowCycleController::HandleCycleWindow,
  126. base::Unretained(Shell::Get()->window_cycle_controller()),
  127. GetWindowCyclingDirection(event)));
  128. } else if (key_code == ui::VKEY_UP || key_code == ui::VKEY_DOWN ||
  129. key_code == ui::VKEY_LEFT || key_code == ui::VKEY_RIGHT) {
  130. Shell::Get()->window_cycle_controller()->HandleKeyboardNavigation(
  131. GetKeyboardNavDirection(event));
  132. }
  133. }
  134. bool WindowCycleEventFilter::IsTriggerKey(ui::KeyEvent* event) const {
  135. const ui::KeyboardCode key_code = event->key_code();
  136. const bool interactive_trigger_key =
  137. (key_code == ui::VKEY_LEFT || key_code == ui::VKEY_RIGHT);
  138. const bool nav_trigger_key =
  139. Shell::Get()
  140. ->window_cycle_controller()
  141. ->IsInteractiveAltTabModeAllowed() &&
  142. (key_code == ui::VKEY_UP || key_code == ui::VKEY_DOWN ||
  143. key_code == ui::VKEY_LEFT || key_code == ui::VKEY_RIGHT);
  144. return key_code == ui::VKEY_TAB ||
  145. (debug::DeveloperAcceleratorsEnabled() && key_code == ui::VKEY_W) ||
  146. interactive_trigger_key || nav_trigger_key;
  147. }
  148. bool WindowCycleEventFilter::IsExitKey(ui::KeyEvent* event) const {
  149. return event->key_code() == ui::VKEY_RETURN ||
  150. event->key_code() == ui::VKEY_SPACE;
  151. }
  152. bool WindowCycleEventFilter::ShouldRepeatKey(ui::KeyEvent* event) const {
  153. return event->type() == ui::ET_KEY_PRESSED && event->is_repeat() &&
  154. !repeat_timer_.IsRunning();
  155. }
  156. void WindowCycleEventFilter::SetHasUserUsedMouse(ui::MouseEvent* event) {
  157. if (event->type() != ui::ET_MOUSE_MOVED &&
  158. event->type() != ui::ET_MOUSE_ENTERED &&
  159. event->type() != ui::ET_MOUSE_EXITED) {
  160. // If a user clicks/drags/scrolls mouse wheel, then they have used the
  161. // mouse.
  162. has_user_used_mouse_ = true;
  163. return;
  164. }
  165. aura::Window* target = static_cast<aura::Window*>(event->target());
  166. aura::Window* event_root = target->GetRootWindow();
  167. gfx::Point event_screen_point = event->root_location();
  168. wm::ConvertPointToScreen(event_root, &event_screen_point);
  169. if ((initial_mouse_location_ - event_screen_point).Length() >
  170. kMouseMovementThreshold) {
  171. has_user_used_mouse_ = true;
  172. }
  173. }
  174. void WindowCycleEventFilter::ProcessMouseEvent(ui::MouseEvent* event) {
  175. auto* window_cycle_controller = Shell::Get()->window_cycle_controller();
  176. if (event->type() == ui::ET_MOUSE_PRESSED &&
  177. !window_cycle_controller->IsEventInCycleView(event)) {
  178. // Close the window cycle list if a user clicks outside of it.
  179. window_cycle_controller->CancelCycling();
  180. return;
  181. }
  182. if (event->IsMouseWheelEvent()) {
  183. if (!scroll_data_)
  184. scroll_data_ = ScrollData();
  185. const ui::MouseWheelEvent* wheel_event = event->AsMouseWheelEvent();
  186. const float y_offset = wheel_event->y_offset();
  187. // Convert mouse wheel events into three-finger scrolls for window cycle
  188. // list and also swap y offset with x offset.
  189. if (ProcessEventImpl(/*finger_count=*/3,
  190. IsReverseScrollOn() ? y_offset : -y_offset,
  191. wheel_event->x_offset())) {
  192. event->SetHandled();
  193. event->StopPropagation();
  194. }
  195. }
  196. }
  197. void WindowCycleEventFilter::ProcessGestureEvent(ui::GestureEvent* event) {
  198. bool should_complete_cycling = false;
  199. switch (event->type()) {
  200. case ui::ET_GESTURE_TAP:
  201. case ui::ET_GESTURE_TAP_DOWN:
  202. case ui::ET_GESTURE_DOUBLE_TAP:
  203. case ui::ET_GESTURE_TAP_UNCONFIRMED:
  204. case ui::ET_GESTURE_TWO_FINGER_TAP:
  205. case ui::ET_GESTURE_LONG_PRESS:
  206. case ui::ET_GESTURE_LONG_TAP: {
  207. tapped_window_ =
  208. Shell::Get()->window_cycle_controller()->GetWindowAtPoint(
  209. event->AsLocatedEvent());
  210. break;
  211. }
  212. case ui::ET_GESTURE_TAP_CANCEL:
  213. // Do nothing because the event after this one determines whether we
  214. // scrolled or tapped.
  215. break;
  216. case ui::ET_GESTURE_SCROLL_BEGIN: {
  217. tapped_window_ = nullptr;
  218. if (!Shell::Get()->window_cycle_controller()->IsEventInCycleView(event))
  219. return;
  220. touch_scrolling_ = true;
  221. break;
  222. }
  223. case ui::ET_GESTURE_SCROLL_UPDATE: {
  224. if (!touch_scrolling_)
  225. return;
  226. Shell::Get()->window_cycle_controller()->Drag(
  227. event->details().scroll_x());
  228. break;
  229. }
  230. case ui::ET_SCROLL_FLING_START: {
  231. tapped_window_ = nullptr;
  232. auto* window_cycle_controller = Shell::Get()->window_cycle_controller();
  233. if (!window_cycle_controller->IsEventInCycleView(event))
  234. return;
  235. // Only start a fling if the x-velocity is non-zero to avoid crashing when
  236. // creating a fling curve. See crbug.com/1224969.
  237. float velocity_x = event->details().velocity_x();
  238. if (velocity_x != 0.f)
  239. window_cycle_controller->StartFling(velocity_x);
  240. break;
  241. }
  242. case ui::ET_GESTURE_END: {
  243. if (tapped_window_) {
  244. // Defer calling WindowCycleController::CompleteCycling() until we've
  245. // set |event| to handled and stop its propagation.
  246. should_complete_cycling = true;
  247. }
  248. tapped_window_ = nullptr;
  249. touch_scrolling_ = false;
  250. break;
  251. }
  252. default:
  253. if (tapped_window_) {
  254. Shell::Get()->window_cycle_controller()->SetFocusedWindow(
  255. tapped_window_);
  256. break;
  257. }
  258. return;
  259. }
  260. event->SetHandled();
  261. event->StopPropagation();
  262. if (should_complete_cycling)
  263. Shell::Get()->window_cycle_controller()->CompleteCycling();
  264. }
  265. bool WindowCycleEventFilter::ProcessEventImpl(int finger_count,
  266. float delta_x,
  267. float delta_y) {
  268. if (!scroll_data_)
  269. return false;
  270. if (finger_count != 2 && finger_count != 3) {
  271. scroll_data_.reset();
  272. return false;
  273. }
  274. if (scroll_data_->finger_count != 0 &&
  275. scroll_data_->finger_count != finger_count) {
  276. scroll_data_.reset();
  277. return false;
  278. }
  279. if (finger_count == 2 && !IsNaturalScrollOn()) {
  280. // Two finger swipe from left to right should move the list right regardless
  281. // of natural scroll settings.
  282. delta_x = -delta_x;
  283. }
  284. scroll_data_->scroll_x += delta_x;
  285. scroll_data_->scroll_y += delta_y;
  286. const bool moved = CycleWindowCycleList(finger_count, scroll_data_->scroll_x,
  287. scroll_data_->scroll_y);
  288. if (moved)
  289. scroll_data_ = ScrollData();
  290. scroll_data_->finger_count = finger_count;
  291. return moved;
  292. }
  293. bool WindowCycleEventFilter::CycleWindowCycleList(int finger_count,
  294. float scroll_x,
  295. float scroll_y) {
  296. if (finger_count != 2 && finger_count != 3)
  297. return false;
  298. auto* window_cycle_controller = Shell::Get()->window_cycle_controller();
  299. if (!window_cycle_controller->IsCycling() ||
  300. std::fabs(scroll_x) < std::fabs(scroll_y) ||
  301. std::fabs(scroll_x) < kHorizontalThresholdDp) {
  302. return false;
  303. }
  304. window_cycle_controller->HandleCycleWindow(
  305. scroll_x > 0 ? WindowCycleController::WindowCyclingDirection::kForward
  306. : WindowCycleController::WindowCyclingDirection::kBackward);
  307. return true;
  308. }
  309. WindowCycleController::WindowCyclingDirection
  310. WindowCycleEventFilter::GetWindowCyclingDirection(ui::KeyEvent* event) const {
  311. DCHECK(IsTriggerKey(event));
  312. // Move backward if left arrow, forward if right arrow, tab, or W. Shift flips
  313. // the direction.
  314. const bool left = event->key_code() == ui::VKEY_LEFT;
  315. const bool shift = event->IsShiftDown();
  316. return (left ^ shift)
  317. ? WindowCycleController::WindowCyclingDirection::kBackward
  318. : WindowCycleController::WindowCyclingDirection::kForward;
  319. }
  320. WindowCycleController::KeyboardNavDirection
  321. WindowCycleEventFilter::GetKeyboardNavDirection(ui::KeyEvent* event) const {
  322. DCHECK(IsTriggerKey(event));
  323. switch (event->key_code()) {
  324. case ui::VKEY_UP:
  325. return WindowCycleController::KeyboardNavDirection::kUp;
  326. case ui::VKEY_DOWN:
  327. return WindowCycleController::KeyboardNavDirection::kDown;
  328. case ui::VKEY_LEFT:
  329. return WindowCycleController::KeyboardNavDirection::kLeft;
  330. case ui::VKEY_RIGHT:
  331. return WindowCycleController::KeyboardNavDirection::kRight;
  332. default:
  333. NOTREACHED();
  334. return WindowCycleController::KeyboardNavDirection::kInvalid;
  335. }
  336. }
  337. WindowCycleEventFilter::AltReleaseHandler::AltReleaseHandler() = default;
  338. WindowCycleEventFilter::AltReleaseHandler::~AltReleaseHandler() = default;
  339. void WindowCycleEventFilter::AltReleaseHandler::OnKeyEvent(
  340. ui::KeyEvent* event) {
  341. // Views uses VKEY_MENU for both left and right Alt keys.
  342. if (event->key_code() == ui::VKEY_MENU &&
  343. event->type() == ui::ET_KEY_RELEASED) {
  344. event->StopPropagation();
  345. Shell::Get()->window_cycle_controller()->CompleteCycling();
  346. // Warning: |this| will be deleted from here on.
  347. }
  348. }
  349. } // namespace ash