Files
sdk/samples-dev/swarm/swarm_ui_lib/touch/ScrollWatcher.dart
T
Leaf Petersen 745f112ef6 Opt out some files from null safety.
Opt an initial batch of files under samples, samples-dev,
utils, and runtime/tools/dartfuzz out of null safety in preparation
for switching the flag on by default.

Change-Id: Icdfd52a5a969e678a7205903332f73fe3841c223
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166960
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Leaf Petersen <leafp@google.com>
2020-10-12 22:15:42 +00:00

69 lines
1.9 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
part of touch;
abstract class ScrollListener {
/**
* The callback invoked for a scroll event.
* [decelerating] specifies whether or not the content is moving due
* to deceleration. It should be false if the content is moving because the
* user is dragging the content.
*/
void onScrollerMoved(double scrollX, double scrollY, bool decelerating);
}
/**
* The scroll watcher is intended to provide a single way to
* listen for scroll events from instances of Scroller.
* TODO(jacobr): this class is obsolete.
*/
class ScrollWatcher {
Scroller _scroller;
List<ScrollListener> _listeners;
Element _scrollerEl;
ScrollWatcher(Scroller scroller)
: _scroller = scroller,
_listeners = new List<ScrollListener>() {}
void addListener(ScrollListener listener) {
_listeners.add(listener);
}
/**
* Send the scroll event to all listeners.
* [decelerating] is true if the offset is changing because of deceleration.
*/
void _dispatchScroll(num scrollX, num scrollY, [bool decelerating = false]) {
for (final listener in _listeners) {
listener.onScrollerMoved(scrollX, scrollY, decelerating);
}
}
/**
* Initializes elements and event handlers. Must be called after construction
* and before usage.
*/
void initialize() {
_scrollerEl = _scroller.getElement();
_scroller.onContentMoved.listen((e) {
_onContentMoved(e);
});
}
/**
* This callback is invoked any time the scroller content offset changes.
*/
void _onContentMoved(Event e) {
num scrollX = _scroller.getHorizontalOffset();
num scrollY = _scroller.getVerticalOffset();
_dispatchScroll(scrollX, scrollY);
}
}