Commit 24b40d87 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Make getVelocity never return null (#9583)

Fixes https://github.com/flutter/flutter/issues/8425
parent ad1c497c
...@@ -123,7 +123,7 @@ abstract class MultiDragPointerState { ...@@ -123,7 +123,7 @@ abstract class MultiDragPointerState {
assert(_arenaEntry != null); assert(_arenaEntry != null);
if (_client != null) { if (_client != null) {
assert(pendingDelta == null); assert(pendingDelta == null);
final DragEndDetails details = new DragEndDetails(velocity: _velocityTracker.getVelocity() ?? Velocity.zero); final DragEndDetails details = new DragEndDetails(velocity: _velocityTracker.getVelocity());
final Drag client = _client; final Drag client = _client;
_client = null; _client = null;
// Call client last to avoid reentrancy. // Call client last to avoid reentrancy.
......
...@@ -193,7 +193,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { ...@@ -193,7 +193,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
assert(tracker != null); assert(tracker != null);
Velocity velocity = tracker.getVelocity(); Velocity velocity = tracker.getVelocity();
if (velocity != null && _isFlingGesture(velocity)) { if (_isFlingGesture(velocity)) {
final Offset pixelsPerSecond = velocity.pixelsPerSecond; final Offset pixelsPerSecond = velocity.pixelsPerSecond;
if (pixelsPerSecond.distanceSquared > kMaxFlingVelocity * kMaxFlingVelocity) if (pixelsPerSecond.distanceSquared > kMaxFlingVelocity * kMaxFlingVelocity)
velocity = new Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * kMaxFlingVelocity); velocity = new Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * kMaxFlingVelocity);
......
...@@ -125,18 +125,16 @@ class _PointAtTime { ...@@ -125,18 +125,16 @@ class _PointAtTime {
String toString() => '_PointAtTime($point at $time)'; String toString() => '_PointAtTime($point at $time)';
} }
/// Computes a pointer's velocity based on data from PointerMove events. /// Computes a pointer's velocity based on data from [PointerMove] events.
/// ///
/// The input data is provided by calling addPosition(). Adding data /// The input data is provided by calling [addPosition]. Adding data is cheap.
/// is cheap.
/// ///
/// To obtain a velocity, call [getVelocity] or [getVelocityEstimate]. /// To obtain a velocity, call [getVelocity] or [getVelocityEstimate]. This will
/// This will compute the velocity based on the data added so far. Only /// compute the velocity based on the data added so far. Only call these when
/// call this when you need to use the velocity, as it is comparatively /// you need to use the velocity, as they are comparatively expensive.
/// expensive.
/// ///
/// The quality of the velocity estimation will be better if more data /// The quality of the velocity estimation will be better if more data points
/// points have been received. /// have been received.
class VelocityTracker { class VelocityTracker {
static const int _kAssumePointerMoveStoppedMilliseconds = 40; static const int _kAssumePointerMoveStoppedMilliseconds = 40;
static const int _kHistorySize = 20; static const int _kHistorySize = 20;
...@@ -157,6 +155,10 @@ class VelocityTracker { ...@@ -157,6 +155,10 @@ class VelocityTracker {
/// Returns an estimate of the velocity of the object being tracked by the /// Returns an estimate of the velocity of the object being tracked by the
/// tracker given the current information available to the tracker. /// tracker given the current information available to the tracker.
///
/// Information is added using [addPosition].
///
/// Returns null if there is no data on which to base an estimate.
VelocityEstimate getVelocityEstimate() { VelocityEstimate getVelocityEstimate() {
final List<double> x = <double>[]; final List<double> x = <double>[];
final List<double> y = <double>[]; final List<double> y = <double>[];
...@@ -228,12 +230,12 @@ class VelocityTracker { ...@@ -228,12 +230,12 @@ class VelocityTracker {
/// ///
/// This can be expensive. Only call this when you need the velocity. /// This can be expensive. Only call this when you need the velocity.
/// ///
/// getVelocity() will return null if no estimate is available or if /// Returns [Velocity.zero] if there is no data from which to compute an
/// the velocity is zero. /// estimate or if the estimated velocity is zero.
Velocity getVelocity() { Velocity getVelocity() {
final VelocityEstimate estimate = getVelocityEstimate(); final VelocityEstimate estimate = getVelocityEstimate();
if (estimate == null || estimate.pixelsPerSecond == Offset.zero) if (estimate == null || estimate.pixelsPerSecond == Offset.zero)
return null; return Velocity.zero;
return new Velocity(pixelsPerSecond: estimate.pixelsPerSecond); return new Velocity(pixelsPerSecond: estimate.pixelsPerSecond);
} }
} }
...@@ -71,4 +71,9 @@ void main() { ...@@ -71,4 +71,9 @@ void main() {
} }
} }
}); });
test('No data velocity estimation', () {
final VelocityTracker tracker = new VelocityTracker();
expect(tracker.getVelocity(), Velocity.zero);
});
} }
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