Index: sky/sdk/lib/framework/layout2.dart |
diff --git a/sky/sdk/lib/framework/layout2.dart b/sky/sdk/lib/framework/layout2.dart |
index 0284dc845acb6614d0ece7aed9ff830258694cbe..f4116ab3240c04ab38004082b43e5efa980c3b51 100644 |
--- a/sky/sdk/lib/framework/layout2.dart |
+++ b/sky/sdk/lib/framework/layout2.dart |
@@ -461,6 +461,22 @@ abstract class RenderBox extends RenderNode { |
layoutDone(); |
} |
+ bool handlePointer(sky.PointerEvent event, { double x: 0.0, double y: 0.0 }) { |
Hixie
2015/05/22 20:32:16
This can't be in a class above ContainerRenderNode
jackson
2015/05/23 00:21:04
Acknowledged.
|
+ // the x, y parameters have the top left of the node's box as the origin |
+ RenderBox child = _lastChild; |
+ while (child != null) { |
+ assert(child.parentData is BoxParentData); |
+ if ((x >= child.parentData.x) && (x < child.parentData.x + child.width) && |
+ (y >= child.parentData.y) && (y < child.parentData.y + child.height)) { |
+ if (child.handlePointer(event, x: x-child.parentData.x, y: y-child.parentData.y)) |
+ return true; |
+ break; |
+ } |
+ child = child.parentData.previousSibling; |
+ } |
+ return super.handlePointer(event, x: x, y: y); |
+ } |
+ |
double width; |
double height; |
} |
@@ -682,22 +698,6 @@ class RenderBlock extends RenderDecoratedBox with ContainerRenderNodeMixin<Rende |
layoutDone(); |
} |
- bool handlePointer(sky.PointerEvent event, { double x: 0.0, double y: 0.0 }) { |
- // the x, y parameters have the top left of the node's box as the origin |
- RenderBox child = _lastChild; |
- while (child != null) { |
- assert(child.parentData is BlockParentData); |
- if ((x >= child.parentData.x) && (x < child.parentData.x + child.width) && |
- (y >= child.parentData.y) && (y < child.parentData.y + child.height)) { |
- if (child.handlePointer(event, x: x-child.parentData.x, y: y-child.parentData.y)) |
- return true; |
- break; |
- } |
- child = child.parentData.previousSibling; |
- } |
- return super.handlePointer(event, x: x, y: y); |
- } |
- |
void paint(RenderNodeDisplayList canvas) { |
super.paint(canvas); |
RenderBox child = _firstChild; |
@@ -710,7 +710,9 @@ class RenderBlock extends RenderDecoratedBox with ContainerRenderNodeMixin<Rende |
} |
-class FlexBoxParentData extends BoxParentData { |
+// FLEXBOX LAYOUT MANAGER |
+ |
+class FlexBoxParentData extends BoxParentData with ContainerParentDataMixin<RenderBox> { |
int flex; |
void merge(FlexBoxParentData other) { |
if (other.flex != null) |
@@ -719,10 +721,127 @@ class FlexBoxParentData extends BoxParentData { |
} |
} |
-enum FlexDirection { Row, Column } |
+enum FlexDirection { Horizontal, Vertical } |
+ |
+class RenderFlex extends RenderDecoratedBox with ContainerRenderNodeMixin<RenderBox, FlexBoxParentData> { |
+ // lays out RenderBox children using flexible layout |
+ |
+ RenderFlex({ |
+ BoxDecoration decoration, |
+ FlexDirection direction: FlexDirection.Horizontal |
+ }) : super(decoration), _direction = direction; |
+ |
+ FlexDirection _direction; |
+ FlexDirection get direction => _direction; |
+ void set direction (FlexDirection value) { |
+ if (_direction != value) { |
+ _direction = value; |
+ markNeedsLayout(); |
+ } |
+ } |
+ |
+ void setParentData(RenderBox child) { |
+ if (child.parentData is! FlexBoxParentData) |
+ child.parentData = new FlexBoxParentData(); |
+ } |
+ |
+ BoxConstraints _constraints; // value cached from parent for relayout call |
+ void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) { |
+ if (relayoutSubtreeRoot != null) |
+ saveRelayoutSubtreeRoot(relayoutSubtreeRoot); |
+ relayoutSubtreeRoot = relayoutSubtreeRoot == null ? this : relayoutSubtreeRoot; |
+ _constraints = constraints; |
+ width = clamp(min: _constraints.minWidth, max: _constraints.maxWidth); |
Hixie
2015/05/22 20:32:16
width = _constraints.constrainWidth(0.0);
jackson
2015/05/23 00:21:04
Acknowledged.
|
+ height = clamp(min: _constraints.minHeight, max: _constraints.maxHeight); |
+ assert(height < double.INFINITY); |
+ assert(width < double.INFINITY); |
+ internalLayout(relayoutSubtreeRoot); |
+ } |
+ |
+ void relayout() { |
+ internalLayout(this); |
+ } |
+ |
+ int _getFlex(RenderBox child) { |
+ assert(child.parentData is FlexBoxParentData); |
+ return (child.parentData.flex != null ? child.parentData.flex : 0); |
+ } |
+ |
+ void internalLayout(RenderNode relayoutSubtreeRoot) { |
+ // Based on http://www.w3.org/TR/css-flexbox-1/ Section 9.7 Resolving Flexible Lengths |
+ // Steps 1-3. Determine used flex factor, size inflexible items, calculate free space |
+ int numFlexibleChildren = 0; |
+ int totalFlex = 0; |
+ assert(_constraints != null); |
+ double freeSpace = (_direction == FlexDirection.Horizontal) ? _constraints.maxWidth : _constraints.maxHeight; |
+ RenderBox child = _firstChild; |
+ while (child != null) { |
+ int flex = _getFlex(child); |
+ if (flex > 0) { |
+ numFlexibleChildren++; |
+ totalFlex += child.parentData.flex; |
+ } else { |
+ BoxConstraints constraints = new BoxConstraints(maxHeight: _constraints.maxHeight, |
+ maxWidth: _constraints.maxWidth); |
+ child.layout(constraints, |
+ relayoutSubtreeRoot: relayoutSubtreeRoot); |
+ freeSpace -= (_direction == FlexDirection.Horizontal) ? child.width : child.height; |
+ } |
+ child = child.parentData.nextSibling; |
+ } |
+ |
+ // Steps 4-5. Distribute remaining space to flexible children. |
+ double spacePerFlex = freeSpace / totalFlex; |
Hixie
2015/05/22 20:32:16
check what happens when totalFlex is 0.0 and eithe
jackson
2015/05/23 00:21:04
Acknowledged.
|
+ double usedSpace = 0.0; |
+ child = _firstChild; |
+ while (child != null) { |
+ int flex = _getFlex(child); |
+ if (flex > 0) { |
+ double spaceForChild = spacePerFlex * flex; |
+ BoxConstraints constraints; |
+ switch (_direction) { |
+ case FlexDirection.Horizontal: |
+ constraints = new BoxConstraints(maxHeight: _constraints.maxHeight, |
+ minWidth: spaceForChild, |
+ maxWidth: spaceForChild); |
+ break; |
+ case FlexDirection.Vertical: |
+ constraints = new BoxConstraints(minHeight: spaceForChild, |
+ maxHeight: spaceForChild, |
+ maxWidth: _constraints.maxWidth); |
+ break; |
+ } |
+ child.layout(constraints, relayoutSubtreeRoot: relayoutSubtreeRoot); |
+ } |
-// TODO(ianh): FlexBox |
+ // For now, center the flex items in the cross direction |
+ switch (_direction) { |
+ case FlexDirection.Horizontal: |
+ child.parentData.x = usedSpace; |
+ usedSpace += child.width; |
+ child.parentData.y = height / 2 - child.height / 2; |
+ break; |
+ case FlexDirection.Vertical: |
+ child.parentData.y = usedSpace; |
+ usedSpace += child.height; |
+ child.parentData.x = width / 2 - child.width / 2; |
+ break; |
+ } |
+ child = child.parentData.nextSibling; |
+ } |
+ layoutDone(); |
+ } |
+ void paint(RenderNodeDisplayList canvas) { |
+ super.paint(canvas); |
+ RenderBox child = _firstChild; |
Hixie
2015/05/22 20:32:16
maybe do the same as for handlePointer() (with a h
jackson
2015/05/23 00:21:04
Acknowledged.
|
+ while (child != null) { |
+ assert(child.parentData is FlexBoxParentData); |
+ canvas.paintChild(child, child.parentData.x, child.parentData.y); |
+ child = child.parentData.nextSibling; |
+ } |
+ } |
+} |
// SCAFFOLD LAYOUT MANAGER |