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 'dart:math' as math; | 5 import 'dart:math' as math; |
6 import 'dart:sky' as sky; | 6 import 'dart:sky' as sky; |
7 | 7 |
8 import 'package:vector_math/vector_math.dart'; | 8 import 'package:vector_math/vector_math.dart'; |
9 | 9 |
10 import '../base/debug.dart'; | 10 import '../base/debug.dart'; |
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1012 double delta = baseline - child.getDistanceToBaseline(baselineType); | 1012 double delta = baseline - child.getDistanceToBaseline(baselineType); |
1013 child.parentData.position = new Point(0.0, delta); | 1013 child.parentData.position = new Point(0.0, delta); |
1014 } else { | 1014 } else { |
1015 performResize(); | 1015 performResize(); |
1016 } | 1016 } |
1017 } | 1017 } |
1018 | 1018 |
1019 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(
prefix)}${prefix}baseline: ${baseline}\nbaselineType: ${baselineType}'; | 1019 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(
prefix)}${prefix}baseline: ${baseline}\nbaselineType: ${baselineType}'; |
1020 } | 1020 } |
1021 | 1021 |
| 1022 enum ViewportScrollDirection { horizontal, vertical, both } |
| 1023 |
| 1024 class RenderViewport extends RenderBox with RenderObjectWithChildMixin<RenderBox
> { |
| 1025 |
| 1026 RenderViewport({ |
| 1027 RenderBox child, |
| 1028 Offset scrollOffset, |
| 1029 ViewportScrollDirection direction: ViewportScrollDirection.vertical |
| 1030 }) : _scrollOffset = scrollOffset, |
| 1031 _scrollDirection = direction { |
| 1032 assert(_offsetIsSane(scrollOffset, direction)); |
| 1033 this.child = child; |
| 1034 } |
| 1035 |
| 1036 bool _offsetIsSane(Offset offset, ViewportScrollDirection direction) { |
| 1037 switch (direction) { |
| 1038 case ViewportScrollDirection.both: |
| 1039 return true; |
| 1040 case ViewportScrollDirection.horizontal: |
| 1041 return offset.dy == 0.0; |
| 1042 case ViewportScrollDirection.vertical: |
| 1043 return offset.dx == 0.0; |
| 1044 } |
| 1045 } |
| 1046 |
| 1047 Offset _scrollOffset; |
| 1048 Offset get scrollOffset => _scrollOffset; |
| 1049 void set scrollOffset(Offset value) { |
| 1050 if (value == _scrollOffset) |
| 1051 return; |
| 1052 assert(_offsetIsSane(value, scrollDirection)); |
| 1053 _scrollOffset = value; |
| 1054 markNeedsPaint(); |
| 1055 } |
| 1056 |
| 1057 ViewportScrollDirection _scrollDirection; |
| 1058 ViewportScrollDirection get scrollDirection => _scrollDirection; |
| 1059 void set scrollDirection(ViewportScrollDirection value) { |
| 1060 if (value == _scrollDirection) |
| 1061 return; |
| 1062 assert(_offsetIsSane(scrollOffset, value)); |
| 1063 _scrollDirection = value; |
| 1064 markNeedsLayout(); |
| 1065 } |
| 1066 |
| 1067 BoxConstraints _getInnerConstraints(BoxConstraints constraints) { |
| 1068 BoxConstraints innerConstraints; |
| 1069 switch (scrollDirection) { |
| 1070 case ViewportScrollDirection.both: |
| 1071 innerConstraints = new BoxConstraints(); |
| 1072 break; |
| 1073 case ViewportScrollDirection.horizontal: |
| 1074 innerConstraints = constraints.heightConstraints(); |
| 1075 break; |
| 1076 case ViewportScrollDirection.vertical: |
| 1077 innerConstraints = constraints.widthConstraints(); |
| 1078 break; |
| 1079 } |
| 1080 return innerConstraints; |
| 1081 } |
| 1082 |
| 1083 double getMinIntrinsicWidth(BoxConstraints constraints) { |
| 1084 if (child != null) |
| 1085 return child.getMinIntrinsicWidth(_getInnerConstraints(constraints)); |
| 1086 return super.getMinIntrinsicWidth(constraints); |
| 1087 } |
| 1088 |
| 1089 double getMaxIntrinsicWidth(BoxConstraints constraints) { |
| 1090 if (child != null) |
| 1091 return child.getMaxIntrinsicWidth(_getInnerConstraints(constraints)); |
| 1092 return super.getMaxIntrinsicWidth(constraints); |
| 1093 } |
| 1094 |
| 1095 double getMinIntrinsicHeight(BoxConstraints constraints) { |
| 1096 if (child != null) |
| 1097 return child.getMinIntrinsicHeight(_getInnerConstraints(constraints)); |
| 1098 return super.getMinIntrinsicHeight(constraints); |
| 1099 } |
| 1100 |
| 1101 double getMaxIntrinsicHeight(BoxConstraints constraints) { |
| 1102 if (child != null) |
| 1103 return child.getMaxIntrinsicHeight(_getInnerConstraints(constraints)); |
| 1104 return super.getMaxIntrinsicHeight(constraints); |
| 1105 } |
| 1106 |
| 1107 // We don't override computeDistanceToActualBaseline(), because we |
| 1108 // want the default behaviour (returning null). Otherwise, as you |
| 1109 // scroll the RenderViewport, it would shift in its parent if the |
| 1110 // parent was baseline-aligned, which makes no sense. |
| 1111 |
| 1112 void performLayout() { |
| 1113 if (child != null) { |
| 1114 child.layout(_getInnerConstraints(constraints), parentUsesSize: true); |
| 1115 size = constraints.constrain(child.size); |
| 1116 assert(child.parentData is BoxParentData); |
| 1117 child.parentData.position = Point.origin; |
| 1118 } else { |
| 1119 performResize(); |
| 1120 } |
| 1121 } |
| 1122 |
| 1123 void paint(PaintingCanvas canvas, Offset offset) { |
| 1124 if (child != null) { |
| 1125 bool _needsClip = offset < Offset.zero || |
| 1126 !(offset & size).contains(((offset - scrollOffset) & chi
ld.size).bottomRight); |
| 1127 if (_needsClip) { |
| 1128 canvas.save(); |
| 1129 canvas.clipRect(offset & size); |
| 1130 } |
| 1131 canvas.paintChild(child, (offset - scrollOffset).toPoint()); |
| 1132 if (_needsClip) |
| 1133 canvas.restore(); |
| 1134 } |
| 1135 } |
| 1136 |
| 1137 void hitTestChildren(HitTestResult result, { Point position }) { |
| 1138 if (child != null) { |
| 1139 assert(child.parentData is BoxParentData); |
| 1140 Rect childBounds = child.parentData.position & child.size; |
| 1141 if (childBounds.contains(position + -scrollOffset)) |
| 1142 child.hitTest(result, position: position + scrollOffset); |
| 1143 } |
| 1144 } |
| 1145 |
| 1146 } |
| 1147 |
1022 class RenderImage extends RenderBox { | 1148 class RenderImage extends RenderBox { |
1023 | 1149 |
1024 RenderImage(sky.Image image, Size requestedSize) | 1150 RenderImage(sky.Image image, Size requestedSize) |
1025 : _image = image, _requestedSize = requestedSize; | 1151 : _image = image, _requestedSize = requestedSize; |
1026 | 1152 |
1027 sky.Image _image; | 1153 sky.Image _image; |
1028 sky.Image get image => _image; | 1154 sky.Image get image => _image; |
1029 void set image (sky.Image value) { | 1155 void set image (sky.Image value) { |
1030 if (value == _image) | 1156 if (value == _image) |
1031 return; | 1157 return; |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1443 | 1569 |
1444 void defaultPaint(PaintingCanvas canvas, Offset offset) { | 1570 void defaultPaint(PaintingCanvas canvas, Offset offset) { |
1445 RenderBox child = firstChild; | 1571 RenderBox child = firstChild; |
1446 while (child != null) { | 1572 while (child != null) { |
1447 assert(child.parentData is ParentDataType); | 1573 assert(child.parentData is ParentDataType); |
1448 canvas.paintChild(child, child.parentData.position + offset); | 1574 canvas.paintChild(child, child.parentData.position + offset); |
1449 child = child.parentData.nextSibling; | 1575 child = child.parentData.nextSibling; |
1450 } | 1576 } |
1451 } | 1577 } |
1452 } | 1578 } |
OLD | NEW |