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.
5 #ifndef UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_
6 #define UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "ui/events/gesture_detection/gesture_detection_export.h"
11 #include "ui/events/gesture_detection/velocity_tracker_state.h"
15 class DoubleTapListener
;
16 class GestureListener
;
19 // Port of GestureDetector.java from Android
20 // * platform/frameworks/base/core/java/android/view/GestureDetector.java
21 // * Change-Id: Ib470735ec929b0b358fca4597e92dc81084e675f
22 // * Please update the Change-Id as upstream Android changes are pulled.
23 class GESTURE_DETECTION_EXPORT GestureDetector
{
25 struct GESTURE_DETECTION_EXPORT Config
{
29 base::TimeDelta longpress_timeout
;
30 base::TimeDelta showpress_timeout
;
31 base::TimeDelta double_tap_timeout
;
33 // The minimum duration between the first tap's up event and the second
34 // tap's down event for an interaction to be considered a double-tap.
35 base::TimeDelta double_tap_min_time
;
37 // Distance a touch can wander before a scroll will occur (in dips).
40 // Distance the first touch can wander before it is no longer considered a
41 // double tap (in dips).
42 float double_tap_slop
;
44 // Minimum velocity to initiate a fling (in dips/second).
45 float minimum_fling_velocity
;
47 // Maximum velocity of an initiated fling (in dips/second).
48 float maximum_fling_velocity
;
50 // Whether |OnSwipe| should be called after a secondary touch is released
51 // while a logical swipe gesture is active. Defaults to false.
54 // Minimum velocity to initiate a swipe (in dips/second).
55 float minimum_swipe_velocity
;
57 // Maximum angle of the swipe from its dominant component axis, between
58 // (0, 45] degrees. The closer this is to 0, the closer the dominant
59 // direction of the swipe must be to up, down left or right.
60 float maximum_swipe_deviation_angle
;
62 // Whether |OnTwoFingerTap| should be called for two finger tap
63 // gestures. Defaults to false.
64 bool two_finger_tap_enabled
;
66 // Maximum distance between pointers for a two finger tap.
67 float two_finger_tap_max_separation
;
69 // Maximum time the second pointer can be active for a two finger tap.
70 base::TimeDelta two_finger_tap_timeout
;
72 VelocityTracker::Strategy velocity_tracker_strategy
;
75 GestureDetector(const Config
& config
,
76 GestureListener
* listener
,
77 DoubleTapListener
* optional_double_tap_listener
);
80 bool OnTouchEvent(const MotionEvent
& ev
);
82 // Setting a valid |double_tap_listener| will enable double-tap detection,
83 // wherein calls to |OnSimpleTapConfirmed| are delayed by the tap timeout.
84 // Note: The listener must never be changed while |is_double_tapping| is true.
85 void SetDoubleTapListener(DoubleTapListener
* double_tap_listener
);
87 bool has_doubletap_listener() const { return double_tap_listener_
!= NULL
; }
89 bool is_double_tapping() const { return is_double_tapping_
; }
91 void set_longpress_enabled(bool enabled
) { longpress_enabled_
= enabled
; }
92 void set_showpress_enabled(bool enabled
) { showpress_enabled_
= enabled
; }
95 void Init(const Config
& config
);
96 void OnShowPressTimeout();
97 void OnLongPressTimeout();
101 bool IsConsideredDoubleTap(const MotionEvent
& first_down
,
102 const MotionEvent
& first_up
,
103 const MotionEvent
& second_down
) const;
104 bool HandleSwipeIfNeeded(const MotionEvent
& up
, float vx
, float vy
);
106 class TimeoutGestureHandler
;
107 scoped_ptr
<TimeoutGestureHandler
> timeout_handler_
;
108 GestureListener
* const listener_
;
109 DoubleTapListener
* double_tap_listener_
;
111 float touch_slop_square_
;
112 float double_tap_touch_slop_square_
;
113 float double_tap_slop_square_
;
114 float two_finger_tap_distance_square_
;
115 float min_fling_velocity_
;
116 float max_fling_velocity_
;
117 float min_swipe_velocity_
;
118 float min_swipe_direction_component_ratio_
;
119 base::TimeDelta double_tap_timeout_
;
120 base::TimeDelta two_finger_tap_timeout_
;
121 base::TimeDelta double_tap_min_time_
;
124 bool defer_confirm_single_tap_
;
125 bool always_in_tap_region_
;
126 bool always_in_bigger_tap_region_
;
127 bool two_finger_tap_allowed_for_gesture_
;
129 scoped_ptr
<MotionEvent
> current_down_event_
;
130 scoped_ptr
<MotionEvent
> previous_up_event_
;
131 scoped_ptr
<MotionEvent
> secondary_pointer_down_event_
;
133 // True when the user is still touching for the second tap (down, move, and
134 // up events). Can only be true if there is a double tap listener attached.
135 bool is_double_tapping_
;
142 bool longpress_enabled_
;
143 bool showpress_enabled_
;
145 bool two_finger_tap_enabled_
;
147 // Determines speed during touch scrolling.
148 VelocityTrackerState velocity_tracker_
;
150 DISALLOW_COPY_AND_ASSIGN(GestureDetector
);
155 #endif // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_