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 '../fn.dart'; |
5 import 'curves.dart'; | 6 import 'curves.dart'; |
6 import 'dart:async'; | 7 import 'dart:async'; |
7 import 'generators.dart'; | 8 import 'generators.dart'; |
8 | 9 |
9 class Animation { | 10 class AnimatedValue { |
| 11 StreamController _controller = new StreamController(sync: true); |
| 12 AnimationGenerator _animation; |
| 13 double _value; |
| 14 |
| 15 AnimatedValue(double initial) { |
| 16 value = initial; |
| 17 } |
| 18 |
10 Stream<double> get onValueChanged => _controller.stream; | 19 Stream<double> get onValueChanged => _controller.stream; |
11 | 20 |
12 double get value => _value; | 21 double get value => _value; |
13 | 22 |
14 void set value(double value) { | 23 void set value(double value) { |
15 stop(); | 24 stop(); |
16 _setValue(value); | 25 _setValue(value); |
17 } | 26 } |
18 | 27 |
19 bool get isAnimating => _animation != null; | 28 bool get isAnimating => _animation != null; |
20 | 29 |
21 StreamController _controller = new StreamController(sync: true); | |
22 | |
23 AnimationGenerator _animation; | |
24 | |
25 double _value; | |
26 | |
27 void _setValue(double value) { | 30 void _setValue(double value) { |
28 _value = value; | 31 _value = value; |
29 _controller.add(_value); | 32 _controller.add(_value); |
30 } | 33 } |
31 | 34 |
32 void stop() { | 35 void stop() { |
33 if (_animation != null) { | 36 if (_animation != null) { |
34 _animation.cancel(); | 37 _animation.cancel(); |
35 _animation = null; | 38 _animation = null; |
36 } | 39 } |
37 } | 40 } |
38 | 41 |
39 void animateTo(double newValue, double duration, | 42 void animateTo(double newValue, double duration, |
40 { Curve curve: linear, double initialDelay: 0.0 }) { | 43 { Curve curve: linear, double initialDelay: 0.0 }) { |
41 stop(); | 44 stop(); |
42 | 45 |
43 _animation = new AnimationGenerator( | 46 _animation = new AnimationGenerator( |
44 duration: duration, | 47 duration: duration, |
45 begin: _value, | 48 begin: _value, |
46 end: newValue, | 49 end: newValue, |
47 curve: curve, | 50 curve: curve, |
48 initialDelay: initialDelay); | 51 initialDelay: initialDelay); |
49 | 52 |
50 _animation.onTick.listen(_setValue, onDone: () { | 53 _animation.onTick.listen(_setValue, onDone: () { |
51 _animation = null; | 54 _animation = null; |
52 }); | 55 }); |
53 } | 56 } |
54 } | 57 } |
| 58 |
| 59 class AnimatedValueListener { |
| 60 final Component _component; |
| 61 final AnimatedValue _value; |
| 62 StreamSubscription<double> _subscription; |
| 63 |
| 64 AnimatedValueListener(this._component, this._value); |
| 65 |
| 66 double get value => _value == null ? null : _value.value; |
| 67 |
| 68 void ensureListening() { |
| 69 if (_subscription != null || _value == null) |
| 70 return; |
| 71 _subscription = _value.onValueChanged.listen((_) { |
| 72 _component.scheduleBuild(); |
| 73 }); |
| 74 } |
| 75 |
| 76 void stopListening() { |
| 77 if (_subscription == null) |
| 78 return; |
| 79 _subscription.cancel(); |
| 80 _subscription = null; |
| 81 } |
| 82 } |
OLD | NEW |