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:math' as math; | |
6 import 'dart:sky' as sky; | 5 import 'dart:sky' as sky; |
7 | 6 |
8 import '../animation/generators.dart'; | 7 import '../animation/generators.dart'; |
9 import '../animation/mechanics.dart'; | 8 import '../animation/mechanics.dart'; |
10 import '../animation/scroll_behavior.dart'; | 9 import '../animation/scroll_behavior.dart'; |
11 import '../theme/view_configuration.dart' as config; | 10 import '../theme/view_configuration.dart' as config; |
12 import 'basic.dart'; | 11 import 'basic.dart'; |
13 import 'material.dart'; | 12 import 'material.dart'; |
14 | 13 |
15 const double _kMillisecondsPerSecond = 1000.0; | 14 const double _kMillisecondsPerSecond = 1000.0; |
16 | 15 |
17 double _velocityForFlingGesture(sky.GestureEvent event) { | 16 double _velocityForFlingGesture(double eventVelocity) { |
18 return math.max(-config.kMaxFlingVelocity, math.min(config.kMaxFlingVelocity, | 17 return eventVelocity.clamp(-config.kMaxFlingVelocity, config.kMaxFlingVelocity
) / |
19 -event.velocityY)) / _kMillisecondsPerSecond; | 18 _kMillisecondsPerSecond; |
20 } | 19 } |
21 | 20 |
22 abstract class ScrollClient { | 21 abstract class ScrollClient { |
23 bool ancestorScrolled(Scrollable ancestor); | 22 bool ancestorScrolled(Scrollable ancestor); |
24 } | 23 } |
25 | 24 |
| 25 enum ScrollDirection { vertical, horizontal } |
| 26 |
26 abstract class Scrollable extends Component { | 27 abstract class Scrollable extends Component { |
27 | 28 |
28 Scrollable({ String key, Color this.backgroundColor }) | 29 Scrollable({ |
29 : super(key: key, stateful: true); | 30 String key, |
| 31 this.backgroundColor, |
| 32 this.direction: ScrollDirection.vertical |
| 33 }) : super(key: key, stateful: true); |
30 | 34 |
31 Color backgroundColor; | 35 Color backgroundColor; |
| 36 ScrollDirection direction; |
32 | 37 |
33 void syncFields(Scrollable source) { | 38 void syncFields(Scrollable source) { |
34 backgroundColor = source.backgroundColor; | 39 backgroundColor = source.backgroundColor; |
| 40 direction == source.direction; |
35 } | 41 } |
36 | 42 |
37 double _scrollOffset = 0.0; | 43 double _scrollOffset = 0.0; |
38 double get scrollOffset => _scrollOffset; | 44 double get scrollOffset => _scrollOffset; |
39 | 45 |
40 ScrollBehavior _scrollBehavior; | 46 ScrollBehavior _scrollBehavior; |
41 ScrollBehavior createScrollBehavior(); | 47 ScrollBehavior createScrollBehavior(); |
42 ScrollBehavior get scrollBehavior { | 48 ScrollBehavior get scrollBehavior { |
43 if (_scrollBehavior == null) | 49 if (_scrollBehavior == null) |
44 _scrollBehavior = createScrollBehavior(); | 50 _scrollBehavior = createScrollBehavior(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 void _handlePointerDown(_) { | 144 void _handlePointerDown(_) { |
139 _stopSimulation(); | 145 _stopSimulation(); |
140 } | 146 } |
141 | 147 |
142 void _handlePointerUpOrCancel(_) { | 148 void _handlePointerUpOrCancel(_) { |
143 if (_simulation == null) | 149 if (_simulation == null) |
144 _startSimulation(_createParticle()); | 150 _startSimulation(_createParticle()); |
145 } | 151 } |
146 | 152 |
147 void _handleScrollUpdate(sky.GestureEvent event) { | 153 void _handleScrollUpdate(sky.GestureEvent event) { |
148 scrollBy(-event.dy); | 154 scrollBy(direction == ScrollDirection.horizontal ? event.dx : -event.dy); |
149 } | 155 } |
150 | 156 |
151 void _handleFlingStart(sky.GestureEvent event) { | 157 void _handleFlingStart(sky.GestureEvent event) { |
152 _startSimulation(_createParticle(_velocityForFlingGesture(event))); | 158 double eventVelocity = direction == ScrollDirection.horizontal |
| 159 ? -event.velocityX |
| 160 : -event.velocityY; |
| 161 _startSimulation(_createParticle(_velocityForFlingGesture(eventVelocity))); |
153 } | 162 } |
154 | 163 |
155 void _handleFlingCancel(sky.GestureEvent event) { | 164 void _handleFlingCancel(sky.GestureEvent event) { |
156 _startSimulation(_createParticle()); | 165 _startSimulation(_createParticle()); |
157 } | 166 } |
158 | 167 |
159 void _handleWheel(sky.WheelEvent event) { | 168 void _handleWheel(sky.WheelEvent event) { |
160 scrollBy(-event.offsetY); | 169 scrollBy(-event.offsetY); |
161 } | 170 } |
162 | 171 |
163 } | 172 } |
OLD | NEW |