OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/gestures/gesture_recognizer_impl.h" | 5 #include "ui/events/gestures/gesture_recognizer_impl.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 GestureConsumer* old_consumer, | 51 GestureConsumer* old_consumer, |
52 GestureConsumer* new_consumer, | 52 GestureConsumer* new_consumer, |
53 GestureRecognizerImpl::TouchIdToConsumerMap* map) { | 53 GestureRecognizerImpl::TouchIdToConsumerMap* map) { |
54 for (GestureRecognizerImpl::TouchIdToConsumerMap::iterator i = map->begin(); | 54 for (GestureRecognizerImpl::TouchIdToConsumerMap::iterator i = map->begin(); |
55 i != map->end(); ++i) { | 55 i != map->end(); ++i) { |
56 if (i->second == old_consumer) | 56 if (i->second == old_consumer) |
57 i->second = new_consumer; | 57 i->second = new_consumer; |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 GestureProviderAura* CreateGestureProvider(GestureProviderAuraClient* client) { | 61 GestureProviderImpl* CreateGestureProvider(GestureProviderImplClient* client) { |
62 return new GestureProviderAura(client); | 62 return new GestureProviderImpl(client); |
63 } | 63 } |
64 | 64 |
65 } // namespace | 65 } // namespace |
66 | 66 |
67 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
68 // GestureRecognizerImpl, public: | 68 // GestureRecognizerImpl, public: |
69 | 69 |
70 GestureRecognizerImpl::GestureRecognizerImpl() { | 70 GestureRecognizerImpl::GestureRecognizerImpl() { |
71 } | 71 } |
72 | 72 |
(...skipping 18 matching lines...) Expand all Loading... |
91 | 91 |
92 GestureConsumer* GestureRecognizerImpl::GetTargetForLocation( | 92 GestureConsumer* GestureRecognizerImpl::GetTargetForLocation( |
93 const gfx::PointF& location, int source_device_id) { | 93 const gfx::PointF& location, int source_device_id) { |
94 const float max_distance = | 94 const float max_distance = |
95 GestureConfiguration::max_separation_for_gesture_touches_in_pixels(); | 95 GestureConfiguration::max_separation_for_gesture_touches_in_pixels(); |
96 | 96 |
97 gfx::PointF closest_point; | 97 gfx::PointF closest_point; |
98 int closest_touch_id = 0; | 98 int closest_touch_id = 0; |
99 double closest_distance_squared = std::numeric_limits<double>::infinity(); | 99 double closest_distance_squared = std::numeric_limits<double>::infinity(); |
100 | 100 |
101 std::map<GestureConsumer*, GestureProviderAura*>::iterator i; | 101 std::map<GestureConsumer*, GestureProviderImpl*>::iterator i; |
102 for (i = consumer_gesture_provider_.begin(); | 102 for (i = consumer_gesture_provider_.begin(); |
103 i != consumer_gesture_provider_.end(); | 103 i != consumer_gesture_provider_.end(); |
104 ++i) { | 104 ++i) { |
105 const MotionEventAura& pointer_state = i->second->pointer_state(); | 105 const MotionEventImpl& pointer_state = i->second->pointer_state(); |
106 for (size_t j = 0; j < pointer_state.GetPointerCount(); ++j) { | 106 for (size_t j = 0; j < pointer_state.GetPointerCount(); ++j) { |
107 if (source_device_id != pointer_state.GetSourceDeviceId(j)) | 107 if (source_device_id != pointer_state.GetSourceDeviceId(j)) |
108 continue; | 108 continue; |
109 gfx::PointF point(pointer_state.GetX(j), pointer_state.GetY(j)); | 109 gfx::PointF point(pointer_state.GetX(j), pointer_state.GetY(j)); |
110 // Relative distance is all we need here, so LengthSquared() is | 110 // Relative distance is all we need here, so LengthSquared() is |
111 // appropriate, and cheaper than Length(). | 111 // appropriate, and cheaper than Length(). |
112 double distance_squared = (point - location).LengthSquared(); | 112 double distance_squared = (point - location).LengthSquared(); |
113 if (distance_squared < closest_distance_squared) { | 113 if (distance_squared < closest_distance_squared) { |
114 closest_point = point; | 114 closest_point = point; |
115 closest_touch_id = pointer_state.GetPointerId(j); | 115 closest_touch_id = pointer_state.GetPointerId(j); |
(...skipping 11 matching lines...) Expand all Loading... |
127 GestureConsumer* new_consumer) { | 127 GestureConsumer* new_consumer) { |
128 // Send cancel to all those save |new_consumer| and |current_consumer|. | 128 // Send cancel to all those save |new_consumer| and |current_consumer|. |
129 // Don't send a cancel to |current_consumer|, unless |new_consumer| is NULL. | 129 // Don't send a cancel to |current_consumer|, unless |new_consumer| is NULL. |
130 // Dispatching a touch-cancel event can end up altering |touch_id_target_| | 130 // Dispatching a touch-cancel event can end up altering |touch_id_target_| |
131 // (e.g. when the target of the event is destroyed, causing it to be removed | 131 // (e.g. when the target of the event is destroyed, causing it to be removed |
132 // from |touch_id_target_| in |CleanupStateForConsumer()|). So create a list | 132 // from |touch_id_target_| in |CleanupStateForConsumer()|). So create a list |
133 // of the touch-ids that need to be cancelled, and dispatch the cancel events | 133 // of the touch-ids that need to be cancelled, and dispatch the cancel events |
134 // for them at the end. | 134 // for them at the end. |
135 | 135 |
136 std::vector<GestureConsumer*> consumers; | 136 std::vector<GestureConsumer*> consumers; |
137 std::map<GestureConsumer*, GestureProviderAura*>::iterator i; | 137 std::map<GestureConsumer*, GestureProviderImpl*>::iterator i; |
138 for (i = consumer_gesture_provider_.begin(); | 138 for (i = consumer_gesture_provider_.begin(); |
139 i != consumer_gesture_provider_.end(); | 139 i != consumer_gesture_provider_.end(); |
140 ++i) { | 140 ++i) { |
141 if (i->first && i->first != new_consumer && | 141 if (i->first && i->first != new_consumer && |
142 (i->first != current_consumer || new_consumer == NULL)) { | 142 (i->first != current_consumer || new_consumer == NULL)) { |
143 consumers.push_back(i->first); | 143 consumers.push_back(i->first); |
144 } | 144 } |
145 } | 145 } |
146 for (std::vector<GestureConsumer*>::iterator iter = consumers.begin(); | 146 for (std::vector<GestureConsumer*>::iterator iter = consumers.begin(); |
147 iter != consumers.end(); | 147 iter != consumers.end(); |
(...skipping 19 matching lines...) Expand all Loading... |
167 const MotionEvent& pointer_state = | 167 const MotionEvent& pointer_state = |
168 consumer_gesture_provider_[consumer]->pointer_state(); | 168 consumer_gesture_provider_[consumer]->pointer_state(); |
169 *point = gfx::PointF(pointer_state.GetX(), pointer_state.GetY()); | 169 *point = gfx::PointF(pointer_state.GetX(), pointer_state.GetY()); |
170 return true; | 170 return true; |
171 } | 171 } |
172 | 172 |
173 bool GestureRecognizerImpl::CancelActiveTouches(GestureConsumer* consumer) { | 173 bool GestureRecognizerImpl::CancelActiveTouches(GestureConsumer* consumer) { |
174 bool cancelled_touch = false; | 174 bool cancelled_touch = false; |
175 if (consumer_gesture_provider_.count(consumer) == 0) | 175 if (consumer_gesture_provider_.count(consumer) == 0) |
176 return false; | 176 return false; |
177 const MotionEventAura& pointer_state = | 177 const MotionEventImpl& pointer_state = |
178 consumer_gesture_provider_[consumer]->pointer_state(); | 178 consumer_gesture_provider_[consumer]->pointer_state(); |
179 if (pointer_state.GetPointerCount() == 0) | 179 if (pointer_state.GetPointerCount() == 0) |
180 return false; | 180 return false; |
181 // Pointer_state is modified every time after DispatchCancelTouchEvent. | 181 // Pointer_state is modified every time after DispatchCancelTouchEvent. |
182 scoped_ptr<MotionEvent> pointer_state_clone = pointer_state.Clone(); | 182 scoped_ptr<MotionEvent> pointer_state_clone = pointer_state.Clone(); |
183 for (size_t i = 0; i < pointer_state_clone->GetPointerCount(); ++i) { | 183 for (size_t i = 0; i < pointer_state_clone->GetPointerCount(); ++i) { |
184 gfx::PointF point(pointer_state_clone->GetX(i), | 184 gfx::PointF point(pointer_state_clone->GetX(i), |
185 pointer_state_clone->GetY(i)); | 185 pointer_state_clone->GetY(i)); |
186 TouchEvent touch_event(ui::ET_TOUCH_CANCELLED, | 186 TouchEvent touch_event(ui::ET_TOUCH_CANCELLED, |
187 point, | 187 point, |
188 ui::EF_IS_SYNTHESIZED, | 188 ui::EF_IS_SYNTHESIZED, |
189 pointer_state_clone->GetPointerId(i), | 189 pointer_state_clone->GetPointerId(i), |
190 ui::EventTimeForNow(), | 190 ui::EventTimeForNow(), |
191 0.0f, | 191 0.0f, |
192 0.0f, | 192 0.0f, |
193 0.0f, | 193 0.0f, |
194 0.0f); | 194 0.0f); |
195 GestureEventHelper* helper = FindDispatchHelperForConsumer(consumer); | 195 GestureEventHelper* helper = FindDispatchHelperForConsumer(consumer); |
196 if (helper) | 196 if (helper) |
197 helper->DispatchCancelTouchEvent(&touch_event); | 197 helper->DispatchCancelTouchEvent(&touch_event); |
198 cancelled_touch = true; | 198 cancelled_touch = true; |
199 } | 199 } |
200 return cancelled_touch; | 200 return cancelled_touch; |
201 } | 201 } |
202 | 202 |
203 //////////////////////////////////////////////////////////////////////////////// | 203 //////////////////////////////////////////////////////////////////////////////// |
204 // GestureRecognizerImpl, private: | 204 // GestureRecognizerImpl, private: |
205 | 205 |
206 GestureProviderAura* GestureRecognizerImpl::GetGestureProviderForConsumer( | 206 GestureProviderImpl* GestureRecognizerImpl::GetGestureProviderForConsumer( |
207 GestureConsumer* consumer) { | 207 GestureConsumer* consumer) { |
208 GestureProviderAura* gesture_provider = consumer_gesture_provider_[consumer]; | 208 GestureProviderImpl* gesture_provider = consumer_gesture_provider_[consumer]; |
209 if (!gesture_provider) { | 209 if (!gesture_provider) { |
210 gesture_provider = CreateGestureProvider(this); | 210 gesture_provider = CreateGestureProvider(this); |
211 consumer_gesture_provider_[consumer] = gesture_provider; | 211 consumer_gesture_provider_[consumer] = gesture_provider; |
212 } | 212 } |
213 return gesture_provider; | 213 return gesture_provider; |
214 } | 214 } |
215 | 215 |
216 void GestureRecognizerImpl::SetupTargets(const TouchEvent& event, | 216 void GestureRecognizerImpl::SetupTargets(const TouchEvent& event, |
217 GestureConsumer* target) { | 217 GestureConsumer* target) { |
218 if (event.type() == ui::ET_TOUCH_RELEASED || | 218 if (event.type() == ui::ET_TOUCH_RELEASED || |
(...skipping 16 matching lines...) Expand all Loading... |
235 } | 235 } |
236 | 236 |
237 bool GestureRecognizerImpl::ProcessTouchEventPreDispatch( | 237 bool GestureRecognizerImpl::ProcessTouchEventPreDispatch( |
238 const TouchEvent& event, | 238 const TouchEvent& event, |
239 GestureConsumer* consumer) { | 239 GestureConsumer* consumer) { |
240 SetupTargets(event, consumer); | 240 SetupTargets(event, consumer); |
241 | 241 |
242 if (event.result() & ER_CONSUMED) | 242 if (event.result() & ER_CONSUMED) |
243 return false; | 243 return false; |
244 | 244 |
245 GestureProviderAura* gesture_provider = | 245 GestureProviderImpl* gesture_provider = |
246 GetGestureProviderForConsumer(consumer); | 246 GetGestureProviderForConsumer(consumer); |
247 return gesture_provider->OnTouchEvent(event); | 247 return gesture_provider->OnTouchEvent(event); |
248 } | 248 } |
249 | 249 |
250 GestureRecognizer::Gestures* | 250 GestureRecognizer::Gestures* |
251 GestureRecognizerImpl::ProcessTouchEventPostDispatch( | 251 GestureRecognizerImpl::ProcessTouchEventPostDispatch( |
252 const TouchEvent& event, | 252 const TouchEvent& event, |
253 ui::EventResult result, | 253 ui::EventResult result, |
254 GestureConsumer* consumer) { | 254 GestureConsumer* consumer) { |
255 GestureProviderAura* gesture_provider = | 255 GestureProviderImpl* gesture_provider = |
256 GetGestureProviderForConsumer(consumer); | 256 GetGestureProviderForConsumer(consumer); |
257 gesture_provider->OnTouchEventAck(result != ER_UNHANDLED); | 257 gesture_provider->OnTouchEventAck(result != ER_UNHANDLED); |
258 return gesture_provider->GetAndResetPendingGestures(); | 258 return gesture_provider->GetAndResetPendingGestures(); |
259 } | 259 } |
260 | 260 |
261 GestureRecognizer::Gestures* GestureRecognizerImpl::ProcessTouchEventOnAsyncAck( | 261 GestureRecognizer::Gestures* GestureRecognizerImpl::ProcessTouchEventOnAsyncAck( |
262 const TouchEvent& event, | 262 const TouchEvent& event, |
263 ui::EventResult result, | 263 ui::EventResult result, |
264 GestureConsumer* consumer) { | 264 GestureConsumer* consumer) { |
265 if (result & ui::ER_CONSUMED) | 265 if (result & ui::ER_CONSUMED) |
266 return NULL; | 266 return NULL; |
267 GestureProviderAura* gesture_provider = | 267 GestureProviderImpl* gesture_provider = |
268 GetGestureProviderForConsumer(consumer); | 268 GetGestureProviderForConsumer(consumer); |
269 gesture_provider->OnTouchEventAck(result != ER_UNHANDLED); | 269 gesture_provider->OnTouchEventAck(result != ER_UNHANDLED); |
270 return gesture_provider->GetAndResetPendingGestures(); | 270 return gesture_provider->GetAndResetPendingGestures(); |
271 } | 271 } |
272 | 272 |
273 bool GestureRecognizerImpl::CleanupStateForConsumer( | 273 bool GestureRecognizerImpl::CleanupStateForConsumer( |
274 GestureConsumer* consumer) { | 274 GestureConsumer* consumer) { |
275 bool state_cleaned_up = false; | 275 bool state_cleaned_up = false; |
276 | 276 |
277 if (consumer_gesture_provider_.count(consumer)) { | 277 if (consumer_gesture_provider_.count(consumer)) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 std::vector<GestureEventHelper*>::iterator it; | 339 std::vector<GestureEventHelper*>::iterator it; |
340 for (it = helpers.begin(); it != helpers.end(); ++it) | 340 for (it = helpers.begin(); it != helpers.end(); ++it) |
341 gesture_recognizer->AddGestureEventHelper(*it); | 341 gesture_recognizer->AddGestureEventHelper(*it); |
342 | 342 |
343 helpers.clear(); | 343 helpers.clear(); |
344 g_gesture_recognizer_instance = | 344 g_gesture_recognizer_instance = |
345 static_cast<GestureRecognizerImpl*>(gesture_recognizer); | 345 static_cast<GestureRecognizerImpl*>(gesture_recognizer); |
346 } | 346 } |
347 | 347 |
348 } // namespace ui | 348 } // namespace ui |
OLD | NEW |