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/fling_curve.dart'; | 5 import '../animation/fling_curve.dart'; |
| 6 import '../animation/scroll_curve.dart'; |
6 import '../fn.dart'; | 7 import '../fn.dart'; |
7 import 'dart:sky' as sky; | 8 import 'dart:sky' as sky; |
8 | 9 |
9 abstract class Scrollable extends Component { | 10 abstract class Scrollable extends Component { |
10 double minOffset; | 11 ScrollCurve scrollCurve; |
11 double maxOffset; | |
12 | |
13 double get scrollOffset => _scrollOffset; | 12 double get scrollOffset => _scrollOffset; |
14 | 13 |
15 double _scrollOffset = 0.0; | 14 double _scrollOffset = 0.0; |
16 FlingCurve _flingCurve; | 15 FlingCurve _flingCurve; |
17 int _flingAnimationId; | 16 int _flingAnimationId; |
18 | 17 |
19 Scrollable({ | 18 Scrollable({Object key, this.scrollCurve}) : super(key: key) { |
20 Object key, | |
21 this.minOffset, | |
22 this.maxOffset | |
23 }) : super(key: key) { | |
24 events.listen('gestureflingstart', _handleFlingStart); | 19 events.listen('gestureflingstart', _handleFlingStart); |
25 events.listen('gestureflingcancel', _handleFlingCancel); | 20 events.listen('gestureflingcancel', _handleFlingCancel); |
26 events.listen('gesturescrollupdate', _handleScrollUpdate); | 21 events.listen('gesturescrollupdate', _handleScrollUpdate); |
27 events.listen('wheel', _handleWheel); | 22 events.listen('wheel', _handleWheel); |
28 } | 23 } |
29 | 24 |
30 void didUnmount() { | 25 void didUnmount() { |
31 super.didUnmount(); | 26 super.didUnmount(); |
32 _stopFling(); | 27 _stopFling(); |
33 } | 28 } |
34 | 29 |
35 bool scrollBy(double scrollDelta) { | 30 bool scrollBy(double scrollDelta) { |
36 var newScrollOffset = _scrollOffset + scrollDelta; | 31 var newScrollOffset = scrollCurve.apply(_scrollOffset, scrollDelta); |
37 if (minOffset != null && newScrollOffset < minOffset) | |
38 newScrollOffset = minOffset; | |
39 else if (maxOffset != null && newScrollOffset > maxOffset) | |
40 newScrollOffset = maxOffset; | |
41 | |
42 if (newScrollOffset == _scrollOffset) | 32 if (newScrollOffset == _scrollOffset) |
43 return false; | 33 return false; |
44 | |
45 setState(() { | 34 setState(() { |
46 _scrollOffset = newScrollOffset; | 35 _scrollOffset = newScrollOffset; |
47 }); | 36 }); |
48 return true; | 37 return true; |
49 } | 38 } |
50 | 39 |
51 void _scheduleFlingUpdate() { | 40 void _scheduleFlingUpdate() { |
52 _flingAnimationId = sky.window.requestAnimationFrame(_updateFling); | 41 _flingAnimationId = sky.window.requestAnimationFrame(_updateFling); |
53 } | 42 } |
54 | 43 |
(...skipping 24 matching lines...) Expand all Loading... |
79 } | 68 } |
80 | 69 |
81 void _handleFlingCancel(sky.GestureEvent event) { | 70 void _handleFlingCancel(sky.GestureEvent event) { |
82 _stopFling(); | 71 _stopFling(); |
83 } | 72 } |
84 | 73 |
85 void _handleWheel(sky.WheelEvent event) { | 74 void _handleWheel(sky.WheelEvent event) { |
86 scrollBy(-event.offsetY); | 75 scrollBy(-event.offsetY); |
87 } | 76 } |
88 } | 77 } |
OLD | NEW |