Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / events / gestures / gesture_provider_aura.cc
blobf053f9b14ace92285009eb03c7d8d5226ff03f6a
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_configuration.h"
11 #include "ui/events/gesture_detection/gesture_event_data.h"
12 #include "ui/events/gesture_detection/gesture_provider_config_helper.h"
14 namespace ui {
16 GestureProviderAura::GestureProviderAura(GestureProviderAuraClient* client)
17 : client_(client),
18 filtered_gesture_provider_(
19 GetGestureProviderConfig(GestureProviderConfigType::CURRENT_PLATFORM),
20 this),
21 handling_event_(false),
22 last_unique_touch_event_id_(
23 std::numeric_limits<unsigned long long>::max()) {
24 filtered_gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false);
27 GestureProviderAura::~GestureProviderAura() {}
29 bool GestureProviderAura::OnTouchEvent(TouchEvent* event) {
30 if (!pointer_state_.OnTouch(*event))
31 return false;
33 last_unique_touch_event_id_ = event->unique_event_id();
34 last_touch_event_latency_info_ = *event->latency();
36 auto result = filtered_gesture_provider_.OnTouchEvent(pointer_state_);
37 if (!result.succeeded)
38 return false;
40 event->set_may_cause_scrolling(result.did_generate_scroll);
41 pointer_state_.CleanupRemovedTouchPoints(*event);
42 return true;
45 void GestureProviderAura::OnAsyncTouchEventAck(bool event_consumed) {
46 DCHECK(pending_gestures_.empty());
47 DCHECK(!handling_event_);
48 base::AutoReset<bool> handling_event(&handling_event_, true);
49 filtered_gesture_provider_.OnAsyncTouchEventAck(event_consumed);
50 last_touch_event_latency_info_.Clear();
53 void GestureProviderAura::OnSyncTouchEventAck(const uint64 unique_event_id,
54 bool event_consumed) {
55 DCHECK_EQ(last_unique_touch_event_id_, unique_event_id);
56 DCHECK(pending_gestures_.empty());
57 DCHECK(!handling_event_);
58 base::AutoReset<bool> handling_event(&handling_event_, true);
59 filtered_gesture_provider_.OnSyncTouchEventAck(event_consumed);
60 last_touch_event_latency_info_.Clear();
63 void GestureProviderAura::OnGestureEvent(
64 const GestureEventData& gesture) {
65 GestureEventDetails details = gesture.details;
66 details.set_oldest_touch_id(gesture.motion_event_id);
68 if (gesture.type() == ET_GESTURE_TAP) {
69 int tap_count = 1;
70 if (previous_tap_ && IsConsideredDoubleTap(*previous_tap_, gesture))
71 tap_count = 1 + (previous_tap_->details.tap_count() % 3);
72 details.set_tap_count(tap_count);
73 if (!previous_tap_)
74 previous_tap_.reset(new GestureEventData(gesture));
75 else
76 *previous_tap_ = gesture;
77 previous_tap_->details = details;
78 } else if (gesture.type() == ET_GESTURE_TAP_CANCEL) {
79 previous_tap_.reset();
82 scoped_ptr<ui::GestureEvent> event(
83 new ui::GestureEvent(gesture.x,
84 gesture.y,
85 gesture.flags,
86 gesture.time - base::TimeTicks(),
87 details));
89 ui::LatencyInfo* gesture_latency = event->latency();
91 gesture_latency->CopyLatencyFrom(
92 last_touch_event_latency_info_,
93 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT);
94 gesture_latency->CopyLatencyFrom(
95 last_touch_event_latency_info_,
96 ui::INPUT_EVENT_LATENCY_UI_COMPONENT);
97 gesture_latency->CopyLatencyFrom(
98 last_touch_event_latency_info_,
99 ui::INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT);
101 if (!handling_event_) {
102 // Dispatching event caused by timer.
103 client_->OnGestureEvent(event.get());
104 } else {
105 // Memory managed by ScopedVector pending_gestures_.
106 pending_gestures_.push_back(event.release());
110 ScopedVector<GestureEvent>* GestureProviderAura::GetAndResetPendingGestures() {
111 if (pending_gestures_.empty())
112 return NULL;
113 // Caller is responsible for deleting old_pending_gestures.
114 ScopedVector<GestureEvent>* old_pending_gestures =
115 new ScopedVector<GestureEvent>();
116 old_pending_gestures->swap(pending_gestures_);
117 return old_pending_gestures;
120 bool GestureProviderAura::IsConsideredDoubleTap(
121 const GestureEventData& previous_tap,
122 const GestureEventData& current_tap) const {
123 if (current_tap.time - previous_tap.time >
124 base::TimeDelta::FromMilliseconds(
125 GestureConfiguration::GetInstance()
126 ->max_time_between_double_click_in_ms())) {
127 return false;
130 float double_tap_slop_square =
131 GestureConfiguration::GetInstance()
132 ->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