OLD | NEW |
---|---|
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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 _columnGap = _toPixels(gap, 'column-gap or font-size'); | 165 _columnGap = _toPixels(gap, 'column-gap or font-size'); |
166 _columnWidth = _toPixels(style.columnWidth, 'column-width'); | 166 _columnWidth = _toPixels(style.columnWidth, 'column-width'); |
167 } | 167 } |
168 | 168 |
169 static int _toPixels(String value, String message) { | 169 static int _toPixels(String value, String message) { |
170 // TODO(jmesserly): Safari 4 has a bug where this property does not end | 170 // TODO(jmesserly): Safari 4 has a bug where this property does not end |
171 // in "px" like it should, but the value is correct. Handle that gracefully. | 171 // in "px" like it should, but the value is correct. Handle that gracefully. |
172 if (value.endsWith('px')) { | 172 if (value.endsWith('px')) { |
173 value = value.substring(0, value.length - 2); | 173 value = value.substring(0, value.length - 2); |
174 } | 174 } |
175 return double.parse(value).round().toInt(); | 175 return double.parse(value).round(); |
176 } | 176 } |
177 | 177 |
178 /** Watch for resize and update page count. */ | 178 /** Watch for resize and update page count. */ |
179 void windowResized() { | 179 void windowResized() { |
180 // TODO(jmesserly): verify we aren't triggering unnecessary layouts. | 180 // TODO(jmesserly): verify we aren't triggering unnecessary layouts. |
181 | 181 |
182 // The content needs to have its height explicitly set, or columns don't | 182 // 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 | 183 // flow to the right correctly. So we copy our own height and set the height |
184 // of the content. | 184 // of the content. |
185 window.requestLayoutFrame(() { | 185 window.requestLayoutFrame(() { |
186 contentView.node.style.height = '${node.offsetHeight}px'; | 186 contentView.node.style.height = '${node.offsetHeight}px'; |
187 }); | 187 }); |
188 _updatePageCount(null); | 188 _updatePageCount(null); |
189 } | 189 } |
190 | 190 |
191 bool _updatePageCount(Callback callback) { | 191 bool _updatePageCount(Callback callback) { |
192 int pageLength = 1; | 192 int pageLength = 1; |
193 window.requestLayoutFrame(() { | 193 window.requestLayoutFrame(() { |
194 if (_container.scrollWidth > _container.offsetWidth) { | 194 if (_container.scrollWidth > _container.offsetWidth) { |
195 pageLength = (_container.scrollWidth / _computePageSize(_container)) | 195 pageLength = (_container.scrollWidth / _computePageSize(_container)) |
196 .ceil().toInt(); | 196 .ceil(); |
197 } | 197 } |
198 pageLength = Math.max(pageLength, 1); | 198 pageLength = Math.max(pageLength, 1); |
199 | 199 |
200 int oldPage = pages.target.value; | 200 int oldPage = pages.target.value; |
201 int newPage = Math.min(oldPage, pageLength - 1); | 201 int newPage = Math.min(oldPage, pageLength - 1); |
202 | 202 |
203 // Hacky: make sure a change event always fires. | 203 // Hacky: make sure a change event always fires. |
204 // This is so we adjust the 3d transform after resize. | 204 // This is so we adjust the 3d transform after resize. |
205 if (oldPage == newPage) { | 205 if (oldPage == newPage) { |
206 pages.target.value = 0; | 206 pages.target.value = 0; |
207 } | 207 } |
208 assert(newPage < pageLength); | 208 assert(newPage < pageLength); |
209 pages.target.value = newPage; | 209 pages.target.value = newPage; |
210 pages.length.value = pageLength; | 210 pages.length.value = pageLength; |
211 if (callback != null) { | 211 if (callback != null) { |
212 callback(); | 212 callback(); |
213 } | 213 } |
214 }); | 214 }); |
215 } | 215 } |
216 | 216 |
217 void _onContentMoved(Event e) { | 217 void _onContentMoved(Event e) { |
218 window.requestLayoutFrame(() { | 218 window.requestLayoutFrame(() { |
219 num current = scroller.contentOffset.x; | 219 num current = scroller.contentOffset.x; |
220 int pageSize = _computePageSize(_container); | 220 int pageSize = _computePageSize(_container); |
221 pages.current.value = -(current / pageSize).round().toInt(); | 221 pages.current.value = -(current / pageSize).round(); |
222 }); | 222 }); |
223 } | 223 } |
224 | 224 |
225 void _snapToPage(Event e) { | 225 void _snapToPage(Event e) { |
226 num current = scroller.contentOffset.x; | 226 num current = scroller.contentOffset.x; |
227 num currentTarget = scroller.currentTarget.x; | 227 num currentTarget = scroller.currentTarget.x; |
228 window.requestLayoutFrame(() { | 228 window.requestLayoutFrame(() { |
229 int pageSize = _computePageSize(_container); | 229 int pageSize = _computePageSize(_container); |
230 int destination; | 230 int destination; |
231 num currentPageNumber = -(current / pageSize).round(); | 231 num currentPageNumber = -(current / pageSize).round(); |
232 num pageNumber = -currentTarget / pageSize; | 232 num pageNumber = -currentTarget / pageSize; |
233 if (current == currentTarget) { | 233 if (current == currentTarget) { |
234 // User was just static dragging so round to the nearest page. | 234 // User was just static dragging so round to the nearest page. |
235 pageNumber = pageNumber.round(); | 235 pageNumber = pageNumber.round(); |
236 } else { | 236 } else { |
237 if (currentPageNumber == pageNumber.round() && | 237 if (currentPageNumber == pageNumber.round() && |
238 (pageNumber - currentPageNumber).abs() > MIN_THROW_PAGE_FRACTION && | 238 (pageNumber - currentPageNumber).abs() > MIN_THROW_PAGE_FRACTION && |
239 -current + _viewportSize < _getViewLength(_container) && | 239 -current + _viewportSize < _getViewLength(_container) && |
240 current < 0) { | 240 current < 0) { |
241 // The user is trying to throw so we want to round up to the | 241 // The user is trying to throw so we want to round up to the |
242 // nearest page in the direction they are throwing. | 242 // nearest page in the direction they are throwing. |
243 pageNumber = currentTarget < current | 243 pageNumber = currentTarget < current |
244 ? currentPageNumber + 1 : currentPageNumber - 1; | 244 ? currentPageNumber + 1 : currentPageNumber - 1; |
245 } else { | 245 } else { |
246 pageNumber = pageNumber.round(); | 246 pageNumber = pageNumber.round(); |
247 } | 247 } |
248 } | 248 } |
249 pageNumber = pageNumber.toInt(); | 249 pageNumber = pageNumber; |
floitsch
2013/03/11 13:39:15
Remove line.
| |
250 num translate = -pageNumber * pageSize; | 250 num translate = -pageNumber * pageSize; |
251 pages.current.value = pageNumber; | 251 pages.current.value = pageNumber; |
252 if (currentTarget != translate) { | 252 if (currentTarget != translate) { |
253 scroller.throwTo(translate, 0); | 253 scroller.throwTo(translate, 0); |
254 } else { | 254 } else { |
255 // Update the target page number when we are done animating. | 255 // Update the target page number when we are done animating. |
256 pages.target.value = pageNumber; | 256 pages.target.value = pageNumber; |
257 } | 257 } |
258 }); | 258 }); |
259 } | 259 } |
(...skipping 16 matching lines...) Expand all Loading... | |
276 return perPage * (columnSize + _columnGap); | 276 return perPage * (columnSize + _columnGap); |
277 } | 277 } |
278 | 278 |
279 void _onPageSelected() { | 279 void _onPageSelected() { |
280 window.requestLayoutFrame(() { | 280 window.requestLayoutFrame(() { |
281 int translate = -pages.target.value * _computePageSize(_container); | 281 int translate = -pages.target.value * _computePageSize(_container); |
282 scroller.throwTo(translate, 0); | 282 scroller.throwTo(translate, 0); |
283 }); | 283 }); |
284 } | 284 } |
285 } | 285 } |
OLD | NEW |