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 '../animation/curves.dart'; | 5 import '../animation/curves.dart'; |
6 import '../animation/fling_curve.dart'; | 6 import '../animation/fling_curve.dart'; |
7 import '../animation/generator.dart'; | 7 import '../animation/generators.dart'; |
8 import '../animation/scroll_curve.dart'; | 8 import '../animation/scroll_behavior.dart'; |
9 import '../animation/mechanics.dart'; | 9 import '../animation/mechanics.dart'; |
10 import '../animation/simulation.dart'; | |
11 import '../fn.dart'; | 10 import '../fn.dart'; |
12 import 'dart:sky' as sky; | 11 import 'dart:sky' as sky; |
13 | 12 |
14 abstract class Scrollable extends Component { | 13 abstract class Scrollable extends Component { |
15 ScrollCurve scrollCurve; | 14 ScrollBehavior scrollBehavior; |
16 double get scrollOffset => _scrollOffset; | 15 double get scrollOffset => _scrollOffset; |
17 | 16 |
18 double _scrollOffset = 0.0; | 17 double _scrollOffset = 0.0; |
19 FlingCurve _flingCurve; | 18 FlingCurve _flingCurve; |
20 int _flingAnimationId; | 19 int _flingAnimationId; |
21 Simulation _simulation; | 20 Simulation _simulation; |
22 | 21 |
23 Scrollable({Object key, this.scrollCurve}) : super(key: key) { | 22 Scrollable({Object key, this.scrollBehavior}) : super(key: key) { |
24 events.listen('pointerdown', _handlePointerDown); | 23 events.listen('pointerdown', _handlePointerDown); |
25 events.listen('pointerup', _handlePointerUpOrCancel); | 24 events.listen('pointerup', _handlePointerUpOrCancel); |
26 events.listen('pointercancel', _handlePointerUpOrCancel); | 25 events.listen('pointercancel', _handlePointerUpOrCancel); |
27 events.listen('gestureflingstart', _handleFlingStart); | 26 events.listen('gestureflingstart', _handleFlingStart); |
28 events.listen('gestureflingcancel', _handleFlingCancel); | 27 events.listen('gestureflingcancel', _handleFlingCancel); |
29 events.listen('gesturescrollupdate', _handleScrollUpdate); | 28 events.listen('gesturescrollupdate', _handleScrollUpdate); |
30 events.listen('wheel', _handleWheel); | 29 events.listen('wheel', _handleWheel); |
31 } | 30 } |
32 | 31 |
33 void didUnmount() { | 32 void didUnmount() { |
34 super.didUnmount(); | 33 super.didUnmount(); |
35 _stopFling(); | 34 _stopFling(); |
36 _stopSimulation(); | 35 _stopSimulation(); |
37 } | 36 } |
38 | 37 |
39 bool scrollBy(double scrollDelta) { | 38 bool scrollBy(double scrollDelta) { |
40 var newScrollOffset = scrollCurve.apply(_scrollOffset, scrollDelta); | 39 var newScrollOffset = scrollBehavior.applyCurve(_scrollOffset, scrollDelta); |
41 if (newScrollOffset == _scrollOffset) | 40 if (newScrollOffset == _scrollOffset) |
42 return false; | 41 return false; |
43 setState(() { | 42 setState(() { |
44 _scrollOffset = newScrollOffset; | 43 _scrollOffset = newScrollOffset; |
45 }); | 44 }); |
46 return true; | 45 return true; |
47 } | 46 } |
48 | 47 |
49 void _scheduleFlingUpdate() { | 48 void _scheduleFlingUpdate() { |
50 _flingAnimationId = sky.window.requestAnimationFrame(_updateFling); | 49 _flingAnimationId = sky.window.requestAnimationFrame(_updateFling); |
(...skipping 17 matching lines...) Expand all Loading... |
68 void _updateFling(double timeStamp) { | 67 void _updateFling(double timeStamp) { |
69 double scrollDelta = _flingCurve.update(timeStamp); | 68 double scrollDelta = _flingCurve.update(timeStamp); |
70 if (!scrollBy(scrollDelta)) | 69 if (!scrollBy(scrollDelta)) |
71 return _settle(); | 70 return _settle(); |
72 _scheduleFlingUpdate(); | 71 _scheduleFlingUpdate(); |
73 } | 72 } |
74 | 73 |
75 void _settle() { | 74 void _settle() { |
76 _stopFling(); | 75 _stopFling(); |
77 Particle particle = new Particle(position: scrollOffset); | 76 Particle particle = new Particle(position: scrollOffset); |
78 _simulation = scrollCurve.release(particle); | 77 _simulation = scrollBehavior.release(particle); |
79 if (_simulation == null) | 78 if (_simulation == null) |
80 return; | 79 return; |
81 _simulation.onTick.listen((_) { | 80 _simulation.onTick.listen((_) { |
82 setState(() { | 81 setState(() { |
83 _scrollOffset = particle.position; | 82 _scrollOffset = particle.position; |
84 }); | 83 }); |
85 }); | 84 }); |
86 } | 85 } |
87 | 86 |
88 void _handlePointerDown(_) { | 87 void _handlePointerDown(_) { |
(...skipping 17 matching lines...) Expand all Loading... |
106 } | 105 } |
107 | 106 |
108 void _handleFlingCancel(sky.GestureEvent event) { | 107 void _handleFlingCancel(sky.GestureEvent event) { |
109 _settle(); | 108 _settle(); |
110 } | 109 } |
111 | 110 |
112 void _handleWheel(sky.WheelEvent event) { | 111 void _handleWheel(sky.WheelEvent event) { |
113 scrollBy(-event.offsetY); | 112 scrollBy(-event.offsetY); |
114 } | 113 } |
115 } | 114 } |
OLD | NEW |