Chromium Code Reviews| 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 // Maximum angle of the swipe from it's dominant component axis, between | |
|
tdresser
2014/04/24 12:42:40
Extremely nitful nit.
it's -> its
jdduke (slow)
2014/04/24 15:13:55
Done.
| |
| 52 // (0, 45] degrees. The closer this is to 0, the closer the dominant | |
| 53 // direction of the swipe must be to up, down left or right. | |
| 54 float maximum_swipe_deviation_angle; | |
| 43 }; | 55 }; |
| 44 | 56 |
| 45 class GestureListener { | 57 class GestureListener { |
| 46 public: | 58 public: |
| 47 virtual ~GestureListener() {} | 59 virtual ~GestureListener() {} |
| 48 virtual bool OnDown(const MotionEvent& e) = 0; | 60 virtual bool OnDown(const MotionEvent& e) = 0; |
| 49 virtual void OnShowPress(const MotionEvent& e) = 0; | 61 virtual void OnShowPress(const MotionEvent& e) = 0; |
| 50 virtual bool OnSingleTapUp(const MotionEvent& e) = 0; | 62 virtual bool OnSingleTapUp(const MotionEvent& e) = 0; |
| 51 virtual bool OnLongPress(const MotionEvent& e) = 0; | 63 virtual bool OnLongPress(const MotionEvent& e) = 0; |
| 52 virtual bool OnScroll(const MotionEvent& e1, const MotionEvent& e2, | 64 virtual bool OnScroll(const MotionEvent& e1, |
| 53 float distance_x, float distance_y) = 0; | 65 const MotionEvent& e2, |
| 54 virtual bool OnFling(const MotionEvent& e1, const MotionEvent& e2, | 66 float distance_x, |
| 55 float velocity_x, float velocity_y) = 0; | 67 float distance_y) = 0; |
| 68 virtual bool OnFling(const MotionEvent& e1, | |
| 69 const MotionEvent& e2, | |
| 70 float velocity_x, | |
| 71 float velocity_y) = 0; | |
| 72 // Added for Chromium (Aura). | |
| 73 virtual bool OnSwipe(const MotionEvent& e1, | |
| 74 const MotionEvent& e2, | |
| 75 float velocity_x, | |
| 76 float velocity_y) = 0; | |
| 56 }; | 77 }; |
| 57 | 78 |
| 58 class DoubleTapListener { | 79 class DoubleTapListener { |
| 59 public: | 80 public: |
| 60 virtual ~DoubleTapListener() {} | 81 virtual ~DoubleTapListener() {} |
| 61 virtual bool OnSingleTapConfirmed(const MotionEvent& e) = 0; | 82 virtual bool OnSingleTapConfirmed(const MotionEvent& e) = 0; |
| 62 virtual bool OnDoubleTap(const MotionEvent& e) = 0; | 83 virtual bool OnDoubleTap(const MotionEvent& e) = 0; |
| 63 virtual bool OnDoubleTapEvent(const MotionEvent& e) = 0; | 84 virtual bool OnDoubleTapEvent(const MotionEvent& e) = 0; |
| 64 }; | 85 }; |
| 65 | 86 |
| 66 // A convenience class to extend when you only want to listen for a subset | 87 // 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 | 88 // of all the gestures. This implements all methods in the |
| 68 // |GestureListener| and |DoubleTapListener| but does | 89 // |GestureListener| and |DoubleTapListener| but does |
| 69 // nothing and returns false for all applicable methods. | 90 // nothing and returns false for all applicable methods. |
| 70 class SimpleGestureListener : public GestureListener, | 91 class SimpleGestureListener : public GestureListener, |
| 71 public DoubleTapListener { | 92 public DoubleTapListener { |
| 72 public: | 93 public: |
| 73 // GestureListener implementation. | 94 // GestureListener implementation. |
| 74 virtual bool OnDown(const MotionEvent& e) OVERRIDE; | 95 virtual bool OnDown(const MotionEvent& e) OVERRIDE; |
| 75 virtual void OnShowPress(const MotionEvent& e) OVERRIDE; | 96 virtual void OnShowPress(const MotionEvent& e) OVERRIDE; |
| 76 virtual bool OnSingleTapUp(const MotionEvent& e) OVERRIDE; | 97 virtual bool OnSingleTapUp(const MotionEvent& e) OVERRIDE; |
| 77 virtual bool OnLongPress(const MotionEvent& e) OVERRIDE; | 98 virtual bool OnLongPress(const MotionEvent& e) OVERRIDE; |
| 78 virtual bool OnScroll(const MotionEvent& e1, const MotionEvent& e2, | 99 virtual bool OnScroll(const MotionEvent& e1, |
| 79 float distance_x, float distance_y) OVERRIDE; | 100 const MotionEvent& e2, |
| 80 virtual bool OnFling(const MotionEvent& e1, const MotionEvent& e2, | 101 float distance_x, |
| 81 float velocity_x, float velocity_y) OVERRIDE; | 102 float distance_y) OVERRIDE; |
| 103 virtual bool OnFling(const MotionEvent& e1, | |
| 104 const MotionEvent& e2, | |
| 105 float velocity_x, | |
| 106 float velocity_y) OVERRIDE; | |
| 107 virtual bool OnSwipe(const MotionEvent& e1, | |
| 108 const MotionEvent& e2, | |
| 109 float velocity_x, | |
| 110 float velocity_y) OVERRIDE; | |
| 82 | 111 |
| 83 // DoubleTapListener implementation. | 112 // DoubleTapListener implementation. |
| 84 virtual bool OnSingleTapConfirmed(const MotionEvent& e) OVERRIDE; | 113 virtual bool OnSingleTapConfirmed(const MotionEvent& e) OVERRIDE; |
| 85 virtual bool OnDoubleTap(const MotionEvent& e) OVERRIDE; | 114 virtual bool OnDoubleTap(const MotionEvent& e) OVERRIDE; |
| 86 virtual bool OnDoubleTapEvent(const MotionEvent& e) OVERRIDE; | 115 virtual bool OnDoubleTapEvent(const MotionEvent& e) OVERRIDE; |
| 87 }; | 116 }; |
| 88 | 117 |
| 89 GestureDetector(const Config& config, | 118 GestureDetector(const Config& config, |
| 90 GestureListener* listener, | 119 GestureListener* listener, |
| 91 DoubleTapListener* optional_double_tap_listener); | 120 DoubleTapListener* optional_double_tap_listener); |
| 92 ~GestureDetector(); | 121 ~GestureDetector(); |
| 93 | 122 |
| 94 bool OnTouchEvent(const MotionEvent& ev); | 123 bool OnTouchEvent(const MotionEvent& ev); |
| 95 | 124 |
| 96 // Setting a valid |double_tap_listener| will enable double-tap detection, | 125 // Setting a valid |double_tap_listener| will enable double-tap detection, |
| 97 // wherein calls to |OnSimpleTapConfirmed| are delayed by the tap timeout. | 126 // wherein calls to |OnSimpleTapConfirmed| are delayed by the tap timeout. |
| 98 // Note: The listener must never be changed while |is_double_tapping| is true. | 127 // Note: The listener must never be changed while |is_double_tapping| is true. |
| 99 void SetDoubleTapListener(DoubleTapListener* double_tap_listener); | 128 void SetDoubleTapListener(DoubleTapListener* double_tap_listener); |
| 100 | 129 |
| 101 bool has_doubletap_listener() const { return double_tap_listener_ != NULL; } | 130 bool has_doubletap_listener() const { return double_tap_listener_ != NULL; } |
| 102 | 131 |
| 103 bool is_double_tapping() const { return is_double_tapping_; } | 132 bool is_double_tapping() const { return is_double_tapping_; } |
| 104 | 133 |
| 105 void set_is_longpress_enabled(bool is_longpress_enabled) { | 134 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 | 135 |
| 111 private: | 136 private: |
| 112 void Init(const Config& config); | 137 void Init(const Config& config); |
| 113 void OnShowPressTimeout(); | 138 void OnShowPressTimeout(); |
| 114 void OnLongPressTimeout(); | 139 void OnLongPressTimeout(); |
| 115 void OnTapTimeout(); | 140 void OnTapTimeout(); |
| 116 void Cancel(); | 141 void Cancel(); |
| 117 void CancelTaps(); | 142 void CancelTaps(); |
| 118 bool IsConsideredDoubleTap(const MotionEvent& first_down, | 143 bool IsConsideredDoubleTap(const MotionEvent& first_down, |
| 119 const MotionEvent& first_up, | 144 const MotionEvent& first_up, |
| 120 const MotionEvent& second_down) const; | 145 const MotionEvent& second_down) const; |
| 121 | 146 |
| 122 class TimeoutGestureHandler; | 147 class TimeoutGestureHandler; |
| 123 scoped_ptr<TimeoutGestureHandler> timeout_handler_; | 148 scoped_ptr<TimeoutGestureHandler> timeout_handler_; |
| 124 GestureListener* const listener_; | 149 GestureListener* const listener_; |
| 125 DoubleTapListener* double_tap_listener_; | 150 DoubleTapListener* double_tap_listener_; |
| 126 | 151 |
| 127 float touch_slop_square_; | 152 float touch_slop_square_; |
| 128 float double_tap_touch_slop_square_; | 153 float double_tap_touch_slop_square_; |
| 129 float double_tap_slop_square_; | 154 float double_tap_slop_square_; |
| 130 float min_fling_velocity_; | 155 float min_fling_velocity_; |
| 131 float max_fling_velocity_; | 156 float max_fling_velocity_; |
| 157 float min_swipe_velocity_; | |
| 158 float min_swipe_direction_component_ratio_; | |
| 132 base::TimeDelta double_tap_timeout_; | 159 base::TimeDelta double_tap_timeout_; |
| 133 | 160 |
| 134 bool still_down_; | 161 bool still_down_; |
| 135 bool defer_confirm_single_tap_; | 162 bool defer_confirm_single_tap_; |
| 136 bool in_longpress_; | 163 bool in_longpress_; |
| 137 bool always_in_tap_region_; | 164 bool always_in_tap_region_; |
| 138 bool always_in_bigger_tap_region_; | 165 bool always_in_bigger_tap_region_; |
| 139 | 166 |
| 140 scoped_ptr<MotionEvent> current_down_event_; | 167 scoped_ptr<MotionEvent> current_down_event_; |
| 141 scoped_ptr<MotionEvent> previous_up_event_; | 168 scoped_ptr<MotionEvent> previous_up_event_; |
| 142 | 169 |
| 143 // True when the user is still touching for the second tap (down, move, and | 170 // 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. | 171 // up events). Can only be true if there is a double tap listener attached. |
| 145 bool is_double_tapping_; | 172 bool is_double_tapping_; |
| 146 | 173 |
| 147 float last_focus_x_; | 174 float last_focus_x_; |
| 148 float last_focus_y_; | 175 float last_focus_y_; |
| 149 float down_focus_x_; | 176 float down_focus_x_; |
| 150 float down_focus_y_; | 177 float down_focus_y_; |
| 151 | 178 |
| 152 bool is_longpress_enabled_; | 179 bool longpress_enabled_; |
| 180 bool swipe_enabled_; | |
| 153 | 181 |
| 154 // Determines speed during touch scrolling. | 182 // Determines speed during touch scrolling. |
| 155 VelocityTrackerState velocity_tracker_; | 183 VelocityTrackerState velocity_tracker_; |
| 156 | 184 |
| 157 DISALLOW_COPY_AND_ASSIGN(GestureDetector); | 185 DISALLOW_COPY_AND_ASSIGN(GestureDetector); |
| 158 }; | 186 }; |
| 159 | 187 |
| 160 } // namespace ui | 188 } // namespace ui |
| 161 | 189 |
| 162 #endif // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ | 190 #endif // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ |
| OLD | NEW |