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 CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_GESTURE_DETECTOR_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_GESTURE_DETECTOR_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "content/browser/renderer_host/input/gestures/velocity_tracker_state.h" |
| 11 |
| 12 namespace content { |
| 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 // * Changes to this class should be cosmetic only, easing fork maintenance. |
| 20 class GestureDetector { |
| 21 public: |
| 22 struct 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 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 void 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 void 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(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 // Constants used by GestureHandler. |
| 106 enum TimeoutEvent { |
| 107 SHOW_PRESS = 0, |
| 108 LONG_PRESS, |
| 109 TAP, |
| 110 TIMEOUT_EVENT_COUNT |
| 111 }; |
| 112 |
| 113 class GestureHandler; |
| 114 scoped_ptr<GestureHandler> handler_; |
| 115 OnGestureListener* const listener_; |
| 116 OnDoubleTapListener* double_tap_listener_; |
| 117 |
| 118 int touch_slop_square_; |
| 119 int double_tap_touch_slop_square_; |
| 120 int double_tap_slop_square_; |
| 121 int min_fling_velocity_; |
| 122 int max_fling_velocity_; |
| 123 base::TimeDelta double_tap_timeout_; |
| 124 |
| 125 bool still_down_; |
| 126 bool defer_confirm_single_tap_; |
| 127 bool in_long_press_; |
| 128 bool always_in_tap_region_; |
| 129 bool always_in_bigger_tap_region_; |
| 130 |
| 131 scoped_ptr<MotionEvent> current_down_event_; |
| 132 scoped_ptr<MotionEvent> previous_up_event_; |
| 133 |
| 134 // True when the user is still touching for the second tap (down, move, and |
| 135 // up events). Can only be true if there is a double tap listener attached. |
| 136 bool is_double_tapping_; |
| 137 |
| 138 float last_focus_x_; |
| 139 float last_focus_y_; |
| 140 float down_focus_x_; |
| 141 float down_focus_y_; |
| 142 |
| 143 bool is_longpress_enabled_; |
| 144 |
| 145 // Determines speed during touch scrolling. |
| 146 VelocityTrackerState velocity_tracker_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(GestureDetector); |
| 149 }; |
| 150 |
| 151 } // namespace content |
| 152 |
| 153 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_DETECTOR_H_ |
OLD | NEW |