gesture_provider.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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/scale_gesture_detector.h"
13#include "ui/events/gesture_detection/snap_scroll_controller.h"
14
15namespace ui {
16
17struct GestureEventData;
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    GestureDetector::Config gesture_detector_config;
33    ScaleGestureDetector::Config scale_gesture_detector_config;
34    SnapScrollController::Config snap_scroll_controller_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.
46    bool gesture_begin_end_types_enabled;
47  };
48
49  GestureProvider(const Config& config, GestureProviderClient* client);
50  ~GestureProvider();
51
52  // Handle the incoming MotionEvent, returning false if the event could not
53  // be handled.
54  bool OnTouchEvent(const MotionEvent& event);
55
56  // Resets all gesture detectors; called on DidStartLoading().
57  void ResetGestureDetectors();
58
59  // Update whether multi-touch gestures are supported.
60  void SetMultiTouchSupportEnabled(bool enabled);
61
62  // Update whether double-tap gestures are supported. This allows
63  // double-tap gesture suppression independent of whether or not the page's
64  // viewport and scale would normally prevent double-tap.
65  // Note: This should not be called while a double-tap gesture is in progress.
66  void SetDoubleTapSupportForPlatformEnabled(bool enabled);
67
68  // Update whether double-tap gesture detection should be suppressed due to
69  // the viewport or scale of the current page. Suppressing double-tap gesture
70  // detection allows for rapid and responsive single-tap gestures.
71  void SetDoubleTapSupportForPageEnabled(bool enabled);
72
73  // Whether a scroll gesture is in-progress.
74  bool IsScrollInProgress() const;
75
76  // Whether a pinch gesture is in-progress (i.e. a pinch update has been
77  // forwarded and detection is still active).
78  bool IsPinchInProgress() const;
79
80  // Whether a double-tap gesture is in-progress (either double-tap or
81  // double-tap drag zoom).
82  bool IsDoubleTapInProgress() const;
83
84  // Whether double-tap gesture detection is supported.
85  bool IsDoubleTapSupported() const;
86
87  // Whether the tap gesture delay is explicitly disabled (independent of
88  // whether double-tap is supported), see |Config.disable_click_delay|.
89  bool IsClickDelayDisabled() const;
90
91  // May be NULL if there is no currently active touch sequence.
92  const ui::MotionEvent* current_down_event() const {
93    return current_down_event_.get();
94  }
95
96 private:
97  void InitGestureDetectors(const Config& config);
98
99  bool CanHandle(const MotionEvent& event) const;
100
101  void Fling(const MotionEvent& e, float velocity_x, float velocity_y);
102  void Send(const GestureEventData& gesture);
103  bool SendLongTapIfNecessary(const MotionEvent& event);
104  void EndTouchScrollIfNecessary(const MotionEvent& event,
105                                 bool send_scroll_end_event);
106  void OnTouchEventHandlingBegin(const MotionEvent& event);
107  void OnTouchEventHandlingEnd(const MotionEvent& event);
108  void UpdateDoubleTapDetectionSupport();
109
110  GestureProviderClient* const client_;
111
112  class GestureListenerImpl;
113  friend class GestureListenerImpl;
114  scoped_ptr<GestureListenerImpl> gesture_listener_;
115
116  class ScaleGestureListenerImpl;
117  friend class ScaleGestureListenerImpl;
118  scoped_ptr<ScaleGestureListenerImpl> scale_gesture_listener_;
119
120  scoped_ptr<MotionEvent> current_down_event_;
121
122  // Whether the respective {SCROLL,PINCH}_BEGIN gestures have been terminated
123  // with a {SCROLL,PINCH}_END.
124  bool touch_scroll_in_progress_;
125  bool pinch_in_progress_;
126
127  // Whether double-tap gesture detection is currently supported.
128  bool double_tap_support_for_page_;
129  bool double_tap_support_for_platform_;
130
131  // Keeps track of the current GESTURE_LONG_PRESS event. If a context menu is
132  // opened after a GESTURE_LONG_PRESS, this is used to insert a
133  // GESTURE_TAP_CANCEL for removing any ::active styling.
134  base::TimeTicks current_longpress_time_;
135
136  bool gesture_begin_end_types_enabled_;
137};
138
139}  //  namespace ui
140
141#endif  // UI_EVENTS_GESTURE_DETECTION_GESTURE_PROVIDER_H_
142