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 #include "ui/events/gesture_detection/gesture_provider.h" | 5 #include "ui/events/gesture_detection/gesture_provider.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 diameter)); | 40 diameter)); |
41 } | 41 } |
42 return bounds; | 42 return bounds; |
43 } | 43 } |
44 | 44 |
45 GestureEventData CreateGesture(const GestureEventDetails& details, | 45 GestureEventData CreateGesture(const GestureEventDetails& details, |
46 int motion_event_id, | 46 int motion_event_id, |
47 base::TimeTicks time, | 47 base::TimeTicks time, |
48 float x, | 48 float x, |
49 float y, | 49 float y, |
| 50 float raw_x, |
| 51 float raw_y, |
50 size_t touch_point_count, | 52 size_t touch_point_count, |
51 const gfx::RectF& bounding_box) { | 53 const gfx::RectF& bounding_box) { |
52 return GestureEventData(details, | 54 return GestureEventData(details, |
53 motion_event_id, | 55 motion_event_id, |
54 time, | 56 time, |
55 x, | 57 x, |
56 y, | 58 y, |
57 static_cast<int>(touch_point_count), | 59 raw_x, |
| 60 raw_y, |
| 61 touch_point_count, |
58 bounding_box); | 62 bounding_box); |
59 } | 63 } |
60 | 64 |
61 GestureEventData CreateGesture(EventType type, | 65 GestureEventData CreateGesture(EventType type, |
62 int motion_event_id, | 66 int motion_event_id, |
63 base::TimeTicks time, | 67 base::TimeTicks time, |
64 float x, | 68 float x, |
65 float y, | 69 float y, |
| 70 float raw_x, |
| 71 float raw_y, |
66 size_t touch_point_count, | 72 size_t touch_point_count, |
67 const gfx::RectF& bounding_box) { | 73 const gfx::RectF& bounding_box) { |
68 return GestureEventData(type, | 74 return GestureEventData(GestureEventDetails(type, 0, 0), |
69 motion_event_id, | 75 motion_event_id, |
70 time, | 76 time, |
71 x, | 77 x, |
72 y, | 78 y, |
73 static_cast<int>(touch_point_count), | 79 raw_x, |
| 80 raw_y, |
| 81 touch_point_count, |
74 bounding_box); | 82 bounding_box); |
75 } | 83 } |
76 | 84 |
77 GestureEventData CreateGesture(const GestureEventDetails& details, | 85 GestureEventData CreateGesture(const GestureEventDetails& details, |
78 const MotionEvent& event) { | 86 const MotionEvent& event) { |
79 return CreateGesture(details, | 87 return GestureEventData(details, |
80 event.GetId(), | 88 event.GetId(), |
81 event.GetEventTime(), | 89 event.GetEventTime(), |
82 event.GetX(), | 90 event.GetX(), |
83 event.GetY(), | 91 event.GetY(), |
84 event.GetPointerCount(), | 92 event.GetRawX(), |
85 GetBoundingBox(event)); | 93 event.GetRawY(), |
| 94 event.GetPointerCount(), |
| 95 GetBoundingBox(event)); |
86 } | 96 } |
87 | 97 |
88 GestureEventData CreateGesture(EventType type, | 98 GestureEventData CreateGesture(EventType type, const MotionEvent& event) { |
89 const MotionEvent& event) { | 99 return CreateGesture(GestureEventDetails(type, 0, 0), event); |
90 return CreateGesture(type, | |
91 event.GetId(), | |
92 event.GetEventTime(), | |
93 event.GetX(), | |
94 event.GetY(), | |
95 event.GetPointerCount(), | |
96 GetBoundingBox(event)); | |
97 } | 100 } |
98 | 101 |
99 GestureEventDetails CreateTapGestureDetails(EventType type) { | 102 GestureEventDetails CreateTapGestureDetails(EventType type) { |
100 // Set the tap count to 1 even for ET_GESTURE_DOUBLE_TAP, in order to be | 103 // Set the tap count to 1 even for ET_GESTURE_DOUBLE_TAP, in order to be |
101 // consistent with double tap behavior on a mobile viewport. See | 104 // consistent with double tap behavior on a mobile viewport. See |
102 // crbug.com/234986 for context. | 105 // crbug.com/234986 for context. |
103 GestureEventDetails tap_details(type, 1, 0); | 106 GestureEventDetails tap_details(type, 1, 0); |
104 return tap_details; | 107 return tap_details; |
105 } | 108 } |
106 | 109 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 if (ignore_multitouch_events_ && !detector.InDoubleTapMode()) | 150 if (ignore_multitouch_events_ && !detector.InDoubleTapMode()) |
148 return false; | 151 return false; |
149 pinch_event_sent_ = false; | 152 pinch_event_sent_ = false; |
150 return true; | 153 return true; |
151 } | 154 } |
152 | 155 |
153 virtual void OnScaleEnd(const ScaleGestureDetector& detector, | 156 virtual void OnScaleEnd(const ScaleGestureDetector& detector, |
154 const MotionEvent& e) OVERRIDE { | 157 const MotionEvent& e) OVERRIDE { |
155 if (!pinch_event_sent_) | 158 if (!pinch_event_sent_) |
156 return; | 159 return; |
157 provider_->Send(CreateGesture(ET_GESTURE_PINCH_END, | 160 provider_->Send(CreateGesture(ET_GESTURE_PINCH_END, e)); |
158 e.GetId(), | |
159 detector.GetEventTime(), | |
160 0, | |
161 0, | |
162 e.GetPointerCount(), | |
163 GetBoundingBox(e))); | |
164 pinch_event_sent_ = false; | 161 pinch_event_sent_ = false; |
165 } | 162 } |
166 | 163 |
167 virtual bool OnScale(const ScaleGestureDetector& detector, | 164 virtual bool OnScale(const ScaleGestureDetector& detector, |
168 const MotionEvent& e) OVERRIDE { | 165 const MotionEvent& e) OVERRIDE { |
169 if (ignore_multitouch_events_ && !detector.InDoubleTapMode()) | 166 if (ignore_multitouch_events_ && !detector.InDoubleTapMode()) |
170 return false; | 167 return false; |
171 if (!pinch_event_sent_) { | 168 if (!pinch_event_sent_) { |
172 pinch_event_sent_ = true; | 169 pinch_event_sent_ = true; |
173 provider_->Send(CreateGesture(ET_GESTURE_PINCH_BEGIN, | 170 provider_->Send(CreateGesture(ET_GESTURE_PINCH_BEGIN, |
174 e.GetId(), | 171 e.GetId(), |
175 detector.GetEventTime(), | 172 detector.GetEventTime(), |
176 detector.GetFocusX(), | 173 detector.GetFocusX(), |
177 detector.GetFocusY(), | 174 detector.GetFocusY(), |
| 175 detector.GetFocusX() + e.GetRawOffsetX(), |
| 176 detector.GetFocusY() + e.GetRawOffsetY(), |
178 e.GetPointerCount(), | 177 e.GetPointerCount(), |
179 GetBoundingBox(e))); | 178 GetBoundingBox(e))); |
180 } | 179 } |
181 | 180 |
182 if (std::abs(detector.GetCurrentSpan() - detector.GetPreviousSpan()) < | 181 if (std::abs(detector.GetCurrentSpan() - detector.GetPreviousSpan()) < |
183 min_pinch_update_span_delta_) { | 182 min_pinch_update_span_delta_) { |
184 return false; | 183 return false; |
185 } | 184 } |
186 | 185 |
187 float scale = detector.GetScaleFactor(); | 186 float scale = detector.GetScaleFactor(); |
(...skipping 11 matching lines...) Expand all Loading... |
199 scale = std::pow(scale > 1 ? 1.0f + kDoubleTapDragZoomSpeed | 198 scale = std::pow(scale > 1 ? 1.0f + kDoubleTapDragZoomSpeed |
200 : 1.0f - kDoubleTapDragZoomSpeed, | 199 : 1.0f - kDoubleTapDragZoomSpeed, |
201 std::abs(dy)); | 200 std::abs(dy)); |
202 } | 201 } |
203 GestureEventDetails pinch_details(ET_GESTURE_PINCH_UPDATE, scale, 0); | 202 GestureEventDetails pinch_details(ET_GESTURE_PINCH_UPDATE, scale, 0); |
204 provider_->Send(CreateGesture(pinch_details, | 203 provider_->Send(CreateGesture(pinch_details, |
205 e.GetId(), | 204 e.GetId(), |
206 detector.GetEventTime(), | 205 detector.GetEventTime(), |
207 detector.GetFocusX(), | 206 detector.GetFocusX(), |
208 detector.GetFocusY(), | 207 detector.GetFocusY(), |
| 208 detector.GetFocusX() + e.GetRawOffsetX(), |
| 209 detector.GetFocusY() + e.GetRawOffsetY(), |
209 e.GetPointerCount(), | 210 e.GetPointerCount(), |
210 GetBoundingBox(e))); | 211 GetBoundingBox(e))); |
211 return true; | 212 return true; |
212 } | 213 } |
213 | 214 |
214 void SetDoubleTapEnabled(bool enabled) { | 215 void SetDoubleTapEnabled(bool enabled) { |
215 DCHECK(!IsDoubleTapInProgress()); | 216 DCHECK(!IsDoubleTapInProgress()); |
216 scale_gesture_detector_.SetQuickScaleEnabled(enabled); | 217 scale_gesture_detector_.SetQuickScaleEnabled(enabled); |
217 } | 218 } |
218 | 219 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 GestureEventDetails scroll_details( | 339 GestureEventDetails scroll_details( |
339 ET_GESTURE_SCROLL_BEGIN, -raw_distance_x, -raw_distance_y); | 340 ET_GESTURE_SCROLL_BEGIN, -raw_distance_x, -raw_distance_y); |
340 | 341 |
341 // Use the co-ordinates from the touch down, as these co-ordinates are | 342 // Use the co-ordinates from the touch down, as these co-ordinates are |
342 // used to determine which layer the scroll should affect. | 343 // used to determine which layer the scroll should affect. |
343 provider_->Send(CreateGesture(scroll_details, | 344 provider_->Send(CreateGesture(scroll_details, |
344 e2.GetId(), | 345 e2.GetId(), |
345 e2.GetEventTime(), | 346 e2.GetEventTime(), |
346 e1.GetX(), | 347 e1.GetX(), |
347 e1.GetY(), | 348 e1.GetY(), |
| 349 e1.GetRawX(), |
| 350 e1.GetRawY(), |
348 e2.GetPointerCount(), | 351 e2.GetPointerCount(), |
349 GetBoundingBox(e2))); | 352 GetBoundingBox(e2))); |
350 } | 353 } |
351 | 354 |
352 if (distance_x || distance_y) { | 355 if (distance_x || distance_y) { |
353 const gfx::RectF bounding_box = GetBoundingBox(e2); | 356 const gfx::RectF bounding_box = GetBoundingBox(e2); |
| 357 const gfx::PointF center = bounding_box.CenterPoint(); |
| 358 const gfx::PointF raw_center = |
| 359 center + gfx::Vector2dF(e2.GetRawOffsetX(), e2.GetRawOffsetY()); |
354 GestureEventDetails scroll_details( | 360 GestureEventDetails scroll_details( |
355 ET_GESTURE_SCROLL_UPDATE, -distance_x, -distance_y); | 361 ET_GESTURE_SCROLL_UPDATE, -distance_x, -distance_y); |
356 provider_->Send(CreateGesture(scroll_details, | 362 provider_->Send(CreateGesture(scroll_details, |
357 e2.GetId(), | 363 e2.GetId(), |
358 e2.GetEventTime(), | 364 e2.GetEventTime(), |
359 bounding_box.CenterPoint().x(), | 365 center.x(), |
360 bounding_box.CenterPoint().y(), | 366 center.y(), |
| 367 raw_center.x(), |
| 368 raw_center.y(), |
361 e2.GetPointerCount(), | 369 e2.GetPointerCount(), |
362 bounding_box)); | 370 bounding_box)); |
363 } | 371 } |
364 | 372 |
365 return true; | 373 return true; |
366 } | 374 } |
367 | 375 |
368 virtual bool OnFling(const MotionEvent& e1, | 376 virtual bool OnFling(const MotionEvent& e1, |
369 const MotionEvent& e2, | 377 const MotionEvent& e2, |
370 float velocity_x, | 378 float velocity_x, |
(...skipping 24 matching lines...) Expand all Loading... |
395 // The location of the two finger tap event should be the location of the | 403 // The location of the two finger tap event should be the location of the |
396 // primary pointer. | 404 // primary pointer. |
397 GestureEventDetails two_finger_tap_details(ET_GESTURE_TWO_FINGER_TAP, | 405 GestureEventDetails two_finger_tap_details(ET_GESTURE_TWO_FINGER_TAP, |
398 e1.GetTouchMajor(), | 406 e1.GetTouchMajor(), |
399 e1.GetTouchMajor()); | 407 e1.GetTouchMajor()); |
400 provider_->Send(CreateGesture(two_finger_tap_details, | 408 provider_->Send(CreateGesture(two_finger_tap_details, |
401 e2.GetId(), | 409 e2.GetId(), |
402 e2.GetEventTime(), | 410 e2.GetEventTime(), |
403 e1.GetX(), | 411 e1.GetX(), |
404 e1.GetY(), | 412 e1.GetY(), |
| 413 e1.GetRawX(), |
| 414 e1.GetRawY(), |
405 e2.GetPointerCount(), | 415 e2.GetPointerCount(), |
406 GetBoundingBox(e2))); | 416 GetBoundingBox(e2))); |
407 return true; | 417 return true; |
408 } | 418 } |
409 | 419 |
410 virtual void OnShowPress(const MotionEvent& e) OVERRIDE { | 420 virtual void OnShowPress(const MotionEvent& e) OVERRIDE { |
411 GestureEventDetails show_press_details(ET_GESTURE_SHOW_PRESS, 0, 0); | 421 GestureEventDetails show_press_details(ET_GESTURE_SHOW_PRESS, 0, 0); |
412 provider_->Send(CreateGesture(show_press_details, e)); | 422 provider_->Send(CreateGesture(show_press_details, e)); |
413 } | 423 } |
414 | 424 |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 case ET_GESTURE_LONG_TAP: | 674 case ET_GESTURE_LONG_TAP: |
665 current_longpress_time_ = base::TimeTicks(); | 675 current_longpress_time_ = base::TimeTicks(); |
666 break; | 676 break; |
667 case ET_GESTURE_SCROLL_BEGIN: | 677 case ET_GESTURE_SCROLL_BEGIN: |
668 DCHECK(!touch_scroll_in_progress_); | 678 DCHECK(!touch_scroll_in_progress_); |
669 touch_scroll_in_progress_ = true; | 679 touch_scroll_in_progress_ = true; |
670 break; | 680 break; |
671 case ET_GESTURE_SCROLL_END: | 681 case ET_GESTURE_SCROLL_END: |
672 DCHECK(touch_scroll_in_progress_); | 682 DCHECK(touch_scroll_in_progress_); |
673 if (pinch_in_progress_) | 683 if (pinch_in_progress_) |
674 Send(CreateGesture(ET_GESTURE_PINCH_END, | 684 Send(GestureEventData(ET_GESTURE_PINCH_END, gesture)); |
675 gesture.motion_event_id, | |
676 gesture.time, | |
677 gesture.x, | |
678 gesture.y, | |
679 gesture.details.touch_points(), | |
680 gesture.details.bounding_box())); | |
681 touch_scroll_in_progress_ = false; | 685 touch_scroll_in_progress_ = false; |
682 break; | 686 break; |
683 case ET_GESTURE_PINCH_BEGIN: | 687 case ET_GESTURE_PINCH_BEGIN: |
684 DCHECK(!pinch_in_progress_); | 688 DCHECK(!pinch_in_progress_); |
685 if (!touch_scroll_in_progress_) | 689 if (!touch_scroll_in_progress_) |
686 Send(CreateGesture(ET_GESTURE_SCROLL_BEGIN, | 690 Send(GestureEventData(ET_GESTURE_SCROLL_BEGIN, gesture)); |
687 gesture.motion_event_id, | |
688 gesture.time, | |
689 gesture.x, | |
690 gesture.y, | |
691 gesture.details.touch_points(), | |
692 gesture.details.bounding_box())); | |
693 pinch_in_progress_ = true; | 691 pinch_in_progress_ = true; |
694 break; | 692 break; |
695 case ET_GESTURE_PINCH_END: | 693 case ET_GESTURE_PINCH_END: |
696 DCHECK(pinch_in_progress_); | 694 DCHECK(pinch_in_progress_); |
697 pinch_in_progress_ = false; | 695 pinch_in_progress_ = false; |
698 break; | 696 break; |
699 case ET_GESTURE_SHOW_PRESS: | 697 case ET_GESTURE_SHOW_PRESS: |
700 // It's possible that a double-tap drag zoom (from ScaleGestureDetector) | 698 // It's possible that a double-tap drag zoom (from ScaleGestureDetector) |
701 // will start before the press gesture fires (from GestureDetector), in | 699 // will start before the press gesture fires (from GestureDetector), in |
702 // which case the press should simply be dropped. | 700 // which case the press should simply be dropped. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 case MotionEvent::ACTION_DOWN: | 732 case MotionEvent::ACTION_DOWN: |
735 current_down_event_ = event.Clone(); | 733 current_down_event_ = event.Clone(); |
736 touch_scroll_in_progress_ = false; | 734 touch_scroll_in_progress_ = false; |
737 pinch_in_progress_ = false; | 735 pinch_in_progress_ = false; |
738 current_longpress_time_ = base::TimeTicks(); | 736 current_longpress_time_ = base::TimeTicks(); |
739 if (gesture_begin_end_types_enabled_) | 737 if (gesture_begin_end_types_enabled_) |
740 Send(CreateGesture(ET_GESTURE_BEGIN, event)); | 738 Send(CreateGesture(ET_GESTURE_BEGIN, event)); |
741 break; | 739 break; |
742 case MotionEvent::ACTION_POINTER_DOWN: | 740 case MotionEvent::ACTION_POINTER_DOWN: |
743 if (gesture_begin_end_types_enabled_) { | 741 if (gesture_begin_end_types_enabled_) { |
| 742 const int action_index = event.GetActionIndex(); |
744 Send(CreateGesture(ET_GESTURE_BEGIN, | 743 Send(CreateGesture(ET_GESTURE_BEGIN, |
745 event.GetId(), | 744 event.GetId(), |
746 event.GetEventTime(), | 745 event.GetEventTime(), |
747 event.GetX(event.GetActionIndex()), | 746 event.GetX(action_index), |
748 event.GetY(event.GetActionIndex()), | 747 event.GetY(action_index), |
| 748 event.GetRawX(action_index), |
| 749 event.GetRawY(action_index), |
749 event.GetPointerCount(), | 750 event.GetPointerCount(), |
750 GetBoundingBox(event))); | 751 GetBoundingBox(event))); |
751 } | 752 } |
752 break; | 753 break; |
753 case MotionEvent::ACTION_POINTER_UP: | 754 case MotionEvent::ACTION_POINTER_UP: |
754 case MotionEvent::ACTION_UP: | 755 case MotionEvent::ACTION_UP: |
755 case MotionEvent::ACTION_CANCEL: | 756 case MotionEvent::ACTION_CANCEL: |
756 case MotionEvent::ACTION_MOVE: | 757 case MotionEvent::ACTION_MOVE: |
757 break; | 758 break; |
758 } | 759 } |
759 } | 760 } |
760 | 761 |
761 void GestureProvider::OnTouchEventHandlingEnd(const MotionEvent& event) { | 762 void GestureProvider::OnTouchEventHandlingEnd(const MotionEvent& event) { |
762 switch (event.GetAction()) { | 763 switch (event.GetAction()) { |
763 case MotionEvent::ACTION_UP: | 764 case MotionEvent::ACTION_UP: |
764 case MotionEvent::ACTION_CANCEL: { | 765 case MotionEvent::ACTION_CANCEL: { |
765 // Note: This call will have no effect if a fling was just generated, as | 766 // Note: This call will have no effect if a fling was just generated, as |
766 // |Fling()| will have already signalled an end to touch-scrolling. | 767 // |Fling()| will have already signalled an end to touch-scrolling. |
767 EndTouchScrollIfNecessary(event, true); | 768 EndTouchScrollIfNecessary(event, true); |
768 | 769 |
769 const gfx::RectF bounding_box = GetBoundingBox(event); | 770 const gfx::RectF bounding_box = GetBoundingBox(event); |
770 | 771 |
771 if (gesture_begin_end_types_enabled_) { | 772 if (gesture_begin_end_types_enabled_) { |
772 for (size_t i = 0; i < event.GetPointerCount(); ++i) { | 773 for (size_t i = 0; i < event.GetPointerCount(); ++i) { |
773 Send(CreateGesture(ET_GESTURE_END, | 774 Send(CreateGesture(ET_GESTURE_END, |
774 event.GetId(), | 775 event.GetId(), |
775 event.GetEventTime(), | 776 event.GetEventTime(), |
776 event.GetX(i), | 777 event.GetX(i), |
777 event.GetY(i), | 778 event.GetY(i), |
| 779 event.GetRawX(i), |
| 780 event.GetRawY(i), |
778 event.GetPointerCount() - i, | 781 event.GetPointerCount() - i, |
779 bounding_box)); | 782 bounding_box)); |
780 } | 783 } |
781 } | 784 } |
782 | 785 |
783 current_down_event_.reset(); | 786 current_down_event_.reset(); |
784 | 787 |
785 UpdateDoubleTapDetectionSupport(); | 788 UpdateDoubleTapDetectionSupport(); |
786 break; | 789 break; |
787 } | 790 } |
(...skipping 15 matching lines...) Expand all Loading... |
803 if (current_down_event_) | 806 if (current_down_event_) |
804 return; | 807 return; |
805 | 808 |
806 const bool double_tap_enabled = double_tap_support_for_page_ && | 809 const bool double_tap_enabled = double_tap_support_for_page_ && |
807 double_tap_support_for_platform_; | 810 double_tap_support_for_platform_; |
808 gesture_listener_->SetDoubleTapEnabled(double_tap_enabled); | 811 gesture_listener_->SetDoubleTapEnabled(double_tap_enabled); |
809 scale_gesture_listener_->SetDoubleTapEnabled(double_tap_enabled); | 812 scale_gesture_listener_->SetDoubleTapEnabled(double_tap_enabled); |
810 } | 813 } |
811 | 814 |
812 } // namespace ui | 815 } // namespace ui |
OLD | NEW |