window_cycle_event_filter.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef ASH_WM_WINDOW_CYCLE_WINDOW_CYCLE_EVENT_FILTER_H_
  5. #define ASH_WM_WINDOW_CYCLE_WINDOW_CYCLE_EVENT_FILTER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/wm/window_cycle/window_cycle_controller.h"
  8. #include "base/timer/timer.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. #include "ui/events/event_handler.h"
  11. #include "ui/gfx/geometry/point.h"
  12. namespace ui {
  13. class GestureEvent;
  14. class KeyEvent;
  15. class MouseEvent;
  16. class ScrollEvent;
  17. } // namespace ui
  18. namespace ash {
  19. // Created by WindowCycleController when cycling through windows. Eats all key
  20. // events and stops cycling when the necessary key sequence is encountered.
  21. // Also allows users to cycle using right/left keys.
  22. class ASH_EXPORT WindowCycleEventFilter : public ui::EventHandler {
  23. public:
  24. // The threshold of performing an action with a touchpad or mouse wheel
  25. // scroll.
  26. static constexpr float kHorizontalThresholdDp = 330.f;
  27. WindowCycleEventFilter();
  28. WindowCycleEventFilter(const WindowCycleEventFilter&) = delete;
  29. WindowCycleEventFilter& operator=(const WindowCycleEventFilter&) = delete;
  30. ~WindowCycleEventFilter() override;
  31. // Overridden from ui::EventHandler:
  32. void OnKeyEvent(ui::KeyEvent* event) override;
  33. void OnMouseEvent(ui::MouseEvent* event) override;
  34. void OnScrollEvent(ui::ScrollEvent* event) override;
  35. void OnGestureEvent(ui::GestureEvent* event) override;
  36. private:
  37. // A struct containing the relevant data during a scroll session.
  38. struct ScrollData {
  39. int finger_count = 0;
  40. // Values are cumulative (ex. |scroll_x| is the total x distance moved
  41. // since the scroll began.
  42. float scroll_x = 0.f;
  43. float scroll_y = 0.f;
  44. };
  45. class AltReleaseHandler : public ui::EventHandler {
  46. public:
  47. AltReleaseHandler();
  48. AltReleaseHandler(const AltReleaseHandler&) = delete;
  49. AltReleaseHandler& operator=(const AltReleaseHandler&) = delete;
  50. ~AltReleaseHandler() override;
  51. // ui::EventHandler:
  52. void OnKeyEvent(ui::KeyEvent* event) override;
  53. };
  54. // Depending on the values of |event| either repeatedly cycle through windows,
  55. // stop repeatedly cycling through windows, or cycle once.
  56. void HandleTriggerKey(ui::KeyEvent* event);
  57. // Returns whether |event| is a trigger key (tab, left, right, w (when
  58. // debugging)).
  59. bool IsTriggerKey(ui::KeyEvent* event) const;
  60. // Returns whether |event| is an exit key (return, space).
  61. bool IsExitKey(ui::KeyEvent* event) const;
  62. // Returns whether the window cycle should repeatedly cycle in the
  63. // direction given by |event|.
  64. bool ShouldRepeatKey(ui::KeyEvent* event) const;
  65. // Given |event|, determine if the user has used their mouse, i.e. moved or
  66. // clicked.
  67. void SetHasUserUsedMouse(ui::MouseEvent* event);
  68. // Depending on the properties of |event|, may cycle the window cycle list or
  69. // complete cycling.
  70. void ProcessMouseEvent(ui::MouseEvent* event);
  71. // Depending on the properties of |event|, may continuously scroll the window
  72. // cycle list, move the cycle view's focus ring or complete cycling.
  73. void ProcessGestureEvent(ui::GestureEvent* event);
  74. // Called by ProcessMouseEvent() and OnScrollEvent(). May cycle the window
  75. // cycle list. Returns true if the event has been handled and should not be
  76. // processed further, false otherwise.
  77. bool ProcessEventImpl(int finger_count, float delta_x, float delta_y);
  78. // Based on the given scroll data, determine whether we should cycle the
  79. // window cycle list. Return true if we do cycle the window cycle list,
  80. // otherwise return false.
  81. bool CycleWindowCycleList(int finger_count, float scroll_x, float scroll_y);
  82. // Returns the cycling direction the window cycle should cycle depending on
  83. // the combination of keys being pressed.
  84. WindowCycleController::WindowCyclingDirection GetWindowCyclingDirection(
  85. ui::KeyEvent* event) const;
  86. // Returns the navigation direction to move the focus to.
  87. WindowCycleController::KeyboardNavDirection GetKeyboardNavDirection(
  88. ui::KeyEvent* event) const;
  89. // When the user holds Alt+Tab, this timer is used to send repeated
  90. // cycle commands to WindowCycleController. Note this is not accomplished
  91. // by marking the Alt+Tab accelerator as "repeatable" in the accelerator
  92. // table because we wish to control the repeat interval.
  93. base::RepeatingTimer repeat_timer_;
  94. AltReleaseHandler alt_release_handler_;
  95. // Stores the initial mouse coordinates. Used to determine whether
  96. // |has_user_used_mouse_| when this handles mouse events.
  97. gfx::Point initial_mouse_location_;
  98. // Bool for tracking whether the user has used their mouse. If this is false,
  99. // mouse events should be filtered. This is to prevent the initial mouse
  100. // position from triggering window cycle items' mouse event handlers despite a
  101. // user not moving their mouse. Should be set to true when a user moves their
  102. // mouse enough or clicks/drags/mousewheel scrolls.
  103. // See crbug.com/114375.
  104. bool has_user_used_mouse_ = false;
  105. // Stores the current scroll session data. If it does not exist, there is no
  106. // active scroll session.
  107. absl::optional<ScrollData> scroll_data_;
  108. // When a user taps on a preview item it should move the focus ring to it.
  109. // However, the focus ring should not move if the user is scrolling. Store
  110. // |tapped_window_| on tap events and determine whether this is a tap or
  111. // scroll with subsequent events.
  112. aura::Window* tapped_window_ = nullptr;
  113. // Tracks whether the user is touch scrolling the window cycle list.
  114. bool touch_scrolling_ = false;
  115. };
  116. } // namespace ash
  117. #endif // ASH_WM_WINDOW_CYCLE_WINDOW_CYCLE_EVENT_FILTER_H_