| 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 class PageState { | 5 class PageState { |
| 6 final ObservableValue<int> current; | 6 final ObservableValue<int> current; |
| 7 final ObservableValue<int> target; | 7 final ObservableValue<int> target; |
| 8 final ObservableValue<int> length; | 8 final ObservableValue<int> length; |
| 9 PageState() : | 9 PageState() : |
| 10 current = new ObservableValue<int>(0), | 10 current = new ObservableValue<int>(0), |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 // this causes the last page to end up right justified. But it seems to | 108 // this causes the last page to end up right justified. But it seems to |
| 109 // work reasonably well for both clicking and throwing. So for now, leave | 109 // work reasonably well for both clicking and throwing. So for now, leave |
| 110 // the scroller configured the default way. | 110 // the scroller configured the default way. |
| 111 | 111 |
| 112 // TODO(jacobr): use named arguments when available. | 112 // TODO(jacobr): use named arguments when available. |
| 113 scroller = new Scroller( | 113 scroller = new Scroller( |
| 114 _container, | 114 _container, |
| 115 false /* verticalScrollEnabled */, | 115 false /* verticalScrollEnabled */, |
| 116 true /* horizontalScrollEnabled */, | 116 true /* horizontalScrollEnabled */, |
| 117 true /* momementumEnabled */, | 117 true /* momementumEnabled */, |
| 118 () => new Size(_viewLength, 1), // Only view width matters. | 118 () { |
| 119 final completer = new Completer<Size>(); |
| 120 _container.rect.then((ElementRect rect) { |
| 121 // Only view width matters. |
| 122 completer.complete(new Size(_getViewLength(rect), 1)); |
| 123 }); |
| 124 return completer.future; |
| 125 }, |
| 119 Scroller.FAST_SNAP_DECELERATION_FACTOR); | 126 Scroller.FAST_SNAP_DECELERATION_FACTOR); |
| 120 | 127 |
| 121 scroller.onDecelStart.add(_snapToPage); | 128 scroller.onDecelStart.add(_snapToPage); |
| 122 scroller.onScrollerDragEnd.add(_snapToPage); | 129 scroller.onScrollerDragEnd.add(_snapToPage); |
| 123 scroller.onContentMoved.add(_onContentMoved); | 130 scroller.onContentMoved.add(_onContentMoved); |
| 124 return node; | 131 return node; |
| 125 } | 132 } |
| 126 | 133 |
| 127 int get _viewLength() => _computePageSize() * pages.length.value; | 134 int _getViewLength(ElementRect rect) { |
| 135 return _computePageSize(rect) * pages.length.value; |
| 136 } |
| 128 | 137 |
| 129 // TODO(jmesserly): would be better to not have this code in enterDocument. | 138 // TODO(jmesserly): would be better to not have this code in enterDocument. |
| 130 // But we need getComputedStyles to read our CSS properties. | 139 // But we need computedStyle to read our CSS properties. |
| 131 void enterDocument() { | 140 void enterDocument() { |
| 132 _computeColumnGap(); | 141 contentView.node.computedStyle.then((CSSStyleDeclaration style) { |
| 142 _computeColumnGap(style); |
| 133 | 143 |
| 134 // Trigger a fake resize event so we measure our height. | 144 // Trigger a fake resize event so we measure our height. |
| 135 windowResized(); | 145 windowResized(); |
| 136 | 146 |
| 137 // Hook img onload events, so we find out about changes in content size | 147 // Hook img onload events, so we find out about changes in content size |
| 138 for (ImageElement img in contentView.node.queryAll("img")) { | 148 for (ImageElement img in contentView.node.queryAll("img")) { |
| 139 if (!img.complete) { | 149 if (!img.complete) { |
| 140 img.on.load.add((e) { | 150 img.on.load.add((e) { |
| 141 _updatePageCount(); | 151 _updatePageCount(null); |
| 142 }); | 152 }); |
| 153 } |
| 143 } | 154 } |
| 144 } | |
| 145 | 155 |
| 146 // If the selected page changes, animate to it. | 156 // If the selected page changes, animate to it. |
| 147 watch(pages.target, (s) => _onPageSelected()); | 157 watch(pages.target, (s) => _onPageSelected()); |
| 148 watch(pages.length, (s) => _onPageSelected()); | 158 watch(pages.length, (s) => _onPageSelected()); |
| 159 }); |
| 149 } | 160 } |
| 150 | 161 |
| 151 /** Read the column-gap setting so we know how far to translate the child. */ | 162 /** Read the column-gap setting so we know how far to translate the child. */ |
| 152 void _computeColumnGap() { | 163 void _computeColumnGap(CSSStyleDeclaration style) { |
| 153 final style = window.getComputedStyle(contentView.node, ''); | |
| 154 String gap = style.columnGap; | 164 String gap = style.columnGap; |
| 155 if (gap == 'normal') { | 165 if (gap == 'normal') { |
| 156 gap = style.fontSize; | 166 gap = style.fontSize; |
| 157 } | 167 } |
| 158 _columnGap = _toPixels(gap, 'column-gap or font-size'); | 168 _columnGap = _toPixels(gap, 'column-gap or font-size'); |
| 159 _columnWidth = _toPixels(style.columnWidth, 'column-width'); | 169 _columnWidth = _toPixels(style.columnWidth, 'column-width'); |
| 160 } | 170 } |
| 161 | 171 |
| 162 static int _toPixels(String value, String message) { | 172 static int _toPixels(String value, String message) { |
| 163 // TODO(jmesserly): Safari 4 has a bug where this property does not end | 173 // TODO(jmesserly): Safari 4 has a bug where this property does not end |
| 164 // in "px" like it should, but the value is correct. Handle that gracefully. | 174 // in "px" like it should, but the value is correct. Handle that gracefully. |
| 165 if (value.endsWith('px')) { | 175 if (value.endsWith('px')) { |
| 166 value = value.substring(0, value.length - 2); | 176 value = value.substring(0, value.length - 2); |
| 167 } | 177 } |
| 168 return Math.parseDouble(value).round().toInt(); | 178 return Math.parseDouble(value).round().toInt(); |
| 169 } | 179 } |
| 170 | 180 |
| 171 /** Watch for resize and update page count. */ | 181 /** Watch for resize and update page count. */ |
| 172 void windowResized() { | 182 void windowResized() { |
| 173 // TODO(jmesserly): verify we aren't triggering unnecessary layouts. | 183 // TODO(jmesserly): verify we aren't triggering unnecessary layouts. |
| 174 | 184 |
| 175 // The content needs to have its height explicitly set, or columns don't | 185 // The content needs to have its height explicitly set, or columns don't |
| 176 // flow to the right correctly. So we copy our own height and set the height | 186 // flow to the right correctly. So we copy our own height and set the height |
| 177 // of the content. | 187 // of the content. |
| 178 contentView.node.style.height = '${node.offsetHeight}px'; | 188 node.rect.then((ElementRect rect) { |
| 179 | 189 contentView.node.style.height = '${rect.offset.height}px'; |
| 180 _updatePageCount(); | 190 }); |
| 181 scroller.reconfigure(); | 191 _updatePageCount(null); |
| 182 // TODO(jacobr): calling snapToPage here is overkill. | |
| 183 _snapToPage(null); | |
| 184 } | 192 } |
| 185 | 193 |
| 186 bool _updatePageCount() { | 194 bool _updatePageCount(Callback callback) { |
| 187 int pageLength = 1; | 195 int pageLength = 1; |
| 188 if (_container.scrollWidth > _container.offsetWidth) { | 196 _container.rect.then((ElementRect rect) { |
| 189 pageLength = (_container.scrollWidth / _computePageSize()).ceil().toInt(); | 197 if (rect.scroll.width > rect.offset.width) { |
| 190 } | 198 pageLength = (rect.scroll.width / _computePageSize(rect)) |
| 191 pageLength = Math.max(pageLength, 1); | 199 .ceil().toInt(); |
| 200 } |
| 201 pageLength = Math.max(pageLength, 1); |
| 192 | 202 |
| 193 int oldPage = pages.target.value; | 203 int oldPage = pages.target.value; |
| 194 int newPage = Math.min(oldPage, pageLength - 1); | 204 int newPage = Math.min(oldPage, pageLength - 1); |
| 195 | 205 |
| 196 // Hacky: make sure a change event always fires. | 206 // Hacky: make sure a change event always fires. |
| 197 // This is so we adjust the 3d transform after resize. | 207 // This is so we adjust the 3d transform after resize. |
| 198 if (oldPage == newPage) { | 208 if (oldPage == newPage) { |
| 199 pages.target.value = 0; | 209 pages.target.value = 0; |
| 200 } | 210 } |
| 201 assert(newPage < pageLength); | 211 assert(newPage < pageLength); |
| 202 pages.target.value = newPage; | 212 pages.target.value = newPage; |
| 203 pages.length.value = pageLength; | 213 pages.length.value = pageLength; |
| 214 if (callback != null) { |
| 215 callback(); |
| 216 } |
| 217 }); |
| 204 } | 218 } |
| 205 | 219 |
| 206 void _onContentMoved(Event e) { | 220 void _onContentMoved(Event e) { |
| 207 num current = scroller.contentOffset.x; | 221 _container.rect.then((ElementRect rect) { |
| 208 int pageSize = _computePageSize(); | 222 num current = scroller.contentOffset.x; |
| 209 pages.current.value = -(current / pageSize).round().toInt(); | 223 int pageSize = _computePageSize(rect); |
| 224 pages.current.value = -(current / pageSize).round().toInt(); |
| 225 }); |
| 210 } | 226 } |
| 211 | 227 |
| 212 void _snapToPage(Event e) { | 228 void _snapToPage(Event e) { |
| 213 num current = scroller.contentOffset.x; | 229 num current = scroller.contentOffset.x; |
| 214 num currentTarget = scroller.currentTarget.x; | 230 num currentTarget = scroller.currentTarget.x; |
| 215 int pageSize = _computePageSize(); | 231 _container.rect.then((ElementRect rect) { |
| 216 int destination; | 232 int pageSize = _computePageSize(rect); |
| 217 num currentPageNumber = -(current / pageSize).round(); | 233 int destination; |
| 218 num pageNumber = -currentTarget / pageSize; | 234 num currentPageNumber = -(current / pageSize).round(); |
| 219 if (current == currentTarget) { | 235 num pageNumber = -currentTarget / pageSize; |
| 220 // User was just static dragging so round to the nearest page. | 236 if (current == currentTarget) { |
| 221 pageNumber = pageNumber.round(); | 237 // User was just static dragging so round to the nearest page. |
| 222 } else { | 238 pageNumber = pageNumber.round(); |
| 223 if (currentPageNumber == pageNumber.round() && | |
| 224 (pageNumber - currentPageNumber).abs() > MIN_THROW_PAGE_FRACTION && | |
| 225 -current + _viewportSize < _viewLength && current < 0) { | |
| 226 // The user is trying to throw so we want to round up to the | |
| 227 // nearest page in the direction they are throwing. | |
| 228 pageNumber = currentTarget < current | |
| 229 ? currentPageNumber + 1 : currentPageNumber - 1; | |
| 230 } else { | 239 } else { |
| 231 pageNumber = pageNumber.round(); | 240 if (currentPageNumber == pageNumber.round() && |
| 241 (pageNumber - currentPageNumber).abs() > MIN_THROW_PAGE_FRACTION && |
| 242 -current + _viewportSize < _getViewLength(rect) && current < 0) { |
| 243 // The user is trying to throw so we want to round up to the |
| 244 // nearest page in the direction they are throwing. |
| 245 pageNumber = currentTarget < current |
| 246 ? currentPageNumber + 1 : currentPageNumber - 1; |
| 247 } else { |
| 248 pageNumber = pageNumber.round(); |
| 249 } |
| 232 } | 250 } |
| 233 } | 251 pageNumber = pageNumber.toInt(); |
| 234 pageNumber = pageNumber.toInt(); | 252 num translate = -pageNumber * pageSize; |
| 235 num translate = -pageNumber * pageSize; | 253 pages.current.value = pageNumber; |
| 236 pages.current.value = pageNumber; | 254 if (currentTarget != translate) { |
| 237 if (currentTarget != translate) { | 255 scroller.throwTo(translate, 0); |
| 238 scroller.throwTo(translate, 0); | 256 } else { |
| 239 } else { | 257 // Update the target page number when we are done animating. |
| 240 // Update the target page number when we are done animating. | 258 pages.target.value = pageNumber; |
| 241 pages.target.value = pageNumber; | 259 } |
| 242 } | 260 }); |
| 243 } | 261 } |
| 244 | 262 |
| 245 int _computePageSize() { | 263 int _computePageSize(ElementRect rect) { |
| 246 // Hacky: we need to duplicate the way the columns are being computed, | 264 // Hacky: we need to duplicate the way the columns are being computed, |
| 247 // including rounding, to figure out how far to translate the div. | 265 // including rounding, to figure out how far to translate the div. |
| 248 // See http://www.w3.org/TR/css3-multicol/#column-width | 266 // See http://www.w3.org/TR/css3-multicol/#column-width |
| 249 _viewportSize = _container.offsetWidth; | 267 _viewportSize = rect.offset.width; |
| 250 | 268 |
| 251 // Figure out how many columns we're rendering. | 269 // Figure out how many columns we're rendering. |
| 252 // The algorithm ensures we're bigger than the specified min size. | 270 // The algorithm ensures we're bigger than the specified min size. |
| 253 int perPage = Math.max(1, | 271 int perPage = Math.max(1, |
| 254 (_viewportSize + _columnGap) ~/ (_columnWidth + _columnGap)); | 272 (_viewportSize + _columnGap) ~/ (_columnWidth + _columnGap)); |
| 255 | 273 |
| 256 // Divide up the viewport between the columns. | 274 // Divide up the viewport between the columns. |
| 257 int columnSize = (_viewportSize - (perPage - 1) * _columnGap) ~/ perPage; | 275 int columnSize = (_viewportSize - (perPage - 1) * _columnGap) ~/ perPage; |
| 258 | 276 |
| 259 // Finally, compute how big each page is, and how far to translate. | 277 // Finally, compute how big each page is, and how far to translate. |
| 260 return perPage * (columnSize + _columnGap); | 278 return perPage * (columnSize + _columnGap); |
| 261 } | 279 } |
| 262 | 280 |
| 263 void _onPageSelected() { | 281 void _onPageSelected() { |
| 264 int translate = -pages.target.value * _computePageSize(); | 282 _container.rect.then((ElementRect rect) { |
| 265 scroller.reconfigure(); | 283 int translate = -pages.target.value * _computePageSize(rect); |
| 266 scroller.throwTo(translate, 0); | 284 scroller.throwTo(translate, 0); |
| 285 }); |
| 267 } | 286 } |
| 268 } | 287 } |
| OLD | NEW |