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 
5 #ifndef ASH_WM_GESTURES_WM_GESTURE_HANDLER_H_
6 #define ASH_WM_GESTURES_WM_GESTURE_HANDLER_H_
7 
8 #include "ash/ash_export.h"
9 #include "base/optional.h"
10 #include "components/prefs/pref_registry_simple.h"
11 
12 namespace ui {
13 class MouseEvent;
14 class ScrollEvent;
15 }
16 
17 namespace ash {
18 
19 // TODO(chinsenj): Consider renaming this to WmEventHandler and moving to parent
20 // directory since this now handles mouse wheel events.
21 
22 // This handles the following interactions:
23 //   - 3-finger touchpad scroll events to enter/exit overview mode and move the
24 //   overview highlight if it is visible.
25 //   - 4-finger horizontal scrolls to switch desks.
26 //
27 // This handles the following interactions if the InteractiveWindowCycleList
28 // flag is enabled. TODO(chinsenj): Merge these comments when the flag is
29 // removed.
30 //   - 3-finger horizontal touchpad scroll events to cycle the window cycle
31 //   list.
32 //   - 2-finger horizontal touchpad scroll events to cycle the window cycle
33 //   list.
34 //   - Mouse wheel events to cycle the window cycle list.
35 class ASH_EXPORT WmGestureHandler {
36  public:
37   // The thresholds of performing a wm action with a touchpad three or four
38   // finger scroll.
39   static constexpr float kVerticalThresholdDp = 300.f;
40   static constexpr float kHorizontalThresholdDp = 330.f;
41 
42   // The amount in trackpad units the fingers must move in a direction before a
43   // continuous gesture animation is started. This is to minimize accidental
44   // scrolls.
45   static constexpr int kContinuousGestureMoveThresholdDp = 10;
46 
47   WmGestureHandler();
48   WmGestureHandler(const WmGestureHandler&) = delete;
49   WmGestureHandler& operator=(const WmGestureHandler&) = delete;
50   virtual ~WmGestureHandler();
51 
52   // Processes a mouse wheel event and may cycle the window cycle list. Returns
53   // true if the event has been handled and should not be processed further,
54   // false otherwise.
55   bool ProcessWheelEvent(const ui::MouseEvent& event);
56 
57   // Processes a scroll event and may switch desks, start overview, move the
58   // overview highlight or cycle the window cycle list. Returns true if
59   // the event has been handled and should not be processed further, false
60   // otherwise.
61   bool ProcessScrollEvent(const ui::ScrollEvent& event);
62 
63  private:
64   // A struct containing the relevant data during a scroll session.
65   struct ScrollData {
66     int finger_count = 0;
67 
68     // Values are cumulative (ex. |scroll_x| is the total x distance moved
69     // since the scroll began.
70     float scroll_x = 0.f;
71     float scroll_y = 0.f;
72 
73     // Continuous gestures need to first pass a threshold before we update the
74     // UI. We still update this struct before that happens.
75     bool continuous_gesture_started = false;
76   };
77 
78   // Called by ProcessWheelEvent() and ProcessScrollEvent(). Depending on
79   // |finger_count|, may switch desks, start overview, move the overview
80   // highlight or cycle the window cycle list. Returns true if the
81   // event has been handled and should not be processed further, false
82   // otherwise. Forwards events to DesksController if
83   // |is_enhanced_desk_animations_| is true.
84   bool ProcessEventImpl(int finger_count, float delta_x, float delta_y);
85 
86   // Called when a scroll is ended. Returns true if the scroll is processed.
87   bool EndScroll();
88 
89   // Tries to move the overview selector. Returns true if successful. Called in
90   // the middle of scrolls and when scrolls have ended.
91   bool MoveOverviewSelection(int finger_count, float scroll_x, float scroll_y);
92 
93   // Tries to cycle the window cycle list. Returns true if successful.
94   // Called in the middle of scrolls and when scrolls have ended.
95   bool CycleWindowCycleList(int finger_count, float scroll_x, float scroll_y);
96 
97   // Returns whether or not a given session of overview/window cycle list should
98   // horizontally scroll.
99   bool ShouldHorizontallyScroll(bool in_session,
100                                 float scroll_x,
101                                 float scroll_y);
102 
103   // Contains the data during a scroll session. Empty is no scroll is underway.
104   base::Optional<ScrollData> scroll_data_;
105 
106   // True when the enhanced desk animations feature is enabled.
107   const bool is_enhanced_desk_animations_;
108 };
109 
110 }  // namespace ash
111 
112 #endif  // ASH_WM_GESTURES_WM_GESTURE_HANDLER_H_
113