diff --git a/packages/flutter/lib/src/gestures/events.dart b/packages/flutter/lib/src/gestures/events.dart index f810394888b..a49119ff83c 100644 --- a/packages/flutter/lib/src/gestures/events.dart +++ b/packages/flutter/lib/src/gestures/events.dart @@ -8,14 +8,9 @@ export 'dart:ui' show Point; /// Base class for input events. class InputEvent { - - const InputEvent({ this.type, this.timeStamp: 0.0 }); - + const InputEvent({ this.type, this.timeStamp: const Duration() }); final String type; - // TODO: Should timeStamp be a DateTime object instead of double? - // Some client code (e.g. drag.dart) does math on the time stamp. - final double timeStamp; - + final Duration timeStamp; } /// Input event representing a touch or button. @@ -23,7 +18,7 @@ class PointerInputEvent extends InputEvent { const PointerInputEvent({ String type, - double timeStamp: 0.0, + Duration timeStamp: const Duration(), this.pointer: 0, this.kind, this.x: 0.0, diff --git a/packages/flutter/lib/src/gestures/velocity_tracker.dart b/packages/flutter/lib/src/gestures/velocity_tracker.dart index 81f5bcfda6d..5c2555be1d7 100644 --- a/packages/flutter/lib/src/gestures/velocity_tracker.dart +++ b/packages/flutter/lib/src/gestures/velocity_tracker.dart @@ -16,7 +16,7 @@ class GestureVelocity { class _Estimator { int degree; - double time; + Duration time; List xCoefficients; List yCoefficients; double confidence; @@ -32,7 +32,7 @@ class _Estimator { } abstract class _VelocityTrackerStrategy { - void addMovement(double timeStamp, double x, double y); + void addMovement(Duration timeStamp, double x, double y); bool getEstimator(_Estimator estimator); void clear(); } @@ -45,7 +45,7 @@ enum _Weighting { } class _Movement { - double eventTime = 0.0; + Duration eventTime = const Duration(); ui.Point position = ui.Point.origin; } @@ -61,7 +61,7 @@ class _LeastSquaresVelocityTrackerStrategy extends _VelocityTrackerStrategy { final List<_Movement> _movements; int _index; - void addMovement(double timeStamp, double x, double y) { + void addMovement(Duration timeStamp, double x, double y) { if (++_index == kHistorySize) _index = 0; _Movement movement = _getMovement(_index); @@ -81,7 +81,7 @@ class _LeastSquaresVelocityTrackerStrategy extends _VelocityTrackerStrategy { do { _Movement movement = _getMovement(index); - double age = newestMovement.eventTime - movement.eventTime; + double age = (newestMovement.eventTime - movement.eventTime).inMilliseconds.toDouble(); if (age > kHorizonMilliseconds) break; @@ -143,8 +143,7 @@ class _LeastSquaresVelocityTrackerStrategy extends _VelocityTrackerStrategy { return 1.0; } int nextIndex = (index + 1) % kHistorySize; - double deltaMilliseconds = _movements[nextIndex].eventTime - - _movements[index].eventTime; + int deltaMilliseconds = (_movements[nextIndex].eventTime - _movements[index].eventTime).inMilliseconds; if (deltaMilliseconds < 0) return 0.5; if (deltaMilliseconds < 10) @@ -159,8 +158,7 @@ class _LeastSquaresVelocityTrackerStrategy extends _VelocityTrackerStrategy { // age 10ms: 1.0 // age 50ms: 1.0 // age 60ms: 0.5 - double ageMilliseconds = _movements[_index].eventTime - - _movements[index].eventTime; + int ageMilliseconds = (_movements[_index].eventTime - _movements[index].eventTime).inMilliseconds; if (ageMilliseconds < 0) return 0.5; if (ageMilliseconds < 10) @@ -177,8 +175,7 @@ class _LeastSquaresVelocityTrackerStrategy extends _VelocityTrackerStrategy { // age 0ms: 1.0 // age 50ms: 1.0 // age 100ms: 0.5 - double ageMilliseconds = _movements[_index].eventTime - - _movements[index].eventTime; + int ageMilliseconds = (_movements[_index].eventTime - _movements[index].eventTime).inMilliseconds; if (ageMilliseconds < 50) { return 1.0; } @@ -207,13 +204,13 @@ class _LeastSquaresVelocityTrackerStrategy extends _VelocityTrackerStrategy { class VelocityTracker { static const int kAssumePointerMoveStoppedTimeMs = 40; - VelocityTracker() : _lastTimeStamp = 0.0, _strategy = _createStrategy(); + VelocityTracker() : _strategy = _createStrategy(); - double _lastTimeStamp; + Duration _lastTimeStamp = const Duration(); _VelocityTrackerStrategy _strategy; - void addPosition(double timeStamp, double x, double y) { - if ((timeStamp - _lastTimeStamp) >= kAssumePointerMoveStoppedTimeMs) + void addPosition(Duration timeStamp, double x, double y) { + if ((timeStamp - _lastTimeStamp).inMilliseconds >= kAssumePointerMoveStoppedTimeMs) _strategy.clear(); _lastTimeStamp = timeStamp; _strategy.addMovement(timeStamp, x, y); diff --git a/packages/flutter/lib/src/rendering/binding.dart b/packages/flutter/lib/src/rendering/binding.dart index 2f0d84945d7..b3d9cfb7047 100644 --- a/packages/flutter/lib/src/rendering/binding.dart +++ b/packages/flutter/lib/src/rendering/binding.dart @@ -90,7 +90,7 @@ class _PointerEventConverter { return new PointerInputEvent( type: eventType, - timeStamp: pointer.timeStamp.toDouble(), + timeStamp: new Duration(microseconds: pointer.timeStamp), pointer: pointerIndex, kind: _mapPointerKindToString(pointer.kind), x: pointer.x, @@ -200,11 +200,12 @@ class FlutterBinding extends HitTestTarget { /// Stops calling listener for every event that isn't localized to a given view coordinate bool removeEventListener(EventListener listener) => _eventListeners.remove(listener); + // TODO(abarth): The engine should give us the timeStamp in Durations. void _handleEvent(String eventType, double timeStamp) { assert(eventType == 'back'); InputEvent ourEvent = new InputEvent( type: eventType, - timeStamp: timeStamp + timeStamp: new Duration(microseconds: timeStamp.round()) ); for (EventListener listener in _eventListeners) listener(ourEvent); diff --git a/packages/unit/test/engine/mock_events.dart b/packages/unit/test/engine/mock_events.dart index cbe55d015b2..0255708bfbb 100644 --- a/packages/unit/test/engine/mock_events.dart +++ b/packages/unit/test/engine/mock_events.dart @@ -11,7 +11,7 @@ class TestPointer { bool isDown = false; ui.Point location; - PointerInputEvent down(ui.Point newLocation, { double timeStamp: 0.0 }) { + PointerInputEvent down(ui.Point newLocation, { Duration timeStamp: const Duration() }) { assert(!isDown); isDown = true; location = newLocation; @@ -24,7 +24,7 @@ class TestPointer { ); } - PointerInputEvent move(ui.Point newLocation, { double timeStamp: 0.0 }) { + PointerInputEvent move(ui.Point newLocation, { Duration timeStamp: const Duration() }) { assert(isDown); ui.Offset delta = newLocation - location; location = newLocation; @@ -39,7 +39,7 @@ class TestPointer { ); } - PointerInputEvent up({ double timeStamp: 0.0 }) { + PointerInputEvent up({ Duration timeStamp: const Duration() }) { assert(isDown); isDown = false; return new PointerInputEvent( @@ -51,7 +51,7 @@ class TestPointer { ); } - PointerInputEvent cancel({ double timeStamp: 0.0 }) { + PointerInputEvent cancel({ Duration timeStamp: const Duration() }) { assert(isDown); isDown = false; return new PointerInputEvent( diff --git a/packages/unit/test/gestures/velocity_tracker_test.dart b/packages/unit/test/gestures/velocity_tracker_test.dart index 0c6a1f1b98e..a14aa8d3ece 100644 --- a/packages/unit/test/gestures/velocity_tracker_test.dart +++ b/packages/unit/test/gestures/velocity_tracker_test.dart @@ -24,7 +24,7 @@ List _eventFromMap(List intermediate) { PointerInputEvent _eventFor(Map entry) { PointerInputEvent result = new PointerInputEvent( type: entry['type'], - timeStamp: entry['timeStamp'], + timeStamp: new Duration(milliseconds: entry['timeStamp'].round()), pointer: entry['pointer'], x: entry['x'], y: entry['y'] diff --git a/packages/unit/test/widget/widget_tester.dart b/packages/unit/test/widget/widget_tester.dart index 002d896b9c7..4a2ea3ccdef 100644 --- a/packages/unit/test/widget/widget_tester.dart +++ b/packages/unit/test/widget/widget_tester.dart @@ -165,13 +165,13 @@ class WidgetTester { final kMoveCount = 50; // Needs to be >= kHistorySize, see _LeastSquaresVelocityTrackerStrategy final double timeStampDelta = 1000.0 * offset.distance / (kMoveCount * velocity); double timeStamp = 0.0; - _dispatchEvent(p.down(startLocation, timeStamp: timeStamp), result); + _dispatchEvent(p.down(startLocation, timeStamp: new Duration(milliseconds: timeStamp.round())), result); for(int i = 0; i < kMoveCount; i++) { final Point location = startLocation + Offset.lerp(Offset.zero, offset, i / kMoveCount); - _dispatchEvent(p.move(location, timeStamp: timeStamp), result); + _dispatchEvent(p.move(location, timeStamp: new Duration(milliseconds: timeStamp.round())), result); timeStamp += timeStampDelta; } - _dispatchEvent(p.up(timeStamp: timeStamp), result); + _dispatchEvent(p.up(timeStamp: new Duration(milliseconds: timeStamp.round())), result); } void scroll(Element element, Offset offset, { int pointer: 1 }) {