OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/overscroll_controller.h" | 5 #include "content/browser/renderer_host/overscroll_controller.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/browser/renderer_host/overscroll_controller_delegate.h" | 9 #include "content/browser/renderer_host/overscroll_controller_delegate.h" |
| 10 #include "content/common/input/input_event_utils.h" |
10 #include "content/public/browser/overscroll_configuration.h" | 11 #include "content/public/browser/overscroll_configuration.h" |
11 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" |
12 | 13 |
13 using blink::WebInputEvent; | 14 using blink::WebInputEvent; |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 bool IsScrollEndEffectEnabled() { | 18 bool IsScrollEndEffectEnabled() { |
18 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 19 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
19 switches::kScrollEndEffect) == "1"; | 20 switches::kScrollEndEffect) == "1"; |
20 } | 21 } |
21 | 22 |
| 23 bool IsGestureEventFromTouchpad(const blink::WebInputEvent& event) { |
| 24 DCHECK(blink::WebInputEvent::isGestureEventType(event.type)); |
| 25 const blink::WebGestureEvent& gesture = |
| 26 static_cast<const blink::WebGestureEvent&>(event); |
| 27 return gesture.sourceDevice == blink::WebGestureDeviceTouchpad; |
| 28 } |
| 29 |
22 } // namespace | 30 } // namespace |
23 | 31 |
24 namespace content { | 32 namespace content { |
25 | 33 |
26 OverscrollController::OverscrollController() | 34 OverscrollController::OverscrollController() |
27 : overscroll_mode_(OVERSCROLL_NONE), | 35 : overscroll_mode_(OVERSCROLL_NONE), |
28 scroll_state_(STATE_UNKNOWN), | 36 scroll_state_(STATE_UNKNOWN), |
29 overscroll_delta_x_(0.f), | 37 overscroll_delta_x_(0.f), |
30 overscroll_delta_y_(0.f), | 38 overscroll_delta_y_(0.f), |
31 delegate_(NULL) { | 39 delegate_(NULL), |
32 } | 40 use_gesture_wheel_scrolling_(UseGestureBasedWheelScrolling()) {} |
33 | 41 |
34 OverscrollController::~OverscrollController() { | 42 OverscrollController::~OverscrollController() { |
35 } | 43 } |
36 | 44 |
| 45 bool OverscrollController::ShouldProcessEvent( |
| 46 const blink::WebInputEvent& event) { |
| 47 if (use_gesture_wheel_scrolling_) { |
| 48 switch (event.type) { |
| 49 case blink::WebInputEvent::MouseWheel: |
| 50 return false; |
| 51 case blink::WebInputEvent::GestureScrollBegin: |
| 52 case blink::WebInputEvent::GestureScrollUpdate: |
| 53 case blink::WebInputEvent::GestureScrollEnd: { |
| 54 const blink::WebGestureEvent& gesture = |
| 55 static_cast<const blink::WebGestureEvent&>(event); |
| 56 if (gesture.sourceDevice == blink::WebGestureDeviceTouchpad) |
| 57 return true; |
| 58 blink::WebGestureEvent::ScrollUnits scrollUnits; |
| 59 switch (event.type) { |
| 60 case blink::WebInputEvent::GestureScrollBegin: |
| 61 scrollUnits = gesture.data.scrollBegin.deltaHintUnits; |
| 62 break; |
| 63 case blink::WebInputEvent::GestureScrollUpdate: |
| 64 scrollUnits = gesture.data.scrollUpdate.deltaUnits; |
| 65 break; |
| 66 case blink::WebInputEvent::GestureScrollEnd: |
| 67 scrollUnits = gesture.data.scrollEnd.deltaUnits; |
| 68 break; |
| 69 default: |
| 70 scrollUnits = blink::WebGestureEvent::Pixels; |
| 71 break; |
| 72 } |
| 73 |
| 74 return scrollUnits == blink::WebGestureEvent::PrecisePixels; |
| 75 } |
| 76 default: |
| 77 break; |
| 78 } |
| 79 } |
| 80 return true; |
| 81 } |
| 82 |
37 bool OverscrollController::WillHandleEvent(const blink::WebInputEvent& event) { | 83 bool OverscrollController::WillHandleEvent(const blink::WebInputEvent& event) { |
| 84 if (!ShouldProcessEvent(event)) |
| 85 return false; |
| 86 |
38 bool reset_scroll_state = false; | 87 bool reset_scroll_state = false; |
39 if (scroll_state_ != STATE_UNKNOWN || | 88 if (scroll_state_ != STATE_UNKNOWN || |
40 overscroll_delta_x_ || overscroll_delta_y_) { | 89 overscroll_delta_x_ || overscroll_delta_y_) { |
41 switch (event.type) { | 90 switch (event.type) { |
42 case blink::WebInputEvent::GestureScrollEnd: | 91 case blink::WebInputEvent::GestureScrollEnd: |
| 92 // Avoid resetting the state on GestureScrollEnd generated |
| 93 // from the touchpad since it is sent based on a timeout. |
| 94 reset_scroll_state = !IsGestureEventFromTouchpad(event); |
| 95 break; |
| 96 |
43 case blink::WebInputEvent::GestureFlingStart: | 97 case blink::WebInputEvent::GestureFlingStart: |
44 reset_scroll_state = true; | 98 reset_scroll_state = true; |
45 break; | 99 break; |
46 | 100 |
47 case blink::WebInputEvent::MouseWheel: { | 101 case blink::WebInputEvent::MouseWheel: { |
48 const blink::WebMouseWheelEvent& wheel = | 102 const blink::WebMouseWheelEvent& wheel = |
49 static_cast<const blink::WebMouseWheelEvent&>(event); | 103 static_cast<const blink::WebMouseWheelEvent&>(event); |
50 if (!wheel.hasPreciseScrollingDeltas || | 104 if (!wheel.hasPreciseScrollingDeltas || |
51 wheel.phase == blink::WebMouseWheelEvent::PhaseEnded || | 105 wheel.phase == blink::WebMouseWheelEvent::PhaseEnded || |
52 wheel.phase == blink::WebMouseWheelEvent::PhaseCancelled) { | 106 wheel.phase == blink::WebMouseWheelEvent::PhaseCancelled) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } else if (reset_scroll_state) { | 142 } else if (reset_scroll_state) { |
89 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 143 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
90 } | 144 } |
91 | 145 |
92 | 146 |
93 return false; | 147 return false; |
94 } | 148 } |
95 | 149 |
96 void OverscrollController::ReceivedEventACK(const blink::WebInputEvent& event, | 150 void OverscrollController::ReceivedEventACK(const blink::WebInputEvent& event, |
97 bool processed) { | 151 bool processed) { |
| 152 if (!ShouldProcessEvent(event)) |
| 153 return; |
| 154 |
98 if (processed) { | 155 if (processed) { |
99 // If a scroll event is consumed by the page, i.e. some content on the page | 156 // If a scroll event is consumed by the page, i.e. some content on the page |
100 // has been scrolled, then there is not going to be an overscroll gesture, | 157 // has been scrolled, then there is not going to be an overscroll gesture, |
101 // until the current scroll ends, and a new scroll gesture starts. | 158 // until the current scroll ends, and a new scroll gesture starts. |
102 if (scroll_state_ == STATE_UNKNOWN && | 159 if (scroll_state_ == STATE_UNKNOWN && |
103 (event.type == blink::WebInputEvent::MouseWheel || | 160 (event.type == blink::WebInputEvent::MouseWheel || |
104 event.type == blink::WebInputEvent::GestureScrollUpdate)) { | 161 event.type == blink::WebInputEvent::GestureScrollUpdate)) { |
105 scroll_state_ = STATE_CONTENT_SCROLLING; | 162 scroll_state_ = STATE_CONTENT_SCROLLING; |
106 } | 163 } |
107 return; | 164 return; |
(...skipping 27 matching lines...) Expand all Loading... |
135 if (overscroll_mode_ == OVERSCROLL_NONE) | 192 if (overscroll_mode_ == OVERSCROLL_NONE) |
136 return false; | 193 return false; |
137 | 194 |
138 // Complete the overscroll gesture if there was a mouse move or a scroll-end | 195 // Complete the overscroll gesture if there was a mouse move or a scroll-end |
139 // after the threshold. | 196 // after the threshold. |
140 if (event.type != blink::WebInputEvent::MouseMove && | 197 if (event.type != blink::WebInputEvent::MouseMove && |
141 event.type != blink::WebInputEvent::GestureScrollEnd && | 198 event.type != blink::WebInputEvent::GestureScrollEnd && |
142 event.type != blink::WebInputEvent::GestureFlingStart) | 199 event.type != blink::WebInputEvent::GestureFlingStart) |
143 return false; | 200 return false; |
144 | 201 |
| 202 // Avoid completing the action on GestureScrollEnd generated |
| 203 // from the touchpad since it is sent based on a timeout not |
| 204 // when the user has stopped interacting. |
| 205 if (event.type == blink::WebInputEvent::GestureScrollEnd && |
| 206 IsGestureEventFromTouchpad(event)) |
| 207 return false; |
| 208 |
145 if (!delegate_) | 209 if (!delegate_) |
146 return false; | 210 return false; |
147 | 211 |
148 gfx::Rect bounds = delegate_->GetVisibleBounds(); | 212 gfx::Rect bounds = delegate_->GetVisibleBounds(); |
149 if (bounds.IsEmpty()) | 213 if (bounds.IsEmpty()) |
150 return false; | 214 return false; |
151 | 215 |
152 if (event.type == blink::WebInputEvent::GestureFlingStart) { | 216 if (event.type == blink::WebInputEvent::GestureFlingStart) { |
153 // Check to see if the fling is in the same direction of the overscroll. | 217 // Check to see if the fling is in the same direction of the overscroll. |
154 const blink::WebGestureEvent gesture = | 218 const blink::WebGestureEvent gesture = |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 const blink::WebInputEvent& event) const { | 256 const blink::WebInputEvent& event) const { |
193 switch (event.type) { | 257 switch (event.type) { |
194 case blink::WebInputEvent::MouseWheel: { | 258 case blink::WebInputEvent::MouseWheel: { |
195 // Only wheel events with precise deltas (i.e. from trackpad) contribute | 259 // Only wheel events with precise deltas (i.e. from trackpad) contribute |
196 // to the overscroll gesture. | 260 // to the overscroll gesture. |
197 const blink::WebMouseWheelEvent& wheel = | 261 const blink::WebMouseWheelEvent& wheel = |
198 static_cast<const blink::WebMouseWheelEvent&>(event); | 262 static_cast<const blink::WebMouseWheelEvent&>(event); |
199 return !wheel.hasPreciseScrollingDeltas; | 263 return !wheel.hasPreciseScrollingDeltas; |
200 } | 264 } |
201 | 265 |
| 266 // Avoid resetting overscroll on GestureScrollBegin/End generated |
| 267 // from the touchpad since it is sent based on a timeout. |
| 268 case blink::WebInputEvent::GestureScrollBegin: |
| 269 case blink::WebInputEvent::GestureScrollEnd: |
| 270 return !IsGestureEventFromTouchpad(event); |
| 271 |
202 case blink::WebInputEvent::GestureScrollUpdate: | 272 case blink::WebInputEvent::GestureScrollUpdate: |
203 case blink::WebInputEvent::GestureFlingCancel: | 273 case blink::WebInputEvent::GestureFlingCancel: |
204 return false; | 274 return false; |
205 | 275 |
206 default: | 276 default: |
207 // Touch events can arrive during an overscroll gesture initiated by | 277 // Touch events can arrive during an overscroll gesture initiated by |
208 // touch-scrolling. These events should not reset the overscroll state. | 278 // touch-scrolling. These events should not reset the overscroll state. |
209 return !blink::WebInputEvent::isTouchEventType(event.type); | 279 return !blink::WebInputEvent::isTouchEventType(event.type); |
210 } | 280 } |
211 } | 281 } |
212 | 282 |
213 bool OverscrollController::ProcessEventForOverscroll( | 283 bool OverscrollController::ProcessEventForOverscroll( |
214 const blink::WebInputEvent& event) { | 284 const blink::WebInputEvent& event) { |
215 bool event_processed = false; | 285 bool event_processed = false; |
216 switch (event.type) { | 286 switch (event.type) { |
217 case blink::WebInputEvent::MouseWheel: { | 287 case blink::WebInputEvent::MouseWheel: { |
218 const blink::WebMouseWheelEvent& wheel = | 288 const blink::WebMouseWheelEvent& wheel = |
219 static_cast<const blink::WebMouseWheelEvent&>(event); | 289 static_cast<const blink::WebMouseWheelEvent&>(event); |
220 if (!wheel.hasPreciseScrollingDeltas) | 290 if (!wheel.hasPreciseScrollingDeltas) |
221 break; | 291 break; |
222 event_processed = | 292 event_processed = |
223 ProcessOverscroll(wheel.deltaX * wheel.accelerationRatioX, | 293 ProcessOverscroll(wheel.deltaX * wheel.accelerationRatioX, |
224 wheel.deltaY * wheel.accelerationRatioY, | 294 wheel.deltaY * wheel.accelerationRatioY, true); |
225 wheel.type); | |
226 break; | 295 break; |
227 } | 296 } |
228 case blink::WebInputEvent::GestureScrollUpdate: { | 297 case blink::WebInputEvent::GestureScrollUpdate: { |
229 const blink::WebGestureEvent& gesture = | 298 const blink::WebGestureEvent& gesture = |
230 static_cast<const blink::WebGestureEvent&>(event); | 299 static_cast<const blink::WebGestureEvent&>(event); |
231 event_processed = ProcessOverscroll(gesture.data.scrollUpdate.deltaX, | 300 event_processed = ProcessOverscroll( |
232 gesture.data.scrollUpdate.deltaY, | 301 gesture.data.scrollUpdate.deltaX, gesture.data.scrollUpdate.deltaY, |
233 gesture.type); | 302 gesture.sourceDevice == blink::WebGestureDeviceTouchpad); |
234 break; | 303 break; |
235 } | 304 } |
236 case blink::WebInputEvent::GestureFlingStart: { | 305 case blink::WebInputEvent::GestureFlingStart: { |
237 const float kFlingVelocityThreshold = 1100.f; | 306 const float kFlingVelocityThreshold = 1100.f; |
238 const blink::WebGestureEvent& gesture = | 307 const blink::WebGestureEvent& gesture = |
239 static_cast<const blink::WebGestureEvent&>(event); | 308 static_cast<const blink::WebGestureEvent&>(event); |
240 float velocity_x = gesture.data.flingStart.velocityX; | 309 float velocity_x = gesture.data.flingStart.velocityX; |
241 float velocity_y = gesture.data.flingStart.velocityY; | 310 float velocity_y = gesture.data.flingStart.velocityY; |
242 if (fabs(velocity_x) > kFlingVelocityThreshold) { | 311 if (fabs(velocity_x) > kFlingVelocityThreshold) { |
243 if ((overscroll_mode_ == OVERSCROLL_WEST && velocity_x < 0) || | 312 if ((overscroll_mode_ == OVERSCROLL_WEST && velocity_x < 0) || |
(...skipping 19 matching lines...) Expand all Loading... |
263 default: | 332 default: |
264 DCHECK(blink::WebInputEvent::isGestureEventType(event.type) || | 333 DCHECK(blink::WebInputEvent::isGestureEventType(event.type) || |
265 blink::WebInputEvent::isTouchEventType(event.type)) | 334 blink::WebInputEvent::isTouchEventType(event.type)) |
266 << "Received unexpected event: " << event.type; | 335 << "Received unexpected event: " << event.type; |
267 } | 336 } |
268 return event_processed; | 337 return event_processed; |
269 } | 338 } |
270 | 339 |
271 bool OverscrollController::ProcessOverscroll(float delta_x, | 340 bool OverscrollController::ProcessOverscroll(float delta_x, |
272 float delta_y, | 341 float delta_y, |
273 blink::WebInputEvent::Type type) { | 342 bool is_touchpad) { |
274 if (scroll_state_ != STATE_CONTENT_SCROLLING) | 343 if (scroll_state_ != STATE_CONTENT_SCROLLING) |
275 overscroll_delta_x_ += delta_x; | 344 overscroll_delta_x_ += delta_x; |
276 overscroll_delta_y_ += delta_y; | 345 overscroll_delta_y_ += delta_y; |
277 | 346 |
278 float horiz_threshold = GetOverscrollConfig( | 347 float horiz_threshold = GetOverscrollConfig( |
279 WebInputEvent::isGestureEventType(type) ? | 348 is_touchpad ? OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD |
280 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN : | 349 : OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN); |
281 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD); | |
282 float vert_threshold = GetOverscrollConfig( | 350 float vert_threshold = GetOverscrollConfig( |
283 OVERSCROLL_CONFIG_VERT_THRESHOLD_START); | 351 OVERSCROLL_CONFIG_VERT_THRESHOLD_START); |
284 if (fabs(overscroll_delta_x_) <= horiz_threshold && | 352 if (fabs(overscroll_delta_x_) <= horiz_threshold && |
285 fabs(overscroll_delta_y_) <= vert_threshold) { | 353 fabs(overscroll_delta_y_) <= vert_threshold) { |
286 SetOverscrollMode(OVERSCROLL_NONE); | 354 SetOverscrollMode(OVERSCROLL_NONE); |
287 return true; | 355 return true; |
288 } | 356 } |
289 | 357 |
290 // Compute the current overscroll direction. If the direction is different | 358 // Compute the current overscroll direction. If the direction is different |
291 // from the current direction, then always switch to no-overscroll mode first | 359 // from the current direction, then always switch to no-overscroll mode first |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 overscroll_mode_ = mode; | 424 overscroll_mode_ = mode; |
357 if (overscroll_mode_ == OVERSCROLL_NONE) | 425 if (overscroll_mode_ == OVERSCROLL_NONE) |
358 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 426 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
359 else | 427 else |
360 scroll_state_ = STATE_OVERSCROLLING; | 428 scroll_state_ = STATE_OVERSCROLLING; |
361 if (delegate_) | 429 if (delegate_) |
362 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_); | 430 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_); |
363 } | 431 } |
364 | 432 |
365 } // namespace content | 433 } // namespace content |
OLD | NEW |