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

Side by Side Diff: runtime/observatory/lib/src/elements/containers/virtual_collection.dart

Issue 2203433002: Added new implementations for virtual containers (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fixed style issues Created 4 years, 4 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
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'dart:async';
6 import 'dart:html';
7 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart';
8 import 'package:observatory/src/elements/helpers/tag.dart';
9
10 typedef HtmlElement VirtualCollectionCreateCallback();
11 typedef void VirtualCollectionUpdateCallback(HtmlElement el, dynamic item,
12 int index);
13
14 class VirtualCollectionElement extends HtmlElement implements Renderable {
15 static const tag =
16 const Tag<VirtualCollectionElement>('virtual-collection');
17
18 RenderingScheduler<VirtualCollectionElement> _r;
19
20 Stream<RenderedEvent<VirtualCollectionElement>> get onRendered =>
21 _r.onRendered;
22
23 VirtualCollectionCreateCallback _create;
24 VirtualCollectionUpdateCallback _update;
25 double _itemHeight;
26 int _top;
27 int _height;
28 List _items;
29 StreamSubscription _onScrollSubscription;
30 StreamSubscription _onResizeSubscription;
31
32 List get items => _items;
33
34 set items(Iterable value) {
35 _items = new List.unmodifiable(value);
36 _r.dirty();
37 }
38
39
40 factory VirtualCollectionElement(VirtualCollectionCreateCallback create,
41 VirtualCollectionUpdateCallback update, {Iterable items: const [],
42 RenderingQueue queue}) {
43 assert(create != null);
44 assert(update != null);
45 assert(items != null);
46 VirtualCollectionElement e = document.createElement(tag.name);
47 e._r = new RenderingScheduler(e, queue: queue);
48 e._create = create;
49 e._update = update;
50 e._items = new List.unmodifiable(items);
51 return e;
52 }
53
54 VirtualCollectionElement.created() : super.created();
55
56 @override
57 attached() {
58 super.attached();
59 _r.enable();
60 _top = 0;
61 _height = getBoundingClientRect().height;
62 _itemHeight = _computeItemHeight();
63 _onScrollSubscription = onScroll.listen(_onScroll);
64 _onResizeSubscription = window.onResize.listen(_onResize);
65 }
66
67 @override
68 detached() {
69 super.detached();
70 _r.disable(notify: true);
71 children = const [];
72 _onScrollSubscription.cancel();
73 _onResizeSubscription.cancel();
74 }
75
76 final DivElement _scroller = new DivElement()..classes = const ['scroller'];
77 final DivElement _shifter = new DivElement()..classes = const ['shifter'];
78
79 dynamic getItemFromElement(HtmlElement element) {
80 final el_index = _shifter.children.indexOf(element);
81 if (el_index < 0) {
82 return null;
83 }
84 final item_index =
85 _top + el_index - (_shifter.children.length * _inverse_preload).floor();
86 if (0 <= item_index && item_index < items.length) {
87 return _items[item_index];
88 }
89 return null;
90 }
91
92 /// The preloaded element before and after the visible area are:
93 /// 1/preload_size of the number of items in the visble area.
94 /// See shared.css for the "top:-25%;".
95 static const int _preload = 2;
96 /// L = length of all the elements loaded
97 /// l = length of the visible area
98 ///
99 /// L = l + 2 * l / _preload
100 /// l = L * _preload / (_preload + 2)
101 ///
102 /// tail = l / _preload = L * 1 / (_preload + 2) = L * _inverse_preload
103 static const double _inverse_preload = 1 / (_preload + 2);
104
105 void render() {
106 _top = (scrollTop / _itemHeight).floor();
107
108 _scroller.style.height = '${_itemHeight*(_items.length)}px';
109 _shifter.style.top = '${_itemHeight*_top}px';
110 final tail_length = (_height / _itemHeight / _preload).ceil();
111 final length = tail_length * 2 + tail_length * _preload;
112
113 if (_shifter.children.length < length) {
114 while (_shifter.children.length != length) {
115 var e = _create();
116 e..style.display = 'hidden';
117 _shifter.children.add(e);
118 }
119 _shifter.style.height = '${_itemHeight*length}px';
120 children = [
121 _scroller
122 ..children = [_shifter]
123 ];
124 }
125
126 int i = _top - tail_length;
127 for (final HtmlElement e in _shifter.children) {
128 if (0 <= i && i < _items.length) {
129 e..style.display = null;
130 _update(e, _items[i], i);
131 } else {
132 e.style.display = 'hidden';
133 }
134 i++;
135 }
136 }
137
138 double _computeItemHeight() {
139 final c = children;
140 children = [_create()];
141 final height = children[0].getBoundingClientRect().height;
142 children = c;
143 return height;
144 }
145
146 void _onScroll(_) {
147 if(_r.isDirty) return;
148 if ((scrollTop - _top * _itemHeight).abs() >=
149 _shifter.children.length * _inverse_preload * _itemHeight) {
150 _r.dirty();
151 }
152 }
153
154 void _onResize(_) {
155 _height = getBoundingClientRect().height;
156 _r.dirty();
157 }
158 }
OLDNEW
« no previous file with comments | « runtime/observatory/lib/elements.dart ('k') | runtime/observatory/lib/src/elements/containers/virtual_tree.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698