| 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 "ui/events/blink/input_scroll_elasticity_controller.h" | 5 #include "ui/events/blink/input_scroll_elasticity_controller.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 return gfx::ToRoundedVector2d(gfx::ScaleVector2d(delta, 1.0f / stiffness)); | 81 return gfx::ToRoundedVector2d(gfx::ScaleVector2d(delta, 1.0f / stiffness)); |
| 82 } | 82 } |
| 83 | 83 |
| 84 gfx::Vector2d StretchScrollForceForStretchAmount(const gfx::Vector2dF& delta) { | 84 gfx::Vector2d StretchScrollForceForStretchAmount(const gfx::Vector2dF& delta) { |
| 85 return gfx::ToRoundedVector2d( | 85 return gfx::ToRoundedVector2d( |
| 86 gfx::ScaleVector2d(delta, kRubberbandStiffness)); | 86 gfx::ScaleVector2d(delta, kRubberbandStiffness)); |
| 87 } | 87 } |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 InputScrollElasticityController::InputScrollElasticityController( | 91 InputScrollElasticityController::InputScrollElasticityController() |
| 92 cc::ScrollElasticityHelper* helper) | 92 : helper_(nullptr), |
| 93 : helper_(helper), | |
| 94 state_(kStateInactive), | 93 state_(kStateInactive), |
| 95 momentum_animation_reset_at_next_frame_(false), | 94 momentum_animation_reset_at_next_frame_(false), |
| 96 weak_factory_(this) { | 95 weak_factory_(this) {} |
| 97 } | |
| 98 | 96 |
| 99 InputScrollElasticityController::~InputScrollElasticityController() { | 97 InputScrollElasticityController::~InputScrollElasticityController() { |
| 100 } | 98 } |
| 101 | 99 |
| 102 base::WeakPtr<InputScrollElasticityController> | 100 base::WeakPtr<InputScrollElasticityController> |
| 103 InputScrollElasticityController::GetWeakPtr() { | 101 InputScrollElasticityController::GetWeakPtr() { |
| 104 if (helper_) | 102 if (helper_) |
| 105 return weak_factory_.GetWeakPtr(); | 103 return weak_factory_.GetWeakPtr(); |
| 106 return base::WeakPtr<InputScrollElasticityController>(); | 104 return base::WeakPtr<InputScrollElasticityController>(); |
| 107 } | 105 } |
| 108 | 106 |
| 107 void InputScrollElasticityController::SetActiveHelper( |
| 108 const base::WeakPtr<cc::ScrollElasticityHelper>& helper) { |
| 109 if (helper_.get() == helper.get()) |
| 110 return; |
| 111 |
| 112 if (helper_) { |
| 113 helper_->SetStretchAmount(gfx::Vector2dF()); |
| 114 if (state_ != kStateInactive) |
| 115 EnterStateInactive(); |
| 116 } |
| 117 |
| 118 helper_ = helper; |
| 119 } |
| 120 |
| 121 void InputScrollElasticityController::ObserveRealScrollBegin( |
| 122 bool enter_momentum, |
| 123 bool leave_momentum) { |
| 124 if (enter_momentum) { |
| 125 if (state_ == kStateInactive) |
| 126 state_ = kStateMomentumScroll; |
| 127 } else if (leave_momentum) { |
| 128 scroll_velocity = gfx::Vector2dF(); |
| 129 last_scroll_event_timestamp_ = base::TimeTicks(); |
| 130 state_ = kStateActiveScroll; |
| 131 pending_overscroll_delta_ = gfx::Vector2dF(); |
| 132 } |
| 133 } |
| 134 |
| 135 void InputScrollElasticityController::ObserveScrollUpdate( |
| 136 const gfx::Vector2dF& event_delta, |
| 137 const gfx::Vector2dF& unused_scroll_delta, |
| 138 const base::TimeTicks& event_timestamp, |
| 139 bool has_momentum) { |
| 140 if (state_ == kStateMomentumAnimated || state_ == kStateInactive) |
| 141 return; |
| 142 |
| 143 UpdateVelocity(event_delta, event_timestamp); |
| 144 Overscroll(event_delta, unused_scroll_delta); |
| 145 if (has_momentum && !helper_->StretchAmount().IsZero()) |
| 146 EnterStateMomentumAnimated(event_timestamp); |
| 147 } |
| 148 |
| 149 void InputScrollElasticityController::ObserveRealScrollEnd( |
| 150 const base::TimeTicks& event_timestamp) { |
| 151 if (state_ == kStateMomentumAnimated || state_ == kStateInactive) |
| 152 return; |
| 153 |
| 154 if (helper_->StretchAmount().IsZero()) { |
| 155 EnterStateInactive(); |
| 156 } else { |
| 157 EnterStateMomentumAnimated(event_timestamp); |
| 158 } |
| 159 } |
| 160 |
| 109 void InputScrollElasticityController::ObserveGestureEventAndResult( | 161 void InputScrollElasticityController::ObserveGestureEventAndResult( |
| 110 const blink::WebGestureEvent& gesture_event, | 162 const blink::WebGestureEvent& gesture_event, |
| 111 const cc::InputHandlerScrollResult& scroll_result) { | 163 const cc::InputHandlerScrollResult& scroll_result) { |
| 112 base::TimeTicks event_timestamp = | 164 base::TimeTicks event_timestamp = |
| 113 base::TimeTicks() + | 165 base::TimeTicks() + |
| 114 base::TimeDelta::FromSecondsD(gesture_event.timeStampSeconds); | 166 base::TimeDelta::FromSecondsD(gesture_event.timeStampSeconds); |
| 115 | 167 |
| 116 switch (gesture_event.type) { | 168 switch (gesture_event.type) { |
| 117 case blink::WebInputEvent::GestureScrollBegin: { | 169 case blink::WebInputEvent::GestureScrollBegin: { |
| 118 if (gesture_event.data.scrollBegin.synthetic) | 170 if (gesture_event.data.scrollBegin.synthetic) |
| 119 return; | 171 return; |
| 120 if (gesture_event.data.scrollBegin.inertialPhase == | 172 bool enter_momentum = gesture_event.data.scrollBegin.inertialPhase == |
| 121 blink::WebGestureEvent::MomentumPhase) { | 173 blink::WebGestureEvent::MomentumPhase; |
| 122 if (state_ == kStateInactive) | 174 bool leave_momentum = gesture_event.data.scrollBegin.inertialPhase == |
| 123 state_ = kStateMomentumScroll; | 175 blink::WebGestureEvent::NonMomentumPhase && |
| 124 } else if (gesture_event.data.scrollBegin.inertialPhase == | 176 gesture_event.data.scrollBegin.deltaHintUnits == |
| 125 blink::WebGestureEvent::NonMomentumPhase && | 177 blink::WebGestureEvent::PrecisePixels; |
| 126 gesture_event.data.scrollBegin.deltaHintUnits == | 178 ObserveRealScrollBegin(enter_momentum, leave_momentum); |
| 127 blink::WebGestureEvent::PrecisePixels) { | |
| 128 scroll_velocity = gfx::Vector2dF(); | |
| 129 last_scroll_event_timestamp_ = base::TimeTicks(); | |
| 130 state_ = kStateActiveScroll; | |
| 131 pending_overscroll_delta_ = gfx::Vector2dF(); | |
| 132 } | |
| 133 break; | 179 break; |
| 134 } | 180 } |
| 135 case blink::WebInputEvent::GestureScrollUpdate: { | 181 case blink::WebInputEvent::GestureScrollUpdate: { |
| 136 gfx::Vector2dF event_delta(-gesture_event.data.scrollUpdate.deltaX, | 182 gfx::Vector2dF event_delta(-gesture_event.data.scrollUpdate.deltaX, |
| 137 -gesture_event.data.scrollUpdate.deltaY); | 183 -gesture_event.data.scrollUpdate.deltaY); |
| 138 switch (state_) { | 184 bool has_momentum = gesture_event.data.scrollUpdate.inertialPhase == |
| 139 case kStateMomentumAnimated: | 185 blink::WebGestureEvent::MomentumPhase; |
| 140 case kStateInactive: | 186 ObserveScrollUpdate(event_delta, scroll_result.unused_scroll_delta, |
| 141 break; | 187 event_timestamp, has_momentum); |
| 142 case kStateActiveScroll: | |
| 143 case kStateMomentumScroll: | |
| 144 UpdateVelocity(event_delta, event_timestamp); | |
| 145 Overscroll(event_delta, scroll_result.unused_scroll_delta); | |
| 146 if (gesture_event.data.scrollUpdate.inertialPhase == | |
| 147 blink::WebGestureEvent::MomentumPhase && | |
| 148 !helper_->StretchAmount().IsZero()) { | |
| 149 EnterStateMomentumAnimated(event_timestamp); | |
| 150 } | |
| 151 break; | |
| 152 } | |
| 153 break; | 188 break; |
| 154 } | 189 } |
| 155 case blink::WebInputEvent::GestureScrollEnd: { | 190 case blink::WebInputEvent::GestureScrollEnd: { |
| 156 if (gesture_event.data.scrollEnd.synthetic) | 191 if (gesture_event.data.scrollEnd.synthetic) |
| 157 return; | 192 return; |
| 158 switch (state_) { | 193 ObserveRealScrollEnd(event_timestamp); |
| 159 case kStateMomentumAnimated: | |
| 160 case kStateInactive: | |
| 161 break; | |
| 162 case kStateActiveScroll: | |
| 163 case kStateMomentumScroll: | |
| 164 if (helper_->StretchAmount().IsZero()) { | |
| 165 EnterStateInactive(); | |
| 166 } else { | |
| 167 EnterStateMomentumAnimated(event_timestamp); | |
| 168 } | |
| 169 break; | |
| 170 } | |
| 171 break; | 194 break; |
| 172 } | 195 } |
| 173 default: | 196 default: |
| 174 break; | 197 break; |
| 175 } | 198 } |
| 176 } | 199 } |
| 177 | 200 |
| 178 void InputScrollElasticityController::UpdateVelocity( | 201 void InputScrollElasticityController::UpdateVelocity( |
| 179 const gfx::Vector2dF& event_delta, | 202 const gfx::Vector2dF& event_delta, |
| 180 const base::TimeTicks& event_timestamp) { | 203 const base::TimeTicks& event_timestamp) { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 | 385 |
| 363 bool InputScrollElasticityController::CanScrollHorizontally() const { | 386 bool InputScrollElasticityController::CanScrollHorizontally() const { |
| 364 return helper_->MaxScrollOffset().x() > 0; | 387 return helper_->MaxScrollOffset().x() > 0; |
| 365 } | 388 } |
| 366 | 389 |
| 367 bool InputScrollElasticityController::CanScrollVertically() const { | 390 bool InputScrollElasticityController::CanScrollVertically() const { |
| 368 return helper_->MaxScrollOffset().y() > 0; | 391 return helper_->MaxScrollOffset().y() > 0; |
| 369 } | 392 } |
| 370 | 393 |
| 371 void InputScrollElasticityController::ReconcileStretchAndScroll() { | 394 void InputScrollElasticityController::ReconcileStretchAndScroll() { |
| 395 // If there has been no scroll, or the last elastic-scrolling layer has been |
| 396 // removed from the tree, then there will be no |helper_|. |
| 397 if (!helper_) |
| 398 return; |
| 399 |
| 372 gfx::Vector2dF stretch = helper_->StretchAmount(); | 400 gfx::Vector2dF stretch = helper_->StretchAmount(); |
| 373 if (stretch.IsZero()) | 401 if (stretch.IsZero()) |
| 374 return; | 402 return; |
| 375 | 403 |
| 376 gfx::ScrollOffset scroll_offset = helper_->ScrollOffset(); | 404 gfx::ScrollOffset scroll_offset = helper_->ScrollOffset(); |
| 377 gfx::ScrollOffset max_scroll_offset = helper_->MaxScrollOffset(); | 405 gfx::ScrollOffset max_scroll_offset = helper_->MaxScrollOffset(); |
| 378 | 406 |
| 379 // Compute stretch_adjustment which will be added to |stretch| and subtracted | 407 // Compute stretch_adjustment which will be added to |stretch| and subtracted |
| 380 // from the |scroll_offset|. | 408 // from the |scroll_offset|. |
| 381 gfx::Vector2dF stretch_adjustment; | 409 gfx::Vector2dF stretch_adjustment; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 break; | 445 break; |
| 418 default: | 446 default: |
| 419 // These cases should not be hit because the stretch must be zero in the | 447 // These cases should not be hit because the stretch must be zero in the |
| 420 // Inactive and MomentumScroll states. | 448 // Inactive and MomentumScroll states. |
| 421 NOTREACHED(); | 449 NOTREACHED(); |
| 422 break; | 450 break; |
| 423 } | 451 } |
| 424 } | 452 } |
| 425 | 453 |
| 426 } // namespace ui | 454 } // namespace ui |
| OLD | NEW |