Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / events / gestures / gesture_provider_aura.cc
blobb491a2f0b85023e85765cd299c921f553e247101
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 filtered_gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false);
25 GestureProviderAura::~GestureProviderAura() {}
27 bool GestureProviderAura::OnTouchEvent(TouchEvent* event) {
28 if (!pointer_state_.OnTouch(*event))
29 return false;
31 auto result = filtered_gesture_provider_.OnTouchEvent(pointer_state_);
32 if (!result.succeeded)
33 return false;
35 event->set_may_cause_scrolling(result.did_generate_scroll);
36 pointer_state_.CleanupRemovedTouchPoints(*event);
37 return true;
40 void GestureProviderAura::OnTouchEventAck(uint32 unique_event_id,
41 bool event_consumed) {
42 DCHECK(pending_gestures_.empty());
43 DCHECK(!handling_event_);
44 base::AutoReset<bool> handling_event(&handling_event_, true);
45 filtered_gesture_provider_.OnTouchEventAck(unique_event_id, event_consumed);
48 void GestureProviderAura::OnGestureEvent(
49 const GestureEventData& gesture) {
50 GestureEventDetails details = gesture.details;
51 details.set_oldest_touch_id(gesture.motion_event_id);
53 if (gesture.type() == ET_GESTURE_TAP) {
54 int tap_count = 1;
55 if (previous_tap_ && IsConsideredDoubleTap(*previous_tap_, gesture))
56 tap_count = 1 + (previous_tap_->details.tap_count() % 3);
57 details.set_tap_count(tap_count);
58 if (!previous_tap_)
59 previous_tap_.reset(new GestureEventData(gesture));
60 else
61 *previous_tap_ = gesture;
62 previous_tap_->details = details;
63 } else if (gesture.type() == ET_GESTURE_TAP_CANCEL) {
64 previous_tap_.reset();
67 scoped_ptr<ui::GestureEvent> event(
68 new ui::GestureEvent(gesture.x,
69 gesture.y,
70 gesture.flags,
71 gesture.time - base::TimeTicks(),
72 details));
74 if (!handling_event_) {
75 // Dispatching event caused by timer.
76 client_->OnGestureEvent(event.get());
77 } else {
78 // Memory managed by ScopedVector pending_gestures_.
79 pending_gestures_.push_back(event.Pass());
83 ScopedVector<GestureEvent>* GestureProviderAura::GetAndResetPendingGestures() {
84 if (pending_gestures_.empty())
85 return NULL;
86 // Caller is responsible for deleting old_pending_gestures.
87 ScopedVector<GestureEvent>* old_pending_gestures =
88 new ScopedVector<GestureEvent>();
89 old_pending_gestures->swap(pending_gestures_);
90 return old_pending_gestures;
93 bool GestureProviderAura::IsConsideredDoubleTap(
94 const GestureEventData& previous_tap,
95 const GestureEventData& current_tap) const {
96 if (current_tap.time - previous_tap.time >
97 base::TimeDelta::FromMilliseconds(
98 GestureConfiguration::GetInstance()
99 ->max_time_between_double_click_in_ms())) {
100 return false;
103 float double_tap_slop_square =
104 GestureConfiguration::GetInstance()
105 ->max_distance_between_taps_for_double_tap();
106 double_tap_slop_square *= double_tap_slop_square;
107 const float delta_x = previous_tap.x - current_tap.x;
108 const float delta_y = previous_tap.y - current_tap.y;
109 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square);
112 } // namespace content