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/scale_gesture_detector.h" | 5 #include "ui/events/gesture_detection/scale_gesture_detector.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "ui/events/gesture_detection/motion_event.h" | 12 #include "ui/events/gesture_detection/motion_event.h" |
13 | 13 |
14 using base::TimeDelta; | 14 using base::TimeDelta; |
15 using base::TimeTicks; | 15 using base::TimeTicks; |
16 | 16 |
17 namespace ui { | 17 namespace ui { |
18 namespace { | 18 namespace { |
19 | 19 |
| 20 // Using a small epsilon when comparing slop distances allows pixel perfect |
| 21 // slop determination when using fractional DPI coordinates (assuming the slop |
| 22 // region and DPI scale are reasonably proportioned). |
| 23 const float kSlopEpsilon = .05f; |
| 24 |
20 const int kTouchStabilizeTimeMs = 128; | 25 const int kTouchStabilizeTimeMs = 128; |
21 | 26 |
22 const float kScaleFactor = .5f; | 27 const float kScaleFactor = .5f; |
23 | 28 |
24 } // namespace | 29 } // namespace |
25 | 30 |
26 // Note: These constants were taken directly from the default (unscaled) | 31 // Note: These constants were taken directly from the default (unscaled) |
27 // versions found in Android's ViewConfiguration. | 32 // versions found in Android's ViewConfiguration. |
28 ScaleGestureDetector::Config::Config() | 33 ScaleGestureDetector::Config::Config() |
29 : quick_scale_enabled(true), | 34 : min_scaling_touch_major(48), |
30 min_scaling_touch_major(48), | 35 min_scaling_span(200), |
31 min_scaling_span(200) {} | 36 quick_scale_enabled(true) {} |
32 | 37 |
33 ScaleGestureDetector::Config::~Config() {} | 38 ScaleGestureDetector::Config::~Config() {} |
34 | 39 |
35 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScale( | 40 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScale( |
36 const ScaleGestureDetector&, const MotionEvent&) { | 41 const ScaleGestureDetector&, const MotionEvent&) { |
37 return false; | 42 return false; |
38 } | 43 } |
39 | 44 |
40 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScaleBegin( | 45 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScaleBegin( |
41 const ScaleGestureDetector&, const MotionEvent&) { | 46 const ScaleGestureDetector&, const MotionEvent&) { |
(...skipping 23 matching lines...) Expand all Loading... |
65 touch_upper_(0), | 70 touch_upper_(0), |
66 touch_lower_(0), | 71 touch_lower_(0), |
67 touch_history_last_accepted_(0), | 72 touch_history_last_accepted_(0), |
68 touch_history_direction_(0), | 73 touch_history_direction_(0), |
69 touch_min_major_(0), | 74 touch_min_major_(0), |
70 double_tap_focus_x_(0), | 75 double_tap_focus_x_(0), |
71 double_tap_focus_y_(0), | 76 double_tap_focus_y_(0), |
72 double_tap_mode_(DOUBLE_TAP_MODE_NONE), | 77 double_tap_mode_(DOUBLE_TAP_MODE_NONE), |
73 event_before_or_above_starting_gesture_event_(false) { | 78 event_before_or_above_starting_gesture_event_(false) { |
74 DCHECK(listener_); | 79 DCHECK(listener_); |
75 span_slop_ = config.gesture_detector_config.scaled_touch_slop * 2; | 80 span_slop_ = |
| 81 (config.gesture_detector_config.touch_slop + kSlopEpsilon) * 2; |
76 touch_min_major_ = config.min_scaling_touch_major; | 82 touch_min_major_ = config.min_scaling_touch_major; |
77 min_span_ = config.min_scaling_span; | 83 min_span_ = config.min_scaling_span + kSlopEpsilon; |
78 SetQuickScaleEnabled(config.quick_scale_enabled); | 84 SetQuickScaleEnabled(config.quick_scale_enabled); |
79 } | 85 } |
80 | 86 |
81 ScaleGestureDetector::~ScaleGestureDetector() {} | 87 ScaleGestureDetector::~ScaleGestureDetector() {} |
82 | 88 |
83 bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { | 89 bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { |
84 curr_time_ = event.GetEventTime(); | 90 curr_time_ = event.GetEventTime(); |
85 | 91 |
86 const int action = event.GetAction(); | 92 const int action = event.GetAction(); |
87 | 93 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 in_progress_ = false; | 198 in_progress_ = false; |
193 initial_span_ = span; | 199 initial_span_ = span; |
194 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; | 200 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; |
195 } | 201 } |
196 if (config_changed) { | 202 if (config_changed) { |
197 prev_span_x_ = curr_span_x_ = span_x; | 203 prev_span_x_ = curr_span_x_ = span_x; |
198 prev_span_y_ = curr_span_y_ = span_y; | 204 prev_span_y_ = curr_span_y_ = span_y; |
199 initial_span_ = prev_span_ = curr_span_ = span; | 205 initial_span_ = prev_span_ = curr_span_ = span; |
200 } | 206 } |
201 | 207 |
202 const int min_span = InDoubleTapMode() ? span_slop_ : min_span_; | 208 const float min_span = InDoubleTapMode() ? span_slop_ : min_span_; |
203 if (!in_progress_ && span >= min_span && | 209 if (!in_progress_ && span >= min_span && |
204 (was_in_progress || std::abs(span - initial_span_) > span_slop_)) { | 210 (was_in_progress || std::abs(span - initial_span_) > span_slop_)) { |
205 prev_span_x_ = curr_span_x_ = span_x; | 211 prev_span_x_ = curr_span_x_ = span_x; |
206 prev_span_y_ = curr_span_y_ = span_y; | 212 prev_span_y_ = curr_span_y_ = span_y; |
207 prev_span_ = curr_span_ = span; | 213 prev_span_ = curr_span_ = span; |
208 prev_time_ = curr_time_; | 214 prev_time_ = curr_time_; |
209 in_progress_ = listener_->OnScaleBegin(*this, event); | 215 in_progress_ = listener_->OnScaleBegin(*this, event); |
210 } | 216 } |
211 | 217 |
212 // Handle motion; focal point and span/scale factor are changing. | 218 // Handle motion; focal point and span/scale factor are changing. |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 | 365 |
360 void ScaleGestureDetector::ClearTouchHistory() { | 366 void ScaleGestureDetector::ClearTouchHistory() { |
361 touch_upper_ = std::numeric_limits<float>::quiet_NaN(); | 367 touch_upper_ = std::numeric_limits<float>::quiet_NaN(); |
362 touch_lower_ = std::numeric_limits<float>::quiet_NaN(); | 368 touch_lower_ = std::numeric_limits<float>::quiet_NaN(); |
363 touch_history_last_accepted_ = std::numeric_limits<float>::quiet_NaN(); | 369 touch_history_last_accepted_ = std::numeric_limits<float>::quiet_NaN(); |
364 touch_history_direction_ = 0; | 370 touch_history_direction_ = 0; |
365 touch_history_last_accepted_time_ = base::TimeTicks(); | 371 touch_history_last_accepted_time_ = base::TimeTicks(); |
366 } | 372 } |
367 | 373 |
368 } // namespace ui | 374 } // namespace ui |
OLD | NEW |