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

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

Issue 2175803002: The helper functions for slop region check and subtraction are modified. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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 // 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 <stddef.h> 10 #include <stddef.h>
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 const float scroll_x = last_focus_x_ - focus_x; 299 const float scroll_x = last_focus_x_ - focus_x;
300 const float scroll_y = last_focus_y_ - focus_y; 300 const float scroll_y = last_focus_y_ - focus_y;
301 if (is_double_tapping_) { 301 if (is_double_tapping_) {
302 // Give the move events of the double-tap. 302 // Give the move events of the double-tap.
303 DCHECK(double_tap_listener_); 303 DCHECK(double_tap_listener_);
304 handled |= double_tap_listener_->OnDoubleTapEvent(ev); 304 handled |= double_tap_listener_->OnDoubleTapEvent(ev);
305 } else if (all_pointers_within_slop_regions_) { 305 } else if (all_pointers_within_slop_regions_) {
306 if (!IsWithinTouchSlop(ev)) { 306 if (!IsWithinTouchSlop(ev)) {
307 handled = listener_->OnScroll( 307 handled = listener_->OnScroll(
308 *current_down_event_, ev, 308 *current_down_event_, ev,
309 (ev.GetPointerCount() > 1 ? *secondary_pointer_down_event_ 309 (maximum_pointer_count_ > 1 ? *secondary_pointer_down_event_
310 : ev), 310 : ev),
311 scroll_x, scroll_y); 311 scroll_x, scroll_y);
312 last_focus_x_ = focus_x; 312 last_focus_x_ = focus_x;
313 last_focus_y_ = focus_y; 313 last_focus_y_ = focus_y;
314 all_pointers_within_slop_regions_ = false; 314 all_pointers_within_slop_regions_ = false;
315 timeout_handler_->Stop(); 315 timeout_handler_->Stop();
316 } 316 }
317 317
318 const float delta_x = focus_x - down_focus_x_; 318 const float delta_x = focus_x - down_focus_x_;
319 const float delta_y = focus_y - down_focus_y_; 319 const float delta_y = focus_y - down_focus_y_;
320 const float distance_square = delta_x * delta_x + delta_y * delta_y; 320 const float distance_square = delta_x * delta_x + delta_y * delta_y;
321 if (distance_square > double_tap_touch_slop_square_) 321 if (distance_square > double_tap_touch_slop_square_)
322 always_in_bigger_tap_region_ = false; 322 always_in_bigger_tap_region_ = false;
323 } else if (std::abs(scroll_x) > kScrollEpsilon || 323 } else if (std::abs(scroll_x) > kScrollEpsilon ||
324 std::abs(scroll_y) > kScrollEpsilon) { 324 std::abs(scroll_y) > kScrollEpsilon) {
325 handled = listener_->OnScroll( 325 handled = listener_->OnScroll(
326 *current_down_event_, ev, 326 *current_down_event_, ev,
327 (ev.GetPointerCount() > 1 ? *secondary_pointer_down_event_ : ev), 327 (maximum_pointer_count_ > 1 ? *secondary_pointer_down_event_
328 : ev),
328 scroll_x, scroll_y); 329 scroll_x, scroll_y);
329 last_focus_x_ = focus_x; 330 last_focus_x_ = focus_x;
330 last_focus_y_ = focus_y; 331 last_focus_y_ = focus_y;
331 } 332 }
332 333
333 if (!two_finger_tap_allowed_for_gesture_) 334 if (!two_finger_tap_allowed_for_gesture_)
334 break; 335 break;
335 336
336 // Two-finger tap should be prevented if either pointer exceeds its 337 // Two-finger tap should be prevented if either pointer exceeds its
337 // (independent) slop region. 338 // (independent) slop region.
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 return false; 532 return false;
532 533
533 if (vx_abs > vy_abs) 534 if (vx_abs > vy_abs)
534 vy = 0; 535 vy = 0;
535 else 536 else
536 vx = 0; 537 vx = 0;
537 return listener_->OnSwipe(*current_down_event_, up, vx, vy); 538 return listener_->OnSwipe(*current_down_event_, up, vx, vy);
538 } 539 }
539 540
540 bool GestureDetector::IsWithinTouchSlop(const MotionEvent& ev) { 541 bool GestureDetector::IsWithinTouchSlop(const MotionEvent& ev) {
541 // If there are more than two down pointers, tapping is not possible. 542 // If there have been more than two down pointers in the touch sequence,
542 // Slop region check is not needed. 543 // tapping is not possible. Slop region check is not needed.
543 if (ev.GetPointerCount() > 2) 544 if (maximum_pointer_count_ > 2)
544 return false; 545 return false;
545 546
546 const int id0 = current_down_event_->GetPointerId(0); 547 for (size_t i = 0; i < ev.GetPointerCount(); i++) {
547 const int ev_idx0 = ev.GetPointerId(0) == id0 ? 0 : 1; 548 const int pid = ev.GetPointerId(i);
tdresser 2016/07/22 15:57:45 Throughout, let's use pointer_id instead of pid, f
sahel 2016/07/22 19:46:42 Done.
548 549 const MotionEvent* source_pointer_down_event = GetSourcePointerDownEvent(
549 // Check if the primary pointer exceeded the slop region. 550 *current_down_event_,
550 float dx = current_down_event_->GetX() - ev.GetX(ev_idx0); 551 maximum_pointer_count_ > 1 ? *secondary_pointer_down_event_
551 float dy = current_down_event_->GetY() - ev.GetY(ev_idx0); 552 : *current_down_event_,
552 if (dx * dx + dy * dy > touch_slop_square_) 553 pid);
553 return false; 554 DCHECK(source_pointer_down_event);
tdresser 2016/07/22 15:57:45 Return if source_pointer_down_event is nullptr.
sahel 2016/07/22 19:46:42 Done.
554 555 int source_index = getPointerIndex(*source_pointer_down_event, pid);
555 if (ev.GetPointerCount() == 2) { 556 DCHECK_GE(source_index, 0);
556 // Check if the secondary pointer exceeded the slop region. 557 float dx = source_pointer_down_event->GetX(source_index) - ev.GetX(i);
tdresser 2016/07/22 15:57:45 What happens if source_index is 0? Should we retur
sahel 2016/07/22 19:46:42 I changed the code, an early return will happen if
557 const int ev_idx1 = ev_idx0 == 0 ? 1 : 0; 558 float dy = source_pointer_down_event->GetY(source_index) - ev.GetY(i);
558 const int idx1 = secondary_pointer_down_event_->GetActionIndex();
559 dx = secondary_pointer_down_event_->GetX(idx1) - ev.GetX(ev_idx1);
560 dy = secondary_pointer_down_event_->GetY(idx1) - ev.GetY(ev_idx1);
561 if (dx * dx + dy * dy > touch_slop_square_) 559 if (dx * dx + dy * dy > touch_slop_square_)
562 return false; 560 return false;
563 } 561 }
564 562
565 return true; 563 return true;
566 } 564 }
567 565
566 const MotionEvent* GestureDetector::GetSourcePointerDownEvent(
567 const MotionEvent& current_down_event,
568 const MotionEvent& secondary_pointer_down_event,
569 const int pid) {
570 if (current_down_event.GetPointerId(0) == pid)
571 return &current_down_event;
572
573 for (size_t i = 0; i < secondary_pointer_down_event.GetPointerCount(); i++) {
574 if (secondary_pointer_down_event.GetPointerId(i) == pid)
575 return &secondary_pointer_down_event;
576 }
577
578 return NULL;
tdresser 2016/07/22 15:57:45 Should this ever happen? If not, maybe we should h
sahel 2016/07/22 19:46:42 Done.
579 }
580
581 int GestureDetector::getPointerIndex(const MotionEvent& event, const int pid) {
582 for (size_t i = 0; i < event.GetPointerCount(); i++) {
583 if (event.GetPointerId(i) == pid)
584 return i;
585 }
586 return -1;
tdresser 2016/07/22 15:57:45 Should this case ever happen? Should we have a DCH
sahel 2016/07/22 19:46:42 Done.
587 }
588
568 } // namespace ui 589 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698