OLD | NEW |
| (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/scroll_behavior.dart'; | |
6 import '../debug/tracing.dart'; | |
7 import '../fn.dart'; | |
8 import 'dart:math' as math; | |
9 import 'dart:async'; | |
10 import 'scrollable.dart'; | |
11 | |
12 abstract class FixedHeightScrollable extends Scrollable { | |
13 static final Style _style = new Style(''' | |
14 overflow: hidden; | |
15 position: relative; | |
16 will-change: transform;''' | |
17 ); | |
18 | |
19 static final Style _scrollAreaStyle = new Style(''' | |
20 position:relative; | |
21 will-change: transform;''' | |
22 ); | |
23 | |
24 FixedHeightScrollable({ | |
25 Object key | |
26 }) : super(key: key); | |
27 | |
28 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); | |
29 OverscrollBehavior get scrollBehavior => super.scrollBehavior as OverscrollBeh
avior; | |
30 | |
31 double _height = 0.0; | |
32 double _itemHeight; | |
33 | |
34 int _itemCount = 0; | |
35 int get itemCount => _itemCount; | |
36 void set itemCount (int value) { | |
37 if (_itemCount != value) { | |
38 _itemCount = value; | |
39 if (_itemHeight != null) | |
40 scrollBehavior.contentsHeight = _itemHeight * _itemCount; | |
41 } | |
42 } | |
43 | |
44 void _measureHeights() { | |
45 trace('FixedHeightScrollable::_measureHeights', () { | |
46 if (_itemHeight != null) | |
47 return; | |
48 var root = getRoot(); | |
49 if (root == null) | |
50 return; | |
51 var item = root.firstChild.firstChild; | |
52 if (item == null) | |
53 return; | |
54 setState(() { | |
55 _height = root.height; | |
56 assert(_height > 0); | |
57 _itemHeight = item.height; | |
58 assert(_itemHeight > 0); | |
59 scrollBehavior.containerHeight = _height; | |
60 scrollBehavior.contentsHeight = _itemHeight * _itemCount; | |
61 }); | |
62 }); | |
63 } | |
64 | |
65 UINode buildContent() { | |
66 var itemNumber = 0; | |
67 var drawCount = 1; | |
68 var transformStyle = ''; | |
69 | |
70 if (_itemHeight == null) | |
71 new Future.microtask(_measureHeights); | |
72 | |
73 if (_height > 0.0 && _itemHeight != null) { | |
74 if (scrollOffset < 0.0) { | |
75 double visibleHeight = _height + scrollOffset; | |
76 drawCount = (visibleHeight / _itemHeight).round() + 1; | |
77 transformStyle = | |
78 'transform: translateY(${(-scrollOffset).toStringAsFixed(2)}px)'; | |
79 } else { | |
80 drawCount = (_height / _itemHeight).ceil() + 1; | |
81 double alignmentDelta = -scrollOffset % _itemHeight; | |
82 if (alignmentDelta != 0.0) | |
83 alignmentDelta -= _itemHeight; | |
84 | |
85 double drawStart = scrollOffset + alignmentDelta; | |
86 itemNumber = math.max(0, (drawStart / _itemHeight).floor()); | |
87 | |
88 transformStyle = | |
89 'transform: translateY(${(alignmentDelta).toStringAsFixed(2)}px)'; | |
90 } | |
91 } | |
92 | |
93 return new Container( | |
94 style: _style, | |
95 children: [ | |
96 new Container( | |
97 style: _scrollAreaStyle, | |
98 inlineStyle: transformStyle, | |
99 children: buildItems(itemNumber, drawCount) | |
100 ) | |
101 ] | |
102 ); | |
103 } | |
104 | |
105 List<UINode> buildItems(int start, int count); | |
106 } | |
OLD | NEW |