OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ | 5 #ifndef UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ |
6 #define UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ | 6 #define UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "ui/events/gesture_detection/gesture_detection_export.h" | 10 #include "ui/events/gesture_detection/gesture_detection_export.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 // Distance the first touch can wander before it is no longer considered a | 34 // Distance the first touch can wander before it is no longer considered a |
35 // double tap (in dips). | 35 // double tap (in dips). |
36 float double_tap_slop; | 36 float double_tap_slop; |
37 | 37 |
38 // Minimum velocity to initiate a fling (in dips/second). | 38 // Minimum velocity to initiate a fling (in dips/second). |
39 float minimum_fling_velocity; | 39 float minimum_fling_velocity; |
40 | 40 |
41 // Maximum velocity of an initiated fling (in dips/second). | 41 // Maximum velocity of an initiated fling (in dips/second). |
42 float maximum_fling_velocity; | 42 float maximum_fling_velocity; |
| 43 |
| 44 // Whether |OnSwipe| should be called after a secondary touch is released |
| 45 // while a logical swipe gesture is active. Defaults to false. |
| 46 bool swipe_enabled; |
| 47 |
| 48 // Minimum velocity to initiate a swipe (in dips/second). |
| 49 float minimum_swipe_velocity; |
| 50 |
| 51 // Minimum ratio between swipe velocity components to trigger a swipe. |
| 52 float minimum_swipe_direction_ratio; |
43 }; | 53 }; |
44 | 54 |
45 class GestureListener { | 55 class GestureListener { |
46 public: | 56 public: |
47 virtual ~GestureListener() {} | 57 virtual ~GestureListener() {} |
48 virtual bool OnDown(const MotionEvent& e) = 0; | 58 virtual bool OnDown(const MotionEvent& e) = 0; |
49 virtual void OnShowPress(const MotionEvent& e) = 0; | 59 virtual void OnShowPress(const MotionEvent& e) = 0; |
50 virtual bool OnSingleTapUp(const MotionEvent& e) = 0; | 60 virtual bool OnSingleTapUp(const MotionEvent& e) = 0; |
51 virtual bool OnLongPress(const MotionEvent& e) = 0; | 61 virtual bool OnLongPress(const MotionEvent& e) = 0; |
52 virtual bool OnScroll(const MotionEvent& e1, const MotionEvent& e2, | 62 virtual bool OnScroll(const MotionEvent& e1, |
53 float distance_x, float distance_y) = 0; | 63 const MotionEvent& e2, |
54 virtual bool OnFling(const MotionEvent& e1, const MotionEvent& e2, | 64 float distance_x, |
55 float velocity_x, float velocity_y) = 0; | 65 float distance_y) = 0; |
| 66 virtual bool OnFling(const MotionEvent& e1, |
| 67 const MotionEvent& e2, |
| 68 float velocity_x, |
| 69 float velocity_y) = 0; |
| 70 // Added for Chromium (Aura). |
| 71 virtual bool OnSwipe(const MotionEvent& e1, |
| 72 const MotionEvent& e2, |
| 73 float velocity_x, |
| 74 float velocity_y) = 0; |
56 }; | 75 }; |
57 | 76 |
58 class DoubleTapListener { | 77 class DoubleTapListener { |
59 public: | 78 public: |
60 virtual ~DoubleTapListener() {} | 79 virtual ~DoubleTapListener() {} |
61 virtual bool OnSingleTapConfirmed(const MotionEvent& e) = 0; | 80 virtual bool OnSingleTapConfirmed(const MotionEvent& e) = 0; |
62 virtual bool OnDoubleTap(const MotionEvent& e) = 0; | 81 virtual bool OnDoubleTap(const MotionEvent& e) = 0; |
63 virtual bool OnDoubleTapEvent(const MotionEvent& e) = 0; | 82 virtual bool OnDoubleTapEvent(const MotionEvent& e) = 0; |
64 }; | 83 }; |
65 | 84 |
66 // A convenience class to extend when you only want to listen for a subset | 85 // A convenience class to extend when you only want to listen for a subset |
67 // of all the gestures. This implements all methods in the | 86 // of all the gestures. This implements all methods in the |
68 // |GestureListener| and |DoubleTapListener| but does | 87 // |GestureListener| and |DoubleTapListener| but does |
69 // nothing and returns false for all applicable methods. | 88 // nothing and returns false for all applicable methods. |
70 class SimpleGestureListener : public GestureListener, | 89 class SimpleGestureListener : public GestureListener, |
71 public DoubleTapListener { | 90 public DoubleTapListener { |
72 public: | 91 public: |
73 // GestureListener implementation. | 92 // GestureListener implementation. |
74 virtual bool OnDown(const MotionEvent& e) OVERRIDE; | 93 virtual bool OnDown(const MotionEvent& e) OVERRIDE; |
75 virtual void OnShowPress(const MotionEvent& e) OVERRIDE; | 94 virtual void OnShowPress(const MotionEvent& e) OVERRIDE; |
76 virtual bool OnSingleTapUp(const MotionEvent& e) OVERRIDE; | 95 virtual bool OnSingleTapUp(const MotionEvent& e) OVERRIDE; |
77 virtual bool OnLongPress(const MotionEvent& e) OVERRIDE; | 96 virtual bool OnLongPress(const MotionEvent& e) OVERRIDE; |
78 virtual bool OnScroll(const MotionEvent& e1, const MotionEvent& e2, | 97 virtual bool OnScroll(const MotionEvent& e1, |
79 float distance_x, float distance_y) OVERRIDE; | 98 const MotionEvent& e2, |
80 virtual bool OnFling(const MotionEvent& e1, const MotionEvent& e2, | 99 float distance_x, |
81 float velocity_x, float velocity_y) OVERRIDE; | 100 float distance_y) OVERRIDE; |
| 101 virtual bool OnFling(const MotionEvent& e1, |
| 102 const MotionEvent& e2, |
| 103 float velocity_x, |
| 104 float velocity_y) OVERRIDE; |
| 105 virtual bool OnSwipe(const MotionEvent& e1, |
| 106 const MotionEvent& e2, |
| 107 float velocity_x, |
| 108 float velocity_y) OVERRIDE; |
82 | 109 |
83 // DoubleTapListener implementation. | 110 // DoubleTapListener implementation. |
84 virtual bool OnSingleTapConfirmed(const MotionEvent& e) OVERRIDE; | 111 virtual bool OnSingleTapConfirmed(const MotionEvent& e) OVERRIDE; |
85 virtual bool OnDoubleTap(const MotionEvent& e) OVERRIDE; | 112 virtual bool OnDoubleTap(const MotionEvent& e) OVERRIDE; |
86 virtual bool OnDoubleTapEvent(const MotionEvent& e) OVERRIDE; | 113 virtual bool OnDoubleTapEvent(const MotionEvent& e) OVERRIDE; |
87 }; | 114 }; |
88 | 115 |
89 GestureDetector(const Config& config, | 116 GestureDetector(const Config& config, |
90 GestureListener* listener, | 117 GestureListener* listener, |
91 DoubleTapListener* optional_double_tap_listener); | 118 DoubleTapListener* optional_double_tap_listener); |
92 ~GestureDetector(); | 119 ~GestureDetector(); |
93 | 120 |
94 bool OnTouchEvent(const MotionEvent& ev); | 121 bool OnTouchEvent(const MotionEvent& ev); |
95 | 122 |
96 // Setting a valid |double_tap_listener| will enable double-tap detection, | 123 // Setting a valid |double_tap_listener| will enable double-tap detection, |
97 // wherein calls to |OnSimpleTapConfirmed| are delayed by the tap timeout. | 124 // wherein calls to |OnSimpleTapConfirmed| are delayed by the tap timeout. |
98 // Note: The listener must never be changed while |is_double_tapping| is true. | 125 // Note: The listener must never be changed while |is_double_tapping| is true. |
99 void SetDoubleTapListener(DoubleTapListener* double_tap_listener); | 126 void SetDoubleTapListener(DoubleTapListener* double_tap_listener); |
100 | 127 |
101 bool has_doubletap_listener() const { return double_tap_listener_ != NULL; } | 128 bool has_doubletap_listener() const { return double_tap_listener_ != NULL; } |
102 | 129 |
103 bool is_double_tapping() const { return is_double_tapping_; } | 130 bool is_double_tapping() const { return is_double_tapping_; } |
104 | 131 |
105 void set_is_longpress_enabled(bool is_longpress_enabled) { | 132 void set_longpress_enabled(bool enabled) { longpress_enabled_ = enabled; } |
106 is_longpress_enabled_ = is_longpress_enabled; | |
107 } | |
108 | |
109 bool is_longpress_enabled() const { return is_longpress_enabled_; } | |
110 | 133 |
111 private: | 134 private: |
112 void Init(const Config& config); | 135 void Init(const Config& config); |
113 void OnShowPressTimeout(); | 136 void OnShowPressTimeout(); |
114 void OnLongPressTimeout(); | 137 void OnLongPressTimeout(); |
115 void OnTapTimeout(); | 138 void OnTapTimeout(); |
116 void Cancel(); | 139 void Cancel(); |
117 void CancelTaps(); | 140 void CancelTaps(); |
118 bool IsConsideredDoubleTap(const MotionEvent& first_down, | 141 bool IsConsideredDoubleTap(const MotionEvent& first_down, |
119 const MotionEvent& first_up, | 142 const MotionEvent& first_up, |
120 const MotionEvent& second_down) const; | 143 const MotionEvent& second_down) const; |
121 | 144 |
122 class TimeoutGestureHandler; | 145 class TimeoutGestureHandler; |
123 scoped_ptr<TimeoutGestureHandler> timeout_handler_; | 146 scoped_ptr<TimeoutGestureHandler> timeout_handler_; |
124 GestureListener* const listener_; | 147 GestureListener* const listener_; |
125 DoubleTapListener* double_tap_listener_; | 148 DoubleTapListener* double_tap_listener_; |
126 | 149 |
127 float touch_slop_square_; | 150 float touch_slop_square_; |
128 float double_tap_touch_slop_square_; | 151 float double_tap_touch_slop_square_; |
129 float double_tap_slop_square_; | 152 float double_tap_slop_square_; |
130 float min_fling_velocity_; | 153 float min_fling_velocity_; |
131 float max_fling_velocity_; | 154 float max_fling_velocity_; |
| 155 float min_swipe_velocity_; |
| 156 float min_swipe_direction_ratio_; |
132 base::TimeDelta double_tap_timeout_; | 157 base::TimeDelta double_tap_timeout_; |
133 | 158 |
134 bool still_down_; | 159 bool still_down_; |
135 bool defer_confirm_single_tap_; | 160 bool defer_confirm_single_tap_; |
136 bool in_longpress_; | 161 bool in_longpress_; |
137 bool always_in_tap_region_; | 162 bool always_in_tap_region_; |
138 bool always_in_bigger_tap_region_; | 163 bool always_in_bigger_tap_region_; |
139 | 164 |
140 scoped_ptr<MotionEvent> current_down_event_; | 165 scoped_ptr<MotionEvent> current_down_event_; |
141 scoped_ptr<MotionEvent> previous_up_event_; | 166 scoped_ptr<MotionEvent> previous_up_event_; |
142 | 167 |
143 // True when the user is still touching for the second tap (down, move, and | 168 // True when the user is still touching for the second tap (down, move, and |
144 // up events). Can only be true if there is a double tap listener attached. | 169 // up events). Can only be true if there is a double tap listener attached. |
145 bool is_double_tapping_; | 170 bool is_double_tapping_; |
146 | 171 |
147 float last_focus_x_; | 172 float last_focus_x_; |
148 float last_focus_y_; | 173 float last_focus_y_; |
149 float down_focus_x_; | 174 float down_focus_x_; |
150 float down_focus_y_; | 175 float down_focus_y_; |
151 | 176 |
152 bool is_longpress_enabled_; | 177 bool longpress_enabled_; |
| 178 bool swipe_enabled_; |
153 | 179 |
154 // Determines speed during touch scrolling. | 180 // Determines speed during touch scrolling. |
155 VelocityTrackerState velocity_tracker_; | 181 VelocityTrackerState velocity_tracker_; |
156 | 182 |
157 DISALLOW_COPY_AND_ASSIGN(GestureDetector); | 183 DISALLOW_COPY_AND_ASSIGN(GestureDetector); |
158 }; | 184 }; |
159 | 185 |
160 } // namespace ui | 186 } // namespace ui |
161 | 187 |
162 #endif // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ | 188 #endif // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ |
OLD | NEW |