Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / events / gestures / gesture_provider_aura.cc
blob30200f93298763a0377919c2ce499bad8e0e4ee5
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 #include "ui/events/gestures/gesture_provider_aura.h"
7 #include "base/auto_reset.h"
8 #include "base/logging.h"
9 #include "ui/events/event.h"
10 #include "ui/events/gesture_detection/gesture_config_helper.h"
11 #include "ui/events/gesture_detection/gesture_event_data.h"
12 #include "ui/events/gestures/gesture_configuration.h"
14 namespace ui {
16 GestureProviderAura::GestureProviderAura(GestureProviderAuraClient* client)
17 : client_(client),
18 filtered_gesture_provider_(ui::DefaultGestureProviderConfig(), this),
19 handling_event_(false) {
20 filtered_gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false);
23 GestureProviderAura::~GestureProviderAura() {}
25 bool GestureProviderAura::OnTouchEvent(const TouchEvent& event) {
26 int index = pointer_state_.FindPointerIndexOfId(event.touch_id());
27 bool pointer_id_is_active = index != -1;
29 if (event.type() == ET_TOUCH_PRESSED && pointer_id_is_active) {
30 // Ignore touch press events if we already believe the pointer is down.
31 return false;
32 } else if (event.type() != ET_TOUCH_PRESSED && !pointer_id_is_active) {
33 // We could have an active touch stream transfered to us, resulting in touch
34 // move or touch up events without associated touch down events. Ignore
35 // them.
36 return false;
39 // If this is a touchmove event, and it isn't different from the last
40 // event, ignore it.
41 if (event.type() == ET_TOUCH_MOVED &&
42 event.x() == pointer_state_.GetX(index) &&
43 event.y() == pointer_state_.GetY(index)) {
44 return false;
47 last_touch_event_flags_ = event.flags();
48 last_touch_event_latency_info_ = *event.latency();
49 pointer_state_.OnTouch(event);
51 bool result = filtered_gesture_provider_.OnTouchEvent(pointer_state_);
52 pointer_state_.CleanupRemovedTouchPoints(event);
53 return result;
56 void GestureProviderAura::OnTouchEventAck(bool event_consumed) {
57 DCHECK(pending_gestures_.empty());
58 DCHECK(!handling_event_);
59 base::AutoReset<bool> handling_event(&handling_event_, true);
60 filtered_gesture_provider_.OnTouchEventAck(event_consumed);
61 last_touch_event_latency_info_.Clear();
64 void GestureProviderAura::OnGestureEvent(
65 const GestureEventData& gesture) {
66 GestureEventDetails details = gesture.details;
67 details.set_oldest_touch_id(gesture.motion_event_id);
69 if (gesture.type() == ET_GESTURE_TAP) {
70 int tap_count = 1;
71 if (previous_tap_ && IsConsideredDoubleTap(*previous_tap_, gesture))
72 tap_count = 1 + (previous_tap_->details.tap_count() % 3);
73 details.set_tap_count(tap_count);
74 if (!previous_tap_)
75 previous_tap_.reset(new GestureEventData(gesture));
76 else
77 *previous_tap_ = gesture;
78 previous_tap_->details = details;
79 } else if (gesture.type() == ET_GESTURE_TAP_CANCEL) {
80 previous_tap_.reset();
83 scoped_ptr<ui::GestureEvent> event(
84 new ui::GestureEvent(gesture.x,
85 gesture.y,
86 last_touch_event_flags_,
87 gesture.time - base::TimeTicks(),
88 details));
90 ui::LatencyInfo* gesture_latency = event->latency();
92 gesture_latency->CopyLatencyFrom(
93 last_touch_event_latency_info_,
94 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT);
95 gesture_latency->CopyLatencyFrom(
96 last_touch_event_latency_info_,
97 ui::INPUT_EVENT_LATENCY_UI_COMPONENT);
98 gesture_latency->CopyLatencyFrom(
99 last_touch_event_latency_info_,
100 ui::INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT);
102 if (!handling_event_) {
103 // Dispatching event caused by timer.
104 client_->OnGestureEvent(event.get());
105 } else {
106 // Memory managed by ScopedVector pending_gestures_.
107 pending_gestures_.push_back(event.release());
111 ScopedVector<GestureEvent>* GestureProviderAura::GetAndResetPendingGestures() {
112 if (pending_gestures_.empty())
113 return NULL;
114 // Caller is responsible for deleting old_pending_gestures.
115 ScopedVector<GestureEvent>* old_pending_gestures =
116 new ScopedVector<GestureEvent>();
117 old_pending_gestures->swap(pending_gestures_);
118 return old_pending_gestures;
121 bool GestureProviderAura::IsConsideredDoubleTap(
122 const GestureEventData& previous_tap,
123 const GestureEventData& current_tap) const {
124 if (current_tap.time - previous_tap.time >
125 base::TimeDelta::FromMilliseconds(
126 ui::GestureConfiguration::max_seconds_between_double_click() *
127 1000)) {
128 return false;
131 double double_tap_slop_square =
132 GestureConfiguration::max_distance_between_taps_for_double_tap();
133 double_tap_slop_square *= double_tap_slop_square;
134 const float delta_x = previous_tap.x - current_tap.x;
135 const float delta_y = previous_tap.y - current_tap.y;
136 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square);
139 } // namespace content