| 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/public/browser/overscroll_configuration.h" | 10 #include "content/public/browser/overscroll_configuration.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 scroll_state_ = STATE_UNKNOWN; | 117 scroll_state_ = STATE_UNKNOWN; |
| 118 | 118 |
| 119 if (DispatchEventCompletesAction(event)) { | 119 if (DispatchEventCompletesAction(event)) { |
| 120 CompleteAction(); | 120 CompleteAction(); |
| 121 | 121 |
| 122 // Let the event be dispatched to the renderer. | 122 // Let the event be dispatched to the renderer. |
| 123 return false; | 123 return false; |
| 124 } | 124 } |
| 125 | 125 |
| 126 if (overscroll_mode_ != OVERSCROLL_NONE && DispatchEventResetsState(event)) { | 126 if (overscroll_mode_ != OVERSCROLL_NONE && DispatchEventResetsState(event)) { |
| 127 SetOverscrollMode(OVERSCROLL_NONE); | 127 SetOverscrollMode(OVERSCROLL_NONE, OverscrollSource::NONE); |
| 128 | 128 |
| 129 // Let the event be dispatched to the renderer. | 129 // Let the event be dispatched to the renderer. |
| 130 return false; | 130 return false; |
| 131 } | 131 } |
| 132 | 132 |
| 133 if (overscroll_mode_ != OVERSCROLL_NONE) { | 133 if (overscroll_mode_ != OVERSCROLL_NONE) { |
| 134 // Consume the event only if it updates the overscroll state. | 134 // Consume the event only if it updates the overscroll state. |
| 135 if (ProcessEventForOverscroll(event)) | 135 if (ProcessEventForOverscroll(event)) |
| 136 return true; | 136 return true; |
| 137 } else if (reset_scroll_state) { | 137 } else if (reset_scroll_state) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 void OverscrollController::Reset() { | 173 void OverscrollController::Reset() { |
| 174 overscroll_mode_ = OVERSCROLL_NONE; | 174 overscroll_mode_ = OVERSCROLL_NONE; |
| 175 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 175 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
| 176 scroll_state_ = STATE_UNKNOWN; | 176 scroll_state_ = STATE_UNKNOWN; |
| 177 } | 177 } |
| 178 | 178 |
| 179 void OverscrollController::Cancel() { | 179 void OverscrollController::Cancel() { |
| 180 SetOverscrollMode(OVERSCROLL_NONE); | 180 SetOverscrollMode(OVERSCROLL_NONE, OverscrollSource::NONE); |
| 181 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 181 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
| 182 scroll_state_ = STATE_UNKNOWN; | 182 scroll_state_ = STATE_UNKNOWN; |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool OverscrollController::DispatchEventCompletesAction ( | 185 bool OverscrollController::DispatchEventCompletesAction ( |
| 186 const blink::WebInputEvent& event) const { | 186 const blink::WebInputEvent& event) const { |
| 187 if (overscroll_mode_ == OVERSCROLL_NONE) | 187 if (overscroll_mode_ == OVERSCROLL_NONE) |
| 188 return false; | 188 return false; |
| 189 | 189 |
| 190 // Complete the overscroll gesture if there was a mouse move or a scroll-end | 190 // Complete the overscroll gesture if there was a mouse move or a scroll-end |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 } else if (fabs(velocity_y) > kFlingVelocityThreshold) { | 313 } else if (fabs(velocity_y) > kFlingVelocityThreshold) { |
| 314 if ((overscroll_mode_ == OVERSCROLL_NORTH && velocity_y < 0) || | 314 if ((overscroll_mode_ == OVERSCROLL_NORTH && velocity_y < 0) || |
| 315 (overscroll_mode_ == OVERSCROLL_SOUTH && velocity_y > 0)) { | 315 (overscroll_mode_ == OVERSCROLL_SOUTH && velocity_y > 0)) { |
| 316 CompleteAction(); | 316 CompleteAction(); |
| 317 event_processed = true; | 317 event_processed = true; |
| 318 break; | 318 break; |
| 319 } | 319 } |
| 320 } | 320 } |
| 321 | 321 |
| 322 // Reset overscroll state if fling didn't complete the overscroll gesture. | 322 // Reset overscroll state if fling didn't complete the overscroll gesture. |
| 323 SetOverscrollMode(OVERSCROLL_NONE); | 323 SetOverscrollMode(OVERSCROLL_NONE, OverscrollSource::NONE); |
| 324 break; | 324 break; |
| 325 } | 325 } |
| 326 | 326 |
| 327 default: | 327 default: |
| 328 DCHECK(blink::WebInputEvent::isGestureEventType(event.type()) || | 328 DCHECK(blink::WebInputEvent::isGestureEventType(event.type()) || |
| 329 blink::WebInputEvent::isTouchEventType(event.type())) | 329 blink::WebInputEvent::isTouchEventType(event.type())) |
| 330 << "Received unexpected event: " << event.type(); | 330 << "Received unexpected event: " << event.type(); |
| 331 } | 331 } |
| 332 return event_processed; | 332 return event_processed; |
| 333 } | 333 } |
| 334 | 334 |
| 335 bool OverscrollController::ProcessOverscroll(float delta_x, | 335 bool OverscrollController::ProcessOverscroll(float delta_x, |
| 336 float delta_y, | 336 float delta_y, |
| 337 bool is_touchpad) { | 337 bool is_touchpad) { |
| 338 if (scroll_state_ != STATE_CONTENT_SCROLLING) | 338 if (scroll_state_ != STATE_CONTENT_SCROLLING) |
| 339 overscroll_delta_x_ += delta_x; | 339 overscroll_delta_x_ += delta_x; |
| 340 overscroll_delta_y_ += delta_y; | 340 overscroll_delta_y_ += delta_y; |
| 341 | 341 |
| 342 float horiz_threshold = GetOverscrollConfig( | 342 float horiz_threshold = GetOverscrollConfig( |
| 343 is_touchpad ? OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD | 343 is_touchpad ? OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD |
| 344 : OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN); | 344 : OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN); |
| 345 float vert_threshold = GetOverscrollConfig( | 345 float vert_threshold = GetOverscrollConfig( |
| 346 OVERSCROLL_CONFIG_VERT_THRESHOLD_START); | 346 OVERSCROLL_CONFIG_VERT_THRESHOLD_START); |
| 347 if (fabs(overscroll_delta_x_) <= horiz_threshold && | 347 if (fabs(overscroll_delta_x_) <= horiz_threshold && |
| 348 fabs(overscroll_delta_y_) <= vert_threshold) { | 348 fabs(overscroll_delta_y_) <= vert_threshold) { |
| 349 SetOverscrollMode(OVERSCROLL_NONE); | 349 SetOverscrollMode(OVERSCROLL_NONE, OverscrollSource::NONE); |
| 350 return true; | 350 return true; |
| 351 } | 351 } |
| 352 | 352 |
| 353 // Compute the current overscroll direction. If the direction is different | 353 // Compute the current overscroll direction. If the direction is different |
| 354 // from the current direction, then always switch to no-overscroll mode first | 354 // from the current direction, then always switch to no-overscroll mode first |
| 355 // to make sure that subsequent scroll events go through to the page first. | 355 // to make sure that subsequent scroll events go through to the page first. |
| 356 OverscrollMode new_mode = OVERSCROLL_NONE; | 356 OverscrollMode new_mode = OVERSCROLL_NONE; |
| 357 const float kMinRatio = 2.5; | 357 const float kMinRatio = 2.5; |
| 358 if (fabs(overscroll_delta_x_) > horiz_threshold && | 358 if (fabs(overscroll_delta_x_) > horiz_threshold && |
| 359 fabs(overscroll_delta_x_) > fabs(overscroll_delta_y_) * kMinRatio) | 359 fabs(overscroll_delta_x_) > fabs(overscroll_delta_y_) * kMinRatio) |
| 360 new_mode = overscroll_delta_x_ > 0.f ? OVERSCROLL_EAST : OVERSCROLL_WEST; | 360 new_mode = overscroll_delta_x_ > 0.f ? OVERSCROLL_EAST : OVERSCROLL_WEST; |
| 361 else if (fabs(overscroll_delta_y_) > vert_threshold && | 361 else if (fabs(overscroll_delta_y_) > vert_threshold && |
| 362 fabs(overscroll_delta_y_) > fabs(overscroll_delta_x_) * kMinRatio) | 362 fabs(overscroll_delta_y_) > fabs(overscroll_delta_x_) * kMinRatio) |
| 363 new_mode = overscroll_delta_y_ > 0.f ? OVERSCROLL_SOUTH : OVERSCROLL_NORTH; | 363 new_mode = overscroll_delta_y_ > 0.f ? OVERSCROLL_SOUTH : OVERSCROLL_NORTH; |
| 364 | 364 |
| 365 // The vertical oversrcoll currently does not have any UX effects other then | 365 // The vertical oversrcoll currently does not have any UX effects other then |
| 366 // for the scroll end effect, so testing if it is enabled. | 366 // for the scroll end effect, so testing if it is enabled. |
| 367 if ((new_mode == OVERSCROLL_SOUTH || new_mode == OVERSCROLL_NORTH) && | 367 if ((new_mode == OVERSCROLL_SOUTH || new_mode == OVERSCROLL_NORTH) && |
| 368 !IsScrollEndEffectEnabled()) | 368 !IsScrollEndEffectEnabled()) |
| 369 new_mode = OVERSCROLL_NONE; | 369 new_mode = OVERSCROLL_NONE; |
| 370 | 370 |
| 371 if (overscroll_mode_ == OVERSCROLL_NONE) | 371 if (overscroll_mode_ == OVERSCROLL_NONE) { |
| 372 SetOverscrollMode(new_mode); | 372 SetOverscrollMode(new_mode, is_touchpad ? OverscrollSource::TOUCHPAD |
| 373 else if (new_mode != overscroll_mode_) | 373 : OverscrollSource::TOUCHSCREEN); |
| 374 SetOverscrollMode(OVERSCROLL_NONE); | 374 } else if (new_mode != overscroll_mode_) { |
| 375 SetOverscrollMode(OVERSCROLL_NONE, OverscrollSource::NONE); |
| 376 } |
| 375 | 377 |
| 376 if (overscroll_mode_ == OVERSCROLL_NONE) | 378 if (overscroll_mode_ == OVERSCROLL_NONE) |
| 377 return false; | 379 return false; |
| 378 | 380 |
| 379 // Tell the delegate about the overscroll update so that it can update | 381 // Tell the delegate about the overscroll update so that it can update |
| 380 // the display accordingly (e.g. show history preview etc.). | 382 // the display accordingly (e.g. show history preview etc.). |
| 381 if (delegate_) { | 383 if (delegate_) { |
| 382 // Do not include the threshold amount when sending the deltas to the | 384 // Do not include the threshold amount when sending the deltas to the |
| 383 // delegate. | 385 // delegate. |
| 384 float delegate_delta_x = overscroll_delta_x_; | 386 float delegate_delta_x = overscroll_delta_x_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 405 return false; | 407 return false; |
| 406 } | 408 } |
| 407 | 409 |
| 408 void OverscrollController::CompleteAction() { | 410 void OverscrollController::CompleteAction() { |
| 409 if (delegate_) | 411 if (delegate_) |
| 410 delegate_->OnOverscrollComplete(overscroll_mode_); | 412 delegate_->OnOverscrollComplete(overscroll_mode_); |
| 411 overscroll_mode_ = OVERSCROLL_NONE; | 413 overscroll_mode_ = OVERSCROLL_NONE; |
| 412 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 414 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
| 413 } | 415 } |
| 414 | 416 |
| 415 void OverscrollController::SetOverscrollMode(OverscrollMode mode) { | 417 void OverscrollController::SetOverscrollMode(OverscrollMode mode, |
| 418 OverscrollSource source) { |
| 416 if (overscroll_mode_ == mode) | 419 if (overscroll_mode_ == mode) |
| 417 return; | 420 return; |
| 418 OverscrollMode old_mode = overscroll_mode_; | 421 OverscrollMode old_mode = overscroll_mode_; |
| 419 overscroll_mode_ = mode; | 422 overscroll_mode_ = mode; |
| 420 if (overscroll_mode_ == OVERSCROLL_NONE) | 423 if (overscroll_mode_ == OVERSCROLL_NONE) |
| 421 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 424 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
| 422 else | 425 else |
| 423 scroll_state_ = STATE_OVERSCROLLING; | 426 scroll_state_ = STATE_OVERSCROLLING; |
| 424 if (delegate_) | 427 if (delegate_) |
| 425 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_); | 428 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_, source); |
| 426 } | 429 } |
| 427 | 430 |
| 428 } // namespace content | 431 } // namespace content |
| OLD | NEW |