Commit f7cdfda4 authored by Rafael Weinstein's avatar Rafael Weinstein

Add initialDelay to AnimationGenerator and add Animation class.

This patch allows for an initial delay before an animation begins and also adds an Animation class which encapsulates a value which is long-lived, can be explicitly set and also animated from its current value to another value.

BUG=
R=abarth@chromium.org

Review URL: https://codereview.chromium.org/994143002
parent 42b7a271
......@@ -6,39 +6,34 @@ const double _kBaseSettleDurationMS = 246.0;
const double _kMaxSettleDurationMS = 600.0;
const Cubic _kAnimationCurve = easeOut;
class DrawerAnimation {
class DrawerAnimation extends Animation {
Stream<double> get onPositionChanged => _controller.stream;
Stream<double> get onPositionChanged => onValueChanged;
StreamController _controller;
AnimationGenerator _animation;
double _position;
bool get _isAnimating => _animation != null;
bool get _isMostlyClosed => _position <= -_kWidth / 2;
bool get _isMostlyClosed => value <= -_kWidth / 2;
DrawerAnimation() {
_controller = new StreamController(sync: true);
_setPosition(-_kWidth);
value = -_kWidth;
}
void toggle(_) => _isMostlyClosed ? _open() : _close();
void handleMaskTap(_) => _close();
void handlePointerDown(_) => _cancelAnimation();
void handlePointerDown(_) => stop();
void handlePointerMove(sky.PointerEvent event) {
assert(_animation == null);
_setPosition(_position + event.dx);
assert(!isAnimating);
value = math.min(0.0, math.max(value + event.dx, -_kWidth));
}
void handlePointerUp(_) {
if (!_isAnimating)
if (!isAnimating)
_settle();
}
void handlePointerCancel(_) {
if (!_isAnimating)
if (!isAnimating)
_settle();
}
......@@ -48,35 +43,12 @@ class DrawerAnimation {
void _settle() => _isMostlyClosed ? _close() : _open();
void _setPosition(double value) {
_position = math.min(0.0, math.max(value, -_kWidth));
_controller.add(_position);
}
void _cancelAnimation() {
if (_animation != null) {
_animation.cancel();
_animation = null;
}
}
void _animate(double duration, double begin, double end, Curve curve) {
_cancelAnimation();
_animation = new AnimationGenerator(duration, begin: begin, end: end,
curve: curve);
_animation.onTick.listen(_setPosition, onDone: () {
_animation = null;
});
}
void _animateToPosition(double targetPosition) {
double distance = (targetPosition - _position).abs();
double distance = (targetPosition - value).abs();
if (distance != 0) {
double targetDuration = distance / _kWidth * _kBaseSettleDurationMS;
double duration = math.min(targetDuration, _kMaxSettleDurationMS);
_animate(duration, _position, targetPosition, _kAnimationCurve);
animateTo(targetPosition, duration, curve: _kAnimationCurve);
}
}
......@@ -87,10 +59,10 @@ class DrawerAnimation {
return;
double targetPosition = direction < 0.0 ? -_kWidth : 0.0;
double distance = (targetPosition - _position).abs();
double distance = (targetPosition - value).abs();
double duration = distance / velocityX;
_animate(duration, _position, targetPosition, linear);
animateTo(targetPosition, duration, curve: linear);
}
}
......
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