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

Side by Side Diff: samples/swarm/swarm_ui_lib/view/PagedViews.dart

Issue 12087004: Changing window.requestLayoutFrame into a microtask API (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 | Annotate | Revision Log
OLDNEW
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 view; 5 part of view;
6 6
7 class PageState { 7 class PageState {
8 final ObservableValue<int> current; 8 final ObservableValue<int> current;
9 final ObservableValue<int> target; 9 final ObservableValue<int> target;
10 final ObservableValue<int> length; 10 final ObservableValue<int> length;
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 return node; 128 return node;
129 } 129 }
130 130
131 int _getViewLength(Element element) { 131 int _getViewLength(Element element) {
132 return _computePageSize(element) * pages.length.value; 132 return _computePageSize(element) * pages.length.value;
133 } 133 }
134 134
135 // TODO(jmesserly): would be better to not have this code in enterDocument. 135 // TODO(jmesserly): would be better to not have this code in enterDocument.
136 // But we need computedStyle to read our CSS properties. 136 // But we need computedStyle to read our CSS properties.
137 void enterDocument() { 137 void enterDocument() {
138 contentView.node.computedStyle.then((CssStyleDeclaration style) { 138 window.setImmediate(() {
139 var style = contentView.node.getComputedStyle();
139 _computeColumnGap(style); 140 _computeColumnGap(style);
140 141
141 // Trigger a fake resize event so we measure our height. 142 // Trigger a fake resize event so we measure our height.
142 windowResized(); 143 windowResized();
143 144
144 // Hook img onload events, so we find out about changes in content size 145 // Hook img onload events, so we find out about changes in content size
145 for (ImageElement img in contentView.node.queryAll("img")) { 146 for (ImageElement img in contentView.node.queryAll("img")) {
146 if (!img.complete) { 147 if (!img.complete) {
147 img.on.load.add((e) { 148 img.on.load.add((e) {
148 _updatePageCount(null); 149 _updatePageCount(null);
(...skipping 26 matching lines...) Expand all
175 return double.parse(value).round().toInt(); 176 return double.parse(value).round().toInt();
176 } 177 }
177 178
178 /** Watch for resize and update page count. */ 179 /** Watch for resize and update page count. */
179 void windowResized() { 180 void windowResized() {
180 // TODO(jmesserly): verify we aren't triggering unnecessary layouts. 181 // TODO(jmesserly): verify we aren't triggering unnecessary layouts.
181 182
182 // The content needs to have its height explicitly set, or columns don't 183 // The content needs to have its height explicitly set, or columns don't
183 // flow to the right correctly. So we copy our own height and set the height 184 // flow to the right correctly. So we copy our own height and set the height
184 // of the content. 185 // of the content.
185 window.requestLayoutFrame(() { 186 window.setImmediate(() {
186 contentView.node.style.height = '${node.offsetHeight}px'; 187 contentView.node.style.height = '${node.offsetHeight}px';
187 }); 188 });
188 _updatePageCount(null); 189 _updatePageCount(null);
189 } 190 }
190 191
191 bool _updatePageCount(Callback callback) { 192 bool _updatePageCount(Callback callback) {
192 int pageLength = 1; 193 int pageLength = 1;
193 window.requestLayoutFrame(() { 194 window.setImmediate(() {
194 if (_container.scrollWidth > _container.offsetWidth) { 195 if (_container.scrollWidth > _container.offsetWidth) {
195 pageLength = (_container.scrollWidth / _computePageSize(_container)) 196 pageLength = (_container.scrollWidth / _computePageSize(_container))
196 .ceil().toInt(); 197 .ceil().toInt();
197 } 198 }
198 pageLength = Math.max(pageLength, 1); 199 pageLength = Math.max(pageLength, 1);
199 200
200 int oldPage = pages.target.value; 201 int oldPage = pages.target.value;
201 int newPage = Math.min(oldPage, pageLength - 1); 202 int newPage = Math.min(oldPage, pageLength - 1);
202 203
203 // Hacky: make sure a change event always fires. 204 // Hacky: make sure a change event always fires.
204 // This is so we adjust the 3d transform after resize. 205 // This is so we adjust the 3d transform after resize.
205 if (oldPage == newPage) { 206 if (oldPage == newPage) {
206 pages.target.value = 0; 207 pages.target.value = 0;
207 } 208 }
208 assert(newPage < pageLength); 209 assert(newPage < pageLength);
209 pages.target.value = newPage; 210 pages.target.value = newPage;
210 pages.length.value = pageLength; 211 pages.length.value = pageLength;
211 if (callback != null) { 212 if (callback != null) {
212 callback(); 213 callback();
213 } 214 }
214 }); 215 });
215 } 216 }
216 217
217 void _onContentMoved(Event e) { 218 void _onContentMoved(Event e) {
218 window.requestLayoutFrame(() { 219 window.setImmediate(() {
219 num current = scroller.contentOffset.x; 220 num current = scroller.contentOffset.x;
220 int pageSize = _computePageSize(_container); 221 int pageSize = _computePageSize(_container);
221 pages.current.value = -(current / pageSize).round().toInt(); 222 pages.current.value = -(current / pageSize).round().toInt();
222 }); 223 });
223 } 224 }
224 225
225 void _snapToPage(Event e) { 226 void _snapToPage(Event e) {
226 num current = scroller.contentOffset.x; 227 num current = scroller.contentOffset.x;
227 num currentTarget = scroller.currentTarget.x; 228 num currentTarget = scroller.currentTarget.x;
228 window.requestLayoutFrame(() { 229 window.setImmediate(() {
229 int pageSize = _computePageSize(_container); 230 int pageSize = _computePageSize(_container);
230 int destination; 231 int destination;
231 num currentPageNumber = -(current / pageSize).round(); 232 num currentPageNumber = -(current / pageSize).round();
232 num pageNumber = -currentTarget / pageSize; 233 num pageNumber = -currentTarget / pageSize;
233 if (current == currentTarget) { 234 if (current == currentTarget) {
234 // User was just static dragging so round to the nearest page. 235 // User was just static dragging so round to the nearest page.
235 pageNumber = pageNumber.round(); 236 pageNumber = pageNumber.round();
236 } else { 237 } else {
237 if (currentPageNumber == pageNumber.round() && 238 if (currentPageNumber == pageNumber.round() &&
238 (pageNumber - currentPageNumber).abs() > MIN_THROW_PAGE_FRACTION && 239 (pageNumber - currentPageNumber).abs() > MIN_THROW_PAGE_FRACTION &&
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 (_viewportSize + _columnGap) ~/ (_columnWidth + _columnGap)); 271 (_viewportSize + _columnGap) ~/ (_columnWidth + _columnGap));
271 272
272 // Divide up the viewport between the columns. 273 // Divide up the viewport between the columns.
273 int columnSize = (_viewportSize - (perPage - 1) * _columnGap) ~/ perPage; 274 int columnSize = (_viewportSize - (perPage - 1) * _columnGap) ~/ perPage;
274 275
275 // Finally, compute how big each page is, and how far to translate. 276 // Finally, compute how big each page is, and how far to translate.
276 return perPage * (columnSize + _columnGap); 277 return perPage * (columnSize + _columnGap);
277 } 278 }
278 279
279 void _onPageSelected() { 280 void _onPageSelected() {
280 window.requestLayoutFrame(() { 281 window.setImmediate(() {
281 int translate = -pages.target.value * _computePageSize(_container); 282 int translate = -pages.target.value * _computePageSize(_container);
282 scroller.throwTo(translate, 0); 283 scroller.throwTo(translate, 0);
283 }); 284 });
284 } 285 }
285 } 286 }
OLDNEW
« no previous file with comments | « samples/swarm/swarm_ui_lib/touch/Scroller.dart ('k') | samples/swarm/swarm_ui_lib/view/SliderMenu.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698