OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/renderer_host/input/synthetic_pinch_gesture.h" | 5 #include "content/browser/renderer_host/input/synthetic_pinch_gesture.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "content/common/input/input_event.h" | 9 #include "content/common/input/input_event.h" |
10 #include "ui/events/latency_info.h" | 10 #include "ui/events/latency_info.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 namespace { | |
14 | |
15 // TODO(dominikg): Use touch slop to compute this value. | |
16 const float kMinPointerDistance = 40.0f; | |
17 | |
18 } | |
19 | 13 |
20 SyntheticPinchGesture::SyntheticPinchGesture( | 14 SyntheticPinchGesture::SyntheticPinchGesture( |
21 const SyntheticPinchGestureParams& params) | 15 const SyntheticPinchGestureParams& params) |
22 : params_(params), started_(false) { | 16 : params_(params), |
| 17 current_y_0_(0.0f), |
| 18 current_y_1_(0.0f), |
| 19 target_y_0_(0.0f), |
| 20 target_y_1_(0.0f), |
| 21 started_(false) { |
23 DCHECK_GE(params_.total_num_pixels_covered, 0); | 22 DCHECK_GE(params_.total_num_pixels_covered, 0); |
24 | |
25 float inner_distance_to_anchor = kMinPointerDistance / 2.0f; | |
26 float outer_distance_to_anchor = | |
27 inner_distance_to_anchor + params_.total_num_pixels_covered / 2.0f; | |
28 | |
29 // Move pointers away from each other to zoom in | |
30 // or towards each other to zoom out. | |
31 if (params_.zoom_in) { | |
32 current_y_0_ = params_.anchor.y() - inner_distance_to_anchor; | |
33 current_y_1_ = params_.anchor.y() + inner_distance_to_anchor; | |
34 target_y_0_ = params_.anchor.y() - outer_distance_to_anchor; | |
35 target_y_1_ = params_.anchor.y() + outer_distance_to_anchor; | |
36 } else { | |
37 current_y_0_ = params_.anchor.y() - outer_distance_to_anchor; | |
38 current_y_1_ = params_.anchor.y() + outer_distance_to_anchor; | |
39 target_y_0_ = params_.anchor.y() - inner_distance_to_anchor; | |
40 target_y_1_ = params_.anchor.y() + inner_distance_to_anchor; | |
41 } | |
42 } | 23 } |
43 | 24 |
44 SyntheticPinchGesture::~SyntheticPinchGesture() {} | 25 SyntheticPinchGesture::~SyntheticPinchGesture() {} |
45 | 26 |
46 SyntheticGesture::Result SyntheticPinchGesture::ForwardInputEvents( | 27 SyntheticGesture::Result SyntheticPinchGesture::ForwardInputEvents( |
47 const base::TimeDelta& interval, SyntheticGestureTarget* target) { | 28 const base::TimeDelta& interval, SyntheticGestureTarget* target) { |
48 | 29 |
49 SyntheticGestureParams::GestureSourceType source = | 30 SyntheticGestureParams::GestureSourceType source = |
50 params_.gesture_source_type; | 31 params_.gesture_source_type; |
51 if (source == SyntheticGestureParams::DEFAULT_INPUT) | 32 if (source == SyntheticGestureParams::DEFAULT_INPUT) |
52 source = target->GetDefaultSyntheticGestureSourceType(); | 33 source = target->GetDefaultSyntheticGestureSourceType(); |
53 | 34 |
54 if (!target->SupportsSyntheticGestureSourceType(source)) | 35 if (!target->SupportsSyntheticGestureSourceType(source)) |
55 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM; | 36 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM; |
56 | 37 |
57 if (source == SyntheticGestureParams::TOUCH_INPUT) | 38 if (source == SyntheticGestureParams::TOUCH_INPUT) |
58 return ForwardTouchInputEvents(interval, target); | 39 return ForwardTouchInputEvents(interval, target); |
59 else | 40 else |
60 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; | 41 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; |
61 } | 42 } |
62 | 43 |
63 SyntheticGesture::Result SyntheticPinchGesture::ForwardTouchInputEvents( | 44 SyntheticGesture::Result SyntheticPinchGesture::ForwardTouchInputEvents( |
64 const base::TimeDelta& interval, SyntheticGestureTarget* target) { | 45 const base::TimeDelta& interval, SyntheticGestureTarget* target) { |
65 if (HasFinished()) | 46 if (!started_) { |
66 return SyntheticGesture::GESTURE_FINISHED; | 47 if (params_.total_num_pixels_covered == 0) |
| 48 return SyntheticGesture::GESTURE_FINISHED; |
67 | 49 |
68 if (!started_) { | 50 SetupCoordinates(target); |
| 51 |
69 touch_event_.PressPoint(params_.anchor.x(), current_y_0_); | 52 touch_event_.PressPoint(params_.anchor.x(), current_y_0_); |
70 touch_event_.PressPoint(params_.anchor.x(), current_y_1_); | 53 touch_event_.PressPoint(params_.anchor.x(), current_y_1_); |
71 ForwardTouchEvent(target); | 54 ForwardTouchEvent(target); |
72 started_ = true; | 55 started_ = true; |
73 } | 56 } |
74 | 57 |
75 // Compute the delta for the first pointer. The other one moves exactly | 58 // Compute the delta for the first pointer. The other one moves exactly |
76 // the same but in the opposite direction. | 59 // the same but in the opposite direction. |
77 float delta = GetDeltaForPointer0(interval); | 60 float delta = GetDeltaForPointer0(interval); |
78 current_y_0_ += delta; | 61 current_y_0_ += delta; |
(...skipping 15 matching lines...) Expand all Loading... |
94 } | 77 } |
95 | 78 |
96 return SyntheticGesture::GESTURE_RUNNING; | 79 return SyntheticGesture::GESTURE_RUNNING; |
97 } | 80 } |
98 | 81 |
99 void SyntheticPinchGesture::ForwardTouchEvent(SyntheticGestureTarget* target) { | 82 void SyntheticPinchGesture::ForwardTouchEvent(SyntheticGestureTarget* target) { |
100 target->DispatchInputEventToPlatform( | 83 target->DispatchInputEventToPlatform( |
101 InputEvent(touch_event_, ui::LatencyInfo(), false)); | 84 InputEvent(touch_event_, ui::LatencyInfo(), false)); |
102 } | 85 } |
103 | 86 |
| 87 void SyntheticPinchGesture::SetupCoordinates(SyntheticGestureTarget* target) { |
| 88 const float kTouchSlopInDips = target->GetTouchSlopInDips(); |
| 89 float inner_distance_to_anchor = 2 * kTouchSlopInDips; |
| 90 float outer_distance_to_anchor = inner_distance_to_anchor + |
| 91 params_.total_num_pixels_covered / 2.0f + |
| 92 kTouchSlopInDips; |
| 93 |
| 94 // Move pointers away from each other to zoom in |
| 95 // or towards each other to zoom out. |
| 96 if (params_.zoom_in) { |
| 97 current_y_0_ = params_.anchor.y() - inner_distance_to_anchor; |
| 98 current_y_1_ = params_.anchor.y() + inner_distance_to_anchor; |
| 99 target_y_0_ = params_.anchor.y() - outer_distance_to_anchor; |
| 100 target_y_1_ = params_.anchor.y() + outer_distance_to_anchor; |
| 101 } else { |
| 102 current_y_0_ = params_.anchor.y() - outer_distance_to_anchor; |
| 103 current_y_1_ = params_.anchor.y() + outer_distance_to_anchor; |
| 104 target_y_0_ = params_.anchor.y() - inner_distance_to_anchor; |
| 105 target_y_1_ = params_.anchor.y() + inner_distance_to_anchor; |
| 106 } |
| 107 } |
| 108 |
104 float SyntheticPinchGesture::GetDeltaForPointer0( | 109 float SyntheticPinchGesture::GetDeltaForPointer0( |
105 const base::TimeDelta& interval) const { | 110 const base::TimeDelta& interval) const { |
106 float total_abs_delta = | 111 float total_abs_delta = |
107 params_.relative_pointer_speed_in_pixels_s * interval.InSecondsF(); | 112 params_.relative_pointer_speed_in_pixels_s * interval.InSecondsF(); |
108 | 113 |
109 // Make sure we're not moving too far in the final step. | 114 // Make sure we're not moving too far in the final step. |
110 total_abs_delta = | 115 total_abs_delta = |
111 std::min(total_abs_delta, ComputeAbsoluteRemainingDistance()); | 116 std::min(total_abs_delta, ComputeAbsoluteRemainingDistance()); |
112 | 117 |
113 float abs_delta_pointer_0 = total_abs_delta / 2; | 118 float abs_delta_pointer_0 = total_abs_delta / 2; |
114 return params_.zoom_in ? -abs_delta_pointer_0 : abs_delta_pointer_0; | 119 return params_.zoom_in ? -abs_delta_pointer_0 : abs_delta_pointer_0; |
115 } | 120 } |
116 | 121 |
117 float SyntheticPinchGesture::ComputeAbsoluteRemainingDistance() const { | 122 float SyntheticPinchGesture::ComputeAbsoluteRemainingDistance() const { |
118 float distance_0 = params_.zoom_in ? (current_y_0_ - target_y_0_) | 123 float distance_0 = params_.zoom_in ? (current_y_0_ - target_y_0_) |
119 : (target_y_0_ - current_y_0_); | 124 : (target_y_0_ - current_y_0_); |
120 DCHECK_GE(distance_0, 0); | 125 DCHECK_GE(distance_0, 0); |
121 | 126 |
122 // Both pointers move the same overall distance at the same speed. | 127 // Both pointers move the same overall distance at the same speed. |
123 return 2 * distance_0; | 128 return 2 * distance_0; |
124 } | 129 } |
125 | 130 |
126 bool SyntheticPinchGesture::HasFinished() const { | 131 bool SyntheticPinchGesture::HasFinished() const { |
127 return ComputeAbsoluteRemainingDistance() == 0; | 132 return ComputeAbsoluteRemainingDistance() == 0; |
128 } | 133 } |
129 | 134 |
130 } // namespace content | 135 } // namespace content |
OLD | NEW |