| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:math' as math; | |
| 6 | |
| 7 import 'box.dart'; | |
| 8 import 'object.dart'; | |
| 9 | |
| 10 class StackParentData extends BoxParentData with ContainerParentDataMixin<Render
Box> { | |
| 11 double top; | |
| 12 double right; | |
| 13 double bottom; | |
| 14 double left; | |
| 15 | |
| 16 void merge(StackParentData other) { | |
| 17 if (other.top != null) | |
| 18 top = other.top; | |
| 19 if (other.right != null) | |
| 20 right = other.right; | |
| 21 if (other.bottom != null) | |
| 22 bottom = other.bottom; | |
| 23 if (other.left != null) | |
| 24 left = other.left; | |
| 25 super.merge(other); | |
| 26 } | |
| 27 | |
| 28 bool get isPositioned => top != null || right != null || bottom != null || lef
t != null; | |
| 29 | |
| 30 String toString() => '${super.toString()}; top=$top; right=$right; bottom=$bot
tom, left=$left'; | |
| 31 } | |
| 32 | |
| 33 class RenderStack extends RenderBox with ContainerRenderObjectMixin<RenderBox, S
tackParentData>, | |
| 34 RenderBoxContainerDefaultsMixin<RenderB
ox, StackParentData> { | |
| 35 RenderStack({ | |
| 36 List<RenderBox> children | |
| 37 }) { | |
| 38 if (children != null) | |
| 39 children.forEach((child) { add(child); }); | |
| 40 } | |
| 41 | |
| 42 void setParentData(RenderBox child) { | |
| 43 if (child.parentData is! StackParentData) | |
| 44 child.parentData = new StackParentData(); | |
| 45 } | |
| 46 | |
| 47 | |
| 48 double getMinIntrinsicWidth(BoxConstraints constraints) { | |
| 49 double width = constraints.minWidth; | |
| 50 RenderBox child = firstChild; | |
| 51 while (child != null) { | |
| 52 assert(child.parentData is StackParentData); | |
| 53 if (!child.parentData.isPositioned) | |
| 54 width = math.max(width, child.getMinIntrinsicWidth(constraints)); | |
| 55 child = child.parentData.nextSibling; | |
| 56 } | |
| 57 assert(width == constraints.constrainWidth(width)); | |
| 58 return width; | |
| 59 } | |
| 60 | |
| 61 double getMaxIntrinsicWidth(BoxConstraints constraints) { | |
| 62 bool hasNonPositionedChildren = false; | |
| 63 double width = constraints.minWidth; | |
| 64 RenderBox child = firstChild; | |
| 65 while (child != null) { | |
| 66 assert(child.parentData is StackParentData); | |
| 67 if (!child.parentData.isPositioned) { | |
| 68 hasNonPositionedChildren = true; | |
| 69 width = math.max(width, child.getMaxIntrinsicWidth(constraints)); | |
| 70 } | |
| 71 child = child.parentData.nextSibling; | |
| 72 } | |
| 73 if (!hasNonPositionedChildren) | |
| 74 return constraints.constrainWidth(double.INFINITY); | |
| 75 assert(width == constraints.constrainWidth(width)); | |
| 76 return width; | |
| 77 } | |
| 78 | |
| 79 double getMinIntrinsicHeight(BoxConstraints constraints) { | |
| 80 double height = constraints.minHeight; | |
| 81 RenderBox child = firstChild; | |
| 82 while (child != null) { | |
| 83 assert(child.parentData is StackParentData); | |
| 84 if (!child.parentData.isPositioned) | |
| 85 height = math.max(height, child.getMinIntrinsicHeight(constraints)); | |
| 86 child = child.parentData.nextSibling; | |
| 87 } | |
| 88 assert(height == constraints.constrainHeight(height)); | |
| 89 return height; | |
| 90 } | |
| 91 | |
| 92 double getMaxIntrinsicHeight(BoxConstraints constraints) { | |
| 93 bool hasNonPositionedChildren = false; | |
| 94 double height = constraints.minHeight; | |
| 95 RenderBox child = firstChild; | |
| 96 while (child != null) { | |
| 97 assert(child.parentData is StackParentData); | |
| 98 if (!child.parentData.isPositioned) { | |
| 99 hasNonPositionedChildren = true; | |
| 100 height = math.max(height, child.getMaxIntrinsicHeight(constraints)); | |
| 101 } | |
| 102 child = child.parentData.nextSibling; | |
| 103 } | |
| 104 if (!hasNonPositionedChildren) | |
| 105 return constraints.constrainHeight(double.INFINITY); | |
| 106 assert(height == constraints.constrainHeight(height)); | |
| 107 return height; | |
| 108 } | |
| 109 | |
| 110 void performLayout() { | |
| 111 bool hasNonPositionedChildren = false; | |
| 112 | |
| 113 double width = 0.0; | |
| 114 double height = 0.0; | |
| 115 | |
| 116 RenderBox child = firstChild; | |
| 117 while (child != null) { | |
| 118 assert(child.parentData is StackParentData); | |
| 119 final StackParentData parentData = child.parentData; | |
| 120 | |
| 121 if (!parentData.isPositioned) { | |
| 122 hasNonPositionedChildren = true; | |
| 123 | |
| 124 child.layout(constraints, parentUsesSize: true); | |
| 125 parentData.position = Point.origin; | |
| 126 | |
| 127 final Size childSize = child.size; | |
| 128 width = math.max(width, childSize.width); | |
| 129 height = math.max(height, childSize.height); | |
| 130 } | |
| 131 | |
| 132 child = parentData.nextSibling; | |
| 133 } | |
| 134 | |
| 135 if (hasNonPositionedChildren) | |
| 136 size = new Size(width, height); | |
| 137 else | |
| 138 size = constraints.constrain(Size.infinite); | |
| 139 | |
| 140 assert(size.width < double.INFINITY); | |
| 141 assert(size.height < double.INFINITY); | |
| 142 assert(size.width == constraints.constrainWidth(width)); | |
| 143 assert(size.height == constraints.constrainHeight(height)); | |
| 144 | |
| 145 BoxConstraints innerConstraints = new BoxConstraints.loose(size); | |
| 146 | |
| 147 child = firstChild; | |
| 148 while (child != null) { | |
| 149 assert(child.parentData is StackParentData); | |
| 150 final StackParentData parentData = child.parentData; | |
| 151 | |
| 152 if (parentData.isPositioned) { | |
| 153 BoxConstraints childConstraints = innerConstraints; | |
| 154 | |
| 155 if (parentData.left != null && parentData.right != null) | |
| 156 childConstraints = childConstraints.applyWidth(parentData.right - pare
ntData.left); | |
| 157 else if (parentData.left != null) | |
| 158 childConstraints = childConstraints.applyMaxWidth(size.width - parentD
ata.left); | |
| 159 else if (parentData.right != null) | |
| 160 childConstraints = childConstraints.applyMaxWidth(size.width - parentD
ata.right); | |
| 161 | |
| 162 if (parentData.top != null && parentData.bottom != null) | |
| 163 childConstraints = childConstraints.applyHeight(parentData.bottom - pa
rentData.top); | |
| 164 else if (parentData.top != null) | |
| 165 childConstraints = childConstraints.applyMaxHeight(size.height - paren
tData.top); | |
| 166 else if (parentData.bottom != null) | |
| 167 childConstraints = childConstraints.applyMaxHeight(size.width - parent
Data.bottom); | |
| 168 | |
| 169 child.layout(childConstraints); | |
| 170 | |
| 171 double x = 0.0; | |
| 172 if (parentData.left != null) | |
| 173 x = parentData.left; | |
| 174 else if (parentData.right != null) | |
| 175 x = size.width - parentData.right - child.size.width; | |
| 176 assert(x >= 0.0 && x + child.size.width <= size.width); | |
| 177 | |
| 178 double y = 0.0; | |
| 179 if (parentData.top != null) | |
| 180 y = parentData.top; | |
| 181 else if (parentData.bottom != null) | |
| 182 y = size.height - parentData.bottom - child.size.height; | |
| 183 assert(y >= 0.0 && y + child.size.height <= size.height); | |
| 184 | |
| 185 parentData.position = new Point(x, y); | |
| 186 } | |
| 187 | |
| 188 child = parentData.nextSibling; | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 void hitTestChildren(HitTestResult result, { Point position }) { | |
| 193 defaultHitTestChildren(result, position: position); | |
| 194 } | |
| 195 | |
| 196 void paint(RenderObjectDisplayList canvas) { | |
| 197 defaultPaint(canvas); | |
| 198 } | |
| 199 } | |
| OLD | NEW |