| 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 "content/renderer/input/input_scroll_elasticity_controller.h" | 5 #include "content/renderer/input/input_scroll_elasticity_controller.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 // ScrollElasticityController and ScrollElasticityControllerClient are based on | 9 // InputScrollElasticityController is based on |
| 10 // WebKit/Source/platform/mac/ScrollElasticityController.mm | 10 // WebKit/Source/platform/mac/InputScrollElasticityController.mm |
| 11 /* | 11 /* |
| 12 * Copyright (C) 2011 Apple Inc. All rights reserved. | 12 * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 13 * | 13 * |
| 14 * Redistribution and use in source and binary forms, with or without | 14 * Redistribution and use in source and binary forms, with or without |
| 15 * modification, are permitted provided that the following conditions | 15 * modification, are permitted provided that the following conditions |
| 16 * are met: | 16 * are met: |
| 17 * 1. Redistributions of source code must retain the above copyright | 17 * 1. Redistributions of source code must retain the above copyright |
| 18 * notice, this list of conditions and the following disclaimer. | 18 * notice, this list of conditions and the following disclaimer. |
| 19 * 2. Redistributions in binary form must reproduce the above copyright | 19 * 2. Redistributions in binary form must reproduce the above copyright |
| 20 * notice, this list of conditions and the following disclaimer in the | 20 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 // multiplier = [[NSUserDefaults standardUserDefaults] | 81 // multiplier = [[NSUserDefaults standardUserDefaults] |
| 82 // floatForKey:@"NSScrollWheelMultiplier"]; | 82 // floatForKey:@"NSScrollWheelMultiplier"]; |
| 83 if (multiplier <= 0) | 83 if (multiplier <= 0) |
| 84 multiplier = 1; | 84 multiplier = 1; |
| 85 } | 85 } |
| 86 return multiplier; | 86 return multiplier; |
| 87 } | 87 } |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 ScrollElasticityController::ScrollElasticityController( | 91 InputScrollElasticityController::InputScrollElasticityController( |
| 92 ScrollElasticityControllerClient* client) | 92 cc::ScrollElasticityControllerClient* client) |
| 93 : client_(client), | 93 : client_(client), |
| 94 in_scroll_gesture_(false), | 94 in_scroll_gesture_(false), |
| 95 has_scrolled_(false), | 95 has_scrolled_(false), |
| 96 momentum_scroll_in_progress_(false), | 96 momentum_scroll_in_progress_(false), |
| 97 ignore_momentum_scrolls_(false), | 97 ignore_momentum_scrolls_(false), |
| 98 last_momentum_scroll_timestamp_(0), | 98 last_momentum_scroll_timestamp_(0), |
| 99 snap_rubberband_timer_is_active_(false) { | 99 snap_rubberband_timer_is_active_(false), |
| 100 weak_factory_(this) { |
| 100 } | 101 } |
| 101 | 102 |
| 102 bool ScrollElasticityController::HandleWheelEvent( | 103 InputScrollElasticityController::~InputScrollElasticityController() { |
| 104 DCHECK(!client_); |
| 105 } |
| 106 |
| 107 base::WeakPtr<InputScrollElasticityController> |
| 108 InputScrollElasticityController::GetWeakPtr() { |
| 109 if (client_) |
| 110 return weak_factory_.GetWeakPtr(); |
| 111 return base::WeakPtr<InputScrollElasticityController>(); |
| 112 } |
| 113 |
| 114 void InputScrollElasticityController::WillShutdown() { |
| 115 weak_factory_.InvalidateWeakPtrs(); |
| 116 client_ = NULL; |
| 117 } |
| 118 |
| 119 void InputScrollElasticityController::Animate(base::TimeTicks time) { |
| 120 if (!snap_rubberband_timer_is_active_) |
| 121 return; |
| 122 |
| 123 // TODO(ccameron): This should send the time parameter to the snap function, |
| 124 // so that animation can be based on frame time, not arbitrarily-sampled |
| 125 // clock time. |
| 126 SnapRubberbandTimerFired(); |
| 127 } |
| 128 |
| 129 void InputScrollElasticityController::ObserveWheelEventAndResult( |
| 130 const blink::WebMouseWheelEvent& wheel_event, |
| 131 const cc::InputHandlerScrollResult& scroll_result) { |
| 132 // TODO(ccameron): Pull non-over-scrolling scroll logic out of |
| 133 // HandleWheelEvent, and read the scroll result instead. |
| 134 HandleWheelEvent(wheel_event); |
| 135 } |
| 136 |
| 137 bool InputScrollElasticityController::HandleWheelEvent( |
| 103 const blink::WebMouseWheelEvent& wheel_event) { | 138 const blink::WebMouseWheelEvent& wheel_event) { |
| 104 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseMayBegin) | 139 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseMayBegin) |
| 105 return false; | 140 return false; |
| 106 | 141 |
| 107 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseBegan) { | 142 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseBegan) { |
| 108 in_scroll_gesture_ = true; | 143 in_scroll_gesture_ = true; |
| 109 has_scrolled_ = false; | 144 has_scrolled_ = false; |
| 110 momentum_scroll_in_progress_ = false; | 145 momentum_scroll_in_progress_ = false; |
| 111 ignore_momentum_scrolls_ = false; | 146 ignore_momentum_scrolls_ = false; |
| 112 last_momentum_scroll_timestamp_ = 0; | 147 last_momentum_scroll_timestamp_ = 0; |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 float RoundToDevicePixelTowardZero(float num) { | 363 float RoundToDevicePixelTowardZero(float num) { |
| 329 float rounded_num = roundf(num); | 364 float rounded_num = roundf(num); |
| 330 if (fabs(num - rounded_num) < 0.125) | 365 if (fabs(num - rounded_num) < 0.125) |
| 331 num = rounded_num; | 366 num = rounded_num; |
| 332 | 367 |
| 333 return RoundTowardZero(num); | 368 return RoundTowardZero(num); |
| 334 } | 369 } |
| 335 | 370 |
| 336 } // namespace | 371 } // namespace |
| 337 | 372 |
| 338 void ScrollElasticityController::SnapRubberbandTimerFired() { | 373 void InputScrollElasticityController::SnapRubberbandTimerFired() { |
| 339 if (!momentum_scroll_in_progress_ || ignore_momentum_scrolls_) { | 374 if (!momentum_scroll_in_progress_ || ignore_momentum_scrolls_) { |
| 340 float time_delta = (base::Time::Now() - start_time_).InSecondsF(); | 375 float time_delta = (base::Time::Now() - start_time_).InSecondsF(); |
| 341 | 376 |
| 342 if (start_stretch_ == gfx::Vector2dF()) { | 377 if (start_stretch_ == gfx::Vector2dF()) { |
| 343 start_stretch_ = client_->StretchAmount(); | 378 start_stretch_ = client_->StretchAmount(); |
| 344 if (start_stretch_ == gfx::Vector2dF()) { | 379 if (start_stretch_ == gfx::Vector2dF()) { |
| 345 StopSnapRubberbandTimer(); | 380 StopSnapRubberbandTimer(); |
| 346 | 381 |
| 347 stretch_scroll_force_ = gfx::Vector2dF(); | 382 stretch_scroll_force_ = gfx::Vector2dF(); |
| 348 start_time_ = base::Time(); | 383 start_time_ = base::Time(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 start_stretch_ = gfx::Vector2dF(); | 427 start_stretch_ = gfx::Vector2dF(); |
| 393 orig_origin_ = gfx::Vector2dF(); | 428 orig_origin_ = gfx::Vector2dF(); |
| 394 orig_velocity_ = gfx::Vector2dF(); | 429 orig_velocity_ = gfx::Vector2dF(); |
| 395 } | 430 } |
| 396 } else { | 431 } else { |
| 397 start_time_ = base::Time::Now(); | 432 start_time_ = base::Time::Now(); |
| 398 start_stretch_ = gfx::Vector2dF(); | 433 start_stretch_ = gfx::Vector2dF(); |
| 399 } | 434 } |
| 400 } | 435 } |
| 401 | 436 |
| 402 bool ScrollElasticityController::IsRubberbandInProgress() const { | 437 bool InputScrollElasticityController::IsRubberbandInProgress() const { |
| 403 if (!in_scroll_gesture_ && !momentum_scroll_in_progress_ && | 438 if (!in_scroll_gesture_ && !momentum_scroll_in_progress_ && |
| 404 !snap_rubberband_timer_is_active_) | 439 !snap_rubberband_timer_is_active_) |
| 405 return false; | 440 return false; |
| 406 | 441 |
| 407 return !client_->StretchAmount().IsZero(); | 442 return !client_->StretchAmount().IsZero(); |
| 408 } | 443 } |
| 409 | 444 |
| 410 void ScrollElasticityController::StopSnapRubberbandTimer() { | 445 void InputScrollElasticityController::StopSnapRubberbandTimer() { |
| 411 client_->StopSnapRubberbandTimer(); | 446 client_->StopSnapRubberbandTimer(); |
| 412 snap_rubberband_timer_is_active_ = false; | 447 snap_rubberband_timer_is_active_ = false; |
| 413 } | 448 } |
| 414 | 449 |
| 415 void ScrollElasticityController::SnapRubberband() { | 450 void InputScrollElasticityController::SnapRubberband() { |
| 416 double time_delta = SystemUptime() - last_momentum_scroll_timestamp_; | 451 double time_delta = SystemUptime() - last_momentum_scroll_timestamp_; |
| 417 if (last_momentum_scroll_timestamp_ && | 452 if (last_momentum_scroll_timestamp_ && |
| 418 time_delta >= kScrollVelocityZeroingTimeout) | 453 time_delta >= kScrollVelocityZeroingTimeout) |
| 419 momentum_velocity_ = gfx::Vector2dF(); | 454 momentum_velocity_ = gfx::Vector2dF(); |
| 420 | 455 |
| 421 in_scroll_gesture_ = false; | 456 in_scroll_gesture_ = false; |
| 422 | 457 |
| 423 if (snap_rubberband_timer_is_active_) | 458 if (snap_rubberband_timer_is_active_) |
| 424 return; | 459 return; |
| 425 | 460 |
| 426 start_stretch_ = gfx::Vector2dF(); | 461 start_stretch_ = gfx::Vector2dF(); |
| 427 orig_origin_ = gfx::Vector2dF(); | 462 orig_origin_ = gfx::Vector2dF(); |
| 428 orig_velocity_ = gfx::Vector2dF(); | 463 orig_velocity_ = gfx::Vector2dF(); |
| 429 | 464 |
| 430 // If there's no momentum scroll or stretch amount, no need to start the | 465 // If there's no momentum scroll or stretch amount, no need to start the |
| 431 // timer. | 466 // timer. |
| 432 if (!momentum_scroll_in_progress_ && | 467 if (!momentum_scroll_in_progress_ && |
| 433 client_->StretchAmount() == gfx::Vector2dF()) { | 468 client_->StretchAmount() == gfx::Vector2dF()) { |
| 434 start_time_ = base::Time(); | 469 start_time_ = base::Time(); |
| 435 stretch_scroll_force_ = gfx::Vector2dF(); | 470 stretch_scroll_force_ = gfx::Vector2dF(); |
| 436 return; | 471 return; |
| 437 } | 472 } |
| 438 | 473 |
| 439 start_time_ = base::Time::Now(); | 474 start_time_ = base::Time::Now(); |
| 440 client_->StartSnapRubberbandTimer(); | 475 client_->StartSnapRubberbandTimer(); |
| 441 snap_rubberband_timer_is_active_ = true; | 476 snap_rubberband_timer_is_active_ = true; |
| 442 } | 477 } |
| 443 | 478 |
| 444 bool ScrollElasticityController::ShouldHandleEvent( | 479 bool InputScrollElasticityController::ShouldHandleEvent( |
| 445 const blink::WebMouseWheelEvent& wheel_event) { | 480 const blink::WebMouseWheelEvent& wheel_event) { |
| 446 // Once any scrolling has happened, all future events should be handled. | 481 // Once any scrolling has happened, all future events should be handled. |
| 447 if (has_scrolled_) | 482 if (has_scrolled_) |
| 448 return true; | 483 return true; |
| 449 | 484 |
| 450 // The event can't cause scrolling to start if its delta is 0. | 485 // The event can't cause scrolling to start if its delta is 0. |
| 451 if (wheel_event.deltaX == 0 && wheel_event.deltaY == 0) | 486 if (wheel_event.deltaX == 0 && wheel_event.deltaY == 0) |
| 452 return false; | 487 return false; |
| 453 | 488 |
| 454 // If the client isn't pinned, then the event is guaranteed to cause | 489 // If the client isn't pinned, then the event is guaranteed to cause |
| 455 // scrolling. | 490 // scrolling. |
| 456 if (!client_->PinnedInDirection(gfx::Vector2dF(-wheel_event.deltaX, 0))) | 491 if (!client_->PinnedInDirection(gfx::Vector2dF(-wheel_event.deltaX, 0))) |
| 457 return true; | 492 return true; |
| 458 | 493 |
| 459 // If the event is pinned, then the client can't scroll, but it might rubber | 494 // If the event is pinned, then the client can't scroll, but it might rubber |
| 460 // band. | 495 // band. |
| 461 // Check if the event allows rubber banding. | 496 // Check if the event allows rubber banding. |
| 462 if (wheel_event.deltaY == 0) { | 497 if (wheel_event.deltaY == 0) { |
| 463 if (wheel_event.deltaX > 0 && !wheel_event.canRubberbandLeft) | 498 if (wheel_event.deltaX > 0 && !wheel_event.canRubberbandLeft) |
| 464 return false; | 499 return false; |
| 465 if (wheel_event.deltaX < 0 && !wheel_event.canRubberbandRight) | 500 if (wheel_event.deltaX < 0 && !wheel_event.canRubberbandRight) |
| 466 return false; | 501 return false; |
| 467 } | 502 } |
| 468 | 503 |
| 469 // The event is going to either cause scrolling or rubber banding. | 504 // The event is going to either cause scrolling or rubber banding. |
| 470 return true; | 505 return true; |
| 471 } | 506 } |
| 472 | 507 |
| 473 } // namespace content | 508 } // namespace content |
| OLD | NEW |