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/gfx/android/scroller.h" | 5 #include "ui/gfx/android/scroller.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 | 10 |
11 namespace gfx { | 11 namespace gfx { |
12 namespace { | 12 namespace { |
13 | 13 |
14 // Default scroll duration from android.widget.Scroller. | 14 // Default scroll duration from android.widget.Scroller. |
15 const int kDefaultDurationMs = 250; | 15 const int kDefaultDurationMs = 250; |
16 | 16 |
17 // Default friction constant in android.view.ViewConfiguration. | 17 // Default friction constant in android.view.ViewConfiguration. |
18 const float kDefaultFriction = 0.015f; | 18 const float kDefaultFriction = 0.015f; |
19 | 19 |
20 // == std::log(0.78f) / std::log(0.9f) | 20 // == std::log(0.78f) / std::log(0.9f) |
21 const float kDecelerationRate = 2.3582018f; | 21 const float kDecelerationRate = 2.3582018f; |
22 | 22 |
23 // Tension lines cross at (kInflexion, 1). | 23 // Tension lines cross at (kInflexion, 1). |
24 const float kInflexion = 0.35f; | 24 const float kInflexion = 0.35f; |
25 | 25 |
26 const float kEpsilon = 1e-5f; | 26 const float kEpsilon = 1e-5f; |
27 | 27 |
| 28 // Fling scroll is stopped when the scroll position is |kThresholdForFlingEnd| |
| 29 // pixels or closer from the end. |
| 30 const float kThresholdForFlingEnd = 0.1; |
| 31 |
28 bool ApproxEquals(float a, float b) { | 32 bool ApproxEquals(float a, float b) { |
29 return std::abs(a - b) < kEpsilon; | 33 return std::abs(a - b) < kEpsilon; |
30 } | 34 } |
31 | 35 |
32 struct ViscosityConstants { | 36 struct ViscosityConstants { |
33 ViscosityConstants() | 37 ViscosityConstants() |
34 : viscous_fluid_scale_(8.f), viscous_fluid_normalize_(1.f) { | 38 : viscous_fluid_scale_(8.f), viscous_fluid_normalize_(1.f) { |
35 viscous_fluid_normalize_ = 1.0f / ApplyViscosity(1.0f); | 39 viscous_fluid_normalize_ = 1.0f / ApplyViscosity(1.0f); |
36 } | 40 } |
37 | 41 |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 t, &distance_coef, &velocity_coef); | 321 t, &distance_coef, &velocity_coef); |
318 | 322 |
319 curr_velocity_ = velocity_coef * distance_ * duration_seconds_reciprocal_; | 323 curr_velocity_ = velocity_coef * distance_ * duration_seconds_reciprocal_; |
320 | 324 |
321 curr_x_ = start_x_ + distance_coef * delta_x_; | 325 curr_x_ = start_x_ + distance_coef * delta_x_; |
322 curr_x_ = Clamped(curr_x_, min_x_, max_x_); | 326 curr_x_ = Clamped(curr_x_, min_x_, max_x_); |
323 | 327 |
324 curr_y_ = start_y_ + distance_coef * delta_y_; | 328 curr_y_ = start_y_ + distance_coef * delta_y_; |
325 curr_y_ = Clamped(curr_y_, min_y_, max_y_); | 329 curr_y_ = Clamped(curr_y_, min_y_, max_y_); |
326 | 330 |
327 if (ApproxEquals(curr_x_, final_x_) && ApproxEquals(curr_y_, final_y_)) { | 331 float diff_x = std::abs(curr_x_ - final_x_); |
| 332 float diff_y = std::abs(curr_y_ - final_y_); |
| 333 if (diff_x < kThresholdForFlingEnd && diff_y < kThresholdForFlingEnd) |
328 finished_ = true; | 334 finished_ = true; |
329 } | |
330 } break; | 335 } break; |
331 } | 336 } |
332 | 337 |
333 return true; | 338 return true; |
334 } | 339 } |
335 | 340 |
336 void Scroller::ExtendDuration(base::TimeDelta extend) { | 341 void Scroller::ExtendDuration(base::TimeDelta extend) { |
337 base::TimeDelta passed = GetTimePassed(); | 342 base::TimeDelta passed = GetTimePassed(); |
338 duration_ = passed + extend; | 343 duration_ = passed + extend; |
339 duration_seconds_reciprocal_ = 1.0 / duration_.InSecondsF(); | 344 duration_seconds_reciprocal_ = 1.0 / duration_.InSecondsF(); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 } | 435 } |
431 | 436 |
432 double Scroller::GetSplineFlingDistance(float velocity) const { | 437 double Scroller::GetSplineFlingDistance(float velocity) const { |
433 const double l = GetSplineDeceleration(velocity); | 438 const double l = GetSplineDeceleration(velocity); |
434 const double decel_minus_one = kDecelerationRate - 1.0; | 439 const double decel_minus_one = kDecelerationRate - 1.0; |
435 return fling_friction_ * tuning_coeff_ * | 440 return fling_friction_ * tuning_coeff_ * |
436 std::exp(kDecelerationRate / decel_minus_one * l); | 441 std::exp(kDecelerationRate / decel_minus_one * l); |
437 } | 442 } |
438 | 443 |
439 } // namespace gfx | 444 } // namespace gfx |
OLD | NEW |