gesture_provider.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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    bool disable_click_delay;
36  };
37
38  GestureProvider(const Config& config, GestureProviderClient* client);
39  ~GestureProvider();
40
41  // Handle the incoming MotionEvent, returning false if the event could not
42  // be handled.
43  bool OnTouchEvent(const MotionEvent& event);
44
45  // Resets all gesture detectors; called on DidStartLoading().
46  void ResetGestureDetectors();
47
48  // Update whether multi-touch gestures are supported.
49  void SetMultiTouchSupportEnabled(bool enabled);
50
51  // Update whether double-tap gestures are supported. This allows
52  // double-tap gesture suppression independent of whether or not the page's
53  // viewport and scale would normally prevent double-tap.
54  // Note: This should not be called while a double-tap gesture is in progress.
55  void SetDoubleTapSupportForPlatformEnabled(bool enabled);
56
57  // Update whether double-tap gesture detection should be suppressed due to
58  // the viewport or scale of the current page. Suppressing double-tap gesture
59  // detection allows for rapid and responsive single-tap gestures.
60  void SetDoubleTapSupportForPageEnabled(bool enabled);
61
62  // Whether a scroll gesture is in-progress.
63  bool IsScrollInProgress() const;
64
65  // Whether a pinch gesture is in-progress (i.e. a pinch update has been
66  // forwarded and detection is still active).
67  bool IsPinchInProgress() const;
68
69  // Whether a double tap-gesture is in-progress.
70  bool IsDoubleTapInProgress() const;
71
72  // Whether the tap gesture delay is explicitly disabled (independent of
73  // whether double-tap is supported), see |Config.disable_click_delay|.
74  bool IsClickDelayDisabled() const;
75
76  // May be NULL if there is no currently active touch sequence.
77  const ui::MotionEvent* current_down_event() const {
78    return current_down_event_.get();
79  }
80
81 private:
82  void InitGestureDetectors(const Config& config);
83
84  bool CanHandle(const MotionEvent& event) const;
85
86  void Fling(base::TimeTicks time,
87             float x,
88             float y,
89             float velocity_x,
90             float velocity_y);
91  void Send(const GestureEventData& gesture);
92  void SendTapCancelIfNecessary(const MotionEvent& event);
93  bool SendLongTapIfNecessary(const MotionEvent& event);
94  void EndTouchScrollIfNecessary(base::TimeTicks time,
95                                 bool send_scroll_end_event);
96
97  GestureProviderClient* const client_;
98
99  class GestureListenerImpl;
100  friend class GestureListenerImpl;
101  scoped_ptr<GestureListenerImpl> gesture_listener_;
102
103  class ScaleGestureListenerImpl;
104  friend class ScaleGestureListenerImpl;
105  scoped_ptr<ScaleGestureListenerImpl> scale_gesture_listener_;
106
107  scoped_ptr<MotionEvent> current_down_event_;
108
109  // Whether a GESTURE_SHOW_PRESS was sent for the current touch sequence.
110  // Sending a GESTURE_TAP event will forward a GESTURE_SHOW_PRESS if one has
111  // not yet been sent.
112  bool needs_show_press_event_;
113
114  // Whether a sent GESTURE_TAP_DOWN event has yet to be accompanied by a
115  // corresponding GESTURE_TAP, GESTURE_TAP_CANCEL or GESTURE_DOUBLE_TAP.
116  bool needs_tap_ending_event_;
117
118  // Whether the respective {SCROLL,PINCH}_BEGIN gestures have been terminated
119  // with a {SCROLL,PINCH}_END.
120  bool touch_scroll_in_progress_;
121  bool pinch_in_progress_;
122
123  // Keeps track of the current GESTURE_LONG_PRESS event. If a context menu is
124  // opened after a GESTURE_LONG_PRESS, this is used to insert a
125  // GESTURE_TAP_CANCEL for removing any ::active styling.
126  base::TimeTicks current_longpress_time_;
127};
128
129}  //  namespace ui
130
131#endif  // UI_EVENTS_GESTURE_DETECTION_GESTURE_PROVIDER_H_
132