Commit 46773864 authored by Adam Barth's avatar Adam Barth

fn-drawer sometimes doesn't tick closed

If there is jank, we might not get a frame time that's just after the last
frame, which means we'll stop generating animation frames before hitting 1.0
exactly.

In this CL, we introduce state to takeWhile to cancel the stream after emitting the
1.0.

R=rafaelw@chromium.org

Review URL: https://codereview.chromium.org/975153002
parent 130124bd
...@@ -45,8 +45,6 @@ class FrameGenerator { ...@@ -45,8 +45,6 @@ class FrameGenerator {
} }
} }
const double _kFrameTime = 1000 / 60;
class AnimationGenerator extends FrameGenerator { class AnimationGenerator extends FrameGenerator {
Stream<double> get onTick => _stream; Stream<double> get onTick => _stream;
...@@ -55,6 +53,7 @@ class AnimationGenerator extends FrameGenerator { ...@@ -55,6 +53,7 @@ class AnimationGenerator extends FrameGenerator {
final double end; final double end;
final Curve curve; final Curve curve;
Stream<double> _stream; Stream<double> _stream;
bool _done = false;
AnimationGenerator(this.duration, { AnimationGenerator(this.duration, {
this.begin: 0.0, this.begin: 0.0,
...@@ -64,24 +63,23 @@ class AnimationGenerator extends FrameGenerator { ...@@ -64,24 +63,23 @@ class AnimationGenerator extends FrameGenerator {
}):super(onDone: onDone) { }):super(onDone: onDone) {
double startTime = 0.0; double startTime = 0.0;
double targetTime = 0.0; double targetTime = 0.0;
bool done = false;
_stream = super.onTick.map((timeStamp) { _stream = super.onTick.map((timeStamp) {
if (startTime == 0.0) { if (startTime == 0.0) {
startTime = timeStamp; startTime = timeStamp;
targetTime = startTime + duration; targetTime = startTime + duration;
} }
return math.min((timeStamp - startTime) / duration, 1.0);
// Clamp the final frame to target time so we terminate the series with
// 1.0 exactly.
if ((timeStamp - targetTime).abs() <= _kFrameTime) {
return 1.0;
}
return (timeStamp - startTime) / duration;
}) })
.takeWhile((t) => t <= 1.0) .takeWhile(_checkForCompletion)
.map((t) => begin + (end - begin) * curve.transform(t)); .map((t) => begin + (end - begin) * curve.transform(t));
} }
bool _checkForCompletion(double t) {
if (_done)
return false;
_done = t >= 1;
return true;
}
} }
double _evaluateCubic(double a, double b, double m) { double _evaluateCubic(double a, double b, double m) {
......
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