Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / events / gesture_detection / gesture_detector.h
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/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"
12
13 namespace ui {
14
15 class DoubleTapListener;
16 class GestureListener;
17 class MotionEvent;
18
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 {
24  public:
25   struct GESTURE_DETECTION_EXPORT Config {
26     Config();
27     ~Config();
28
29     base::TimeDelta longpress_timeout;
30     base::TimeDelta showpress_timeout;
31     base::TimeDelta double_tap_timeout;
32
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;
36
37     // Distance a touch can wander before a scroll will occur (in dips).
38     float touch_slop;
39
40     // Distance the first touch can wander before it is no longer considered a
41     // double tap (in dips).
42     float double_tap_slop;
43
44     // Minimum velocity to initiate a fling (in dips/second).
45     float minimum_fling_velocity;
46
47     // Maximum velocity of an initiated fling (in dips/second).
48     float maximum_fling_velocity;
49
50     // Whether |OnSwipe| should be called after a secondary touch is released
51     // while a logical swipe gesture is active. Defaults to false.
52     bool swipe_enabled;
53
54     // Minimum velocity to initiate a swipe (in dips/second).
55     float minimum_swipe_velocity;
56
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;
61
62     // Whether |OnTwoFingerTap| should be called for two finger tap
63     // gestures. Defaults to false.
64     bool two_finger_tap_enabled;
65
66     // Maximum distance between pointers for a two finger tap.
67     float two_finger_tap_max_separation;
68
69     // Maximum time the second pointer can be active for a two finger tap.
70     base::TimeDelta two_finger_tap_timeout;
71
72     VelocityTracker::Strategy velocity_tracker_strategy;
73   };
74
75   GestureDetector(const Config& config,
76                   GestureListener* listener,
77                   DoubleTapListener* optional_double_tap_listener);
78   ~GestureDetector();
79
80   bool OnTouchEvent(const MotionEvent& ev);
81
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);
86
87   bool has_doubletap_listener() const { return double_tap_listener_ != NULL; }
88
89   bool is_double_tapping() const { return is_double_tapping_; }
90
91   void set_longpress_enabled(bool enabled) { longpress_enabled_ = enabled; }
92   void set_showpress_enabled(bool enabled) { showpress_enabled_ = enabled; }
93
94  private:
95   void Init(const 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) const;
104   bool HandleSwipeIfNeeded(const MotionEvent& up, float vx, float vy);
105
106   class TimeoutGestureHandler;
107   scoped_ptr<TimeoutGestureHandler> timeout_handler_;
108   GestureListener* const listener_;
109   DoubleTapListener* double_tap_listener_;
110
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_;
122
123   bool still_down_;
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_;
128
129   scoped_ptr<MotionEvent> current_down_event_;
130   scoped_ptr<MotionEvent> previous_up_event_;
131   scoped_ptr<MotionEvent> secondary_pointer_down_event_;
132
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_;
136
137   float last_focus_x_;
138   float last_focus_y_;
139   float down_focus_x_;
140   float down_focus_y_;
141
142   bool longpress_enabled_;
143   bool showpress_enabled_;
144   bool swipe_enabled_;
145   bool two_finger_tap_enabled_;
146
147   // Determines speed during touch scrolling.
148   VelocityTrackerState velocity_tracker_;
149
150   DISALLOW_COPY_AND_ASSIGN(GestureDetector);
151 };
152
153 }  // namespace ui
154
155 #endif  // UI_EVENTS_GESTURE_DETECTION_GESTURE_DETECTOR_H_