| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:sky' as sky; | 5 import 'dart:sky' as sky; |
| 6 | 6 |
| 7 import '../animation/generators.dart'; | 7 import 'package:newton/newton.dart'; |
| 8 import '../animation/mechanics.dart'; | 8 |
| 9 import '../animation/animated_simulation.dart'; |
| 9 import '../animation/scroll_behavior.dart'; | 10 import '../animation/scroll_behavior.dart'; |
| 10 import '../theme/view_configuration.dart' as config; | 11 import '../theme/view_configuration.dart' as config; |
| 11 import 'basic.dart'; | 12 import 'basic.dart'; |
| 12 import 'material.dart'; | 13 import 'material.dart'; |
| 13 | 14 |
| 14 const double _kMillisecondsPerSecond = 1000.0; | 15 const double _kMillisecondsPerSecond = 1000.0; |
| 15 | 16 |
| 16 double _velocityForFlingGesture(double eventVelocity) { | 17 double _velocityForFlingGesture(double eventVelocity) { |
| 17 return eventVelocity.clamp(-config.kMaxFlingVelocity, config.kMaxFlingVelocity
) / | 18 return eventVelocity.clamp(-config.kMaxFlingVelocity, config.kMaxFlingVelocity
) / |
| 18 _kMillisecondsPerSecond; | 19 _kMillisecondsPerSecond; |
| 19 } | 20 } |
| 20 | 21 |
| 21 abstract class ScrollClient { | 22 abstract class ScrollClient { |
| 22 bool ancestorScrolled(Scrollable ancestor); | 23 bool ancestorScrolled(Scrollable ancestor); |
| 23 } | 24 } |
| 24 | 25 |
| 25 enum ScrollDirection { vertical, horizontal } | 26 enum ScrollDirection { vertical, horizontal } |
| 26 | 27 |
| 27 abstract class Scrollable extends StatefulComponent { | 28 abstract class Scrollable extends StatefulComponent { |
| 28 | 29 |
| 29 Scrollable({ | 30 Scrollable({ |
| 30 String key, | 31 String key, |
| 31 this.backgroundColor, | 32 this.backgroundColor, |
| 32 this.direction: ScrollDirection.vertical | 33 this.direction: ScrollDirection.vertical |
| 33 }) : super(key: key); | 34 }) : super(key: key) { |
| 35 _animation = new AnimatedSimulation(_tickScrollOffset); |
| 36 } |
| 34 | 37 |
| 35 Color backgroundColor; | 38 Color backgroundColor; |
| 36 ScrollDirection direction; | 39 ScrollDirection direction; |
| 37 | 40 |
| 38 void syncFields(Scrollable source) { | 41 void syncFields(Scrollable source) { |
| 39 backgroundColor = source.backgroundColor; | 42 backgroundColor = source.backgroundColor; |
| 40 direction == source.direction; | 43 direction == source.direction; |
| 41 } | 44 } |
| 42 | 45 |
| 43 double _scrollOffset = 0.0; | 46 double _scrollOffset = 0.0; |
| 44 double get scrollOffset => _scrollOffset; | 47 double get scrollOffset => _scrollOffset; |
| 45 | 48 |
| 46 ScrollBehavior _scrollBehavior; | 49 ScrollBehavior _scrollBehavior; |
| 47 ScrollBehavior createScrollBehavior(); | 50 ScrollBehavior createScrollBehavior(); |
| 48 ScrollBehavior get scrollBehavior { | 51 ScrollBehavior get scrollBehavior { |
| 49 if (_scrollBehavior == null) | 52 if (_scrollBehavior == null) |
| 50 _scrollBehavior = createScrollBehavior(); | 53 _scrollBehavior = createScrollBehavior(); |
| 51 return _scrollBehavior; | 54 return _scrollBehavior; |
| 52 } | 55 } |
| 53 | 56 |
| 54 Simulation _simulation; | 57 AnimatedSimulation _animation; |
| 55 | 58 |
| 56 Widget buildContent(); | 59 Widget buildContent(); |
| 57 | 60 |
| 58 Widget build() { | 61 Widget build() { |
| 59 return new Listener( | 62 return new Listener( |
| 60 child: new Material( | 63 child: new Material( |
| 61 type: MaterialType.canvas, | 64 type: MaterialType.canvas, |
| 62 child: buildContent(), | 65 child: buildContent(), |
| 63 color: backgroundColor | 66 color: backgroundColor |
| 64 ), | 67 ), |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 var newScrollOffset = scrollBehavior.applyCurve(_scrollOffset, scrollDelta); | 119 var newScrollOffset = scrollBehavior.applyCurve(_scrollOffset, scrollDelta); |
| 117 return scrollTo(newScrollOffset); | 120 return scrollTo(newScrollOffset); |
| 118 } | 121 } |
| 119 | 122 |
| 120 void didUnmount() { | 123 void didUnmount() { |
| 121 _stopSimulation(); | 124 _stopSimulation(); |
| 122 super.didUnmount(); | 125 super.didUnmount(); |
| 123 } | 126 } |
| 124 | 127 |
| 125 void settleScrollOffset() { | 128 void settleScrollOffset() { |
| 126 _startSimulation(_createParticle()); | 129 _startSimulation(); |
| 127 } | 130 } |
| 128 | 131 |
| 129 void _stopSimulation() { | 132 void _stopSimulation() { |
| 130 if (_simulation == null) | 133 _animation.stop(); |
| 131 return; | |
| 132 _simulation.cancel(); | |
| 133 _simulation = null; | |
| 134 } | 134 } |
| 135 | 135 |
| 136 void _startSimulation(Particle particle) { | 136 void _startSimulation({ double velocity: 0.0 }) { |
| 137 _stopSimulation(); | 137 _stopSimulation(); |
| 138 _simulation = scrollBehavior.release(particle); | 138 print("velocity=$velocity"); |
| 139 if (_simulation == null) | 139 Simulation simulation = scrollBehavior.release(scrollOffset, velocity); |
| 140 return; | 140 if (simulation != null) |
| 141 _simulation.onTick.listen((_) => scrollTo(particle.position)); | 141 _animation.start(simulation); |
| 142 } | 142 } |
| 143 | 143 |
| 144 Particle _createParticle([double velocity = 0.0]) { | 144 void _tickScrollOffset(double value) { |
| 145 return new Particle(position: _scrollOffset, velocity: velocity); | 145 scrollTo(value); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void _handlePointerDown(_) { | 148 void _handlePointerDown(_) { |
| 149 _stopSimulation(); | 149 _stopSimulation(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void _handlePointerUpOrCancel(_) { | 152 void _handlePointerUpOrCancel(_) { |
| 153 if (_simulation == null) | 153 if (!_animation.isAnimating) |
| 154 settleScrollOffset(); | 154 settleScrollOffset(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void _handleScrollUpdate(sky.GestureEvent event) { | 157 void _handleScrollUpdate(sky.GestureEvent event) { |
| 158 scrollBy(direction == ScrollDirection.horizontal ? event.dx : -event.dy); | 158 scrollBy(direction == ScrollDirection.horizontal ? event.dx : -event.dy); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void _handleFlingStart(sky.GestureEvent event) { | 161 void _handleFlingStart(sky.GestureEvent event) { |
| 162 double eventVelocity = direction == ScrollDirection.horizontal | 162 double eventVelocity = direction == ScrollDirection.horizontal |
| 163 ? -event.velocityX | 163 ? -event.velocityX |
| 164 : -event.velocityY; | 164 : -event.velocityY; |
| 165 _startSimulation(_createParticle(_velocityForFlingGesture(eventVelocity))); | 165 _startSimulation(velocity: _velocityForFlingGesture(eventVelocity)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void _handleFlingCancel(sky.GestureEvent event) { | 168 void _handleFlingCancel(sky.GestureEvent event) { |
| 169 settleScrollOffset(); | 169 settleScrollOffset(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 void _handleWheel(sky.WheelEvent event) { | 172 void _handleWheel(sky.WheelEvent event) { |
| 173 scrollBy(-event.offsetY); | 173 scrollBy(-event.offsetY); |
| 174 } | 174 } |
| 175 | 175 |
| 176 } | 176 } |
| OLD | NEW |