1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/events/gesture_detection/gesture_provider.h"
#include <stddef.h>
#include <cmath>
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/event_constants.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
#include "ui/events/gesture_detection/gesture_event_data.h"
#include "ui/events/gesture_detection/gesture_listeners.h"
#include "ui/events/gesture_detection/scale_gesture_listeners.h"
#include "ui/events/types/event_type.h"
#include "ui/events/velocity_tracker/motion_event.h"
#include "ui/events/velocity_tracker/motion_event_generic.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/vector2d_f.h"
namespace ui {
namespace {
// Double-tap drag zoom sensitivity (speed).
const float kDoubleTapDragZoomSpeed = 0.005f;
const char* GetMotionEventActionName(MotionEvent::Action action) {
switch (action) {
case MotionEvent::Action::NONE:
return "Action::NONE";
case MotionEvent::Action::POINTER_DOWN:
return "Action::POINTER_DOWN";
case MotionEvent::Action::POINTER_UP:
return "Action::POINTER_UP";
case MotionEvent::Action::DOWN:
return "Action::DOWN";
case MotionEvent::Action::UP:
return "Action::UP";
case MotionEvent::Action::CANCEL:
return "Action::CANCEL";
case MotionEvent::Action::MOVE:
return "Action::MOVE";
case MotionEvent::Action::HOVER_ENTER:
return "Action::HOVER_ENTER";
case MotionEvent::Action::HOVER_EXIT:
return "Action::HOVER_EXIT";
case MotionEvent::Action::HOVER_MOVE:
return "Action::HOVER_MOVE";
case MotionEvent::Action::BUTTON_PRESS:
return "Action::BUTTON_PRESS";
case MotionEvent::Action::BUTTON_RELEASE:
return "Action::BUTTON_RELEASE";
}
return "";
}
gfx::RectF ClampBoundingBox(const gfx::RectF& bounds,
float min_length,
float max_length) {
float width = bounds.width();
float height = bounds.height();
if (min_length) {
width = std::max(min_length, width);
height = std::max(min_length, height);
}
if (max_length) {
width = std::min(max_length, width);
height = std::min(max_length, height);
}
const gfx::PointF center = bounds.CenterPoint();
return gfx::RectF(
center.x() - width / 2.f, center.y() - height / 2.f, width, height);
}
float EffectiveSlopDistance(const MotionEvent& event,
const GestureProvider::Config& config) {
return (base::FeatureList::IsEnabled(features::kStylusSpecificTapSlop) &&
event.GetToolType() == MotionEvent::ToolType::STYLUS)
? config.gesture_detector_config.stylus_slop
: config.gesture_detector_config.touch_slop;
}
} // namespace
// GestureProviderClient:
bool GestureProviderClient::RequiresDoubleTapGestureEvents() const {
return false;
}
// GestureProvider:::Config
GestureProvider::Config::Config()
: display(display::kInvalidDisplayId, gfx::Rect(1, 1)),
double_tap_support_for_platform_enabled(true),
gesture_begin_end_types_enabled(false),
min_gesture_bounds_length(0),
max_gesture_bounds_length(0) {}
GestureProvider::Config::Config(const Config& other) = default;
GestureProvider::Config::~Config() {
}
// GestureProvider::GestureListener
class GestureProvider::GestureListenerImpl : public ScaleGestureListener,
public GestureListener,
public DoubleTapListener {
public:
GestureListenerImpl(const GestureProvider::Config& config,
GestureProviderClient* client,
GestureProvider* gesture_provider)
: config_(config),
client_(client),
gesture_provider_(gesture_provider),
gesture_detector_(config.gesture_detector_config, this, this),
scale_gesture_detector_(config.scale_gesture_detector_config, this),
snap_scroll_controller_(gfx::SizeF(config.display.size())),
ignore_multitouch_zoom_events_(false),
ignore_single_tap_(false),
pinch_event_sent_(false),
scroll_event_sent_(false),
max_diameter_before_show_press_(0),
show_press_event_sent_(false) {}
GestureListenerImpl(const GestureListenerImpl&) = delete;
GestureListenerImpl& operator=(const GestureListenerImpl&) = delete;
void OnTouchEvent(const MotionEvent& event) {
const bool in_scale_gesture = IsScaleGestureDetectionInProgress();
snap_scroll_controller_.SetSnapScrollMode(
event, in_scale_gesture, EffectiveSlopDistance(event, config_));
if (in_scale_gesture) {
SetIgnoreSingleTap(true);
}
const MotionEvent::Action action = event.GetAction();
if (action == MotionEvent::Action::DOWN) {
current_down_action_event_time_ = event.GetEventTime();
DCHECK(gesture_provider_->current_down_event());
current_down_action_unique_touch_event_id_ =
gesture_provider_->current_down_event()->GetUniqueEventId();
current_longpress_time_ = base::TimeTicks();
ignore_single_tap_ = false;
scroll_event_sent_ = false;
pinch_event_sent_ = false;
show_press_event_sent_ = false;
gesture_detector_.set_press_and_hold_enabled(true);
tap_down_point_ = gfx::PointF(event.GetX(), event.GetY());
max_diameter_before_show_press_ = event.GetTouchMajor();
}
gesture_detector_.OnTouchEvent(event,
client_->RequiresDoubleTapGestureEvents());
scale_gesture_detector_.OnTouchEvent(event);
if (action == MotionEvent::Action::UP ||
action == MotionEvent::Action::CANCEL) {
// Note: This call will have no effect if a fling was just generated, as
// |Fling()| will have already signalled an end to touch-scrolling.
if (scroll_event_sent_)
Send(CreateGesture(
CreateTouchGestureDetails(EventType::kGestureScrollEnd), event));
// If this was the last pointer that was canceled or lifted reset the
// |current_down_action_event_time_| to indicate no sequence is going on.
if (action != MotionEvent::Action::CANCEL ||
!GestureConfiguration::GetInstance()
->single_pointer_cancel_enabled() ||
event.GetPointerCount() == 1)
current_down_action_event_time_ = base::TimeTicks();
} else if (action == MotionEvent::Action::MOVE) {
if (!show_press_event_sent_ && !scroll_event_sent_) {
max_diameter_before_show_press_ =
std::max(max_diameter_before_show_press_, event.GetTouchMajor());
}
}
}
void UpdateStateForEventPost(GestureEventData gesture) {
switch (gesture.type()) {
case EventType::kGestureLongPress:
DCHECK(!IsScaleGestureDetectionInProgress());
current_longpress_time_ = gesture.time;
break;
case EventType::kGestureLongTap:
current_longpress_time_ = base::TimeTicks();
break;
case EventType::kGestureScrollBegin:
DCHECK(!scroll_event_sent_);
scroll_event_sent_ = true;
break;
case EventType::kGestureScrollEnd:
DCHECK(scroll_event_sent_);
scroll_event_sent_ = false;
break;
case EventType::kScrollFlingStart:
DCHECK(scroll_event_sent_);
scroll_event_sent_ = false;
break;
case EventType::kGesturePinchBegin:
DCHECK(!pinch_event_sent_);
pinch_event_sent_ = true;
break;
case EventType::kGesturePinchEnd:
DCHECK(pinch_event_sent_);
pinch_event_sent_ = false;
break;
case EventType::kGestureShowPress:
// It's possible that a double-tap drag zoom (from ScaleGestureDetector)
// will start before the press gesture fires (from GestureDetector), in
// which case the press should simply be dropped.
if (pinch_event_sent_ || scroll_event_sent_)
return;
break;
default:
break;
};
}
// `should_update` indicates whether sending `gesture` should update the
// internal states.
void SendImpl(GestureEventData gesture, bool should_update) {
DCHECK(!gesture.time.is_null());
// The only valid events that should be sent without an active touch
// sequence are SHOW_PRESS, TAP and TAP_CANCEL, potentially triggered by
// the double-tap delay timing out or being cancelled.
DCHECK(!current_down_action_event_time_.is_null() ||
gesture.type() == EventType::kGestureTap ||
gesture.type() == EventType::kGestureShowPress ||
gesture.type() == EventType::kGestureTapCancel ||
gesture.type() == EventType::kGestureBegin ||
gesture.type() == EventType::kGestureEnd);
if (gesture.primary_tool_type == MotionEvent::ToolType::UNKNOWN ||
gesture.primary_tool_type == MotionEvent::ToolType::FINGER) {
gesture.details.set_bounding_box(ClampBoundingBox(
gesture.details.bounding_box_f(), config_.min_gesture_bounds_length,
config_.max_gesture_bounds_length));
}
// Sending one gesture event may trigger propagation of another.
switch (gesture.type()) {
case EventType::kGestureScrollEnd:
DCHECK(scroll_event_sent_);
if (pinch_event_sent_)
SendImpl(GestureEventData(EventType::kGesturePinchEnd, gesture),
should_update);
break;
case EventType::kGesturePinchBegin:
DCHECK(!pinch_event_sent_);
if (!scroll_event_sent_ &&
!scale_gesture_detector_.InAnchoredScaleMode()) {
SendImpl(GestureEventData(EventType::kGestureScrollBegin, gesture),
should_update);
}
break;
default:
break;
}
if (should_update) {
UpdateStateForEventPost(gesture);
GestureTouchUMAHistogram::RecordGestureEvent(gesture);
}
client_->OnGestureEvent(gesture);
}
void Send(GestureEventData gesture) {
SendImpl(gesture, /*should_update=*/true);
}
void SendSynthesizedEndEvents() {
MotionEventGeneric generic_cancel_event(MotionEvent::Action::CANCEL,
base::TimeTicks::Now(),
PointerProperties());
// Sending the synthesized end events should not update the internal states.
// Because this function may be called when a new event handler replaces the
// old one. In that scenario, the old event handler is informed of the
// gesture end through the synthesized end events while the new handler
// should handle the incoming gestures.
if (scroll_event_sent_) {
SendImpl(
CreateGesture(CreateTouchGestureDetails(EventType::kGestureScrollEnd),
generic_cancel_event),
/*should_update=*/false);
}
SendImpl(CreateGesture(CreateTouchGestureDetails(EventType::kGestureEnd),
generic_cancel_event),
/*should_update=*/false);
}
// ScaleGestureListener implementation.
bool OnScaleBegin(const ScaleGestureDetector& detector,
const MotionEvent& e) override {
if (ignore_multitouch_zoom_events_ && !detector.InAnchoredScaleMode())
return false;
return true;
}
void OnScaleEnd(const ScaleGestureDetector& detector,
const MotionEvent& e) override {
if (!pinch_event_sent_)
return;
Send(CreateGesture(CreateTouchGestureDetails(EventType::kGesturePinchEnd),
e));
}
bool OnScale(const ScaleGestureDetector& detector,
const MotionEvent& e) override {
if (ignore_multitouch_zoom_events_ && !detector.InAnchoredScaleMode())
return false;
bool first_scale = false;
if (!pinch_event_sent_) {
first_scale = true;
GestureEventDetails details =
CreateTouchGestureDetails(EventType::kGesturePinchBegin);
Send(CreateGesture(
details, e.GetPointerId(), e.GetToolType(), detector.GetEventTime(),
detector.GetFocusX(), detector.GetFocusY(),
detector.GetFocusX() + e.GetRawOffsetX(),
detector.GetFocusY() + e.GetRawOffsetY(), e.GetPointerCount(),
GetBoundingBox(e, EventType::kGesturePinchBegin), e.GetFlags()));
}
if (std::abs(detector.GetCurrentSpan() - detector.GetPreviousSpan()) <
config_.scale_gesture_detector_config.min_pinch_update_span_delta) {
return false;
}
float scale = detector.GetScaleFactor();
float angle = detector.GetAngleChange();
if (scale == 1)
return true;
if (detector.InAnchoredScaleMode()) {
// Relative changes in the double-tap scale factor computed by |detector|
// diminish as the touch moves away from the original double-tap focus.
// For historical reasons, Chrome has instead adopted a scale factor
// computation that is invariant to the focal distance, where
// the scale delta remains constant if the touch velocity is constant.
// Note: Because we calculate the scale here manually based on the
// y-span, but the scale factor accounts for slop in the first previous
// span, we manaully reproduce the behavior here for previous span y.
float prev_y = first_scale
? config_.gesture_detector_config.touch_slop * 2
: detector.GetPreviousSpanY();
float dy = (detector.GetCurrentSpanY() - prev_y) * 0.5f;
scale = std::pow(scale > 1 ? 1.0f + kDoubleTapDragZoomSpeed
: 1.0f - kDoubleTapDragZoomSpeed,
std::abs(dy));
}
GestureEventDetails pinch_details =
CreateTouchGestureDetails(EventType::kGesturePinchUpdate);
pinch_details.set_scale(scale);
pinch_details.set_pinch_angle(angle);
Send(CreateGesture(pinch_details,
e.GetPointerId(),
e.GetToolType(),
detector.GetEventTime(),
detector.GetFocusX(),
detector.GetFocusY(),
detector.GetFocusX() + e.GetRawOffsetX(),
detector.GetFocusY() + e.GetRawOffsetY(),
e.GetPointerCount(),
GetBoundingBox(e, pinch_details.type()),
e.GetFlags()));
return true;
}
// GestureListener implementation.
bool OnDown(const MotionEvent& e, int tap_down_count) override {
DCHECK_GE(tap_down_count, 0);
GestureEventDetails tap_details =
CreateTouchGestureDetails(EventType::kGestureTapDown);
tap_details.set_tap_down_count(tap_down_count);
Send(CreateGesture(tap_details, e));
// Return true to indicate that we want to handle touch.
return true;
}
bool OnScroll(const MotionEvent& e1,
const MotionEvent& e2,
const MotionEvent& secondary_pointer_down,
float raw_distance_x,
float raw_distance_y) override {
float distance_x = raw_distance_x;
float distance_y = raw_distance_y;
if (!scroll_event_sent_ && e2.GetPointerCount() < 3) {
// Remove the touch slop region from the first scroll event to avoid a
// jump. Touch slop isn't used for scroll gestures with greater than 2
// pointers down, in those cases we don't subtract the slop.
gfx::Vector2dF delta =
ComputeFirstScrollDelta(e1, e2, secondary_pointer_down);
distance_x = delta.x();
distance_y = delta.y();
}
snap_scroll_controller_.UpdateSnapScrollMode(
distance_x, distance_y, EffectiveSlopDistance(e2, config_));
if (snap_scroll_controller_.IsSnappingScrolls()) {
if (snap_scroll_controller_.IsSnapHorizontal())
distance_y = 0;
else
distance_x = 0;
}
if (!distance_x && !distance_y)
return true;
if (!scroll_event_sent_) {
// Note that scroll start hints are in distance traveled, where
// scroll deltas are in the opposite direction.
GestureEventDetails scroll_details = CreateTouchGestureDetails(
EventType::kGestureScrollBegin, -distance_x, -distance_y);
// Scroll focus point always starts with the first touch down point.
scroll_focus_point_.SetPoint(e1.GetX(), e1.GetY());
// Use the co-ordinates from the touch down, as these co-ordinates are
// used to determine which layer the scroll should affect.
Send(CreateGesture(scroll_details, e2.GetPointerId(), e2.GetToolType(),
e2.GetEventTime(), e1.GetX(), e1.GetY(), e1.GetRawX(),
e1.GetRawY(), e2.GetPointerCount(),
GetBoundingBox(e2, scroll_details.type()),
e2.GetFlags()));
DCHECK(scroll_event_sent_);
}
scroll_focus_point_.SetPoint(scroll_focus_point_.x() - raw_distance_x,
scroll_focus_point_.y() - raw_distance_y);
GestureEventDetails scroll_details = CreateTouchGestureDetails(
EventType::kGestureScrollUpdate, -distance_x, -distance_y);
const gfx::RectF bounding_box = GetBoundingBox(e2, scroll_details.type());
const gfx::PointF raw_center =
scroll_focus_point_ +
gfx::Vector2dF(e2.GetRawOffsetX(), e2.GetRawOffsetY());
Send(CreateGesture(scroll_details, e2.GetPointerId(), e2.GetToolType(),
e2.GetEventTime(), scroll_focus_point_.x(),
scroll_focus_point_.y(), raw_center.x(), raw_center.y(),
e2.GetPointerCount(), bounding_box, e2.GetFlags()));
return true;
}
bool OnFling(const MotionEvent& e1,
const MotionEvent& e2,
float velocity_x,
float velocity_y) override {
if (snap_scroll_controller_.IsSnappingScrolls()) {
if (snap_scroll_controller_.IsSnapHorizontal()) {
velocity_y = 0;
} else {
velocity_x = 0;
}
}
if (!velocity_x && !velocity_y)
return true;
DCHECK(scroll_event_sent_);
if (!scroll_event_sent_) {
// The native side needs a EventType::kGestureScrollBegin before
// EventType::kScrollFlingStart to send the fling to the correct target.
// The distance traveled in one second is a reasonable scroll start hint.
GestureEventDetails scroll_details = CreateTouchGestureDetails(
EventType::kGestureScrollBegin, velocity_x, velocity_y);
Send(CreateGesture(scroll_details, e2));
}
GestureEventDetails fling_details = CreateTouchGestureDetails(
EventType::kScrollFlingStart, velocity_x, velocity_y);
Send(CreateGesture(fling_details, e2));
return true;
}
bool OnSwipe(const MotionEvent& e1,
const MotionEvent& e2,
float velocity_x,
float velocity_y) override {
GestureEventDetails swipe_details = CreateTouchGestureDetails(
EventType::kGestureSwipe, velocity_x, velocity_y);
Send(CreateGesture(swipe_details, e2));
return true;
}
bool OnTwoFingerTap(const MotionEvent& e1, const MotionEvent& e2) override {
// The location of the two finger tap event should be the location of the
// primary pointer.
GestureEventDetails two_finger_tap_details =
CreateTouchGestureDetails(EventType::kGestureTwoFingerTap,
e1.GetTouchMajor(), e1.GetTouchMajor());
Send(CreateGesture(two_finger_tap_details,
e2.GetPointerId(),
e2.GetToolType(),
e2.GetEventTime(),
e1.GetX(),
e1.GetY(),
e1.GetRawX(),
e1.GetRawY(),
e2.GetPointerCount(),
GetBoundingBox(e2, two_finger_tap_details.type()),
e2.GetFlags()));
return true;
}
void OnTapCancel(const MotionEvent& e) override {
GestureEventDetails tap_cancel_details =
CreateTouchGestureDetails(EventType::kGestureTapCancel);
Send(CreateGesture(tap_cancel_details, e));
}
void OnShowPress(const MotionEvent& e) override {
GestureEventDetails show_press_details =
CreateTouchGestureDetails(EventType::kGestureShowPress);
show_press_event_sent_ = true;
Send(CreateGesture(show_press_details, e));
}
bool OnSingleTapUp(const MotionEvent& e, int tap_count) override {
// This is a hack to address the issue where user hovers
// over a link for longer than double_tap_timeout_, then
// OnSingleTapConfirmed() is not triggered. But we still
// want to trigger the tap event at UP. So we override
// OnSingleTapUp() in this case. This assumes singleTapUp
// gets always called before singleTapConfirmed.
if (!ignore_single_tap_) {
if (e.GetEventTime() - current_down_action_event_time_ >
config_.gesture_detector_config.double_tap_timeout) {
return OnSingleTapImpl(e, tap_count);
} else if (!IsDoubleTapEnabled()) {
// If double-tap has been disabled, there is no need to wait
// for the double-tap timeout.
return OnSingleTapImpl(e, tap_count);
} else {
// Notify Blink about this tapUp event anyway, when none of the above
// conditions applied.
Send(CreateTapGesture(EventType::kGestureTapUnconfirmed, e, 1));
}
}
if (e.GetAction() == MotionEvent::Action::UP &&
!current_longpress_time_.is_null() &&
!IsScaleGestureDetectionInProgress()) {
GestureEventDetails long_tap_details =
CreateTouchGestureDetails(EventType::kGestureLongTap);
Send(CreateGesture(long_tap_details, e));
return true;
}
return false;
}
// DoubleTapListener implementation.
bool OnSingleTapConfirmed(const MotionEvent& e) override {
return OnSingleTapImpl(e, 1);
}
bool OnDoubleTap(const MotionEvent& e) override {
return scale_gesture_detector_.OnDoubleTap(e);
}
bool OnDoubleTapEvent(const MotionEvent& e) override {
switch (e.GetAction()) {
case MotionEvent::Action::DOWN:
gesture_detector_.set_press_and_hold_enabled(false);
break;
case MotionEvent::Action::UP:
if (!IsPinchInProgress() && !IsScrollInProgress()) {
Send(CreateTapGesture(EventType::kGestureDoubleTap, e, 1));
return true;
}
break;
default:
break;
}
return false;
}
void OnShortPress(const MotionEvent& e) override {
DCHECK(!IsDoubleTapInProgress());
GestureEventDetails short_press_details =
CreateTouchGestureDetails(EventType::kGestureShortPress);
Send(CreateGesture(short_press_details, e));
}
void OnLongPress(const MotionEvent& e) override {
DCHECK(!IsDoubleTapInProgress());
SetIgnoreSingleTap(true);
GestureEventDetails long_press_details =
CreateTouchGestureDetails(EventType::kGestureLongPress);
Send(CreateGesture(long_press_details, e));
}
GestureEventDetails CreateTouchGestureDetails(EventType type) const {
GestureEventDetails details(type);
details.set_device_type(GestureDeviceType::DEVICE_TOUCHSCREEN);
details.set_primary_unique_touch_event_id(
current_down_action_unique_touch_event_id_);
return details;
}
GestureEventDetails CreateTouchGestureDetails(EventType type,
float delta_x,
float delta_y) const {
GestureEventDetails details(type, delta_x, delta_y);
details.set_device_type(GestureDeviceType::DEVICE_TOUCHSCREEN);
details.set_primary_unique_touch_event_id(
current_down_action_unique_touch_event_id_);
return details;
}
GestureEventData CreateGesture(const GestureEventDetails& details,
int motion_event_id,
MotionEvent::ToolType primary_tool_type,
base::TimeTicks time,
float x,
float y,
float raw_x,
float raw_y,
size_t touch_point_count,
const gfx::RectF& bounding_box,
int flags) const {
return GestureEventData(details,
motion_event_id,
primary_tool_type,
time,
x,
y,
raw_x,
raw_y,
touch_point_count,
bounding_box,
flags,
0U);
}
GestureEventData CreateGesture(const GestureEventDetails& details,
const MotionEvent& event) const {
return GestureEventData(details, event.GetPointerId(), event.GetToolType(),
event.GetEventTime(), event.GetX(), event.GetY(),
event.GetRawX(), event.GetRawY(),
event.GetPointerCount(),
GetBoundingBox(event, details.type()),
event.GetFlags(), event.GetUniqueEventId());
}
GestureEventData CreateTapGesture(EventType type,
const MotionEvent& event,
int tap_count) const {
DCHECK_GE(tap_count, 0);
GestureEventDetails details = CreateTouchGestureDetails(type);
details.set_tap_count(tap_count);
return CreateGesture(details, event);
}
gfx::RectF GetBoundingBox(const MotionEvent& event, EventType type) const {
// Can't use gfx::RectF::Union, as it ignores touches with a radius of 0.
float left = std::numeric_limits<float>::max();
float top = std::numeric_limits<float>::max();
float right = -std::numeric_limits<float>::max();
float bottom = -std::numeric_limits<float>::max();
for (size_t i = 0; i < event.GetPointerCount(); ++i) {
float x, y, diameter;
// Only for the show press and tap events, the bounding box is calculated
// based on the touch start point and the maximum diameter before the
// show press event is sent.
if (type == EventType::kGestureShowPress ||
type == EventType::kGestureTap ||
type == EventType::kGestureTapUnconfirmed) {
DCHECK_EQ(0U, i);
diameter = max_diameter_before_show_press_;
x = tap_down_point_.x();
y = tap_down_point_.y();
} else {
diameter = event.GetTouchMajor(i);
x = event.GetX(i);
y = event.GetY(i);
}
x = x - diameter / 2;
y = y - diameter / 2;
left = std::min(left, x);
right = std::max(right, x + diameter);
top = std::min(top, y);
bottom = std::max(bottom, y + diameter);
}
return gfx::RectF(left, top, right - left, bottom - top);
}
void SetDoubleTapEnabled(bool enabled) {
DCHECK(!IsDoubleTapInProgress());
gesture_detector_.SetDoubleTapListener(enabled ? this : NULL);
}
void SetMultiTouchZoomEnabled(bool enabled) {
// Note that returning false from |OnScaleBegin()| or |OnScale()| prevents
// the detector from emitting further scale updates for the current touch
// sequence. Thus, if multitouch events are enabled in the middle of a
// gesture, it will only take effect with the next gesture.
ignore_multitouch_zoom_events_ = !enabled;
}
bool IsDoubleTapInProgress() const {
return gesture_detector_.is_double_tapping() ||
(IsScaleGestureDetectionInProgress() && InAnchoredScaleMode());
}
bool IsScrollInProgress() const { return scroll_event_sent_; }
bool IsPinchInProgress() const { return pinch_event_sent_; }
private:
bool OnSingleTapImpl(const MotionEvent& e, int tap_count) {
// Long taps in the edges of the screen have their events delayed by
// ContentViewHolder for tab swipe operations. As a consequence of the delay
// this method might be called after receiving the up event.
// These corner cases should be ignored.
if (ignore_single_tap_)
return true;
ignore_single_tap_ = true;
Send(CreateTapGesture(EventType::kGestureTap, e, tap_count));
return true;
}
bool IsScaleGestureDetectionInProgress() const {
return scale_gesture_detector_.IsInProgress();
}
bool InAnchoredScaleMode() const {
return scale_gesture_detector_.InAnchoredScaleMode();
}
bool IsDoubleTapEnabled() const {
return gesture_detector_.has_doubletap_listener() &&
client_->RequiresDoubleTapGestureEvents();
}
void SetIgnoreSingleTap(bool value) { ignore_single_tap_ = value; }
gfx::Vector2dF SubtractSlopRegion(const float dx, const float dy) {
float distance = std::sqrt(dx * dx + dy * dy);
float epsilon = 1e-3f;
if (distance > epsilon) {
float ratio =
std::max(0.f, distance - config_.gesture_detector_config.touch_slop) /
distance;
gfx::Vector2dF delta(dx * ratio, dy * ratio);
return delta;
}
gfx::Vector2dF delta(dx, dy);
return delta;
}
// When any of the currently down pointers exceeds its slop region
// for the first time, scroll delta is adjusted.
// The new deltas are calculated for each pointer individually,
// and the final scroll delta is the average over all delta values.
gfx::Vector2dF ComputeFirstScrollDelta(
const MotionEvent& ev1,
const MotionEvent& ev2,
const MotionEvent& secondary_pointer_down) {
// If there are more than two down pointers, tapping is not possible,
// so Slop region is not deducted.
DCHECK(ev2.GetPointerCount() < 3);
gfx::Vector2dF delta(0, 0);
for (size_t i = 0; i < ev2.GetPointerCount(); i++) {
const int pointer_id = ev2.GetPointerId(i);
const MotionEvent* source_pointer_down_event =
gesture_detector_.GetSourcePointerDownEvent(
ev1, &secondary_pointer_down, pointer_id);
if (!source_pointer_down_event)
continue;
int source_index =
source_pointer_down_event->FindPointerIndexOfId(pointer_id);
DCHECK_GE(source_index, 0);
if (source_index < 0)
continue;
float dx = source_pointer_down_event->GetX(source_index) - ev2.GetX(i);
float dy = source_pointer_down_event->GetY(source_index) - ev2.GetY(i);
delta += SubtractSlopRegion(dx, dy);
}
delta.InvScale(ev2.GetPointerCount());
return delta;
}
const GestureProvider::Config config_;
const raw_ptr<GestureProviderClient> client_;
const raw_ptr<GestureProvider> gesture_provider_;
GestureDetector gesture_detector_;
ScaleGestureDetector scale_gesture_detector_;
SnapScrollController snap_scroll_controller_;
// Keeps track of the event time of the first down action in current touch
// sequence.
base::TimeTicks current_down_action_event_time_;
// Keeps track of the unique touch event id of the first down action in
// current touch sequence.
int current_down_action_unique_touch_event_id_;
// Keeps track of the current GESTURE_LONG_PRESS event. If a context menu is
// opened after a GESTURE_LONG_PRESS, this is used to insert a
// GESTURE_TAP_CANCEL for removing any ::active styling.
base::TimeTicks current_longpress_time_;
// Completely silence multi-touch (pinch) scaling events. Used in WebView when
// zoom support is turned off.
bool ignore_multitouch_zoom_events_;
// TODO(klobag): This is to avoid a bug in GestureDetector. With multi-touch,
// always_in_tap_region_ is not reset. So when the last finger is up,
// |OnSingleTapUp()| will be mistakenly fired.
bool ignore_single_tap_;
// Tracks whether {PINCH|SCROLL}_BEGIN events have been forwarded for the
// current touch sequence.
bool pinch_event_sent_;
bool scroll_event_sent_;
// Only track the maximum diameter before the show press event has been
// sent and a tap must still be possible for this touch sequence.
float max_diameter_before_show_press_;
gfx::PointF tap_down_point_;
// Tracks whether an EventType::kGestureShowPress event has been sent for this
// touch sequence.
bool show_press_event_sent_;
// The scroll focus point is set to the first touch down point when scroll
// begins and is later updated based on the delta of touch points.
gfx::PointF scroll_focus_point_;
};
// GestureProvider
GestureProvider::GestureProvider(const Config& config,
GestureProviderClient* client)
: double_tap_support_for_page_(true),
double_tap_support_for_platform_(
config.double_tap_support_for_platform_enabled),
gesture_begin_end_types_enabled_(config.gesture_begin_end_types_enabled) {
DCHECK(client);
DCHECK(!config.min_gesture_bounds_length ||
!config.max_gesture_bounds_length ||
config.min_gesture_bounds_length <= config.max_gesture_bounds_length);
TRACE_EVENT0("input", "GestureProvider::InitGestureDetectors");
gesture_listener_ =
std::make_unique<GestureListenerImpl>(config, client, this);
UpdateDoubleTapDetectionSupport();
}
GestureProvider::~GestureProvider() {
}
bool GestureProvider::OnTouchEvent(const MotionEvent& event) {
TRACE_EVENT1("input",
"GestureProvider::OnTouchEvent",
"action",
GetMotionEventActionName(event.GetAction()));
DCHECK_NE(0u, event.GetPointerCount());
// We record the histograms before the |CanHandle()| call below because we
// want to check the event stream before gesture processing takes place. For
// example, we want to see |MotionEvent::Action::UP| event even for a panning
// gesture where the UP is not dispatched to content.
uma_histogram_.RecordTouchEvent(event);
if (!CanHandle(event))
return false;
OnTouchEventHandlingBegin(event);
gesture_listener_->OnTouchEvent(event);
OnTouchEventHandlingEnd(event);
return true;
}
void GestureProvider::ResetDetection() {
MotionEventGeneric generic_cancel_event(
MotionEvent::Action::CANCEL, base::TimeTicks::Now(), PointerProperties());
OnTouchEvent(generic_cancel_event);
}
void GestureProvider::SetMultiTouchZoomSupportEnabled(bool enabled) {
gesture_listener_->SetMultiTouchZoomEnabled(enabled);
}
void GestureProvider::SetDoubleTapSupportForPlatformEnabled(bool enabled) {
if (double_tap_support_for_platform_ == enabled)
return;
double_tap_support_for_platform_ = enabled;
UpdateDoubleTapDetectionSupport();
}
void GestureProvider::SetDoubleTapSupportForPageEnabled(bool enabled) {
if (double_tap_support_for_page_ == enabled)
return;
double_tap_support_for_page_ = enabled;
UpdateDoubleTapDetectionSupport();
}
bool GestureProvider::IsScrollInProgress() const {
return gesture_listener_->IsScrollInProgress();
}
bool GestureProvider::IsPinchInProgress() const {
return gesture_listener_->IsPinchInProgress();
}
bool GestureProvider::IsDoubleTapInProgress() const {
return gesture_listener_->IsDoubleTapInProgress();
}
void GestureProvider::SendSynthesizedEndEvents() {
gesture_listener_->SendSynthesizedEndEvents();
}
bool GestureProvider::CanHandle(const MotionEvent& event) const {
// Aura requires one cancel event per touch point, whereas Android requires
// one cancel event per touch sequence. Thus we need to allow extra cancel
// events.
return current_down_event_ ||
event.GetAction() == MotionEvent::Action::DOWN ||
event.GetAction() == MotionEvent::Action::CANCEL;
}
void GestureProvider::OnTouchEventHandlingBegin(const MotionEvent& event) {
switch (event.GetAction()) {
case MotionEvent::Action::DOWN:
current_down_event_ = event.Clone();
if (gesture_begin_end_types_enabled_) {
GestureEventDetails details(EventType::kGestureBegin);
details.set_device_type(GestureDeviceType::DEVICE_TOUCHSCREEN);
details.set_primary_unique_touch_event_id(event.GetUniqueEventId());
gesture_listener_->Send(
gesture_listener_->CreateGesture(details, event));
}
break;
case MotionEvent::Action::POINTER_DOWN:
if (gesture_begin_end_types_enabled_) {
const int action_index = event.GetActionIndex();
GestureEventDetails details(EventType::kGestureBegin);
details.set_device_type(GestureDeviceType::DEVICE_TOUCHSCREEN);
details.set_primary_unique_touch_event_id(event.GetUniqueEventId());
gesture_listener_->Send(gesture_listener_->CreateGesture(
details, event.GetPointerId(), event.GetToolType(),
event.GetEventTime(), event.GetX(action_index),
event.GetY(action_index), event.GetRawX(action_index),
event.GetRawY(action_index), event.GetPointerCount(),
gesture_listener_->GetBoundingBox(event, EventType::kGestureBegin),
event.GetFlags()));
}
break;
case MotionEvent::Action::POINTER_UP:
case MotionEvent::Action::UP:
case MotionEvent::Action::CANCEL:
case MotionEvent::Action::MOVE:
break;
case MotionEvent::Action::NONE:
case MotionEvent::Action::HOVER_ENTER:
case MotionEvent::Action::HOVER_EXIT:
case MotionEvent::Action::HOVER_MOVE:
case MotionEvent::Action::BUTTON_PRESS:
case MotionEvent::Action::BUTTON_RELEASE:
NOTREACHED();
}
}
void GestureProvider::OnTouchEventHandlingEnd(const MotionEvent& event) {
switch (event.GetAction()) {
case MotionEvent::Action::UP:
case MotionEvent::Action::CANCEL: {
if (gesture_begin_end_types_enabled_) {
GestureEventDetails details(EventType::kGestureEnd);
details.set_device_type(GestureDeviceType::DEVICE_TOUCHSCREEN);
if (current_down_event_) {
details.set_primary_unique_touch_event_id(
current_down_event_->GetUniqueEventId());
}
gesture_listener_->Send(
gesture_listener_->CreateGesture(details, event));
}
if (event.GetAction() != MotionEvent::Action::CANCEL ||
!GestureConfiguration::GetInstance()
->single_pointer_cancel_enabled() ||
event.GetPointerCount() == 1)
current_down_event_.reset();
UpdateDoubleTapDetectionSupport();
break;
}
case MotionEvent::Action::POINTER_UP:
if (gesture_begin_end_types_enabled_) {
GestureEventDetails details(EventType::kGestureEnd);
details.set_device_type(GestureDeviceType::DEVICE_TOUCHSCREEN);
if (current_down_event_) {
details.set_primary_unique_touch_event_id(
current_down_event_->GetUniqueEventId());
}
gesture_listener_->Send(
gesture_listener_->CreateGesture(details, event));
}
break;
case MotionEvent::Action::DOWN:
case MotionEvent::Action::POINTER_DOWN:
case MotionEvent::Action::MOVE:
break;
case MotionEvent::Action::NONE:
case MotionEvent::Action::HOVER_ENTER:
case MotionEvent::Action::HOVER_EXIT:
case MotionEvent::Action::HOVER_MOVE:
case MotionEvent::Action::BUTTON_PRESS:
case MotionEvent::Action::BUTTON_RELEASE:
NOTREACHED();
}
}
void GestureProvider::UpdateDoubleTapDetectionSupport() {
// The GestureDetector requires that any provided DoubleTapListener remain
// attached to it for the duration of a touch sequence. Defer any potential
// null'ing of the listener until the sequence has ended.
if (current_down_event_)
return;
const bool double_tap_enabled =
double_tap_support_for_page_ && double_tap_support_for_platform_;
gesture_listener_->SetDoubleTapEnabled(double_tap_enabled);
}
} // namespace ui
|