OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of touch; | 5 part of touch; |
6 | 6 |
7 abstract class ScrollListener { | 7 abstract class ScrollListener { |
8 /** | 8 /** |
9 * The callback invoked for a scroll event. | 9 * The callback invoked for a scroll event. |
10 * [decelerating] specifies whether or not the content is moving due | 10 * [decelerating] specifies whether or not the content is moving due |
11 * to deceleration. It should be false if the content is moving because the | 11 * to deceleration. It should be false if the content is moving because the |
12 * user is dragging the content. | 12 * user is dragging the content. |
13 */ | 13 */ |
14 void onScrollerMoved(double scrollX, double scrollY, bool decelerating); | 14 void onScrollerMoved(double scrollX, double scrollY, bool decelerating); |
15 } | 15 } |
16 | 16 |
17 /** | 17 /** |
18 * The scroll watcher is intended to provide a single way to | 18 * The scroll watcher is intended to provide a single way to |
19 * listen for scroll events from instances of Scroller. | 19 * listen for scroll events from instances of Scroller. |
20 * TODO(jacobr): this class is obsolete. | 20 * TODO(jacobr): this class is obsolete. |
21 */ | 21 */ |
22 class ScrollWatcher { | 22 class ScrollWatcher { |
23 Scroller _scroller; | 23 Scroller _scroller; |
24 | 24 |
25 List<ScrollListener> _listeners; | 25 List<ScrollListener> _listeners; |
26 | 26 |
27 Element _scrollerEl; | 27 Element _scrollerEl; |
28 | 28 |
29 ScrollWatcher(Scroller scroller) | 29 ScrollWatcher(Scroller scroller) |
30 : _scroller = scroller, _listeners = new List<ScrollListener>() { | 30 : _scroller = scroller, |
31 } | 31 _listeners = new List<ScrollListener>() {} |
32 | 32 |
33 void addListener(ScrollListener listener) { | 33 void addListener(ScrollListener listener) { |
34 _listeners.add(listener); | 34 _listeners.add(listener); |
35 } | 35 } |
36 | 36 |
37 /** | 37 /** |
38 * Send the scroll event to all listeners. | 38 * Send the scroll event to all listeners. |
39 * [decelerating] is true if the offset is changing because of deceleration. | 39 * [decelerating] is true if the offset is changing because of deceleration. |
40 */ | 40 */ |
41 void _dispatchScroll(num scrollX, num scrollY, | 41 void _dispatchScroll(num scrollX, num scrollY, [bool decelerating = false]) { |
42 [bool decelerating = false]) { | |
43 for (final listener in _listeners) { | 42 for (final listener in _listeners) { |
44 listener.onScrollerMoved(scrollX, scrollY, decelerating); | 43 listener.onScrollerMoved(scrollX, scrollY, decelerating); |
45 } | 44 } |
46 } | 45 } |
47 | 46 |
48 /** | 47 /** |
49 * Initializes elements and event handlers. Must be called after construction | 48 * Initializes elements and event handlers. Must be called after construction |
50 * and before usage. | 49 * and before usage. |
51 */ | 50 */ |
52 void initialize() { | 51 void initialize() { |
53 _scrollerEl = _scroller.getElement(); | 52 _scrollerEl = _scroller.getElement(); |
54 _scroller.onContentMoved.listen((e) { _onContentMoved(e); }); | 53 _scroller.onContentMoved.listen((e) { |
| 54 _onContentMoved(e); |
| 55 }); |
55 } | 56 } |
56 | 57 |
57 /** | 58 /** |
58 * This callback is invoked any time the scroller content offset changes. | 59 * This callback is invoked any time the scroller content offset changes. |
59 */ | 60 */ |
60 void _onContentMoved(Event e) { | 61 void _onContentMoved(Event e) { |
61 num scrollX = _scroller.getHorizontalOffset(); | 62 num scrollX = _scroller.getHorizontalOffset(); |
62 num scrollY = _scroller.getVerticalOffset(); | 63 num scrollY = _scroller.getVerticalOffset(); |
63 _dispatchScroll(scrollX, scrollY); | 64 _dispatchScroll(scrollX, scrollY); |
64 } | 65 } |
65 } | 66 } |
OLD | NEW |