From cf991a3412ee9e5feb2fc8002791c18f9d02c519 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 4 Apr 2017 17:21:10 -0700 Subject: [PATCH] Code cleanup for physics (#9193) * Add tolerance argument to FrictionSimulation. * Change FrictionSimulation.through to setting tolerance via the constructor rather than afterwards. * Allow SimulationGroup constructor to take tolerance argument. * Add a toString for SpringDescription. * Add a toString for SpringSimulation. * Push this change to BouncingScrollSimulation. --- .../lib/src/physics/friction_simulation.dart | 17 ++++++++++------- .../lib/src/physics/simulation_group.dart | 2 ++ .../lib/src/physics/spring_simulation.dart | 6 ++++++ .../lib/src/widgets/scroll_simulation.dart | 10 ++++++---- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/physics/friction_simulation.dart b/packages/flutter/lib/src/physics/friction_simulation.dart index 32bbb71629..d744900f90 100644 --- a/packages/flutter/lib/src/physics/friction_simulation.dart +++ b/packages/flutter/lib/src/physics/friction_simulation.dart @@ -18,11 +18,13 @@ class FrictionSimulation extends Simulation { /// drag coefficient, a unitless value; the initial position, in the same /// length units as used for [x]; and the initial velocity, in the same /// velocity units as used for [dx]. - FrictionSimulation(double drag, double position, double velocity) - : _drag = drag, - _dragLog = math.log(drag), - _x = position, - _v = velocity; + FrictionSimulation(double drag, double position, double velocity, { + Tolerance tolerance: Tolerance.defaultTolerance, + }) : _drag = drag, + _dragLog = math.log(drag), + _x = position, + _v = velocity, + super(tolerance: tolerance); /// Creates a new friction simulation with its fluid drag coefficient set so /// as to ensure that the simulation starts and ends at the specified @@ -42,8 +44,9 @@ class FrictionSimulation extends Simulation { return new FrictionSimulation( _dragFor(startPosition, endPosition, startVelocity, endVelocity), startPosition, - startVelocity - )..tolerance = new Tolerance(velocity: endVelocity.abs()); + startVelocity, + tolerance: new Tolerance(velocity: endVelocity.abs()), + ); } final double _drag; diff --git a/packages/flutter/lib/src/physics/simulation_group.dart b/packages/flutter/lib/src/physics/simulation_group.dart index bea5648bcc..8145db29a6 100644 --- a/packages/flutter/lib/src/physics/simulation_group.dart +++ b/packages/flutter/lib/src/physics/simulation_group.dart @@ -19,6 +19,8 @@ import 'utils.dart'; /// by this group as they become active. This mean simulations should not be /// shared among different groups that are active at the same time. abstract class SimulationGroup extends Simulation { + /// Initializes the [tolerance] field for subclasses. + SimulationGroup({ Tolerance tolerance: Tolerance.defaultTolerance }) : super(tolerance: tolerance); /// The currently active simulation. /// diff --git a/packages/flutter/lib/src/physics/spring_simulation.dart b/packages/flutter/lib/src/physics/spring_simulation.dart index 376e5124a7..cf234501d9 100644 --- a/packages/flutter/lib/src/physics/spring_simulation.dart +++ b/packages/flutter/lib/src/physics/spring_simulation.dart @@ -53,6 +53,9 @@ class SpringDescription { /// used for the value of the [mass] property, and T is the time unit used for /// driving the [SpringSimulation]. final double damping; + + @override + String toString() => '$runtimeType(mass: ${mass.toStringAsFixed(1)}, springConstant: ${springConstant.toStringAsFixed(1)}, damping: ${damping.toStringAsFixed(1)})'; } /// The kind of spring solution that the [SpringSimulation] is using to simulate the spring. @@ -114,6 +117,9 @@ class SpringSimulation extends Simulation { return nearZero(_solution.x(time), tolerance.distance) && nearZero(_solution.dx(time), tolerance.velocity); } + + @override + String toString() => '$runtimeType(end: $_endPosition, $type)'; } /// A SpringSimulation where the value of [x] is guaranteed to have exactly the diff --git a/packages/flutter/lib/src/widgets/scroll_simulation.dart b/packages/flutter/lib/src/widgets/scroll_simulation.dart index 1955ce7b88..d1f2d2dde1 100644 --- a/packages/flutter/lib/src/widgets/scroll_simulation.dart +++ b/packages/flutter/lib/src/widgets/scroll_simulation.dart @@ -34,9 +34,11 @@ class BouncingScrollSimulation extends SimulationGroup { @required double leadingExtent, @required double trailingExtent, @required SpringDescription spring, + Tolerance tolerance: Tolerance.defaultTolerance, }) : _leadingExtent = leadingExtent, _trailingExtent = trailingExtent, - _spring = spring { + _spring = spring, + super(tolerance: tolerance) { assert(position != null); assert(velocity != null); assert(_leadingExtent != null); @@ -73,15 +75,15 @@ class BouncingScrollSimulation extends SimulationGroup { if (position > _trailingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = new ScrollSpringSimulation(_spring, position, _trailingExtent, velocity); + _currentSimulation = new ScrollSpringSimulation(_spring, position, _trailingExtent, velocity, tolerance: tolerance); return true; } else if (position < _leadingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = new ScrollSpringSimulation(_spring, position, _leadingExtent, velocity); + _currentSimulation = new ScrollSpringSimulation(_spring, position, _leadingExtent, velocity, tolerance: tolerance); return true; } else if (_currentSimulation == null) { - _currentSimulation = new FrictionSimulation(0.135, position, velocity); + _currentSimulation = new FrictionSimulation(0.135, position, velocity, tolerance: tolerance); return true; } }