Remove AnimatedSimulation This patch folds the functionality from AnimatedSimulation into Timeline.
diff --git a/packages/flutter/lib/animation.dart b/packages/flutter/lib/animation.dart index f35b253..564f8cd 100644 --- a/packages/flutter/lib/animation.dart +++ b/packages/flutter/lib/animation.dart
@@ -7,7 +7,6 @@ /// This library depends only on core Dart libraries and the `newton` package. library animation; -export 'src/animation/animated_simulation.dart'; export 'src/animation/animated_value.dart'; export 'src/animation/animation_performance.dart'; export 'src/animation/clamped_simulation.dart'; @@ -15,4 +14,5 @@ export 'src/animation/forces.dart'; export 'src/animation/scheduler.dart'; export 'src/animation/scroll_behavior.dart'; -export 'src/animation/timeline.dart'; +export 'src/animation/simulation_stepper.dart'; +export 'src/animation/ticker.dart';
diff --git a/packages/flutter/lib/src/animation/animated_simulation.dart b/packages/flutter/lib/src/animation/animated_simulation.dart deleted file mode 100644 index 32f0a91..0000000 --- a/packages/flutter/lib/src/animation/animated_simulation.dart +++ /dev/null
@@ -1,134 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -import 'dart:async'; - -import 'package:newton/newton.dart'; -import 'package:sky/src/animation/scheduler.dart'; - -typedef _TickerCallback(Duration elapsed); - -/// Calls its callback once per animation frame -class Ticker { - /// Constructs a ticker that will call onTick once per frame while running - Ticker(_TickerCallback onTick) : _onTick = onTick; - - final _TickerCallback _onTick; - - Completer _completer; - int _animationId; - Duration _startTime; - - /// Start calling onTick once per animation frame - /// - /// The returned future resolves once the ticker stops ticking. - Future start() { - assert(!isTicking); - assert(_startTime == null); - _completer = new Completer(); - _scheduleTick(); - return _completer.future; - } - - /// Stop calling onTick - /// - /// Causes the future returned by [start] to resolve. - void stop() { - if (!isTicking) - return; - - _startTime = null; - - if (_animationId != null) { - scheduler.cancelAnimationFrame(_animationId); - _animationId = null; - } - - // We take the _completer into a local variable so that !isTicking - // when we actually complete the future (isTicking uses _completer - // to determine its state). - Completer localCompleter = _completer; - _completer = null; - assert(!isTicking); - localCompleter.complete(); - } - - /// Whether this ticker has scheduled a call to onTick - bool get isTicking => _completer != null; - - void _tick(Duration timeStamp) { - assert(isTicking); - assert(_animationId != null); - _animationId = null; - - if (_startTime == null) - _startTime = timeStamp; - - _onTick(timeStamp - _startTime); - - // The onTick callback may have scheduled another tick already. - if (isTicking && _animationId == null) - _scheduleTick(); - } - - void _scheduleTick() { - assert(isTicking); - assert(_animationId == null); - _animationId = scheduler.requestAnimationFrame(_tick); - } -} - -/// Ticks a simulation once per frame -class AnimatedSimulation { - - AnimatedSimulation(Function onTick) : _onTick = onTick { - _ticker = new Ticker(_tick); - } - - final Function _onTick; - Ticker _ticker; - - Simulation _simulation; - - double _value = 0.0; - /// The current value of the simulation - double get value => _value; - void set value(double newValue) { - assert(!_ticker.isTicking); - _value = newValue; - _onTick(_value); - } - - /// Start ticking the given simulation once per frame - /// - /// Returns a future that resolves when the simulation stops ticking. - Future start(Simulation simulation) { - assert(simulation != null); - assert(!_ticker.isTicking); - _simulation = simulation; - _value = simulation.x(0.0); - return _ticker.start(); - } - - /// Stop ticking the current simulation - void stop() { - _simulation = null; - _ticker.stop(); - } - - /// Whether this object is currently ticking a simulation - bool get isAnimating => _ticker.isTicking; - - void _tick(Duration elapsed) { - - double elapsedInSeconds = elapsed.inMicroseconds.toDouble() / Duration.MICROSECONDS_PER_SECOND; - _value = _simulation.x(elapsedInSeconds); - - if (_simulation.isDone(elapsedInSeconds)) - stop(); - - _onTick(_value); - } - -}
diff --git a/packages/flutter/lib/src/animation/animation_performance.dart b/packages/flutter/lib/src/animation/animation_performance.dart index 8111754..a3340d4 100644 --- a/packages/flutter/lib/src/animation/animation_performance.dart +++ b/packages/flutter/lib/src/animation/animation_performance.dart
@@ -6,7 +6,7 @@ import 'package:sky/src/animation/animated_value.dart'; import 'package:sky/src/animation/forces.dart'; -import 'package:sky/src/animation/timeline.dart'; +import 'package:sky/src/animation/simulation_stepper.dart'; /// The status of an animation enum AnimationStatus { @@ -53,7 +53,7 @@ /// progression. class AnimationPerformance implements WatchableAnimationPerformance { AnimationPerformance({ this.duration, double progress }) { - _timeline = new Timeline(_tick); + _timeline = new SimulationStepper(_tick); if (progress != null) _timeline.value = progress.clamp(0.0, 1.0); } @@ -66,7 +66,7 @@ /// The length of time this performance should last Duration duration; - Timeline _timeline; + SimulationStepper _timeline; Direction _direction; /// The direction used to select the current curve @@ -159,7 +159,7 @@ if (force == null) force = kDefaultSpringForce; _direction = velocity < 0.0 ? Direction.reverse : Direction.forward; - return _timeline.fling(force.release(progress, velocity)); + return _timeline.animateWith(force.release(progress, velocity)); } final List<AnimationPerformanceListener> _listeners = new List<AnimationPerformanceListener>();
diff --git a/packages/flutter/lib/src/animation/simulation_stepper.dart b/packages/flutter/lib/src/animation/simulation_stepper.dart new file mode 100644 index 0000000..39ff00c --- /dev/null +++ b/packages/flutter/lib/src/animation/simulation_stepper.dart
@@ -0,0 +1,105 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; + +import 'package:newton/newton.dart'; +import 'package:sky/src/animation/animated_value.dart'; +import 'package:sky/src/animation/curves.dart'; +import 'package:sky/src/animation/ticker.dart'; + +/// A simulation that varies from [begin] to [end] over [duration] using [curve] +/// +/// This class is an adaptor between the Simulation interface and the +/// AnimatedValue interface. +class _TweenSimulation extends Simulation { + _TweenSimulation(double begin, double end, Duration duration, Curve curve) + : _durationInSeconds = duration.inMicroseconds.toDouble() / Duration.MICROSECONDS_PER_SECOND, + _tween = new AnimatedValue<double>(begin, end: end, curve: curve) { + assert(_durationInSeconds > 0.0); + assert(begin != null); + assert(end != null); + } + + final double _durationInSeconds; + final AnimatedValue<double> _tween; + + double x(double timeInSeconds) { + assert(timeInSeconds >= 0.0); + final double t = (timeInSeconds / _durationInSeconds).clamp(0.0, 1.0); + _tween.setProgress(t, Direction.forward); + return _tween.value; + } + + double dx(double timeInSeconds) => 1.0; + + bool isDone(double timeInSeconds) => timeInSeconds > _durationInSeconds; +} + +typedef TimelineCallback(double value); + +/// Steps a simulation one per frame +class SimulationStepper { + SimulationStepper(TimelineCallback onTick) : _onTick = onTick { + _ticker = new Ticker(_tick); + } + + final TimelineCallback _onTick; + Ticker _ticker; + Simulation _simulation; + + /// The current value of the timeline + double get value => _value; + double _value = 0.0; + void set value(double newValue) { + assert(newValue != null); + assert(!isAnimating); + _value = newValue; + _onTick(_value); + } + + /// Whether the timeline is currently animating + bool get isAnimating => _ticker.isTicking; + + /// Animate value of the timeline to the given target over the given duration + /// + /// Returns a future that resolves when the timeline stops animating, + /// typically when the timeline arives at the target value. + Future animateTo(double target, { Duration duration, Curve curve: linear }) { + assert(duration > Duration.ZERO); + assert(!isAnimating); + return _start(new _TweenSimulation(value, target, duration, curve)); + } + + /// Gives the given simulation control over the timeline + Future animateWith(Simulation simulation) { + stop(); + return _start(simulation); + } + + /// Start ticking the given simulation once per frame + /// + /// Returns a future that resolves when the simulation stops ticking. + Future _start(Simulation simulation) { + assert(simulation != null); + assert(!isAnimating); + _simulation = simulation; + _value = simulation.x(0.0); + return _ticker.start(); + } + + /// Stop animating the timeline + void stop() { + _simulation = null; + _ticker.stop(); + } + + void _tick(Duration elapsed) { + double elapsedInSeconds = elapsed.inMicroseconds.toDouble() / Duration.MICROSECONDS_PER_SECOND; + _value = _simulation.x(elapsedInSeconds); + if (_simulation.isDone(elapsedInSeconds)) + stop(); + _onTick(_value); + } +}
diff --git a/packages/flutter/lib/src/animation/ticker.dart b/packages/flutter/lib/src/animation/ticker.dart new file mode 100644 index 0000000..6c0dd32 --- /dev/null +++ b/packages/flutter/lib/src/animation/ticker.dart
@@ -0,0 +1,79 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; + +import 'package:sky/src/animation/scheduler.dart'; + +typedef TickerCallback(Duration elapsed); + +/// Calls its callback once per animation frame +class Ticker { + /// Constructs a ticker that will call onTick once per frame while running + Ticker(TickerCallback onTick) : _onTick = onTick; + + final TickerCallback _onTick; + + Completer _completer; + int _animationId; + Duration _startTime; + + /// Start calling onTick once per animation frame + /// + /// The returned future resolves once the ticker stops ticking. + Future start() { + assert(!isTicking); + assert(_startTime == null); + _completer = new Completer(); + _scheduleTick(); + return _completer.future; + } + + /// Stop calling onTick + /// + /// Causes the future returned by [start] to resolve. + void stop() { + if (!isTicking) + return; + + _startTime = null; + + if (_animationId != null) { + scheduler.cancelAnimationFrame(_animationId); + _animationId = null; + } + + // We take the _completer into a local variable so that isTicking is false + // when we actually complete the future (isTicking uses _completer + // to determine its state). + Completer localCompleter = _completer; + _completer = null; + assert(!isTicking); + localCompleter.complete(); + } + + /// Whether this ticker has scheduled a call to onTick + bool get isTicking => _completer != null; + + void _tick(Duration timeStamp) { + assert(isTicking); + assert(_animationId != null); + _animationId = null; + + if (_startTime == null) + _startTime = timeStamp; + + _onTick(timeStamp - _startTime); + + // The onTick callback may have scheduled another tick already. + if (isTicking && _animationId == null) + _scheduleTick(); + } + + void _scheduleTick() { + assert(isTicking); + assert(_animationId == null); + _animationId = scheduler.requestAnimationFrame(_tick); + } +}
diff --git a/packages/flutter/lib/src/animation/timeline.dart b/packages/flutter/lib/src/animation/timeline.dart deleted file mode 100644 index 29005c7..0000000 --- a/packages/flutter/lib/src/animation/timeline.dart +++ /dev/null
@@ -1,76 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -import 'dart:async'; - -import 'package:newton/newton.dart'; -import 'package:sky/src/animation/curves.dart'; -import 'package:sky/src/animation/animated_value.dart'; -import 'package:sky/src/animation/animated_simulation.dart'; - -/// A simulation that linearly varies from [begin] to [end] over [duration] -class _TweenSimulation extends Simulation { - final double _durationInSeconds; - final AnimatedValue<double> _tween; - - _TweenSimulation(double begin, double end, Duration duration, Curve curve) - : _durationInSeconds = duration.inMicroseconds / Duration.MICROSECONDS_PER_SECOND, - _tween = new AnimatedValue<double>(begin, end: end, curve: curve) { - assert(_durationInSeconds > 0.0); - assert(begin != null); - assert(end != null); - } - - double x(double timeInSeconds) { - assert(timeInSeconds >= 0.0); - final double t = (timeInSeconds / _durationInSeconds).clamp(0.0, 1.0); - _tween.setProgress(t, Direction.forward); - return _tween.value; - } - - double dx(double timeInSeconds) => 1.0; - - bool isDone(double timeInSeconds) => timeInSeconds > _durationInSeconds; -} - -/// A timeline for an animation -class Timeline { - Timeline(Function onTick) { - _animation = new AnimatedSimulation(onTick); - } - - AnimatedSimulation _animation; - - /// The current value of the timeline - double get value => _animation.value; - void set value(double newValue) { - assert(newValue != null); - assert(!isAnimating); - _animation.value = newValue; - } - - /// Whether the timeline is currently animating - bool get isAnimating => _animation.isAnimating; - - /// Animate value of the timeline to the given target over the given duration - /// - /// Returns a future that resolves when the timeline stops animating, - /// typically when the timeline arives at the target value. - Future animateTo(double target, { Duration duration, Curve curve: linear }) { - assert(duration > Duration.ZERO); - assert(!_animation.isAnimating); - return _animation.start(new _TweenSimulation(value, target, duration, curve)); - } - - /// Stop animating the timeline - void stop() { - _animation.stop(); - } - - /// Gives the given simulation control over the timeline - Future fling(Simulation simulation) { - stop(); - return _animation.start(simulation); - } -}
diff --git a/packages/flutter/lib/src/widgets/scrollable.dart b/packages/flutter/lib/src/widgets/scrollable.dart index e09fe4f..cf1693a 100644 --- a/packages/flutter/lib/src/widgets/scrollable.dart +++ b/packages/flutter/lib/src/widgets/scrollable.dart
@@ -51,10 +51,10 @@ super.initState(); if (config.initialScrollOffset is double) _scrollOffset = config.initialScrollOffset; - _animation = new Timeline(_setScrollOffset); + _animation = new SimulationStepper(_setScrollOffset); } - Timeline _animation; + SimulationStepper _animation; double _scrollOffset = 0.0; double get scrollOffset => _scrollOffset; @@ -151,7 +151,7 @@ _createSnapSimulation(velocity) ?? _createFlingSimulation(velocity ?? 0.0); if (simulation == null) return new Future.value(); - return _animation.fling(simulation); + return _animation.animateWith(simulation); } void dispose() {
diff --git a/packages/flutter/lib/src/widgets/tabs.dart b/packages/flutter/lib/src/widgets/tabs.dart index c00bc8b..19b9c67 100644 --- a/packages/flutter/lib/src/widgets/tabs.dart +++ b/packages/flutter/lib/src/widgets/tabs.dart
@@ -13,7 +13,6 @@ import 'package:sky/rendering.dart'; import 'package:sky/src/widgets/basic.dart'; import 'package:sky/src/widgets/framework.dart'; -import 'package:sky/src/widgets/gesture_detector.dart'; import 'package:sky/src/widgets/icon.dart'; import 'package:sky/src/widgets/ink_well.dart'; import 'package:sky/src/widgets/scrollable.dart';