Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ | |
| 6 #define UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ | |
| 7 | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "ui/events/gesture_detection/gesture_detection_export.h" | |
| 10 #include "ui/events/gesture_detection/velocity_tracker_state.h" | |
| 11 | |
| 12 namespace ui { | |
| 13 | |
| 14 class MotionEvent; | |
| 15 | |
| 16 // Port of GestureDetector.java from Android | |
| 17 // * platform/frameworks/base/core/java/android/view/GestureDetector.java | |
| 18 // * Change-Id: Ib470735ec929b0b358fca4597e92dc81084e675f | |
| 19 // * Please update the Change-Id as upstream Android changes are pulled. | |
| 20 class GestureDetector { | |
| 21 public: | |
| 22 struct GESTURE_DETECTION_EXPORT Config { | |
| 23 Config(); | |
| 24 ~Config(); | |
| 25 base::TimeDelta longpress_timeout; | |
| 26 base::TimeDelta tap_timeout; | |
| 27 base::TimeDelta double_tap_timeout; | |
| 28 int scaled_touch_slop; | |
|
tdresser
2014/02/19 17:44:12
I'm not too keen on the use of "scaled" in these n
jdduke (slow)
2014/02/19 18:51:58
Yeah, all of the scaled prefixes are remnants of t
| |
| 29 int scaled_double_tap_slop; | |
| 30 int scaled_minimum_fling_velocity; | |
| 31 int scaled_maximum_fling_velocity; | |
| 32 }; | |
| 33 | |
| 34 class OnGestureListener { | |
| 35 public: | |
| 36 virtual ~OnGestureListener() {} | |
| 37 virtual bool OnDown(const MotionEvent& e) = 0; | |
| 38 virtual void OnShowPress(const MotionEvent& e) = 0; | |
| 39 virtual bool OnSingleTapUp(const MotionEvent& e) = 0; | |
| 40 virtual bool OnLongPress(const MotionEvent& e) = 0; | |
| 41 virtual bool OnScroll(const MotionEvent& e1, const MotionEvent& e2, | |
| 42 float distance_x, float distance_y) = 0; | |
| 43 virtual bool OnFling(const MotionEvent& e1, const MotionEvent& e2, | |
| 44 float velocity_x, float velocity_y) = 0; | |
| 45 }; | |
| 46 | |
| 47 class OnDoubleTapListener { | |
| 48 public: | |
| 49 virtual ~OnDoubleTapListener() {} | |
| 50 virtual bool OnSingleTapConfirmed(const MotionEvent& e) = 0; | |
| 51 virtual bool OnDoubleTap(const MotionEvent& e) = 0; | |
| 52 virtual bool OnDoubleTapEvent(const MotionEvent& e) = 0; | |
| 53 }; | |
| 54 | |
| 55 // A convenience class to extend when you only want to listen for a subset | |
| 56 // of all the gestures. This implements all methods in the | |
| 57 // |OnGestureListener| and |OnDoubleTapListener| but does | |
| 58 // nothing and returns false for all applicable methods. | |
| 59 class SimpleOnGestureListener : public OnGestureListener, | |
| 60 public OnDoubleTapListener { | |
| 61 // OnGestureListener | |
| 62 virtual bool OnDown(const MotionEvent& e) OVERRIDE; | |
| 63 virtual void OnShowPress(const MotionEvent& e) OVERRIDE; | |
| 64 virtual bool OnSingleTapUp(const MotionEvent& e) OVERRIDE; | |
| 65 virtual bool OnLongPress(const MotionEvent& e) OVERRIDE; | |
| 66 virtual bool OnScroll(const MotionEvent& e1, const MotionEvent& e2, | |
| 67 float distance_x, float distance_y) OVERRIDE; | |
| 68 virtual bool OnFling(const MotionEvent& e1, const MotionEvent& e2, | |
| 69 float velocity_x, float velocity_y) OVERRIDE; | |
| 70 | |
| 71 // OnDoubleTapListener | |
| 72 virtual bool OnSingleTapConfirmed(const MotionEvent& e) OVERRIDE; | |
| 73 virtual bool OnDoubleTap(const MotionEvent& e) OVERRIDE; | |
| 74 virtual bool OnDoubleTapEvent(const MotionEvent& e) OVERRIDE; | |
| 75 }; | |
| 76 | |
| 77 GestureDetector(const Config& config, | |
| 78 OnGestureListener* listener, | |
| 79 OnDoubleTapListener* optional_double_tap_listener); | |
| 80 ~GestureDetector(); | |
| 81 | |
| 82 bool OnTouchEvent(const MotionEvent& ev); | |
| 83 | |
| 84 void set_on_doubletap_listener(OnDoubleTapListener* double_tap_listener) { | |
| 85 double_tap_listener_ = double_tap_listener; | |
| 86 } | |
| 87 | |
| 88 void set_is_longpress_enabled(bool is_longpress_enabled) { | |
| 89 is_longpress_enabled_ = is_longpress_enabled; | |
| 90 } | |
| 91 | |
| 92 bool is_longpress_enabled() const { return is_longpress_enabled_; } | |
| 93 | |
| 94 private: | |
| 95 void Init(Config config); | |
| 96 void OnShowPressTimeout(); | |
| 97 void OnLongPressTimeout(); | |
| 98 void OnTapTimeout(); | |
| 99 void Cancel(); | |
| 100 void CancelTaps(); | |
| 101 bool IsConsideredDoubleTap(const MotionEvent& first_down, | |
| 102 const MotionEvent& first_up, | |
| 103 const MotionEvent& second_down); | |
| 104 | |
| 105 class TimeoutGestureHandler; | |
| 106 scoped_ptr<TimeoutGestureHandler> timeout_handler_; | |
| 107 OnGestureListener* const listener_; | |
| 108 OnDoubleTapListener* double_tap_listener_; | |
| 109 | |
| 110 int touch_slop_square_; | |
| 111 int double_tap_touch_slop_square_; | |
| 112 int double_tap_slop_square_; | |
| 113 int min_fling_velocity_; | |
| 114 int max_fling_velocity_; | |
| 115 base::TimeDelta double_tap_timeout_; | |
| 116 | |
| 117 bool still_down_; | |
| 118 bool defer_confirm_single_tap_; | |
| 119 bool in_long_press_; | |
| 120 bool always_in_tap_region_; | |
| 121 bool always_in_bigger_tap_region_; | |
| 122 | |
| 123 scoped_ptr<MotionEvent> current_down_event_; | |
| 124 scoped_ptr<MotionEvent> previous_up_event_; | |
| 125 | |
| 126 // True when the user is still touching for the second tap (down, move, and | |
| 127 // up events). Can only be true if there is a double tap listener attached. | |
| 128 bool is_double_tapping_; | |
| 129 | |
| 130 float last_focus_x_; | |
| 131 float last_focus_y_; | |
| 132 float down_focus_x_; | |
| 133 float down_focus_y_; | |
| 134 | |
| 135 bool is_longpress_enabled_; | |
| 136 | |
| 137 // Determines speed during touch scrolling. | |
| 138 VelocityTrackerState velocity_tracker_; | |
| 139 | |
| 140 DISALLOW_COPY_AND_ASSIGN(GestureDetector); | |
| 141 }; | |
| 142 | |
| 143 } // namespace ui | |
| 144 | |
| 145 #endif // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ | |
| OLD | NEW |