gesture_provider.h revision 116680a4aac90f2aa7413d9095a592090648e557
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_PROVIDER_H_
6#define UI_EVENTS_GESTURE_DETECTION_GESTURE_PROVIDER_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "ui/events/gesture_detection/gesture_detection_export.h"
11#include "ui/events/gesture_detection/gesture_detector.h"
12#include "ui/events/gesture_detection/gesture_event_data.h"
13#include "ui/events/gesture_detection/scale_gesture_detector.h"
14#include "ui/events/gesture_detection/snap_scroll_controller.h"
15#include "ui/gfx/display.h"
16
17namespace ui {
18
19class GESTURE_DETECTION_EXPORT GestureProviderClient {
20 public:
21  virtual ~GestureProviderClient() {}
22  virtual void OnGestureEvent(const GestureEventData& gesture) = 0;
23};
24
25// Given a stream of |MotionEvent|'s, provides gesture detection and gesture
26// event dispatch.
27class GESTURE_DETECTION_EXPORT GestureProvider {
28 public:
29  struct GESTURE_DETECTION_EXPORT Config {
30    Config();
31    ~Config();
32    gfx::Display display;
33    GestureDetector::Config gesture_detector_config;
34    ScaleGestureDetector::Config scale_gesture_detector_config;
35
36    // If |disable_click_delay| is true and double-tap support is disabled,
37    // there will be no delay before tap events. When double-tap support is
38    // enabled, there will always be a delay before a tap event is fired, in
39    // order to allow the double tap gesture to occur without firing any tap
40    // events.
41    bool disable_click_delay;
42
43    // If |gesture_begin_end_types_enabled| is true, fire an ET_GESTURE_BEGIN
44    // event for every added touch point, and an ET_GESTURE_END event for every
45    // removed touch point. Defaults to false.
46    bool gesture_begin_end_types_enabled;
47
48    // The min and max size (both length and width, in dips) of the generated
49    // bounding box for all gesture types. This is useful for touch streams
50    // that may report zero or unreasonably small or large touch sizes.
51    // Both values default to 0 (disabled).
52    float min_gesture_bounds_length;
53    float max_gesture_bounds_length;
54  };
55
56  GestureProvider(const Config& config, GestureProviderClient* client);
57  ~GestureProvider();
58
59  // Handle the incoming MotionEvent, returning false if the event could not
60  // be handled.
61  bool OnTouchEvent(const MotionEvent& event);
62
63  // Update whether multi-touch pinch zoom is supported by the platform.
64  void SetMultiTouchZoomSupportEnabled(bool enabled);
65
66  // Update whether double-tap gestures are supported by the platform.
67  void SetDoubleTapSupportForPlatformEnabled(bool enabled);
68
69  // Update whether double-tap gesture detection should be suppressed, e.g.,
70  // if the page scale is fixed or the page has a mobile viewport. This disables
71  // the tap delay, allowing rapid and responsive single-tap gestures.
72  void SetDoubleTapSupportForPageEnabled(bool enabled);
73
74  // Whether a scroll gesture is in-progress.
75  bool IsScrollInProgress() const;
76
77  // Whether a pinch gesture is in-progress (i.e. a pinch update has been
78  // forwarded and detection is still active).
79  bool IsPinchInProgress() const;
80
81  // Whether a double-tap gesture is in-progress (either double-tap or
82  // double-tap drag zoom).
83  bool IsDoubleTapInProgress() const;
84
85  // May be NULL if there is no currently active touch sequence.
86  const ui::MotionEvent* current_down_event() const {
87    return current_down_event_.get();
88  }
89
90 private:
91  void InitGestureDetectors(const Config& config);
92
93  bool CanHandle(const MotionEvent& event) const;
94
95  void Fling(const MotionEvent& e, float velocity_x, float velocity_y);
96  void Send(GestureEventData gesture);
97  bool SendLongTapIfNecessary(const MotionEvent& event);
98  void EndTouchScrollIfNecessary(const MotionEvent& event,
99                                 bool send_scroll_end_event);
100  void OnTouchEventHandlingBegin(const MotionEvent& event);
101  void OnTouchEventHandlingEnd(const MotionEvent& event);
102  void UpdateDoubleTapDetectionSupport();
103
104  GestureProviderClient* const client_;
105
106  class GestureListenerImpl;
107  friend class GestureListenerImpl;
108  scoped_ptr<GestureListenerImpl> gesture_listener_;
109
110  class ScaleGestureListenerImpl;
111  friend class ScaleGestureListenerImpl;
112  scoped_ptr<ScaleGestureListenerImpl> scale_gesture_listener_;
113
114  scoped_ptr<MotionEvent> current_down_event_;
115
116  // Whether the respective {SCROLL,PINCH}_BEGIN gestures have been terminated
117  // with a {SCROLL,PINCH}_END.
118  bool touch_scroll_in_progress_;
119  bool pinch_in_progress_;
120
121  // Whether double-tap gesture detection is currently supported.
122  bool double_tap_support_for_page_;
123  bool double_tap_support_for_platform_;
124
125  // Keeps track of the current GESTURE_LONG_PRESS event. If a context menu is
126  // opened after a GESTURE_LONG_PRESS, this is used to insert a
127  // GESTURE_TAP_CANCEL for removing any ::active styling.
128  base::TimeTicks current_longpress_time_;
129
130  const bool gesture_begin_end_types_enabled_;
131
132  const float min_gesture_bounds_length_;
133  const float max_gesture_bounds_length_;
134};
135
136}  //  namespace ui
137
138#endif  // UI_EVENTS_GESTURE_DETECTION_GESTURE_PROVIDER_H_
139