Index: sky/sdk/lib/framework/rendering/flex.dart |
diff --git a/sky/sdk/lib/framework/rendering/flex.dart b/sky/sdk/lib/framework/rendering/flex.dart |
index 1a0a1f61d1a0760f3a0749ae573f818e02d81366..0f5dc51072fdb697ba9ec82a82b093e8d8a7aaa0 100644 |
--- a/sky/sdk/lib/framework/rendering/flex.dart |
+++ b/sky/sdk/lib/framework/rendering/flex.dart |
@@ -2,6 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+import 'dart:math' as math; |
import 'box.dart'; |
import 'object.dart'; |
@@ -16,14 +17,22 @@ class FlexBoxParentData extends BoxParentData with ContainerParentDataMixin<Rend |
} |
enum FlexDirection { horizontal, vertical } |
+enum FlexJustifyContent { |
+ flexStart, |
+ flexEnd, |
+ center, |
+ spaceBetween, |
+ spaceAround, |
+} |
class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, FlexBoxParentData>, |
RenderBoxContainerDefaultsMixin<RenderBox, FlexBoxParentData> { |
// lays out RenderBox children using flexible layout |
RenderFlex({ |
- FlexDirection direction: FlexDirection.horizontal |
- }) : _direction = direction; |
+ FlexDirection direction: FlexDirection.horizontal, |
+ FlexJustifyContent justifyContent: FlexJustifyContent.flexStart |
+ }) : _direction = direction, _justifyContent = justifyContent; |
FlexDirection _direction; |
FlexDirection get direction => _direction; |
@@ -34,6 +43,15 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl |
} |
} |
+ FlexJustifyContent _justifyContent; |
+ FlexJustifyContent get justifyContent => _justifyContent; |
+ void set justifyContent (FlexJustifyContent value) { |
+ if (_justifyContent != value) { |
+ _justifyContent = value; |
+ markNeedsLayout(); |
+ } |
+ } |
+ |
void setParentData(RenderBox child) { |
if (child.parentData is! FlexBoxParentData) |
child.parentData = new FlexBoxParentData(); |
@@ -55,10 +73,12 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl |
// 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 totalFlex = 0; |
+ int totalChildren = 0; |
assert(constraints != null); |
double freeSpace = (_direction == FlexDirection.horizontal) ? constraints.maxWidth : constraints.maxHeight; |
RenderBox child = firstChild; |
while (child != null) { |
+ totalChildren++; |
int flex = _getFlex(child); |
if (flex > 0) { |
totalFlex += child.parentData.flex; |
@@ -93,19 +113,54 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl |
break; |
} |
child.layout(innerConstraints, parentUsesSize: true); |
+ usedSpace += _direction == FlexDirection.horizontal ? child.size.width : child.size.height; |
} |
+ child = child.parentData.nextSibling; |
+ } |
+ |
+ // Section 8.2: Axis Alignment using the justify-content property |
+ double remainingSpace = math.max(0.0, freeSpace - usedSpace); |
+ double leadingSpace; |
+ double betweenSpace; |
+ child = firstChild; |
+ switch (_justifyContent) { |
+ case FlexJustifyContent.flexStart: |
+ leadingSpace = 0.0; |
+ betweenSpace = 0.0; |
+ break; |
+ case FlexJustifyContent.flexEnd: |
+ leadingSpace = remainingSpace; |
+ betweenSpace = 0.0; |
+ break; |
+ case FlexJustifyContent.center: |
+ leadingSpace = remainingSpace / 2.0; |
+ betweenSpace = 0.0; |
+ break; |
+ case FlexJustifyContent.spaceBetween: |
+ leadingSpace = 0.0; |
+ betweenSpace = totalChildren > 1 ? remainingSpace / (totalChildren - 1) : 0.0; |
+ break; |
+ case FlexJustifyContent.spaceAround: |
+ betweenSpace = totalChildren > 0 ? remainingSpace / totalChildren : 0.0; |
+ leadingSpace = betweenSpace / 2.0; |
+ break; |
+ } |
- // For now, center the flex items in the cross direction |
+ // Position elements. For now, center the flex items in the cross direction |
+ double mainDimPosition = leadingSpace; |
+ child = firstChild; |
+ while (child != null) { |
switch (_direction) { |
case FlexDirection.horizontal: |
- child.parentData.position = new Point(usedSpace, size.height / 2.0 - child.size.height / 2.0); |
- usedSpace += child.size.width; |
+ child.parentData.position = new Point(mainDimPosition, size.height / 2.0 - child.size.height / 2.0); |
+ mainDimPosition += child.size.width; |
break; |
case FlexDirection.vertical: |
- child.parentData.position = new Point(size.width / 2.0 - child.size.width / 2.0, usedSpace); |
- usedSpace += child.size.height; |
+ child.parentData.position = new Point(size.width / 2.0 - child.size.width / 2.0, mainDimPosition); |
+ mainDimPosition += child.size.height; |
break; |
} |
+ mainDimPosition += betweenSpace; |
child = child.parentData.nextSibling; |
} |
} |