| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import '../animation/scroll_behavior.dart'; | 5 import '../animation/scroll_behavior.dart'; |
| 6 import '../debug/tracing.dart'; | |
| 7 import '../fn2.dart'; | 6 import '../fn2.dart'; |
| 7 import 'dart:async'; |
| 8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
| 9 import 'dart:async'; | 9 import 'package:vector_math/vector_math.dart'; |
| 10 import 'scrollable.dart'; | 10 import 'scrollable.dart'; |
| 11 | 11 |
| 12 abstract class FixedHeightScrollable extends Scrollable { | 12 abstract class FixedHeightScrollable extends Scrollable { |
| 13 static final Style _style = new Style(''' | 13 FixedHeightScrollable({ Object key }) : super(key: key); |
| 14 overflow: hidden; | |
| 15 position: relative; | |
| 16 will-change: transform;''' | |
| 17 ); | |
| 18 | |
| 19 static final Style _scrollAreaStyle = new Style(''' | |
| 20 position:relative; | |
| 21 will-change: transform;''' | |
| 22 ); | |
| 23 | |
| 24 FixedHeightScrollable({ | |
| 25 Object key | |
| 26 }) : super(key: key); | |
| 27 | 14 |
| 28 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); | 15 ScrollBehavior createScrollBehavior() => new OverscrollBehavior(); |
| 29 OverscrollBehavior get scrollBehavior => super.scrollBehavior as OverscrollBeh
avior; | 16 OverscrollBehavior get scrollBehavior => super.scrollBehavior as OverscrollBeh
avior; |
| 30 | 17 |
| 31 double _height = 0.0; | 18 double _height = 0.0; |
| 32 double _itemHeight; | 19 double _itemHeight; |
| 33 | 20 |
| 34 int _itemCount = 0; | 21 int _itemCount = 0; |
| 35 int get itemCount => _itemCount; | 22 int get itemCount => _itemCount; |
| 36 void set itemCount (int value) { | 23 void set itemCount (int value) { |
| 37 if (_itemCount != value) { | 24 if (_itemCount != value) { |
| 38 _itemCount = value; | 25 _itemCount = value; |
| 39 if (_itemHeight != null) | 26 if (_itemHeight != null) |
| 40 scrollBehavior.contentsHeight = _itemHeight * _itemCount; | 27 scrollBehavior.contentsHeight = _itemHeight * _itemCount; |
| 41 } | 28 } |
| 42 } | 29 } |
| 43 | 30 |
| 44 void _measureHeights() { | 31 void _measureHeights() { |
| 45 trace('FixedHeightScrollable::_measureHeights', () { | 32 if (_itemHeight != null) |
| 46 if (_itemHeight != null) | 33 return; |
| 47 return; | 34 setState(() { |
| 48 var root = getRoot(); | 35 // TODO(abarth): Actually measure these heights. |
| 49 if (root == null) | 36 _height = 500.0; // root.height; |
| 50 return; | 37 assert(_height > 0); |
| 51 var item = root.firstChild.firstChild; | 38 _itemHeight = 100.0; // item.height; |
| 52 if (item == null) | 39 assert(_itemHeight > 0); |
| 53 return; | 40 scrollBehavior.containerHeight = _height; |
| 54 setState(() { | 41 scrollBehavior.contentsHeight = _itemHeight * _itemCount; |
| 55 _height = root.height; | |
| 56 assert(_height > 0); | |
| 57 _itemHeight = item.height; | |
| 58 assert(_itemHeight > 0); | |
| 59 scrollBehavior.containerHeight = _height; | |
| 60 scrollBehavior.contentsHeight = _itemHeight * _itemCount; | |
| 61 }); | |
| 62 }); | 42 }); |
| 63 } | 43 } |
| 64 | 44 |
| 65 UINode buildContent() { | 45 UINode buildContent() { |
| 66 var itemNumber = 0; | 46 var itemNumber = 0; |
| 67 var drawCount = 1; | 47 var drawCount = 1; |
| 68 var transformStyle = ''; | 48 var transformStyle = ''; |
| 69 | 49 |
| 70 if (_itemHeight == null) | 50 if (_itemHeight == null) |
| 71 new Future.microtask(_measureHeights); | 51 new Future.microtask(_measureHeights); |
| 72 | 52 |
| 53 Matrix4 transform = new Matrix4.identity(); |
| 54 |
| 73 if (_height > 0.0 && _itemHeight != null) { | 55 if (_height > 0.0 && _itemHeight != null) { |
| 74 if (scrollOffset < 0.0) { | 56 if (scrollOffset < 0.0) { |
| 75 double visibleHeight = _height + scrollOffset; | 57 double visibleHeight = _height + scrollOffset; |
| 76 drawCount = (visibleHeight / _itemHeight).round() + 1; | 58 drawCount = (visibleHeight / _itemHeight).round() + 1; |
| 77 transformStyle = | 59 transform.translate(0.0, -scrollOffset); |
| 78 'transform: translateY(${(-scrollOffset).toStringAsFixed(2)}px)'; | |
| 79 } else { | 60 } else { |
| 80 drawCount = (_height / _itemHeight).ceil() + 1; | 61 drawCount = (_height / _itemHeight).ceil() + 1; |
| 81 double alignmentDelta = -scrollOffset % _itemHeight; | 62 double alignmentDelta = -scrollOffset % _itemHeight; |
| 82 if (alignmentDelta != 0.0) | 63 if (alignmentDelta != 0.0) |
| 83 alignmentDelta -= _itemHeight; | 64 alignmentDelta -= _itemHeight; |
| 84 | 65 |
| 85 double drawStart = scrollOffset + alignmentDelta; | 66 double drawStart = scrollOffset + alignmentDelta; |
| 86 itemNumber = math.max(0, (drawStart / _itemHeight).floor()); | 67 itemNumber = math.max(0, (drawStart / _itemHeight).floor()); |
| 87 | 68 |
| 88 transformStyle = | 69 transform.translate(0.0, alignmentDelta); |
| 89 'transform: translateY(${(alignmentDelta).toStringAsFixed(2)}px)'; | |
| 90 } | 70 } |
| 91 } | 71 } |
| 92 | 72 |
| 93 return new Container( | 73 // TODO(abarth): Add a clip. |
| 94 style: _style, | 74 return new BlockContainer( |
| 95 children: [ | 75 children: [ |
| 96 new Container( | 76 new Transform( |
| 97 style: _scrollAreaStyle, | 77 transform: transform, |
| 98 inlineStyle: transformStyle, | 78 child: new BlockContainer(children: buildItems(itemNumber, drawCount)) |
| 99 children: buildItems(itemNumber, drawCount) | |
| 100 ) | 79 ) |
| 101 ] | 80 ] |
| 102 ); | 81 ); |
| 103 } | 82 } |
| 104 | 83 |
| 105 List<UINode> buildItems(int start, int count); | 84 List<UINode> buildItems(int start, int count); |
| 106 } | 85 } |
| OLD | NEW |