Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: sky/framework/components/scrollable.dart

Issue 1006533002: Factor a Scrollable base class out of FixedHeightScrollable (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/framework/components/fixed_height_scrollable.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 import '../animation/fling_curve.dart';
6 import '../fn.dart';
7 import 'dart:sky' as sky;
8
9 abstract class Scrollable extends Component {
10 double minOffset;
11 double maxOffset;
12
13 double get scrollOffset => _scrollOffset;
14
15 double _scrollOffset = 0.0;
16 FlingCurve _flingCurve;
17 int _flingAnimationId;
18
19 Scrollable({
20 Object key,
21 this.minOffset,
22 this.maxOffset
23 }) : super(key: key) {
24 events.listen('gestureflingstart', _handleFlingStart);
25 events.listen('gestureflingcancel', _handleFlingCancel);
26 events.listen('gesturescrollupdate', _handleScrollUpdate);
27 events.listen('wheel', _handleWheel);
28 }
29
30 void didUnmount() {
31 super.didUnmount();
32 _stopFling();
33 }
34
35 bool scrollBy(double scrollDelta) {
36 var newScrollOffset = _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)
43 return false;
44
45 setState(() {
46 _scrollOffset = newScrollOffset;
47 });
48 return true;
49 }
50
51 void _scheduleFlingUpdate() {
52 _flingAnimationId = sky.window.requestAnimationFrame(_updateFling);
53 }
54
55 void _stopFling() {
56 if (_flingAnimationId == null)
57 return;
58 sky.window.cancelAnimationFrame(_flingAnimationId);
59 _flingCurve = null;
60 _flingAnimationId = null;
61 }
62
63 void _updateFling(double timeStamp) {
64 double scrollDelta = _flingCurve.update(timeStamp);
65 if (!scrollBy(scrollDelta))
66 return _stopFling();
67 _scheduleFlingUpdate();
68 }
69
70 void _handleScrollUpdate(sky.GestureEvent event) {
71 scrollBy(-event.dy);
72 }
73
74 void _handleFlingStart(sky.GestureEvent event) {
75 setState(() {
76 _flingCurve = new FlingCurve(-event.velocityY, event.timeStamp);
77 _scheduleFlingUpdate();
78 });
79 }
80
81 void _handleFlingCancel(sky.GestureEvent event) {
82 _stopFling();
83 }
84
85 void _handleWheel(sky.WheelEvent event) {
86 scrollBy(-event.offsetY);
87 }
88 }
OLDNEW
« no previous file with comments | « sky/framework/components/fixed_height_scrollable.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698