OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Implementations can be used to simulate the deceleration of an element within | 6 * Implementations can be used to simulate the deceleration of an element within |
7 * a certain region. To use this behavior you need to provide an initial | 7 * a certain region. To use this behavior you need to provide an initial |
8 * velocity that is meant to represent the gesture that is initiating this | 8 * velocity that is meant to represent the gesture that is initiating this |
9 * deceleration. You also provide the bounds of the region that the element | 9 * deceleration. You also provide the bounds of the region that the element |
10 * exists in, and the current offset of the element within that region. The | 10 * exists in, and the current offset of the element within that region. The |
11 * transitions will have the element decelerate to rest, or stretch past the | 11 * transitions will have the element decelerate to rest, or stretch past the |
12 * offset boundaries and then come to rest. | 12 * offset boundaries and then come to rest. |
13 * | 13 * |
14 * This is primarily designed to solve the problem of slow scrolling in mobile | 14 * This is primarily designed to solve the problem of slow scrolling in mobile |
15 * safari. You can use this along with the [Scroller] behavior to make a | 15 * safari. You can use this along with the [Scroller] behavior to make a |
16 * scrollable area scroll the same way it would in a native application. | 16 * scrollable area scroll the same way it would in a native application. |
17 * | 17 * |
18 * Implementations of this interface do not maintain any references to HTML | 18 * Implementations of this interface do not maintain any references to HTML |
19 * elements, and therefore cannot do any redrawing of elements. They only | 19 * elements, and therefore cannot do any redrawing of elements. They only |
20 * calculates where the element should be on an interval. It is the delegate's | 20 * calculates where the element should be on an interval. It is the delegate's |
21 * responsibility to redraw the element when the onDecelerate callback is | 21 * responsibility to redraw the element when the onDecelerate callback is |
22 * invoked. It is recommended that you move the element with a hardware | 22 * invoked. It is recommended that you move the element with a hardware |
23 * accelerated method such as using 'translate3d' on the element's | 23 * accelerated method such as using 'translate3d' on the element's |
24 * -webkit-transform style property. | 24 * -webkit-transform style property. |
25 */ | 25 */ |
26 interface Momentum default TimeoutMomentum { | 26 interface Momentum default TimeoutMomentum { |
27 | 27 |
28 Momentum(MomentumDelegate delegate, [num defaultDecelerationFactor]); | 28 Momentum(MomentumDelegate delegate, [num defaultDecelerationFactor]); |
29 | 29 |
30 bool get decelerating(); | 30 bool get decelerating; |
31 | 31 |
32 num get decelerationFactor(); | 32 num get decelerationFactor; |
33 | 33 |
34 /** | 34 /** |
35 * Transition end handler. This function must be invoked after any transition | 35 * Transition end handler. This function must be invoked after any transition |
36 * that occurred as a result of a call to the delegate's onDecelerate callback. | 36 * that occurred as a result of a call to the delegate's onDecelerate callback. |
37 */ | 37 */ |
38 void onTransitionEnd(); | 38 void onTransitionEnd(); |
39 | 39 |
40 /** | 40 /** |
41 * Start decelerating. | 41 * Start decelerating. |
42 * The [velocity] passed should be in terms of number of pixels / millisecond. | 42 * The [velocity] passed should be in terms of number of pixels / millisecond. |
(...skipping 12 matching lines...) Expand all Loading... |
55 Coordinate calculateVelocity(Coordinate start, Coordinate target, | 55 Coordinate calculateVelocity(Coordinate start, Coordinate target, |
56 [num decelerationFactor]); | 56 [num decelerationFactor]); |
57 | 57 |
58 /** Stop decelerating and return the current velocity. */ | 58 /** Stop decelerating and return the current velocity. */ |
59 Coordinate stop(); | 59 Coordinate stop(); |
60 | 60 |
61 /** Aborts decelerating without dispatching any notification events. */ | 61 /** Aborts decelerating without dispatching any notification events. */ |
62 void abort(); | 62 void abort(); |
63 | 63 |
64 /** null if no transition is in progress. */ | 64 /** null if no transition is in progress. */ |
65 Coordinate get destination(); | 65 Coordinate get destination; |
66 } | 66 } |
67 | 67 |
68 /** | 68 /** |
69 * Momentum Delegate interface. | 69 * Momentum Delegate interface. |
70 * You are required to implement this interface in order to use the | 70 * You are required to implement this interface in order to use the |
71 * Momentum behavior. | 71 * Momentum behavior. |
72 */ | 72 */ |
73 interface MomentumDelegate { | 73 interface MomentumDelegate { |
74 /** | 74 /** |
75 * Callback for a deceleration step. The delegate is responsible for redrawing | 75 * Callback for a deceleration step. The delegate is responsible for redrawing |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 if (_isStepNecessary()) { | 399 if (_isStepNecessary()) { |
400 _moves.add(new _Move(_nextX, _nextY, | 400 _moves.add(new _Move(_nextX, _nextY, |
401 physicsX.velocity, | 401 physicsX.velocity, |
402 physicsY.velocity, time)); | 402 physicsY.velocity, time)); |
403 _previousOffset.y = _nextY; | 403 _previousOffset.y = _nextY; |
404 _previousOffset.x = _nextX; | 404 _previousOffset.x = _nextX; |
405 } | 405 } |
406 } | 406 } |
407 } | 407 } |
408 | 408 |
409 bool get decelerating() => _decelerating; | 409 bool get decelerating => _decelerating; |
410 num get decelerationFactor() => _customDecelerationFactor; | 410 num get decelerationFactor => _customDecelerationFactor; |
411 | 411 |
412 /** | 412 /** |
413 * Checks whether or not an animation step is necessary or not. Animations | 413 * Checks whether or not an animation step is necessary or not. Animations |
414 * steps are not necessary when the velocity gets so low that in several | 414 * steps are not necessary when the velocity gets so low that in several |
415 * frames the offset is the same. | 415 * frames the offset is the same. |
416 * Returns true if there is movement to be done in the next frame. | 416 * Returns true if there is movement to be done in the next frame. |
417 */ | 417 */ |
418 bool _isStepNecessary() { | 418 bool _isStepNecessary() { |
419 return _nextY != _previousOffset.y || _nextX != _previousOffset.x; | 419 return _nextY != _previousOffset.y || _nextX != _previousOffset.x; |
420 } | 420 } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 if (_stepTimeout !== null) { | 536 if (_stepTimeout !== null) { |
537 Env.cancelRequestAnimationFrame(_stepTimeout); | 537 Env.cancelRequestAnimationFrame(_stepTimeout); |
538 _stepTimeout = null; | 538 _stepTimeout = null; |
539 } | 539 } |
540 if (wasDecelerating) { | 540 if (wasDecelerating) { |
541 _delegate.onDecelerationEnd(); | 541 _delegate.onDecelerationEnd(); |
542 } | 542 } |
543 return velocity; | 543 return velocity; |
544 } | 544 } |
545 | 545 |
546 Coordinate get destination() { | 546 Coordinate get destination { |
547 if (!_moves.isEmpty()) { | 547 if (!_moves.isEmpty()) { |
548 final lastMove = _moves.last(); | 548 final lastMove = _moves.last(); |
549 return new Coordinate(lastMove.x, lastMove.y); | 549 return new Coordinate(lastMove.x, lastMove.y); |
550 } else { | 550 } else { |
551 return null; | 551 return null; |
552 } | 552 } |
553 } | 553 } |
554 } | 554 } |
OLD | NEW |