MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / ui / events / gestures / motion_event_aura.cc
blob54139e5590f7073297e808d6d0d9e7eec0a61cc5
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/motion_event_aura.h"
7 #include "base/logging.h"
8 #include "ui/events/gesture_detection/gesture_configuration.h"
10 namespace ui {
11 namespace {
13 PointerProperties GetPointerPropertiesFromTouchEvent(const TouchEvent& touch) {
14 PointerProperties pointer_properties;
15 pointer_properties.x = touch.x();
16 pointer_properties.y = touch.y();
17 pointer_properties.raw_x = touch.root_location_f().x();
18 pointer_properties.raw_y = touch.root_location_f().y();
19 pointer_properties.id = touch.touch_id();
20 pointer_properties.pressure = touch.pointer_details().force();
21 pointer_properties.source_device_id = touch.source_device_id();
23 pointer_properties.SetAxesAndOrientation(touch.pointer_details().radius_x(),
24 touch.pointer_details().radius_y(),
25 touch.rotation_angle());
26 if (!pointer_properties.touch_major) {
27 pointer_properties.touch_major =
28 2.f * GestureConfiguration::GetInstance()->default_radius();
29 pointer_properties.touch_minor =
30 2.f * GestureConfiguration::GetInstance()->default_radius();
31 pointer_properties.orientation = 0;
34 // TODO(jdduke): Plumb tool type from the platform, crbug.com/404128.
35 pointer_properties.tool_type = MotionEvent::TOOL_TYPE_UNKNOWN;
37 return pointer_properties;
40 } // namespace
42 MotionEventAura::MotionEventAura() {}
44 MotionEventAura::~MotionEventAura() {}
46 bool MotionEventAura::OnTouch(const TouchEvent& touch) {
47 int index = FindPointerIndexOfId(touch.touch_id());
48 bool pointer_id_is_active = index != -1;
50 if (touch.type() == ET_TOUCH_PRESSED && pointer_id_is_active) {
51 // TODO(tdresser): This should return false (or NOTREACHED()), and
52 // ignore the touch; however, there is at least one case where we
53 // need to allow a touch press from a currently used touch id. See
54 // crbug.com/446852 for details.
56 // Cancel the existing touch, before handling the touch press.
57 TouchEvent cancel(ET_TOUCH_CANCELLED, touch.location(), touch.touch_id(),
58 touch.time_stamp());
59 OnTouch(cancel);
60 CleanupRemovedTouchPoints(cancel);
61 DCHECK_EQ(-1, FindPointerIndexOfId(touch.touch_id()));
62 } else if (touch.type() != ET_TOUCH_PRESSED && !pointer_id_is_active) {
63 // We could have an active touch stream transfered to us, resulting in touch
64 // move or touch up events without associated touch down events. Ignore
65 // them.
66 return false;
69 if (touch.type() == ET_TOUCH_MOVED && touch.x() == GetX(index) &&
70 touch.y() == GetY(index)) {
71 return false;
74 switch (touch.type()) {
75 case ET_TOUCH_PRESSED:
76 if (!AddTouch(touch))
77 return false;
78 case ET_TOUCH_RELEASED:
79 case ET_TOUCH_CANCELLED:
80 // Removing these touch points needs to be postponed until after the
81 // MotionEvent has been dispatched. This cleanup occurs in
82 // CleanupRemovedTouchPoints.
83 UpdateTouch(touch);
84 break;
85 case ET_TOUCH_MOVED:
86 UpdateTouch(touch);
87 break;
88 default:
89 NOTREACHED();
90 return false;
93 UpdateCachedAction(touch);
94 set_unique_event_id(touch.unique_event_id());
95 set_flags(touch.flags());
96 set_event_time(touch.time_stamp() + base::TimeTicks());
97 return true;
100 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent& event) {
101 if (event.type() != ET_TOUCH_RELEASED &&
102 event.type() != ET_TOUCH_CANCELLED) {
103 return;
106 DCHECK(GetPointerCount());
107 int index_to_delete = GetIndexFromId(event.touch_id());
108 set_action_index(-1);
109 set_action(MotionEvent::ACTION_NONE);
110 pointer(index_to_delete) = pointer(GetPointerCount() - 1);
111 PopPointer();
114 int MotionEventAura::GetSourceDeviceId(size_t pointer_index) const {
115 DCHECK_LT(pointer_index, GetPointerCount());
116 return pointer(pointer_index).source_device_id;
119 bool MotionEventAura::AddTouch(const TouchEvent& touch) {
120 if (GetPointerCount() == MotionEvent::MAX_TOUCH_POINT_COUNT)
121 return false;
123 PushPointer(GetPointerPropertiesFromTouchEvent(touch));
124 return true;
127 void MotionEventAura::UpdateTouch(const TouchEvent& touch) {
128 pointer(GetIndexFromId(touch.touch_id())) =
129 GetPointerPropertiesFromTouchEvent(touch);
132 void MotionEventAura::UpdateCachedAction(const TouchEvent& touch) {
133 DCHECK(GetPointerCount());
134 switch (touch.type()) {
135 case ET_TOUCH_PRESSED:
136 if (GetPointerCount() == 1) {
137 set_action(ACTION_DOWN);
138 } else {
139 set_action(ACTION_POINTER_DOWN);
140 set_action_index(GetIndexFromId(touch.touch_id()));
142 break;
143 case ET_TOUCH_RELEASED:
144 if (GetPointerCount() == 1) {
145 set_action(ACTION_UP);
146 } else {
147 set_action(ACTION_POINTER_UP);
148 set_action_index(GetIndexFromId(touch.touch_id()));
150 break;
151 case ET_TOUCH_CANCELLED:
152 set_action(ACTION_CANCEL);
153 break;
154 case ET_TOUCH_MOVED:
155 set_action(ACTION_MOVE);
156 break;
157 default:
158 NOTREACHED();
159 break;
163 int MotionEventAura::GetIndexFromId(int id) const {
164 int index = FindPointerIndexOfId(id);
165 // TODO(tdresser): remove these checks once crbug.com/525189 is fixed.
166 CHECK_GE(index, 0);
167 CHECK_LT(index, static_cast<int>(GetPointerCount()));
168 return index;
171 } // namespace ui