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