OLD | NEW |
(Empty) | |
| 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 |
| 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; |
| 29 int scaled_double_tap_slop; |
| 30 int scaled_minimum_fling_velocity; |
| 31 int scaled_maximum_fling_velocity; |
| 32 }; |
| 33 |
| 34 class GestureListener { |
| 35 public: |
| 36 virtual ~GestureListener() {} |
| 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 DoubleTapListener { |
| 48 public: |
| 49 virtual ~DoubleTapListener() {} |
| 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 // |GestureListener| and |DoubleTapListener| but does |
| 58 // nothing and returns false for all applicable methods. |
| 59 class SimpleGestureListener : public GestureListener, |
| 60 public DoubleTapListener { |
| 61 public: |
| 62 // GestureListener implementation. |
| 63 virtual bool OnDown(const MotionEvent& e) OVERRIDE; |
| 64 virtual void OnShowPress(const MotionEvent& e) OVERRIDE; |
| 65 virtual bool OnSingleTapUp(const MotionEvent& e) OVERRIDE; |
| 66 virtual bool OnLongPress(const MotionEvent& e) OVERRIDE; |
| 67 virtual bool OnScroll(const MotionEvent& e1, const MotionEvent& e2, |
| 68 float distance_x, float distance_y) OVERRIDE; |
| 69 virtual bool OnFling(const MotionEvent& e1, const MotionEvent& e2, |
| 70 float velocity_x, float velocity_y) OVERRIDE; |
| 71 |
| 72 // DoubleTapListener implementation. |
| 73 virtual bool OnSingleTapConfirmed(const MotionEvent& e) OVERRIDE; |
| 74 virtual bool OnDoubleTap(const MotionEvent& e) OVERRIDE; |
| 75 virtual bool OnDoubleTapEvent(const MotionEvent& e) OVERRIDE; |
| 76 }; |
| 77 |
| 78 GestureDetector(const Config& config, |
| 79 GestureListener* listener, |
| 80 DoubleTapListener* optional_double_tap_listener); |
| 81 ~GestureDetector(); |
| 82 |
| 83 bool OnTouchEvent(const MotionEvent& ev); |
| 84 |
| 85 void set_doubletap_listener(DoubleTapListener* double_tap_listener) { |
| 86 double_tap_listener_ = double_tap_listener; |
| 87 } |
| 88 |
| 89 void set_is_longpress_enabled(bool is_longpress_enabled) { |
| 90 is_longpress_enabled_ = is_longpress_enabled; |
| 91 } |
| 92 |
| 93 bool is_longpress_enabled() const { return is_longpress_enabled_; } |
| 94 |
| 95 private: |
| 96 void Init(const Config& config); |
| 97 void OnShowPressTimeout(); |
| 98 void OnLongPressTimeout(); |
| 99 void OnTapTimeout(); |
| 100 void Cancel(); |
| 101 void CancelTaps(); |
| 102 bool IsConsideredDoubleTap(const MotionEvent& first_down, |
| 103 const MotionEvent& first_up, |
| 104 const MotionEvent& second_down) const; |
| 105 |
| 106 class TimeoutGestureHandler; |
| 107 scoped_ptr<TimeoutGestureHandler> timeout_handler_; |
| 108 GestureListener* const listener_; |
| 109 DoubleTapListener* double_tap_listener_; |
| 110 |
| 111 int touch_slop_square_; |
| 112 int double_tap_touch_slop_square_; |
| 113 int double_tap_slop_square_; |
| 114 int min_fling_velocity_; |
| 115 int max_fling_velocity_; |
| 116 base::TimeDelta double_tap_timeout_; |
| 117 |
| 118 bool still_down_; |
| 119 bool defer_confirm_single_tap_; |
| 120 bool in_longpress_; |
| 121 bool always_in_tap_region_; |
| 122 bool always_in_bigger_tap_region_; |
| 123 |
| 124 scoped_ptr<MotionEvent> current_down_event_; |
| 125 scoped_ptr<MotionEvent> previous_up_event_; |
| 126 |
| 127 // True when the user is still touching for the second tap (down, move, and |
| 128 // up events). Can only be true if there is a double tap listener attached. |
| 129 bool is_double_tapping_; |
| 130 |
| 131 float last_focus_x_; |
| 132 float last_focus_y_; |
| 133 float down_focus_x_; |
| 134 float down_focus_y_; |
| 135 |
| 136 bool is_longpress_enabled_; |
| 137 |
| 138 // Determines speed during touch scrolling. |
| 139 VelocityTrackerState velocity_tracker_; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(GestureDetector); |
| 142 }; |
| 143 |
| 144 } // namespace ui |
| 145 |
| 146 #endif // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_ |
OLD | NEW |