Commit 169dea32 authored by Adam Barth's avatar Adam Barth

Move animationgenerator.dart to sky/framework/animation/generator.dart

That's where it belongs.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/979283002
parent 55421096
part of widgets;
class FrameGenerator {
Function onDone;
StreamController _controller;
Stream<double> get onTick => _controller.stream;
int _animationId = 0;
bool _cancelled = false;
FrameGenerator({this.onDone}) {
_controller = new StreamController(
sync: true,
onListen: _scheduleTick,
onCancel: cancel);
}
void cancel() {
if (_cancelled) {
return;
}
if (_animationId != 0) {
sky.window.cancelAnimationFrame(_animationId);
}
_animationId = 0;
_cancelled = true;
if (onDone != null) {
onDone();
}
}
void _scheduleTick() {
assert(_animationId == 0);
_animationId = sky.window.requestAnimationFrame(_tick);
}
void _tick(double timeStamp) {
_animationId = 0;
_controller.add(timeStamp);
if (!_cancelled) {
_scheduleTick();
}
}
}
class AnimationGenerator extends FrameGenerator {
Stream<double> get onTick => _stream;
final double duration;
final double begin;
final double end;
final Curve curve;
Stream<double> _stream;
bool _done = false;
AnimationGenerator(this.duration, {
this.begin: 0.0,
this.end: 1.0,
this.curve: linear,
Function onDone
}):super(onDone: onDone) {
double startTime = 0.0;
double targetTime = 0.0;
_stream = super.onTick.map((timeStamp) {
if (startTime == 0.0) {
startTime = timeStamp;
targetTime = startTime + duration;
}
return math.min((timeStamp - startTime) / duration, 1.0);
})
.takeWhile(_checkForCompletion)
.map((t) => begin + (end - begin) * curve.transform(t));
}
bool _checkForCompletion(double t) {
if (_done)
return false;
_done = t >= 1;
return true;
}
}
...@@ -2,6 +2,7 @@ library widgets; ...@@ -2,6 +2,7 @@ library widgets;
import '../../../framework/animation/curves.dart'; import '../../../framework/animation/curves.dart';
import '../../../framework/animation/fling-curve.dart'; import '../../../framework/animation/fling-curve.dart';
import '../../../framework/animation/generator.dart';
import '../../../framework/fn.dart'; import '../../../framework/fn.dart';
import '../../../framework/theme/colors.dart'; import '../../../framework/theme/colors.dart';
import '../../../framework/theme/shadows.dart'; import '../../../framework/theme/shadows.dart';
...@@ -10,7 +11,6 @@ import 'dart:async'; ...@@ -10,7 +11,6 @@ import 'dart:async';
import 'dart:math' as math; import 'dart:math' as math;
import 'dart:sky' as sky; import 'dart:sky' as sky;
part 'animationgenerator.dart';
part 'box.dart'; part 'box.dart';
part 'button.dart'; part 'button.dart';
part 'buttonbase.dart'; part 'buttonbase.dart';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment