OLD | NEW |
1 part of widgets; | 1 part of widgets; |
2 | 2 |
3 const double _kWidth = 256.0; | 3 const double _kWidth = 256.0; |
4 const double _kMinFlingVelocity = 0.4; | 4 const double _kMinFlingVelocity = 0.4; |
5 const double _kBaseSettleDurationMS = 246.0; | 5 const double _kBaseSettleDurationMS = 246.0; |
6 const double _kMaxSettleDurationMS = 600.0; | 6 const double _kMaxSettleDurationMS = 600.0; |
7 const Cubic _kAnimationCurve = easeOut; | 7 const Cubic _kAnimationCurve = easeOut; |
8 | 8 |
9 class DrawerAnimation { | 9 class DrawerAnimation extends Animation { |
10 | 10 |
11 Stream<double> get onPositionChanged => _controller.stream; | 11 Stream<double> get onPositionChanged => onValueChanged; |
12 | 12 |
13 StreamController _controller; | 13 bool get _isMostlyClosed => value <= -_kWidth / 2; |
14 AnimationGenerator _animation; | |
15 double _position; | |
16 bool get _isAnimating => _animation != null; | |
17 bool get _isMostlyClosed => _position <= -_kWidth / 2; | |
18 | 14 |
19 DrawerAnimation() { | 15 DrawerAnimation() { |
20 _controller = new StreamController(sync: true); | 16 value = -_kWidth; |
21 _setPosition(-_kWidth); | |
22 } | 17 } |
23 | 18 |
24 void toggle(_) => _isMostlyClosed ? _open() : _close(); | 19 void toggle(_) => _isMostlyClosed ? _open() : _close(); |
25 | 20 |
26 void handleMaskTap(_) => _close(); | 21 void handleMaskTap(_) => _close(); |
27 | 22 |
28 void handlePointerDown(_) => _cancelAnimation(); | 23 void handlePointerDown(_) => stop(); |
29 | 24 |
30 void handlePointerMove(sky.PointerEvent event) { | 25 void handlePointerMove(sky.PointerEvent event) { |
31 assert(_animation == null); | 26 assert(!isAnimating); |
32 _setPosition(_position + event.dx); | 27 value = math.min(0.0, math.max(value + event.dx, -_kWidth)); |
33 } | 28 } |
34 | 29 |
35 void handlePointerUp(_) { | 30 void handlePointerUp(_) { |
36 if (!_isAnimating) | 31 if (!isAnimating) |
37 _settle(); | 32 _settle(); |
38 } | 33 } |
39 | 34 |
40 void handlePointerCancel(_) { | 35 void handlePointerCancel(_) { |
41 if (!_isAnimating) | 36 if (!isAnimating) |
42 _settle(); | 37 _settle(); |
43 } | 38 } |
44 | 39 |
45 void _open() => _animateToPosition(0.0); | 40 void _open() => _animateToPosition(0.0); |
46 | 41 |
47 void _close() => _animateToPosition(-_kWidth); | 42 void _close() => _animateToPosition(-_kWidth); |
48 | 43 |
49 void _settle() => _isMostlyClosed ? _close() : _open(); | 44 void _settle() => _isMostlyClosed ? _close() : _open(); |
50 | 45 |
51 void _setPosition(double value) { | 46 void _animateToPosition(double targetPosition) { |
52 _position = math.min(0.0, math.max(value, -_kWidth)); | 47 double distance = (targetPosition - value).abs(); |
53 _controller.add(_position); | 48 if (distance != 0) { |
54 } | 49 double targetDuration = distance / _kWidth * _kBaseSettleDurationMS; |
55 | 50 double duration = math.min(targetDuration, _kMaxSettleDurationMS); |
56 void _cancelAnimation() { | 51 animateTo(targetPosition, duration, curve: _kAnimationCurve); |
57 if (_animation != null) { | |
58 _animation.cancel(); | |
59 _animation = null; | |
60 } | 52 } |
61 } | 53 } |
62 | 54 |
63 void _animate(double duration, double begin, double end, Curve curve) { | |
64 _cancelAnimation(); | |
65 | |
66 _animation = new AnimationGenerator(duration, begin: begin, end: end, | |
67 curve: curve); | |
68 | |
69 _animation.onTick.listen(_setPosition, onDone: () { | |
70 _animation = null; | |
71 }); | |
72 } | |
73 | |
74 void _animateToPosition(double targetPosition) { | |
75 double distance = (targetPosition - _position).abs(); | |
76 if (distance != 0) { | |
77 double targetDuration = distance / _kWidth * _kBaseSettleDurationMS; | |
78 double duration = math.min(targetDuration, _kMaxSettleDurationMS); | |
79 _animate(duration, _position, targetPosition, _kAnimationCurve); | |
80 } | |
81 } | |
82 | |
83 void handleFlingStart(event) { | 55 void handleFlingStart(event) { |
84 double direction = event.velocityX.sign; | 56 double direction = event.velocityX.sign; |
85 double velocityX = event.velocityX.abs() / 1000; | 57 double velocityX = event.velocityX.abs() / 1000; |
86 if (velocityX < _kMinFlingVelocity) | 58 if (velocityX < _kMinFlingVelocity) |
87 return; | 59 return; |
88 | 60 |
89 double targetPosition = direction < 0.0 ? -_kWidth : 0.0; | 61 double targetPosition = direction < 0.0 ? -_kWidth : 0.0; |
90 double distance = (targetPosition - _position).abs(); | 62 double distance = (targetPosition - value).abs(); |
91 double duration = distance / velocityX; | 63 double duration = distance / velocityX; |
92 | 64 |
93 _animate(duration, _position, targetPosition, linear); | 65 animateTo(targetPosition, duration, curve: linear); |
94 } | 66 } |
95 } | 67 } |
96 | 68 |
97 class Drawer extends Component { | 69 class Drawer extends Component { |
98 | 70 |
99 static Style _style = new Style(''' | 71 static Style _style = new Style(''' |
100 position: absolute; | 72 position: absolute; |
101 z-index: 2; | 73 z-index: 2; |
102 top: 0; | 74 top: 0; |
103 left: 0; | 75 left: 0; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 style: _style, | 150 style: _style, |
179 inlineStyle: inlineStyle, | 151 inlineStyle: inlineStyle, |
180 children: [ mask, content ] | 152 children: [ mask, content ] |
181 )..events.listen('pointerdown', animation.handlePointerDown) | 153 )..events.listen('pointerdown', animation.handlePointerDown) |
182 ..events.listen('pointermove', animation.handlePointerMove) | 154 ..events.listen('pointermove', animation.handlePointerMove) |
183 ..events.listen('pointerup', animation.handlePointerUp) | 155 ..events.listen('pointerup', animation.handlePointerUp) |
184 ..events.listen('pointercancel', animation.handlePointerCancel); | 156 ..events.listen('pointercancel', animation.handlePointerCancel); |
185 | 157 |
186 } | 158 } |
187 } | 159 } |
OLD | NEW |