| 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 | 6 |
| 7 import 'package:newton/newton.dart'; | 7 import 'package:newton/newton.dart'; |
| 8 | 8 |
| 9 const double _kSecondsPerMillisecond = 1000.0; | 9 const double _kSecondsPerMillisecond = 1000.0; |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 double get maxScrollOffset => math.max(0.0, _contentsSize - _containerSize); | 42 double get maxScrollOffset => math.max(0.0, _contentsSize - _containerSize); |
| 43 | 43 |
| 44 double applyCurve(double scrollOffset, double scrollDelta) { | 44 double applyCurve(double scrollOffset, double scrollDelta) { |
| 45 return (scrollOffset + scrollDelta).clamp(0.0, maxScrollOffset); | 45 return (scrollOffset + scrollDelta).clamp(0.0, maxScrollOffset); |
| 46 } | 46 } |
| 47 } | 47 } |
| 48 | 48 |
| 49 Simulation createDefaultScrollSimulation(double position, double velocity, doubl
e minScrollOffset, double maxScrollOffset) { | 49 Simulation createDefaultScrollSimulation(double position, double velocity, doubl
e minScrollOffset, double maxScrollOffset) { |
| 50 double velocityPerSecond = velocity * _kSecondsPerMillisecond; | 50 double velocityPerSecond = velocity * _kSecondsPerMillisecond; |
| 51 SpringDescription spring = new SpringDescription.withDampingRatio( | 51 SpringDescription spring = new SpringDescription.withDampingRatio( |
| 52 mass: 1.0, springConstant: 85.0, ratio: 1.1); | 52 mass: 1.0, springConstant: 170.0, ratio: 1.1); |
| 53 double drag = 0.4; | 53 double drag = 0.025; |
| 54 return new ScrollSimulation(position, velocityPerSecond, minScrollOffset, maxS
crollOffset, spring, drag); | 54 return new ScrollSimulation(position, velocityPerSecond, minScrollOffset, maxS
crollOffset, spring, drag); |
| 55 } | 55 } |
| 56 | 56 |
| 57 class FlingBehavior extends BoundedBehavior { | 57 class FlingBehavior extends BoundedBehavior { |
| 58 FlingBehavior({ double contentsSize: 0.0, double containerSize: 0.0 }) | 58 FlingBehavior({ double contentsSize: 0.0, double containerSize: 0.0 }) |
| 59 : super(contentsSize: contentsSize, containerSize: containerSize); | 59 : super(contentsSize: contentsSize, containerSize: containerSize); |
| 60 | 60 |
| 61 Simulation release(double position, double velocity) { | 61 Simulation release(double position, double velocity) { |
| 62 return createDefaultScrollSimulation(position, 0.0, minScrollOffset, maxScro
llOffset); | 62 return createDefaultScrollSimulation(position, 0.0, minScrollOffset, maxScro
llOffset); |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 | 65 |
| 66 class OverscrollBehavior extends BoundedBehavior { | 66 class OverscrollBehavior extends BoundedBehavior { |
| 67 OverscrollBehavior({ double contentsSize: 0.0, double containerSize: 0.0 }) | 67 OverscrollBehavior({ double contentsSize: 0.0, double containerSize: 0.0 }) |
| 68 : super(contentsSize: contentsSize, containerSize: containerSize); | 68 : super(contentsSize: contentsSize, containerSize: containerSize); |
| 69 | 69 |
| 70 Simulation release(double position, double velocity) { | 70 Simulation release(double position, double velocity) { |
| 71 return createDefaultScrollSimulation(position, velocity, minScrollOffset, ma
xScrollOffset); | 71 return createDefaultScrollSimulation(position, velocity, minScrollOffset, ma
xScrollOffset); |
| 72 } | 72 } |
| 73 | 73 |
| 74 double applyCurve(double scrollOffset, double scrollDelta) { | 74 double applyCurve(double scrollOffset, double scrollDelta) { |
| 75 double newScrollOffset = scrollOffset + scrollDelta; | 75 double newScrollOffset = scrollOffset + scrollDelta; |
| 76 // If we're overscrolling, we want move the scroll offset 2x | 76 // If we're overscrolling, we want move the scroll offset 2x |
| 77 // slower than we would otherwise. Therefore, we "rewind" the | 77 // slower than we would otherwise. Therefore, we "rewind" the |
| 78 // newScrollOffset by half the amount that we moved it above. | 78 // newScrollOffset by half the amount that we moved it above. |
| 79 // Notice that we clap the "old" value to 0.0 so that we only | 79 // Notice that we clamp the "old" value to 0.0 so that we only |
| 80 // reduce the portion of scrollDelta that's applied beyond 0.0. We | 80 // reduce the portion of scrollDelta that's applied beyond 0.0. We |
| 81 // do similar things for overscroll in the other direction. | 81 // do similar things for overscroll in the other direction. |
| 82 if (newScrollOffset < 0.0) { | 82 if (newScrollOffset < 0.0) { |
| 83 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; | 83 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; |
| 84 } else if (newScrollOffset > maxScrollOffset) { | 84 } else if (newScrollOffset > maxScrollOffset) { |
| 85 newScrollOffset -= (newScrollOffset - math.max(maxScrollOffset, scrollOffs
et)) / 2.0; | 85 newScrollOffset -= (newScrollOffset - math.max(maxScrollOffset, scrollOffs
et)) / 2.0; |
| 86 } | 86 } |
| 87 return newScrollOffset; | 87 return newScrollOffset; |
| 88 } | 88 } |
| 89 } | 89 } |
| OLD | NEW |