| 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 '../animation/generators.dart'; |
| 8 import '../animation/mechanics.dart'; | 8 import '../animation/mechanics.dart'; |
| 9 import '../animation/scroll_behavior.dart'; | 9 import '../animation/scroll_behavior.dart'; |
| 10 import '../theme/view_configuration.dart' as config; | 10 import '../theme/view_configuration.dart' as config; |
| 11 import 'basic.dart'; | 11 import 'basic.dart'; |
| 12 import 'material.dart'; | 12 import 'material.dart'; |
| 13 | 13 |
| 14 const double _kMillisecondsPerSecond = 1000.0; | 14 const double _kMillisecondsPerSecond = 1000.0; |
| 15 | 15 |
| 16 double _velocityForFlingGesture(double eventVelocity) { | 16 double _velocityForFlingGesture(double eventVelocity) { |
| 17 return eventVelocity.clamp(-config.kMaxFlingVelocity, config.kMaxFlingVelocity
) / | 17 return eventVelocity.clamp(-config.kMaxFlingVelocity, config.kMaxFlingVelocity
) / |
| 18 _kMillisecondsPerSecond; | 18 _kMillisecondsPerSecond; |
| 19 } | 19 } |
| 20 | 20 |
| 21 abstract class ScrollClient { | 21 abstract class ScrollClient { |
| 22 bool ancestorScrolled(Scrollable ancestor); | 22 bool ancestorScrolled(Scrollable ancestor); |
| 23 } | 23 } |
| 24 | 24 |
| 25 enum ScrollDirection { vertical, horizontal } | 25 enum ScrollDirection { vertical, horizontal } |
| 26 | 26 |
| 27 abstract class Scrollable extends StatefulComponent { | 27 abstract class Scrollable extends StatefulComponent { |
| 28 | 28 |
| 29 Scrollable({ | 29 Scrollable({ |
| 30 String key, | 30 String key, |
| 31 this.backgroundColor, | 31 this.backgroundColor, |
| 32 this.direction: ScrollDirection.vertical | 32 this.direction: ScrollDirection.vertical |
| 33 }) : super(key: key); | 33 }) : super(key: key); |
| 34 | 34 |
| 35 Color backgroundColor; | 35 Color backgroundColor; |
| 36 ScrollDirection direction; | 36 ScrollDirection direction; |
| 37 | 37 |
| 38 void syncFields(Scrollable source) { | 38 void syncFields(Scrollable source) { |
| 39 backgroundColor = source.backgroundColor; | 39 backgroundColor = source.backgroundColor; |
| 40 direction == source.direction; | 40 direction == source.direction; |
| 41 } | 41 } |
| 42 | 42 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 bool scrollBy(double scrollDelta) { | 115 bool scrollBy(double scrollDelta) { |
| 116 var newScrollOffset = scrollBehavior.applyCurve(_scrollOffset, scrollDelta); | 116 var newScrollOffset = scrollBehavior.applyCurve(_scrollOffset, scrollDelta); |
| 117 return scrollTo(newScrollOffset); | 117 return scrollTo(newScrollOffset); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void didUnmount() { | 120 void didUnmount() { |
| 121 _stopSimulation(); | 121 _stopSimulation(); |
| 122 super.didUnmount(); | 122 super.didUnmount(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void settleScrollOffset() { |
| 126 _startSimulation(_createParticle()); |
| 127 } |
| 128 |
| 125 void _stopSimulation() { | 129 void _stopSimulation() { |
| 126 if (_simulation == null) | 130 if (_simulation == null) |
| 127 return; | 131 return; |
| 128 _simulation.cancel(); | 132 _simulation.cancel(); |
| 129 _simulation = null; | 133 _simulation = null; |
| 130 } | 134 } |
| 131 | 135 |
| 132 void _startSimulation(Particle particle) { | 136 void _startSimulation(Particle particle) { |
| 133 _stopSimulation(); | 137 _stopSimulation(); |
| 134 _simulation = scrollBehavior.release(particle); | 138 _simulation = scrollBehavior.release(particle); |
| 135 if (_simulation == null) | 139 if (_simulation == null) |
| 136 return; | 140 return; |
| 137 _simulation.onTick.listen((_) => scrollTo(particle.position)); | 141 _simulation.onTick.listen((_) => scrollTo(particle.position)); |
| 138 } | 142 } |
| 139 | 143 |
| 140 Particle _createParticle([double velocity = 0.0]) { | 144 Particle _createParticle([double velocity = 0.0]) { |
| 141 return new Particle(position: _scrollOffset, velocity: velocity); | 145 return new Particle(position: _scrollOffset, velocity: velocity); |
| 142 } | 146 } |
| 143 | 147 |
| 144 void _handlePointerDown(_) { | 148 void _handlePointerDown(_) { |
| 145 _stopSimulation(); | 149 _stopSimulation(); |
| 146 } | 150 } |
| 147 | 151 |
| 148 void _handlePointerUpOrCancel(_) { | 152 void _handlePointerUpOrCancel(_) { |
| 149 if (_simulation == null) | 153 if (_simulation == null) |
| 150 _startSimulation(_createParticle()); | 154 settleScrollOffset(); |
| 151 } | 155 } |
| 152 | 156 |
| 153 void _handleScrollUpdate(sky.GestureEvent event) { | 157 void _handleScrollUpdate(sky.GestureEvent event) { |
| 154 scrollBy(direction == ScrollDirection.horizontal ? event.dx : -event.dy); | 158 scrollBy(direction == ScrollDirection.horizontal ? event.dx : -event.dy); |
| 155 } | 159 } |
| 156 | 160 |
| 157 void _handleFlingStart(sky.GestureEvent event) { | 161 void _handleFlingStart(sky.GestureEvent event) { |
| 158 double eventVelocity = direction == ScrollDirection.horizontal | 162 double eventVelocity = direction == ScrollDirection.horizontal |
| 159 ? -event.velocityX | 163 ? -event.velocityX |
| 160 : -event.velocityY; | 164 : -event.velocityY; |
| 161 _startSimulation(_createParticle(_velocityForFlingGesture(eventVelocity))); | 165 _startSimulation(_createParticle(_velocityForFlingGesture(eventVelocity))); |
| 162 } | 166 } |
| 163 | 167 |
| 164 void _handleFlingCancel(sky.GestureEvent event) { | 168 void _handleFlingCancel(sky.GestureEvent event) { |
| 165 _startSimulation(_createParticle()); | 169 settleScrollOffset(); |
| 166 } | 170 } |
| 167 | 171 |
| 168 void _handleWheel(sky.WheelEvent event) { | 172 void _handleWheel(sky.WheelEvent event) { |
| 169 scrollBy(-event.offsetY); | 173 scrollBy(-event.offsetY); |
| 170 } | 174 } |
| 171 | 175 |
| 172 } | 176 } |
| OLD | NEW |