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_smooth_scroll_gesture.h" | 5 #include "content/browser/renderer_host/input/synthetic_smooth_move_gesture.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/gfx/geometry/point_f.h" | 8 #include "ui/gfx/geometry/point_f.h" |
9 | 9 |
10 namespace content { | 10 namespace content { |
11 namespace { | 11 namespace { |
12 | 12 |
13 gfx::Vector2d FloorTowardZero(const gfx::Vector2dF& vector) { | 13 gfx::Vector2d FloorTowardZero(const gfx::Vector2dF& vector) { |
14 int x = vector.x() > 0 ? floor(vector.x()) : ceil(vector.x()); | 14 int x = vector.x() > 0 ? floor(vector.x()) : ceil(vector.x()); |
15 int y = vector.y() > 0 ? floor(vector.y()) : ceil(vector.y()); | 15 int y = vector.y() > 0 ? floor(vector.y()) : ceil(vector.y()); |
16 return gfx::Vector2d(x, y); | 16 return gfx::Vector2d(x, y); |
17 } | 17 } |
18 | 18 |
19 gfx::Vector2d CeilFromZero(const gfx::Vector2dF& vector) { | 19 gfx::Vector2d CeilFromZero(const gfx::Vector2dF& vector) { |
20 int x = vector.x() > 0 ? ceil(vector.x()) : floor(vector.x()); | 20 int x = vector.x() > 0 ? ceil(vector.x()) : floor(vector.x()); |
21 int y = vector.y() > 0 ? ceil(vector.y()) : floor(vector.y()); | 21 int y = vector.y() > 0 ? ceil(vector.y()) : floor(vector.y()); |
22 return gfx::Vector2d(x, y); | 22 return gfx::Vector2d(x, y); |
23 } | 23 } |
24 | 24 |
25 gfx::Vector2dF ProjectScalarOntoVector( | 25 gfx::Vector2dF ProjectScalarOntoVector(float scalar, |
26 float scalar, const gfx::Vector2d& vector) { | 26 const gfx::Vector2dF& vector) { |
27 return gfx::ScaleVector2d(vector, scalar / vector.Length()); | 27 return gfx::ScaleVector2d(vector, scalar / vector.Length()); |
28 } | 28 } |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 SyntheticSmoothScrollGesture::SyntheticSmoothScrollGesture( | 32 SyntheticSmoothMoveGestureParams::SyntheticSmoothMoveGestureParams() |
33 const SyntheticSmoothScrollGestureParams& params) | 33 : speed_in_pixels_s(800) { |
picksi
2015/02/23 12:22:24
I don't think you can have such a 'naked' arbitrar
ssid
2015/02/23 14:03:04
My bad. Moved this code.
| |
34 } | |
35 | |
36 SyntheticSmoothMoveGestureParams::SyntheticSmoothMoveGestureParams( | |
37 SyntheticSmoothScrollGestureParams params) | |
38 : start_point(params.anchor), | |
39 speed_in_pixels_s(params.speed_in_pixels_s), | |
40 prevent_fling(params.prevent_fling), | |
41 prevent_slop(false) { | |
42 // TODO(ssid): Remove this when params for scroll is changed to float | |
43 for (uint64 i = 0; i < params.distances.size(); i++) | |
44 distances.push_back(params.distances[i]); | |
picksi
2015/02/23 12:22:24
My STL is weak, but isn't there a way to do this w
ssid
2015/02/23 14:03:04
The copy of vector doesn't work because the object
| |
45 } | |
46 | |
47 SyntheticSmoothMoveGestureParams::SyntheticSmoothMoveGestureParams( | |
48 SyntheticSmoothDragGestureParams params) | |
49 : start_point(params.start_point), | |
50 distances(params.distances), | |
51 speed_in_pixels_s(params.speed_in_pixels_s), | |
52 prevent_fling(true), | |
53 prevent_slop(true) { | |
54 } | |
55 | |
56 SyntheticSmoothMoveGestureParams::~SyntheticSmoothMoveGestureParams() { | |
57 } | |
58 | |
59 SyntheticSmoothMoveGesture::SyntheticSmoothMoveGesture( | |
60 SyntheticSmoothMoveGestureParams params) | |
34 : params_(params), | 61 : params_(params), |
35 gesture_source_type_(SyntheticGestureParams::DEFAULT_INPUT), | 62 current_move_segment_start_position_(params.start_point), |
36 state_(SETUP) {} | 63 state_(SETUP) { |
64 } | |
37 | 65 |
38 SyntheticSmoothScrollGesture::~SyntheticSmoothScrollGesture() {} | 66 SyntheticSmoothMoveGesture::~SyntheticSmoothMoveGesture() { |
67 } | |
39 | 68 |
40 SyntheticGesture::Result SyntheticSmoothScrollGesture::ForwardInputEvents( | 69 SyntheticGesture::Result SyntheticSmoothMoveGesture::ForwardInputEvents( |
41 const base::TimeTicks& timestamp, SyntheticGestureTarget* target) { | 70 const base::TimeTicks& timestamp, |
71 SyntheticGestureTarget* target) { | |
42 if (state_ == SETUP) { | 72 if (state_ == SETUP) { |
43 gesture_source_type_ = params_.gesture_source_type; | |
44 if (gesture_source_type_ == SyntheticGestureParams::DEFAULT_INPUT) | |
45 gesture_source_type_ = target->GetDefaultSyntheticGestureSourceType(); | |
46 | |
47 state_ = STARTED; | 73 state_ = STARTED; |
48 current_scroll_segment_ = -1; | 74 current_move_segment_ = -1; |
49 current_scroll_segment_stop_time_ = timestamp; | 75 current_move_segment_stop_time_ = timestamp; |
50 } | 76 } |
51 | 77 |
picksi
2015/02/23 12:22:24
Should the code below be a switch?
ssid
2015/02/23 14:03:04
Done.
| |
52 DCHECK_NE(gesture_source_type_, SyntheticGestureParams::DEFAULT_INPUT); | 78 if (params_.input_type == SyntheticSmoothMoveGestureParams::TOUCH_INPUT) |
53 if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT) | |
54 ForwardTouchInputEvents(timestamp, target); | 79 ForwardTouchInputEvents(timestamp, target); |
55 else if (gesture_source_type_ == SyntheticGestureParams::MOUSE_INPUT) | 80 else if (params_.input_type == |
56 ForwardMouseInputEvents(timestamp, target); | 81 SyntheticSmoothMoveGestureParams::MOUSE_DRAG_INPUT) |
82 ForwardMouseClickInputEvents(timestamp, target); | |
83 else if (params_.input_type == | |
84 SyntheticSmoothMoveGestureParams::MOUSE_WHEEL_INPUT) | |
85 ForwardMouseWheelInputEvents(timestamp, target); | |
57 else | 86 else |
58 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; | 87 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; |
59 | 88 |
60 return (state_ == DONE) ? SyntheticGesture::GESTURE_FINISHED | 89 return (state_ == DONE) ? SyntheticGesture::GESTURE_FINISHED |
61 : SyntheticGesture::GESTURE_RUNNING; | 90 : SyntheticGesture::GESTURE_RUNNING; |
62 } | 91 } |
63 | 92 |
64 void SyntheticSmoothScrollGesture::ForwardTouchInputEvents( | 93 // TODO (ssid) clean up the switch statements by adding functions, instead of |
65 const base::TimeTicks& timestamp, SyntheticGestureTarget* target) { | 94 // large code, in the Forward*Events functions. Move the actions for all input |
95 // types to different class (SyntheticInputDevice) and in gesture events, use | |
96 // them. | |
97 | |
98 void SyntheticSmoothMoveGesture::ForwardTouchInputEvents( | |
99 const base::TimeTicks& timestamp, | |
100 SyntheticGestureTarget* target) { | |
66 base::TimeTicks event_timestamp = timestamp; | 101 base::TimeTicks event_timestamp = timestamp; |
67 switch (state_) { | 102 switch (state_) { |
68 case STARTED: | 103 case STARTED: |
69 if (ScrollIsNoOp()) { | 104 if (MoveIsNoOp()) { |
70 state_ = DONE; | 105 state_ = DONE; |
71 break; | 106 break; |
72 } | 107 } |
73 AddTouchSlopToFirstDistance(target); | 108 if (!params_.prevent_slop) |
picksi
2015/02/23 12:22:24
Do we need {} brackets for single line ifs? It wou
ssid
2015/02/23 19:10:36
I would prefer no brackets. Changed to add_slop.
| |
74 ComputeNextScrollSegment(); | 109 AddTouchSlopToFirstDistance(target); |
75 current_scroll_segment_start_position_ = params_.anchor; | 110 ComputeNextMoveSegment(); |
76 PressTouchPoint(target, event_timestamp); | 111 PressTouchPoint(target, event_timestamp); |
77 state_ = MOVING; | 112 state_ = MOVING; |
78 break; | 113 break; |
79 case MOVING: { | 114 case MOVING: { |
80 event_timestamp = ClampTimestamp(timestamp); | 115 event_timestamp = ClampTimestamp(timestamp); |
81 gfx::Vector2dF delta = GetPositionDeltaAtTime(event_timestamp); | 116 gfx::Vector2dF delta = GetPositionDeltaAtTime(event_timestamp); |
82 MoveTouchPoint(target, delta, event_timestamp); | 117 MoveTouchPoint(target, delta, event_timestamp); |
83 | 118 |
84 if (FinishedCurrentScrollSegment(event_timestamp)) { | 119 if (FinishedCurrentMoveSegment(event_timestamp)) { |
85 if (!IsLastScrollSegment()) { | 120 if (!IsLastMoveSegment()) { |
picksi
2015/02/23 12:22:24
Bikesheeding: This might be more readable as:
if
ssid
2015/02/23 19:10:36
Can be done in clean up Cl. Noted.
| |
86 current_scroll_segment_start_position_ += | 121 current_move_segment_start_position_ += |
87 params_.distances[current_scroll_segment_]; | 122 params_.distances[current_move_segment_]; |
88 ComputeNextScrollSegment(); | 123 ComputeNextMoveSegment(); |
89 } else if (params_.prevent_fling) { | 124 } else if (params_.prevent_fling) { |
90 state_ = STOPPING; | 125 state_ = STOPPING; |
91 } else { | 126 } else { |
92 ReleaseTouchPoint(target, event_timestamp); | 127 ReleaseTouchPoint(target, event_timestamp); |
93 state_ = DONE; | 128 state_ = DONE; |
94 } | 129 } |
95 } | 130 } |
96 } break; | 131 } break; |
97 case STOPPING: | 132 case STOPPING: |
98 if (timestamp - current_scroll_segment_stop_time_ >= | 133 if (timestamp - current_move_segment_stop_time_ >= |
99 target->PointerAssumedStoppedTime()) { | 134 target->PointerAssumedStoppedTime()) { |
100 event_timestamp = current_scroll_segment_stop_time_ + | 135 event_timestamp = current_move_segment_stop_time_ + |
101 target->PointerAssumedStoppedTime(); | 136 target->PointerAssumedStoppedTime(); |
102 ReleaseTouchPoint(target, event_timestamp); | 137 ReleaseTouchPoint(target, event_timestamp); |
103 state_ = DONE; | 138 state_ = DONE; |
104 } | 139 } |
105 break; | 140 break; |
106 case SETUP: | 141 case SETUP: |
107 NOTREACHED() | 142 NOTREACHED() |
108 << "State STARTED invalid for synthetic scroll using touch input."; | 143 << "State SETUP invalid for synthetic scroll using touch input."; |
109 case DONE: | 144 case DONE: |
110 NOTREACHED() | 145 NOTREACHED() |
111 << "State DONE invalid for synthetic scroll using touch input."; | 146 << "State DONE invalid for synthetic scroll using touch input."; |
112 } | 147 } |
113 } | 148 } |
114 | 149 |
115 void SyntheticSmoothScrollGesture::ForwardMouseInputEvents( | 150 void SyntheticSmoothMoveGesture::ForwardMouseWheelInputEvents( |
116 const base::TimeTicks& timestamp, SyntheticGestureTarget* target) { | 151 const base::TimeTicks& timestamp, |
152 SyntheticGestureTarget* target) { | |
117 switch (state_) { | 153 switch (state_) { |
118 case STARTED: | 154 case STARTED: |
119 if (ScrollIsNoOp()) { | 155 if (MoveIsNoOp()) { |
120 state_ = DONE; | 156 state_ = DONE; |
121 break; | 157 break; |
122 } | 158 } |
123 ComputeNextScrollSegment(); | 159 ComputeNextMoveSegment(); |
124 state_ = MOVING; | 160 state_ = MOVING; |
125 // Fall through to forward the first event. | 161 // Fall through to forward the first event. |
126 case MOVING: { | 162 case MOVING: { |
127 // Even though WebMouseWheelEvents take floating point deltas, | 163 // Even though WebMouseWheelEvents take floating point deltas, |
128 // internally the scroll position is stored as an integer. We therefore | 164 // internally the scroll position is stored as an integer. We therefore |
129 // keep track of the discrete delta which is consistent with the | 165 // keep track of the discrete delta which is consistent with the |
130 // internal scrolling state. This ensures that when the gesture has | 166 // internal scrolling state. This ensures that when the gesture has |
131 // finished we've scrolled exactly the specified distance. | 167 // finished we've scrolled exactly the specified distance. |
132 base::TimeTicks event_timestamp = ClampTimestamp(timestamp); | 168 base::TimeTicks event_timestamp = ClampTimestamp(timestamp); |
133 gfx::Vector2dF current_scroll_segment_total_delta = | 169 gfx::Vector2dF current_move_segment_total_delta = |
134 GetPositionDeltaAtTime(event_timestamp); | 170 GetPositionDeltaAtTime(event_timestamp); |
135 gfx::Vector2d delta_discrete = | 171 gfx::Vector2d delta_discrete = |
136 FloorTowardZero(current_scroll_segment_total_delta - | 172 FloorTowardZero(current_move_segment_total_delta - |
137 current_scroll_segment_total_delta_discrete_); | 173 current_move_segment_total_delta_discrete_); |
138 ForwardMouseWheelEvent(target, delta_discrete, event_timestamp); | 174 ForwardMouseWheelEvent(target, delta_discrete, event_timestamp); |
139 current_scroll_segment_total_delta_discrete_ += delta_discrete; | 175 current_move_segment_total_delta_discrete_ += delta_discrete; |
140 | 176 |
141 if (FinishedCurrentScrollSegment(event_timestamp)) { | 177 if (FinishedCurrentMoveSegment(event_timestamp)) { |
142 if (!IsLastScrollSegment()) { | 178 if (!IsLastMoveSegment()) { |
143 current_scroll_segment_total_delta_discrete_ = gfx::Vector2d(); | 179 current_move_segment_total_delta_discrete_ = gfx::Vector2d(); |
144 ComputeNextScrollSegment(); | 180 ComputeNextMoveSegment(); |
145 ForwardMouseInputEvents(timestamp, target); | 181 ForwardMouseWheelInputEvents(timestamp, target); |
146 } else { | 182 } else { |
147 state_ = DONE; | 183 state_ = DONE; |
148 } | 184 } |
149 } | 185 } |
150 } break; | 186 } break; |
151 case SETUP: | 187 case SETUP: |
152 NOTREACHED() | 188 NOTREACHED() << "State SETUP invalid for synthetic scroll using mouse " |
153 << "State STARTED invalid for synthetic scroll using touch input."; | 189 "wheel input."; |
154 case STOPPING: | 190 case STOPPING: |
155 NOTREACHED() | 191 NOTREACHED() << "State STOPPING invalid for synthetic scroll using mouse " |
156 << "State STOPPING invalid for synthetic scroll using touch input."; | 192 "wheel input."; |
157 case DONE: | 193 case DONE: |
158 NOTREACHED() | 194 NOTREACHED() |
159 << "State DONE invalid for synthetic scroll using touch input."; | 195 << "State DONE invalid for synthetic scroll using mouse wheel input."; |
160 } | 196 } |
161 } | 197 } |
162 | 198 |
163 void SyntheticSmoothScrollGesture::ForwardTouchEvent( | 199 void SyntheticSmoothMoveGesture::ForwardMouseClickInputEvents( |
164 SyntheticGestureTarget* target, const base::TimeTicks& timestamp) { | 200 const base::TimeTicks& timestamp, |
201 SyntheticGestureTarget* target) { | |
202 base::TimeTicks event_timestamp = timestamp; | |
203 switch (state_) { | |
204 case STARTED: | |
205 if (MoveIsNoOp()) { | |
206 state_ = DONE; | |
207 break; | |
208 } | |
209 ComputeNextMoveSegment(); | |
210 PressMousePoint(target, event_timestamp); | |
211 state_ = MOVING; | |
212 // Fall through to forward the first event. | |
213 case MOVING: { | |
214 base::TimeTicks event_timestamp = ClampTimestamp(timestamp); | |
215 gfx::Vector2dF delta = GetPositionDeltaAtTime(event_timestamp); | |
216 MoveMousePoint(target, delta, event_timestamp); | |
217 | |
218 if (FinishedCurrentMoveSegment(event_timestamp)) { | |
219 if (!IsLastMoveSegment()) { | |
picksi
2015/02/23 12:22:24
See my comment above about '!' and swapping if/els
| |
220 current_move_segment_start_position_ += | |
221 params_.distances[current_move_segment_]; | |
222 ComputeNextMoveSegment(); | |
223 } else { | |
224 ReleaseMousePoint(target, event_timestamp); | |
225 state_ = DONE; | |
226 } | |
227 } | |
228 } break; | |
229 case STOPPING: | |
230 NOTREACHED() | |
231 << "State STOPPING invalid for synthetic drag using mouse input."; | |
232 case SETUP: | |
233 NOTREACHED() | |
234 << "State SETUP invalid for synthetic drag using mouse input."; | |
235 case DONE: | |
236 NOTREACHED() | |
237 << "State DONE invalid for synthetic drag using mouse input."; | |
238 } | |
239 } | |
240 | |
241 void SyntheticSmoothMoveGesture::ForwardTouchEvent( | |
242 SyntheticGestureTarget* target, | |
243 const base::TimeTicks& timestamp) { | |
165 touch_event_.timeStampSeconds = ConvertTimestampToSeconds(timestamp); | 244 touch_event_.timeStampSeconds = ConvertTimestampToSeconds(timestamp); |
166 | 245 |
167 target->DispatchInputEventToPlatform(touch_event_); | 246 target->DispatchInputEventToPlatform(touch_event_); |
168 } | 247 } |
169 | 248 |
170 void SyntheticSmoothScrollGesture::ForwardMouseWheelEvent( | 249 void SyntheticSmoothMoveGesture::ForwardMouseWheelEvent( |
171 SyntheticGestureTarget* target, | 250 SyntheticGestureTarget* target, |
172 const gfx::Vector2dF& delta, | 251 const gfx::Vector2dF& delta, |
173 const base::TimeTicks& timestamp) const { | 252 const base::TimeTicks& timestamp) const { |
174 blink::WebMouseWheelEvent mouse_wheel_event = | 253 blink::WebMouseWheelEvent mouse_wheel_event = |
175 SyntheticWebMouseWheelEventBuilder::Build(delta.x(), delta.y(), 0, false); | 254 SyntheticWebMouseWheelEventBuilder::Build(delta.x(), delta.y(), 0, false); |
176 | 255 |
177 mouse_wheel_event.x = params_.anchor.x(); | 256 mouse_wheel_event.x = current_move_segment_start_position_.x(); |
178 mouse_wheel_event.y = params_.anchor.y(); | 257 mouse_wheel_event.y = current_move_segment_start_position_.y(); |
179 | 258 |
180 mouse_wheel_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp); | 259 mouse_wheel_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp); |
181 | 260 |
182 target->DispatchInputEventToPlatform(mouse_wheel_event); | 261 target->DispatchInputEventToPlatform(mouse_wheel_event); |
183 } | 262 } |
184 | 263 |
185 void SyntheticSmoothScrollGesture::PressTouchPoint( | 264 void SyntheticSmoothMoveGesture::PressTouchPoint( |
186 SyntheticGestureTarget* target, const base::TimeTicks& timestamp) { | 265 SyntheticGestureTarget* target, |
187 DCHECK_EQ(current_scroll_segment_, 0); | 266 const base::TimeTicks& timestamp) { |
188 touch_event_.PressPoint(params_.anchor.x(), params_.anchor.y()); | 267 DCHECK_EQ(current_move_segment_, 0); |
268 touch_event_.PressPoint(current_move_segment_start_position_.x(), | |
269 current_move_segment_start_position_.y()); | |
189 ForwardTouchEvent(target, timestamp); | 270 ForwardTouchEvent(target, timestamp); |
190 } | 271 } |
191 | 272 |
192 void SyntheticSmoothScrollGesture::MoveTouchPoint( | 273 void SyntheticSmoothMoveGesture::MoveTouchPoint( |
193 SyntheticGestureTarget* target, | 274 SyntheticGestureTarget* target, |
194 const gfx::Vector2dF& delta, | 275 const gfx::Vector2dF& delta, |
195 const base::TimeTicks& timestamp) { | 276 const base::TimeTicks& timestamp) { |
196 DCHECK_GE(current_scroll_segment_, 0); | 277 DCHECK_GE(current_move_segment_, 0); |
197 DCHECK_LT(current_scroll_segment_, | 278 DCHECK_LT(current_move_segment_, static_cast<int>(params_.distances.size())); |
198 static_cast<int>(params_.distances.size())); | 279 gfx::PointF touch_position = current_move_segment_start_position_ + delta; |
199 gfx::PointF touch_position = current_scroll_segment_start_position_ + delta; | |
200 touch_event_.MovePoint(0, touch_position.x(), touch_position.y()); | 280 touch_event_.MovePoint(0, touch_position.x(), touch_position.y()); |
201 ForwardTouchEvent(target, timestamp); | 281 ForwardTouchEvent(target, timestamp); |
202 } | 282 } |
203 | 283 |
204 void SyntheticSmoothScrollGesture::ReleaseTouchPoint( | 284 void SyntheticSmoothMoveGesture::ReleaseTouchPoint( |
205 SyntheticGestureTarget* target, const base::TimeTicks& timestamp) { | 285 SyntheticGestureTarget* target, |
206 DCHECK_EQ(current_scroll_segment_, | 286 const base::TimeTicks& timestamp) { |
287 DCHECK_EQ(current_move_segment_, | |
207 static_cast<int>(params_.distances.size()) - 1); | 288 static_cast<int>(params_.distances.size()) - 1); |
208 touch_event_.ReleasePoint(0); | 289 touch_event_.ReleasePoint(0); |
209 ForwardTouchEvent(target, timestamp); | 290 ForwardTouchEvent(target, timestamp); |
210 } | 291 } |
211 | 292 |
212 void SyntheticSmoothScrollGesture::AddTouchSlopToFirstDistance( | 293 void SyntheticSmoothMoveGesture::PressMousePoint( |
294 SyntheticGestureTarget* target, | |
295 const base::TimeTicks& timestamp) { | |
296 DCHECK(params_.input_type == | |
297 SyntheticSmoothMoveGestureParams::MOUSE_DRAG_INPUT); | |
298 blink::WebMouseEvent mouse_event = SyntheticWebMouseEventBuilder::Build( | |
299 blink::WebInputEvent::MouseDown, current_move_segment_start_position_.x(), | |
300 current_move_segment_start_position_.y(), 0); | |
301 mouse_event.clickCount = 1; | |
302 mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp); | |
303 target->DispatchInputEventToPlatform(mouse_event); | |
304 } | |
305 | |
306 void SyntheticSmoothMoveGesture::ReleaseMousePoint( | |
307 SyntheticGestureTarget* target, | |
308 const base::TimeTicks& timestamp) { | |
309 DCHECK(params_.input_type == | |
310 SyntheticSmoothMoveGestureParams::MOUSE_DRAG_INPUT); | |
311 gfx::PointF mouse_position = | |
312 current_move_segment_start_position_ + GetPositionDeltaAtTime(timestamp); | |
313 blink::WebMouseEvent mouse_event = SyntheticWebMouseEventBuilder::Build( | |
314 blink::WebInputEvent::MouseUp, mouse_position.x(), mouse_position.y(), 0); | |
315 mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp); | |
316 target->DispatchInputEventToPlatform(mouse_event); | |
317 } | |
318 | |
319 void SyntheticSmoothMoveGesture::MoveMousePoint( | |
320 SyntheticGestureTarget* target, | |
321 const gfx::Vector2dF& delta, | |
322 const base::TimeTicks& timestamp) { | |
323 gfx::PointF mouse_position = current_move_segment_start_position_ + delta; | |
324 DCHECK(params_.input_type == | |
325 SyntheticSmoothMoveGestureParams::MOUSE_DRAG_INPUT); | |
326 blink::WebMouseEvent mouse_event = SyntheticWebMouseEventBuilder::Build( | |
327 blink::WebInputEvent::MouseMove, mouse_position.x(), mouse_position.y(), | |
328 0); | |
329 mouse_event.button = blink::WebMouseEvent::ButtonLeft; | |
330 mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp); | |
331 target->DispatchInputEventToPlatform(mouse_event); | |
332 } | |
333 | |
334 void SyntheticSmoothMoveGesture::AddTouchSlopToFirstDistance( | |
213 SyntheticGestureTarget* target) { | 335 SyntheticGestureTarget* target) { |
214 DCHECK_GE(params_.distances.size(), 1ul); | 336 DCHECK_GE(params_.distances.size(), 1ul); |
215 gfx::Vector2d& first_scroll_distance = params_.distances[0]; | 337 gfx::Vector2dF& first_move_distance = params_.distances[0]; |
216 DCHECK_GT(first_scroll_distance.Length(), 0); | 338 DCHECK_GT(first_move_distance.Length(), 0); |
217 first_scroll_distance += CeilFromZero(ProjectScalarOntoVector( | 339 first_move_distance += CeilFromZero(ProjectScalarOntoVector( |
218 target->GetTouchSlopInDips(), first_scroll_distance)); | 340 target->GetTouchSlopInDips(), first_move_distance)); |
219 } | 341 } |
220 | 342 |
221 gfx::Vector2dF SyntheticSmoothScrollGesture::GetPositionDeltaAtTime( | 343 gfx::Vector2dF SyntheticSmoothMoveGesture::GetPositionDeltaAtTime( |
222 const base::TimeTicks& timestamp) const { | 344 const base::TimeTicks& timestamp) const { |
223 // Make sure the final delta is correct. Using the computation below can lead | 345 // Make sure the final delta is correct. Using the computation below can lead |
224 // to issues with floating point precision. | 346 // to issues with floating point precision. |
225 if (FinishedCurrentScrollSegment(timestamp)) | 347 if (FinishedCurrentMoveSegment(timestamp)) |
226 return params_.distances[current_scroll_segment_]; | 348 return params_.distances[current_move_segment_]; |
227 | 349 |
228 float delta_length = | 350 float delta_length = |
229 params_.speed_in_pixels_s * | 351 params_.speed_in_pixels_s * |
230 (timestamp - current_scroll_segment_start_time_).InSecondsF(); | 352 (timestamp - current_move_segment_start_time_).InSecondsF(); |
231 return ProjectScalarOntoVector(delta_length, | 353 return ProjectScalarOntoVector(delta_length, |
232 params_.distances[current_scroll_segment_]); | 354 params_.distances[current_move_segment_]); |
233 } | 355 } |
234 | 356 |
235 void SyntheticSmoothScrollGesture::ComputeNextScrollSegment() { | 357 void SyntheticSmoothMoveGesture::ComputeNextMoveSegment() { |
236 current_scroll_segment_++; | 358 current_move_segment_++; |
237 DCHECK_LT(current_scroll_segment_, | 359 DCHECK_LT(current_move_segment_, static_cast<int>(params_.distances.size())); |
238 static_cast<int>(params_.distances.size())); | |
239 int64 total_duration_in_us = static_cast<int64>( | 360 int64 total_duration_in_us = static_cast<int64>( |
240 1e6 * (params_.distances[current_scroll_segment_].Length() / | 361 1e6 * (params_.distances[current_move_segment_].Length() / |
241 params_.speed_in_pixels_s)); | 362 params_.speed_in_pixels_s)); |
242 DCHECK_GT(total_duration_in_us, 0); | 363 DCHECK_GT(total_duration_in_us, 0); |
243 current_scroll_segment_start_time_ = current_scroll_segment_stop_time_; | 364 current_move_segment_start_time_ = current_move_segment_stop_time_; |
244 current_scroll_segment_stop_time_ = | 365 current_move_segment_stop_time_ = |
245 current_scroll_segment_start_time_ + | 366 current_move_segment_start_time_ + |
246 base::TimeDelta::FromMicroseconds(total_duration_in_us); | 367 base::TimeDelta::FromMicroseconds(total_duration_in_us); |
247 } | 368 } |
248 | 369 |
249 base::TimeTicks SyntheticSmoothScrollGesture::ClampTimestamp( | 370 base::TimeTicks SyntheticSmoothMoveGesture::ClampTimestamp( |
250 const base::TimeTicks& timestamp) const { | 371 const base::TimeTicks& timestamp) const { |
251 return std::min(timestamp, current_scroll_segment_stop_time_); | 372 return std::min(timestamp, current_move_segment_stop_time_); |
252 } | 373 } |
253 | 374 |
254 bool SyntheticSmoothScrollGesture::FinishedCurrentScrollSegment( | 375 bool SyntheticSmoothMoveGesture::FinishedCurrentMoveSegment( |
255 const base::TimeTicks& timestamp) const { | 376 const base::TimeTicks& timestamp) const { |
256 return timestamp >= current_scroll_segment_stop_time_; | 377 return timestamp >= current_move_segment_stop_time_; |
257 } | 378 } |
258 | 379 |
259 bool SyntheticSmoothScrollGesture::IsLastScrollSegment() const { | 380 bool SyntheticSmoothMoveGesture::IsLastMoveSegment() const { |
260 DCHECK_LT(current_scroll_segment_, | 381 DCHECK_LT(current_move_segment_, static_cast<int>(params_.distances.size())); |
261 static_cast<int>(params_.distances.size())); | 382 return current_move_segment_ == |
262 return current_scroll_segment_ == | |
263 static_cast<int>(params_.distances.size()) - 1; | 383 static_cast<int>(params_.distances.size()) - 1; |
264 } | 384 } |
265 | 385 |
266 bool SyntheticSmoothScrollGesture::ScrollIsNoOp() const { | 386 bool SyntheticSmoothMoveGesture::MoveIsNoOp() const { |
267 return params_.distances.size() == 0 || params_.distances[0].IsZero(); | 387 return params_.distances.size() == 0 || params_.distances[0].IsZero(); |
268 } | 388 } |
269 | 389 |
270 } // namespace content | 390 } // namespace content |
OLD | NEW |