| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 import 'mechanics.dart'; | 6 import 'mechanics.dart'; |
| 7 import 'generators.dart'; | 7 import 'generators.dart'; |
| 8 | 8 |
| 9 const double _kSlope = 0.01; | 9 const double _kSlope = 0.02; |
| 10 const double _kFriction = 0.004; |
| 10 | 11 |
| 11 abstract class ScrollBehavior { | 12 abstract class ScrollBehavior { |
| 12 Simulation release(Particle particle) => null; | 13 Simulation release(Particle particle) => null; |
| 13 | 14 |
| 14 // Returns the new scroll offset. | 15 // Returns the new scroll offset. |
| 15 double applyCurve(double scrollOffset, double scrollDelta); | 16 double applyCurve(double scrollOffset, double scrollDelta); |
| 16 } | 17 } |
| 17 | 18 |
| 18 class BoundedScrollBehavior extends ScrollBehavior { | 19 class BoundedScrollBehavior extends ScrollBehavior { |
| 19 double minOffset; | 20 double minOffset; |
| 20 double maxOffset; | 21 double maxOffset; |
| 21 | 22 |
| 22 BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset}); | 23 BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset}); |
| 23 | 24 |
| 24 double applyCurve(double scrollOffset, double scrollDelta) { | 25 double applyCurve(double scrollOffset, double scrollDelta) { |
| 25 double newScrollOffset = scrollOffset + scrollDelta; | 26 double newScrollOffset = scrollOffset + scrollDelta; |
| 26 if (minOffset != null) | 27 if (minOffset != null) |
| 27 newScrollOffset = math.max(minOffset, newScrollOffset); | 28 newScrollOffset = math.max(minOffset, newScrollOffset); |
| 28 if (maxOffset != null) | 29 if (maxOffset != null) |
| 29 newScrollOffset = math.min(maxOffset, newScrollOffset); | 30 newScrollOffset = math.min(maxOffset, newScrollOffset); |
| 30 return newScrollOffset; | 31 return newScrollOffset; |
| 31 } | 32 } |
| 32 } | 33 } |
| 33 | 34 |
| 34 class OverscrollBehavior extends ScrollBehavior { | 35 class OverscrollBehavior extends ScrollBehavior { |
| 35 Simulation release(Particle particle) { | 36 Simulation release(Particle particle) { |
| 36 if (particle.position >= 0.0) | 37 if (particle.position >= 0.0) { |
| 37 return null; | 38 if (particle.velocity == 0.0) |
| 39 return null; |
| 40 System system = new ParticleInBoxWithFriction( |
| 41 particle: particle, |
| 42 box: new Box(min: 0.0), |
| 43 friction: _kFriction); |
| 44 return new Simulation(system, |
| 45 terminationCondition: () => particle.velocity == 0.0); |
| 46 } |
| 38 System system = new ParticleClimbingRamp( | 47 System system = new ParticleClimbingRamp( |
| 39 particle: particle, | 48 particle: particle, |
| 40 box: new Box(max: 0.0), | 49 box: new Box(max: 0.0), |
| 41 slope: _kSlope, | 50 slope: _kSlope, |
| 42 targetPosition: 0.0); | 51 targetPosition: 0.0); |
| 43 return new Simulation(system, | 52 return new Simulation(system, |
| 44 terminationCondition: () => particle.position == 0.0); | 53 terminationCondition: () => particle.position == 0.0); |
| 45 } | 54 } |
| 46 | 55 |
| 47 double applyCurve(double scrollOffset, double scrollDelta) { | 56 double applyCurve(double scrollOffset, double scrollDelta) { |
| 48 double newScrollOffset = scrollOffset + scrollDelta; | 57 double newScrollOffset = scrollOffset + scrollDelta; |
| 49 if (newScrollOffset < 0.0) { | 58 if (newScrollOffset < 0.0) { |
| 50 // If we're overscrolling, we want move the scroll offset 2x slower than | 59 // If we're overscrolling, we want move the scroll offset 2x slower than |
| 51 // we would otherwise. Therefore, we "rewind" the newScrollOffset by half | 60 // we would otherwise. Therefore, we "rewind" the newScrollOffset by half |
| 52 // the amount that we moved it above. Notice that we clap the "old" value | 61 // the amount that we moved it above. Notice that we clap the "old" value |
| 53 // to 0.0 so that we only reduce the portion of scrollDelta that's applied | 62 // to 0.0 so that we only reduce the portion of scrollDelta that's applied |
| 54 // beyond 0.0. | 63 // beyond 0.0. |
| 55 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; | 64 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; |
| 56 } | 65 } |
| 57 return newScrollOffset; | 66 return newScrollOffset; |
| 58 } | 67 } |
| 59 } | 68 } |
| OLD | NEW |