| OLD | NEW |
| 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
| 7 | 7 |
| 8 #include "ui/events/gesture_detection/gesture_detector.h" | 8 #include "ui/events/gesture_detection/gesture_detector.h" |
| 9 | 9 |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 enum TimeoutEvent { | 31 enum TimeoutEvent { |
| 32 SHOW_PRESS = 0, | 32 SHOW_PRESS = 0, |
| 33 LONG_PRESS, | 33 LONG_PRESS, |
| 34 TAP, | 34 TAP, |
| 35 TIMEOUT_EVENT_COUNT | 35 TIMEOUT_EVENT_COUNT |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 // Note: These constants were taken directly from the default (unscaled) | 40 // Note: These constants were taken directly from the default (unscaled) |
| 41 // versions found in Android's ViewConfiguration. | 41 // versions found in Android's ViewConfiguration. Do not change these default |
| 42 // values without explicitly consulting an OWNER. |
| 42 GestureDetector::Config::Config() | 43 GestureDetector::Config::Config() |
| 43 : longpress_timeout(base::TimeDelta::FromMilliseconds(500)), | 44 : longpress_timeout(base::TimeDelta::FromMilliseconds(500)), |
| 44 showpress_timeout(base::TimeDelta::FromMilliseconds(180)), | 45 showpress_timeout(base::TimeDelta::FromMilliseconds(180)), |
| 45 double_tap_timeout(base::TimeDelta::FromMilliseconds(300)), | 46 double_tap_timeout(base::TimeDelta::FromMilliseconds(300)), |
| 46 double_tap_min_time(base::TimeDelta::FromMilliseconds(40)), | 47 double_tap_min_time(base::TimeDelta::FromMilliseconds(40)), |
| 47 touch_slop(8), | 48 touch_slop(8), |
| 48 double_tap_slop(100), | 49 double_tap_slop(100), |
| 49 minimum_fling_velocity(50), | 50 minimum_fling_velocity(50), |
| 50 maximum_fling_velocity(8000), | 51 maximum_fling_velocity(8000), |
| 51 swipe_enabled(false), | 52 swipe_enabled(false), |
| 52 minimum_swipe_velocity(20), | 53 minimum_swipe_velocity(20), |
| 53 maximum_swipe_deviation_angle(20.f), | 54 maximum_swipe_deviation_angle(20.f), |
| 54 two_finger_tap_enabled(false), | 55 two_finger_tap_enabled(false), |
| 55 two_finger_tap_max_separation(300), | 56 two_finger_tap_max_separation(300), |
| 56 two_finger_tap_timeout(base::TimeDelta::FromMilliseconds(700)) { | 57 two_finger_tap_timeout(base::TimeDelta::FromMilliseconds(700)), |
| 58 velocity_tracker_strategy(VelocityTracker::Strategy::STRATEGY_DEFAULT) { |
| 57 } | 59 } |
| 58 | 60 |
| 59 GestureDetector::Config::~Config() {} | 61 GestureDetector::Config::~Config() {} |
| 60 | 62 |
| 61 class GestureDetector::TimeoutGestureHandler { | 63 class GestureDetector::TimeoutGestureHandler { |
| 62 public: | 64 public: |
| 63 TimeoutGestureHandler(const Config& config, GestureDetector* gesture_detector) | 65 TimeoutGestureHandler(const Config& config, GestureDetector* gesture_detector) |
| 64 : gesture_detector_(gesture_detector) { | 66 : gesture_detector_(gesture_detector) { |
| 65 DCHECK(config.showpress_timeout <= config.longpress_timeout); | 67 DCHECK(config.showpress_timeout <= config.longpress_timeout); |
| 66 | 68 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 always_in_bigger_tap_region_(false), | 129 always_in_bigger_tap_region_(false), |
| 128 two_finger_tap_allowed_for_gesture_(false), | 130 two_finger_tap_allowed_for_gesture_(false), |
| 129 is_double_tapping_(false), | 131 is_double_tapping_(false), |
| 130 last_focus_x_(0), | 132 last_focus_x_(0), |
| 131 last_focus_y_(0), | 133 last_focus_y_(0), |
| 132 down_focus_x_(0), | 134 down_focus_x_(0), |
| 133 down_focus_y_(0), | 135 down_focus_y_(0), |
| 134 longpress_enabled_(true), | 136 longpress_enabled_(true), |
| 135 showpress_enabled_(true), | 137 showpress_enabled_(true), |
| 136 swipe_enabled_(false), | 138 swipe_enabled_(false), |
| 137 two_finger_tap_enabled_(false) { | 139 two_finger_tap_enabled_(false), |
| 140 velocity_tracker_(config.velocity_tracker_strategy) { |
| 138 DCHECK(listener_); | 141 DCHECK(listener_); |
| 139 Init(config); | 142 Init(config); |
| 140 } | 143 } |
| 141 | 144 |
| 142 GestureDetector::~GestureDetector() {} | 145 GestureDetector::~GestureDetector() {} |
| 143 | 146 |
| 144 bool GestureDetector::OnTouchEvent(const MotionEvent& ev) { | 147 bool GestureDetector::OnTouchEvent(const MotionEvent& ev) { |
| 145 const MotionEvent::Action action = ev.GetAction(); | 148 const MotionEvent::Action action = ev.GetAction(); |
| 146 | 149 |
| 147 velocity_tracker_.AddMovement(ev); | 150 velocity_tracker_.AddMovement(ev); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 *current_down_event_, ev, scroll_x, scroll_y); | 288 *current_down_event_, ev, scroll_x, scroll_y); |
| 286 last_focus_x_ = focus_x; | 289 last_focus_x_ = focus_x; |
| 287 last_focus_y_ = focus_y; | 290 last_focus_y_ = focus_y; |
| 288 always_in_tap_region_ = false; | 291 always_in_tap_region_ = false; |
| 289 timeout_handler_->Stop(); | 292 timeout_handler_->Stop(); |
| 290 } | 293 } |
| 291 if (distance_square > double_tap_touch_slop_square_) | 294 if (distance_square > double_tap_touch_slop_square_) |
| 292 always_in_bigger_tap_region_ = false; | 295 always_in_bigger_tap_region_ = false; |
| 293 } else if (std::abs(scroll_x) > kScrollEpsilon || | 296 } else if (std::abs(scroll_x) > kScrollEpsilon || |
| 294 std::abs(scroll_y) > kScrollEpsilon) { | 297 std::abs(scroll_y) > kScrollEpsilon) { |
| 298 // We should eventually apply touch slop for multi-finger |
| 299 // scrolls as well as single finger scrolls. See |
| 300 // crbug.com/492185 for details. |
| 295 handled = | 301 handled = |
| 296 listener_->OnScroll(*current_down_event_, ev, scroll_x, scroll_y); | 302 listener_->OnScroll(*current_down_event_, ev, scroll_x, scroll_y); |
| 297 last_focus_x_ = focus_x; | 303 last_focus_x_ = focus_x; |
| 298 last_focus_y_ = focus_y; | 304 last_focus_y_ = focus_y; |
| 299 } | 305 } |
| 300 | 306 |
| 301 if (!two_finger_tap_allowed_for_gesture_) | 307 if (!two_finger_tap_allowed_for_gesture_) |
| 302 break; | 308 break; |
| 303 | 309 |
| 304 // Two-finger tap should be prevented if either pointer exceeds its | 310 // Two-finger tap should be prevented if either pointer exceeds its |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 432 |
| 427 void GestureDetector::OnLongPressTimeout() { | 433 void GestureDetector::OnLongPressTimeout() { |
| 428 timeout_handler_->StopTimeout(TAP); | 434 timeout_handler_->StopTimeout(TAP); |
| 429 defer_confirm_single_tap_ = false; | 435 defer_confirm_single_tap_ = false; |
| 430 listener_->OnLongPress(*current_down_event_); | 436 listener_->OnLongPress(*current_down_event_); |
| 431 } | 437 } |
| 432 | 438 |
| 433 void GestureDetector::OnTapTimeout() { | 439 void GestureDetector::OnTapTimeout() { |
| 434 if (!double_tap_listener_) | 440 if (!double_tap_listener_) |
| 435 return; | 441 return; |
| 436 if (!still_down_) | 442 if (!still_down_) { |
| 437 double_tap_listener_->OnSingleTapConfirmed(*current_down_event_); | 443 CHECK(previous_up_event_); |
| 438 else | 444 double_tap_listener_->OnSingleTapConfirmed(*previous_up_event_); |
| 445 } else { |
| 439 defer_confirm_single_tap_ = true; | 446 defer_confirm_single_tap_ = true; |
| 447 } |
| 440 } | 448 } |
| 441 | 449 |
| 442 void GestureDetector::Cancel() { | 450 void GestureDetector::Cancel() { |
| 443 CancelTaps(); | 451 CancelTaps(); |
| 444 velocity_tracker_.Clear(); | 452 velocity_tracker_.Clear(); |
| 445 still_down_ = false; | 453 still_down_ = false; |
| 446 } | 454 } |
| 447 | 455 |
| 448 void GestureDetector::CancelTaps() { | 456 void GestureDetector::CancelTaps() { |
| 449 timeout_handler_->Stop(); | 457 timeout_handler_->Stop(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 return false; | 499 return false; |
| 492 | 500 |
| 493 if (vx_abs > vy_abs) | 501 if (vx_abs > vy_abs) |
| 494 vy = 0; | 502 vy = 0; |
| 495 else | 503 else |
| 496 vx = 0; | 504 vx = 0; |
| 497 return listener_->OnSwipe(*current_down_event_, up, vx, vy); | 505 return listener_->OnSwipe(*current_down_event_, up, vx, vy); |
| 498 } | 506 } |
| 499 | 507 |
| 500 } // namespace ui | 508 } // namespace ui |
| OLD | NEW |