Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: ui/events/gesture_detection/gesture_detector.cc

Issue 220063002: [Android] Use DIP coordinates with MotionEventAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More fixes Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/gesture_detection/gesture_detector.h" 5 #include "ui/events/gesture_detection/gesture_detector.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/timer/timer.h" 9 #include "base/timer/timer.h"
10 #include "ui/events/gesture_detection/motion_event.h" 10 #include "ui/events/gesture_detection/motion_event.h"
11 11
12 namespace ui { 12 namespace ui {
13 namespace { 13 namespace {
14 14
15 // Using a small epsilon when comparing slop distances allows pixel perfect
16 // slop determination when using fractional DPI coordinates (assuming the slop
tdresser 2014/04/09 14:00:01 fractional DPI -> fractional DIP
jdduke (slow) 2014/04/10 22:04:53 Done.
17 // region and DPI scale are reasonably proportioned).
18 const float SLOP_EPSILON = .05f;
19
15 // Constants used by TimeoutGestureHandler. 20 // Constants used by TimeoutGestureHandler.
16 enum TimeoutEvent { 21 enum TimeoutEvent {
17 SHOW_PRESS = 0, 22 SHOW_PRESS = 0,
18 LONG_PRESS, 23 LONG_PRESS,
19 TAP, 24 TAP,
20 TIMEOUT_EVENT_COUNT 25 TIMEOUT_EVENT_COUNT
21 }; 26 };
22 27
23 } // namespace 28 } // namespace
24 29
25 // Note: These constants were taken directly from the default (unscaled) 30 // Note: These constants were taken directly from the default (unscaled)
26 // versions found in Android's ViewConfiguration. 31 // versions found in Android's ViewConfiguration.
27 GestureDetector::Config::Config() 32 GestureDetector::Config::Config()
28 : longpress_timeout(base::TimeDelta::FromMilliseconds(500)), 33 : longpress_timeout(base::TimeDelta::FromMilliseconds(500)),
29 showpress_timeout(base::TimeDelta::FromMilliseconds(180)), 34 showpress_timeout(base::TimeDelta::FromMilliseconds(180)),
30 double_tap_timeout(base::TimeDelta::FromMilliseconds(300)), 35 double_tap_timeout(base::TimeDelta::FromMilliseconds(300)),
31 scaled_touch_slop(8), 36 touch_slop(8),
32 scaled_double_tap_slop(100), 37 double_tap_slop(100),
33 scaled_minimum_fling_velocity(50), 38 minimum_fling_velocity(50),
34 scaled_maximum_fling_velocity(8000) {} 39 maximum_fling_velocity(8000) {}
35 40
36 GestureDetector::Config::~Config() {} 41 GestureDetector::Config::~Config() {}
37 42
38 bool GestureDetector::SimpleGestureListener::OnDown(const MotionEvent& e) { 43 bool GestureDetector::SimpleGestureListener::OnDown(const MotionEvent& e) {
39 return false; 44 return false;
40 } 45 }
41 46
42 void GestureDetector::SimpleGestureListener::OnShowPress(const MotionEvent& e) { 47 void GestureDetector::SimpleGestureListener::OnShowPress(const MotionEvent& e) {
43 } 48 }
44 49
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 break; 265 break;
261 266
262 { 267 {
263 const float scroll_x = last_focus_x_ - focus_x; 268 const float scroll_x = last_focus_x_ - focus_x;
264 const float scroll_y = last_focus_y_ - focus_y; 269 const float scroll_y = last_focus_y_ - focus_y;
265 if (is_double_tapping_) { 270 if (is_double_tapping_) {
266 // Give the move events of the double-tap. 271 // Give the move events of the double-tap.
267 DCHECK(double_tap_listener_); 272 DCHECK(double_tap_listener_);
268 handled |= double_tap_listener_->OnDoubleTapEvent(ev); 273 handled |= double_tap_listener_->OnDoubleTapEvent(ev);
269 } else if (always_in_tap_region_) { 274 } else if (always_in_tap_region_) {
270 const int delta_x = static_cast<int>(focus_x - down_focus_x_); 275 const float delta_x = focus_x - down_focus_x_;
271 const int delta_y = static_cast<int>(focus_y - down_focus_y_); 276 const float delta_y = focus_y - down_focus_y_;
272 int distance = (delta_x * delta_x) + (delta_y * delta_y); 277 const float distance = delta_x * delta_x + delta_y * delta_y;
tdresser 2014/04/09 14:00:01 Maybe rename |distance| to |distance_squared| or |
jdduke (slow) 2014/04/10 22:04:53 Done.
273 if (distance > touch_slop_square_) { 278 if (distance > touch_slop_square_) {
274 handled = listener_->OnScroll( 279 handled = listener_->OnScroll(
275 *current_down_event_, ev, scroll_x, scroll_y); 280 *current_down_event_, ev, scroll_x, scroll_y);
276 last_focus_x_ = focus_x; 281 last_focus_x_ = focus_x;
277 last_focus_y_ = focus_y; 282 last_focus_y_ = focus_y;
278 always_in_tap_region_ = false; 283 always_in_tap_region_ = false;
279 timeout_handler_->Stop(); 284 timeout_handler_->Stop();
280 } 285 }
281 if (distance > double_tap_touch_slop_square_) 286 if (distance > double_tap_touch_slop_square_)
282 always_in_bigger_tap_region_ = false; 287 always_in_bigger_tap_region_ = false;
283 } else if ((std::abs(scroll_x) >= 1) || (std::abs(scroll_y) >= 1)) { 288 } else if (scroll_x || scroll_y) {
tdresser 2014/04/09 14:00:01 Shouldn't there be some kind of epsilon check here
jdduke (slow) 2014/04/10 22:04:53 Ahh, of course.
284 handled = 289 handled =
285 listener_->OnScroll(*current_down_event_, ev, scroll_x, scroll_y); 290 listener_->OnScroll(*current_down_event_, ev, scroll_x, scroll_y);
286 last_focus_x_ = focus_x; 291 last_focus_x_ = focus_x;
287 last_focus_y_ = focus_y; 292 last_focus_y_ = focus_y;
288 } 293 }
289 } 294 }
290 break; 295 break;
291 296
292 case MotionEvent::ACTION_UP: 297 case MotionEvent::ACTION_UP:
293 still_down_ = false; 298 still_down_ = false;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 Cancel(); 338 Cancel();
334 break; 339 break;
335 } 340 }
336 341
337 return handled; 342 return handled;
338 } 343 }
339 344
340 void GestureDetector::Init(const Config& config) { 345 void GestureDetector::Init(const Config& config) {
341 DCHECK(listener_); 346 DCHECK(listener_);
342 347
343 const int touch_slop = config.scaled_touch_slop; 348 const float touch_slop = config.touch_slop + SLOP_EPSILON;
344 const int double_tap_touch_slop = touch_slop; 349 const float double_tap_touch_slop = touch_slop;
345 const int double_tap_slop = config.scaled_double_tap_slop; 350 const float double_tap_slop = config.double_tap_slop + SLOP_EPSILON;
346 min_fling_velocity_ = config.scaled_minimum_fling_velocity;
347 max_fling_velocity_ = config.scaled_maximum_fling_velocity;
348 touch_slop_square_ = touch_slop * touch_slop; 351 touch_slop_square_ = touch_slop * touch_slop;
349 double_tap_touch_slop_square_ = double_tap_touch_slop * double_tap_touch_slop; 352 double_tap_touch_slop_square_ = double_tap_touch_slop * double_tap_touch_slop;
350 double_tap_slop_square_ = double_tap_slop * double_tap_slop; 353 double_tap_slop_square_ = double_tap_slop * double_tap_slop;
351 double_tap_timeout_ = config.double_tap_timeout; 354 double_tap_timeout_ = config.double_tap_timeout;
355 min_fling_velocity_ = config.minimum_fling_velocity;
356 max_fling_velocity_ = config.maximum_fling_velocity;
352 } 357 }
353 358
354 void GestureDetector::OnShowPressTimeout() { 359 void GestureDetector::OnShowPressTimeout() {
355 listener_->OnShowPress(*current_down_event_); 360 listener_->OnShowPress(*current_down_event_);
356 } 361 }
357 362
358 void GestureDetector::OnLongPressTimeout() { 363 void GestureDetector::OnLongPressTimeout() {
359 timeout_handler_->StopTimeout(TAP); 364 timeout_handler_->StopTimeout(TAP);
360 defer_confirm_single_tap_ = false; 365 defer_confirm_single_tap_ = false;
361 in_longpress_ = listener_->OnLongPress(*current_down_event_); 366 in_longpress_ = listener_->OnLongPress(*current_down_event_);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 const MotionEvent& first_down, 399 const MotionEvent& first_down,
395 const MotionEvent& first_up, 400 const MotionEvent& first_up,
396 const MotionEvent& second_down) const { 401 const MotionEvent& second_down) const {
397 if (!always_in_bigger_tap_region_) 402 if (!always_in_bigger_tap_region_)
398 return false; 403 return false;
399 404
400 if (second_down.GetEventTime() - first_up.GetEventTime() > 405 if (second_down.GetEventTime() - first_up.GetEventTime() >
401 double_tap_timeout_) 406 double_tap_timeout_)
402 return false; 407 return false;
403 408
404 int delta_x = static_cast<int>(first_down.GetX() - second_down.GetX()); 409 const float delta_x = first_down.GetX() - second_down.GetX();
405 int delta_y = static_cast<int>(first_down.GetY() - second_down.GetY()); 410 const float delta_y = first_down.GetY() - second_down.GetY();
406 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square_); 411 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square_);
407 } 412 }
408 413
409 } // namespace ui 414 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698