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_SCALE_GESTURE_DETECTOR_H_
6 #define UI_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_
8 #include "base/time/time.h"
9 #include "ui/events/gesture_detection/gesture_detection_export.h"
15 // Port of ScaleGestureDetector.java from Android
16 // * platform/frameworks/base/core/java/android/view/ScaleGestureDetector.java
17 // * Change-Id: I3e7926a4f6f9ab4951f380bd004499c78b3bda69
18 // * Please update the Change-Id as upstream Android changes are pulled.
19 class ScaleGestureDetector
{
21 struct GESTURE_DETECTION_EXPORT Config
{
25 // Distance the current span can deviate from the initial span before
26 // scaling will start (in dips). The span is the diameter of the circle with
27 // a radius of average pointer deviation from the focal point.
30 // Minimum accepted value for TouchMajor while scaling (in dips).
31 float min_scaling_touch_major
;
33 // Minimum span needed to initiate a scaling gesture (in dips).
34 float min_scaling_span
;
36 // Minimum pinch span change before pinch occurs (in dips). See
38 float min_pinch_update_span_delta
;
41 class ScaleGestureListener
{
43 virtual ~ScaleGestureListener() {}
44 virtual bool OnScale(const ScaleGestureDetector
& detector
,
45 const MotionEvent
& e
) = 0;
46 virtual bool OnScaleBegin(const ScaleGestureDetector
& detector
,
47 const MotionEvent
& e
) = 0;
48 virtual void OnScaleEnd(const ScaleGestureDetector
& detector
,
49 const MotionEvent
& e
) = 0;
52 // A convenience class to extend when you only want to listen for a subset of
53 // scaling-related events. This implements all methods in
54 // |ScaleGestureListener| but does nothing.
55 // |OnScale()| returns false so that a subclass can retrieve the accumulated
56 // scale factor in an overridden |OnScaleEnd()|.
57 // |OnScaleBegin() returns true.
58 class SimpleScaleGestureListener
: public ScaleGestureListener
{
60 // ScaleGestureListener implementation.
61 virtual bool OnScale(const ScaleGestureDetector
&,
62 const MotionEvent
&) OVERRIDE
;
63 virtual bool OnScaleBegin(const ScaleGestureDetector
&,
64 const MotionEvent
&) OVERRIDE
;
65 virtual void OnScaleEnd(const ScaleGestureDetector
&,
66 const MotionEvent
&) OVERRIDE
;
69 ScaleGestureDetector(const Config
& config
, ScaleGestureListener
* listener
);
70 virtual ~ScaleGestureDetector();
72 // Accepts MotionEvents and dispatches events to a |ScaleGestureListener|
75 // Note: Applications should pass a complete and consistent event stream to
76 // this method. A complete and consistent event stream involves all
77 // MotionEvents from the initial ACTION_DOWN to the final ACTION_UP or
80 // Returns true if the event was processed and the detector wants to receive
81 // the rest of the MotionEvents in this event stream.
82 bool OnTouchEvent(const MotionEvent
& event
);
84 // This method may be called by the owner when a a double-tap event has been
85 // detected *for the same event stream* being fed to this instance of the
86 // ScaleGestureDetector. As call order is important here, the double-tap
87 // detector should always be offered events *before* the ScaleGestureDetector.
88 bool OnDoubleTap(const MotionEvent
& event
);
90 // Set whether the associated |ScaleGestureListener| should receive
91 // OnScale callbacks when the user performs a doubletap followed by a swipe.
92 bool IsInProgress() const;
93 bool InDoubleTapMode() const;
94 float GetFocusX() const;
95 float GetFocusY() const;
96 float GetCurrentSpan() const;
97 float GetCurrentSpanX() const;
98 float GetCurrentSpanY() const;
99 float GetPreviousSpan() const;
100 float GetPreviousSpanX() const;
101 float GetPreviousSpanY() const;
102 float GetScaleFactor() const;
103 base::TimeDelta
GetTimeDelta() const;
104 base::TimeTicks
GetEventTime() const;
107 enum DoubleTapMode
{ DOUBLE_TAP_MODE_NONE
, DOUBLE_TAP_MODE_IN_PROGRESS
};
109 // The TouchMajor/TouchMinor elements of a MotionEvent can flutter/jitter on
110 // some hardware/driver combos. Smooth out to get kinder, gentler behavior.
111 void AddTouchHistory(const MotionEvent
& ev
);
112 void ResetTouchHistory();
114 void ResetScaleWithSpan(float span
);
116 ScaleGestureListener
* const listener_
;
127 base::TimeTicks curr_time_
;
128 base::TimeTicks prev_time_
;
133 // Bounds for recently seen values.
136 float touch_history_last_accepted_
;
137 int touch_history_direction_
;
138 base::TimeTicks touch_history_last_accepted_time_
;
139 float touch_min_major_
;
140 float touch_max_major_
;
141 float double_tap_focus_x_
;
142 float double_tap_focus_y_
;
143 DoubleTapMode double_tap_mode_
;
145 bool event_before_or_above_starting_gesture_event_
;
147 DISALLOW_COPY_AND_ASSIGN(ScaleGestureDetector
);
152 #endif // UI_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_