1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef UI_EVENTS_GESTURE_DETECTION_GESTURE_PROVIDER_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define UI_EVENTS_GESTURE_DETECTION_GESTURE_PROVIDER_H_
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/basictypes.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/events/gesture_detection/gesture_detection_export.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/events/gesture_detection/gesture_detector.h"
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "ui/events/gesture_detection/gesture_event_data.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/events/gesture_detection/gesture_touch_uma_histogram.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/events/gesture_detection/scale_gesture_detector.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/events/gesture_detection/snap_scroll_controller.h"
16a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ui/gfx/display.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace ui {
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class GESTURE_DETECTION_EXPORT GestureProviderClient {
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~GestureProviderClient() {}
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnGestureEvent(const GestureEventData& gesture) = 0;
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Given a stream of |MotionEvent|'s, provides gesture detection and gesture
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// event dispatch.
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class GESTURE_DETECTION_EXPORT GestureProvider {
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct GESTURE_DETECTION_EXPORT Config {
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Config();
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ~Config();
33a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    gfx::Display display;
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    GestureDetector::Config gesture_detector_config;
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ScaleGestureDetector::Config scale_gesture_detector_config;
36c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
37c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // If |disable_click_delay| is true and double-tap support is disabled,
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // there will be no delay before tap events. When double-tap support is
39c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // enabled, there will always be a delay before a tap event is fired, in
40c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // order to allow the double tap gesture to occur without firing any tap
41c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // events.
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool disable_click_delay;
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
44c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // If |gesture_begin_end_types_enabled| is true, fire an ET_GESTURE_BEGIN
45c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // event for every added touch point, and an ET_GESTURE_END event for every
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // removed touch point. This requires one ACTION_CANCEL event to be sent per
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // touch point, which only occurs on Aura. Defaults to false.
48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool gesture_begin_end_types_enabled;
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // The min and max size (both length and width, in dips) of the generated
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // bounding box for all gesture types. This is useful for touch streams
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // that may report zero or unreasonably small or large touch sizes.
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // Note that these bounds are only applied for touch or unknown tool types;
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // mouse and stylus-derived gestures will not be affected.
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Both values default to 0 (disabled).
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    float min_gesture_bounds_length;
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    float max_gesture_bounds_length;
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GestureProvider(const Config& config, GestureProviderClient* client);
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ~GestureProvider();
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Handle the incoming MotionEvent, returning false if the event could not
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be handled.
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool OnTouchEvent(const MotionEvent& event);
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Update whether multi-touch pinch zoom is supported by the platform.
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void SetMultiTouchZoomSupportEnabled(bool enabled);
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Update whether double-tap gestures are supported by the platform.
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void SetDoubleTapSupportForPlatformEnabled(bool enabled);
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Update whether double-tap gesture detection should be suppressed, e.g.,
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // if the page scale is fixed or the page has a mobile viewport. This disables
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // the tap delay, allowing rapid and responsive single-tap gestures.
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void SetDoubleTapSupportForPageEnabled(bool enabled);
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Whether a scroll gesture is in-progress.
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool IsScrollInProgress() const;
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Whether a pinch gesture is in-progress (i.e. a pinch update has been
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // forwarded and detection is still active).
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool IsPinchInProgress() const;
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
85c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Whether a double-tap gesture is in-progress (either double-tap or
86c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // double-tap drag zoom).
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool IsDoubleTapInProgress() const;
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // May be NULL if there is no currently active touch sequence.
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const ui::MotionEvent* current_down_event() const {
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return current_down_event_.get();
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool CanHandle(const MotionEvent& event) const;
96c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void OnTouchEventHandlingBegin(const MotionEvent& event);
97c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void OnTouchEventHandlingEnd(const MotionEvent& event);
98c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void UpdateDoubleTapDetectionSupport();
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class GestureListenerImpl;
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<GestureListenerImpl> gesture_listener_;
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<MotionEvent> current_down_event_;
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Logs information on touch and gesture events.
1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  GestureTouchUMAHistogram uma_histogram_;
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Whether double-tap gesture detection is currently supported.
109c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool double_tap_support_for_page_;
110c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool double_tap_support_for_platform_;
111c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const bool gesture_begin_end_types_enabled_;
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  //  namespace ui
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // UI_EVENTS_GESTURE_DETECTION_GESTURE_PROVIDER_H_
118