Initial implementation of flexWrap
This commit is contained in:
@@ -14,6 +14,7 @@ function __transpileToJavaCommon(code) {
|
||||
.replace(/CSS_FLEX_DIRECTION_/g, 'CSSFlexDirection.')
|
||||
.replace(/css_align_t/g, 'CSSAlign')
|
||||
.replace(/CSS_ALIGN_/g, 'CSSAlign.')
|
||||
.replace(/CSS_WRAP/g, 'CSSWrap.WRAP')
|
||||
.replace(/CSS_POSITION_/g, 'CSSPositionType.')
|
||||
.replace(/css_justify_t/g, 'CSSJustify')
|
||||
.replace(/CSS_JUSTIFY_/g, 'CSSJustify.')
|
||||
|
97
src/Layout.c
97
src/Layout.c
@@ -276,6 +276,10 @@ static bool isFlex(css_node_t *node) {
|
||||
);
|
||||
}
|
||||
|
||||
static bool isFlexWrap(css_node_t *node) {
|
||||
return node->style.flex_wrap == CSS_WRAP;
|
||||
}
|
||||
|
||||
static float getDimWithMargin(css_node_t *node, css_flex_direction_t axis) {
|
||||
return node->layout.dimensions[dim[axis]] +
|
||||
getMargin(node, leading[axis]) +
|
||||
@@ -425,6 +429,20 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
}
|
||||
}
|
||||
|
||||
float definedMainDim = CSS_UNDEFINED;
|
||||
if (!isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
||||
definedMainDim = node->layout.dimensions[dim[mainAxis]] -
|
||||
getPaddingAndBorderAxis(node, mainAxis);
|
||||
}
|
||||
|
||||
// We want to execute the next two loops one per line with flex-wrap
|
||||
int startLine = 0;
|
||||
int endLine = 0;
|
||||
int nextLine = 0;
|
||||
// We aggregate the total dimensions of the container in those two variables
|
||||
float linesCrossDim = 0;
|
||||
float linesMainDim = 0;
|
||||
while (endLine != node->children_count) {
|
||||
// <Loop A> Layout non flexible children and count children by type
|
||||
|
||||
// mainContentDim is accumulation of the dimensions and margin of all the
|
||||
@@ -438,8 +456,9 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
int flexibleChildrenCount = 0;
|
||||
float totalFlexible = 0;
|
||||
int nonFlexibleChildrenCount = 0;
|
||||
for (int i = 0; i < node->children_count; ++i) {
|
||||
for (int i = startLine; i < node->children_count; ++i) {
|
||||
css_node_t* child = node->get_child(node->context, i);
|
||||
float nextContentDim = 0;
|
||||
|
||||
// It only makes sense to consider a child flexible if we have a computed
|
||||
// dimension for the node->
|
||||
@@ -450,7 +469,7 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
// Even if we don't know its exact size yet, we already know the padding,
|
||||
// border and margin. We'll use this partial information to compute the
|
||||
// remaining space.
|
||||
mainContentDim += getPaddingAndBorderAxis(child, mainAxis) +
|
||||
nextContentDim = getPaddingAndBorderAxis(child, mainAxis) +
|
||||
getMarginAxis(child, mainAxis);
|
||||
|
||||
} else {
|
||||
@@ -467,16 +486,29 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
}
|
||||
|
||||
// This is the main recursive call. We layout non flexible children.
|
||||
if (nextLine == 0) {
|
||||
layoutNode(child, maxWidth);
|
||||
}
|
||||
|
||||
// Absolute positioned elements do not take part of the layout, so we
|
||||
// don't use them to compute mainContentDim
|
||||
if (getPositionType(child) == CSS_POSITION_RELATIVE) {
|
||||
nonFlexibleChildrenCount++;
|
||||
// At this point we know the final size and margin of the element.
|
||||
mainContentDim += getDimWithMargin(child, mainAxis);
|
||||
nextContentDim = getDimWithMargin(child, mainAxis);
|
||||
}
|
||||
}
|
||||
|
||||
// The element we are about to add would make us go to the next line
|
||||
if (isFlexWrap(node) &&
|
||||
!isUndefined(node->layout.dimensions[dim[mainAxis]]) &&
|
||||
mainContentDim + nextContentDim > definedMainDim) {
|
||||
nextLine = i + 1;
|
||||
break;
|
||||
}
|
||||
nextLine = 0;
|
||||
mainContentDim += nextContentDim;
|
||||
endLine = i + 1;
|
||||
}
|
||||
|
||||
// <Loop B> Layout flexible children and allocate empty space
|
||||
@@ -487,13 +519,13 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
float leadingMainDim = 0;
|
||||
float betweenMainDim = 0;
|
||||
|
||||
float definedMainDim = fmaxf(mainContentDim, 0);
|
||||
if (!isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
||||
definedMainDim = node->layout.dimensions[dim[mainAxis]] -
|
||||
getPaddingAndBorderAxis(node, mainAxis);
|
||||
}
|
||||
// The remaining available space that needs to be allocated
|
||||
float remainingMainDim = definedMainDim - mainContentDim;
|
||||
float remainingMainDim = 0;
|
||||
if (!isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
||||
remainingMainDim = definedMainDim - mainContentDim;
|
||||
} else {
|
||||
remainingMainDim = fmaxf(mainContentDim, 0) - mainContentDim;
|
||||
}
|
||||
|
||||
// If there are flexible children in the mix, they are going to fill the
|
||||
// remaining space
|
||||
@@ -508,7 +540,7 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
// We iterate over the full array and only apply the action on flexible
|
||||
// children. This is faster than actually allocating a new array that
|
||||
// contains only flexible children.
|
||||
for (int i = 0; i < node->children_count; ++i) {
|
||||
for (int i = startLine; i < endLine; ++i) {
|
||||
css_node_t* child = node->get_child(node->context, i);
|
||||
if (isFlex(child)) {
|
||||
// At this point we know the final size of the element in the main
|
||||
@@ -568,7 +600,8 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
float crossDim = 0;
|
||||
float mainDim = leadingMainDim +
|
||||
getPaddingAndBorder(node, leading[mainAxis]);
|
||||
for (int i = 0; i < node->children_count; ++i) {
|
||||
|
||||
for (int i = startLine; i < endLine; ++i) {
|
||||
css_node_t* child = node->get_child(node->context, i);
|
||||
|
||||
if (getPositionType(child) == CSS_POSITION_ABSOLUTE &&
|
||||
@@ -598,10 +631,11 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
}
|
||||
}
|
||||
|
||||
float containerMainAxis = node->layout.dimensions[dim[mainAxis]];
|
||||
// If the user didn't specify a width or height, and it has not been set
|
||||
// by the container, then we set it via the children.
|
||||
if (isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
||||
node->layout.dimensions[dim[mainAxis]] = fmaxf(
|
||||
containerMainAxis = fmaxf(
|
||||
// We're missing the last padding at this point to get the final
|
||||
// dimension
|
||||
mainDim + getPaddingAndBorder(node, trailing[mainAxis]),
|
||||
@@ -610,8 +644,9 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
);
|
||||
}
|
||||
|
||||
float containerCrossAxis = node->layout.dimensions[dim[crossAxis]];
|
||||
if (isUndefined(node->layout.dimensions[dim[crossAxis]])) {
|
||||
node->layout.dimensions[dim[crossAxis]] = fmaxf(
|
||||
containerCrossAxis = fmaxf(
|
||||
// For the cross dim, we add both sides at the end because the value
|
||||
// is aggregate via a max function. Intermediate negative values
|
||||
// can mess this computation otherwise
|
||||
@@ -620,10 +655,9 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// <Loop D> Position elements in the cross axis
|
||||
|
||||
for (int i = 0; i < node->children_count; ++i) {
|
||||
for (int i = startLine; i < endLine; ++i) {
|
||||
css_node_t* child = node->get_child(node->context, i);
|
||||
|
||||
if (getPositionType(child) == CSS_POSITION_ABSOLUTE &&
|
||||
@@ -649,7 +683,7 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
// previously.
|
||||
if (!isDimDefined(child, crossAxis)) {
|
||||
child->layout.dimensions[dim[crossAxis]] = fmaxf(
|
||||
node->layout.dimensions[dim[crossAxis]] -
|
||||
containerCrossAxis -
|
||||
getPaddingAndBorderAxis(node, crossAxis) -
|
||||
getMarginAxis(child, crossAxis),
|
||||
// You never want to go smaller than padding
|
||||
@@ -659,7 +693,7 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
} else {
|
||||
// The remaining space between the parent dimensions+padding and child
|
||||
// dimensions+margin.
|
||||
float remainingCrossDim = node->layout.dimensions[dim[crossAxis]] -
|
||||
float remainingCrossDim = containerCrossAxis -
|
||||
getPaddingAndBorderAxis(node, crossAxis) -
|
||||
getDimWithMargin(child, crossAxis);
|
||||
|
||||
@@ -672,10 +706,37 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
}
|
||||
|
||||
// And we apply the position
|
||||
child->layout.position[pos[crossAxis]] += leadingCrossDim;
|
||||
child->layout.position[pos[crossAxis]] += linesCrossDim + leadingCrossDim;
|
||||
}
|
||||
}
|
||||
|
||||
linesCrossDim += crossDim;
|
||||
linesMainDim = fmaxf(linesMainDim, mainDim);
|
||||
startLine = endLine;
|
||||
}
|
||||
|
||||
// If the user didn't specify a width or height, and it has not been set
|
||||
// by the container, then we set it via the children.
|
||||
if (isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
||||
node->layout.dimensions[dim[mainAxis]] = fmaxf(
|
||||
// We're missing the last padding at this point to get the final
|
||||
// dimension
|
||||
linesMainDim + getPaddingAndBorder(node, trailing[mainAxis]),
|
||||
// We can never assign a width smaller than the padding and borders
|
||||
getPaddingAndBorderAxis(node, mainAxis)
|
||||
);
|
||||
}
|
||||
|
||||
if (isUndefined(node->layout.dimensions[dim[crossAxis]])) {
|
||||
node->layout.dimensions[dim[crossAxis]] = fmaxf(
|
||||
// For the cross dim, we add both sides at the end because the value
|
||||
// is aggregate via a max function. Intermediate negative values
|
||||
// can mess this computation otherwise
|
||||
linesCrossDim + getPaddingAndBorderAxis(node, crossAxis),
|
||||
getPaddingAndBorderAxis(node, crossAxis)
|
||||
);
|
||||
}
|
||||
|
||||
// <Loop E> Calculate dimensions for absolutely positioned elements
|
||||
|
||||
for (int i = 0; i < node->children_count; ++i) {
|
||||
|
@@ -42,6 +42,11 @@ typedef enum {
|
||||
CSS_POSITION_ABSOLUTE
|
||||
} css_position_type_t;
|
||||
|
||||
typedef enum {
|
||||
CSS_NOWRAP = 0,
|
||||
CSS_WRAP
|
||||
} css_wrap_type_t;
|
||||
|
||||
// Note: left and top are shared between position[2] and position[4], so
|
||||
// they have to be before right and bottom.
|
||||
typedef enum {
|
||||
@@ -80,6 +85,7 @@ typedef struct {
|
||||
css_align_t align_items;
|
||||
css_align_t align_self;
|
||||
css_position_type_t position_type;
|
||||
css_wrap_type_t flex_wrap;
|
||||
float flex;
|
||||
float margin[4];
|
||||
float position[4];
|
||||
|
101
src/Layout.js
101
src/Layout.js
@@ -111,6 +111,10 @@ var computeLayout = (function() {
|
||||
);
|
||||
}
|
||||
|
||||
function isFlexWrap(node) {
|
||||
return node.style.flexWrap === 'wrap';
|
||||
}
|
||||
|
||||
function getDimWithMargin(node, axis) {
|
||||
return node.layout[dim[axis]] + getMarginAxis(node, axis);
|
||||
}
|
||||
@@ -298,6 +302,20 @@ var computeLayout = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
var/*float*/ definedMainDim = CSS_UNDEFINED;
|
||||
if (!isUndefined(node.layout[dim[mainAxis]])) {
|
||||
definedMainDim = node.layout[dim[mainAxis]] -
|
||||
getPaddingAndBorderAxis(node, mainAxis);
|
||||
}
|
||||
|
||||
// We want to execute the next two loops one per line with flex-wrap
|
||||
var/*int*/ startLine = 0;
|
||||
var/*int*/ endLine = 0;
|
||||
var/*int*/ nextLine = 0;
|
||||
// We aggregate the total dimensions of the container in those two variables
|
||||
var/*float*/ linesCrossDim = 0;
|
||||
var/*float*/ linesMainDim = 0;
|
||||
while (endLine !== node.children.length) {
|
||||
// <Loop A> Layout non flexible children and count children by type
|
||||
|
||||
// mainContentDim is accumulation of the dimensions and margin of all the
|
||||
@@ -311,8 +329,9 @@ var computeLayout = (function() {
|
||||
var/*int*/ flexibleChildrenCount = 0;
|
||||
var/*float*/ totalFlexible = 0;
|
||||
var/*int*/ nonFlexibleChildrenCount = 0;
|
||||
for (var/*int*/ i = 0; i < node.children.length; ++i) {
|
||||
for (var/*int*/ i = startLine; i < node.children.length; ++i) {
|
||||
var/*css_node_t**/ child = node.children[i];
|
||||
var/*float*/ nextContentDim = 0;
|
||||
|
||||
// It only makes sense to consider a child flexible if we have a computed
|
||||
// dimension for the node.
|
||||
@@ -323,7 +342,7 @@ var computeLayout = (function() {
|
||||
// Even if we don't know its exact size yet, we already know the padding,
|
||||
// border and margin. We'll use this partial information to compute the
|
||||
// remaining space.
|
||||
mainContentDim += getPaddingAndBorderAxis(child, mainAxis) +
|
||||
nextContentDim = getPaddingAndBorderAxis(child, mainAxis) +
|
||||
getMarginAxis(child, mainAxis);
|
||||
|
||||
} else {
|
||||
@@ -340,16 +359,29 @@ var computeLayout = (function() {
|
||||
}
|
||||
|
||||
// This is the main recursive call. We layout non flexible children.
|
||||
if (nextLine === 0) {
|
||||
layoutNode(child, maxWidth);
|
||||
}
|
||||
|
||||
// Absolute positioned elements do not take part of the layout, so we
|
||||
// don't use them to compute mainContentDim
|
||||
if (getPositionType(child) === CSS_POSITION_RELATIVE) {
|
||||
nonFlexibleChildrenCount++;
|
||||
// At this point we know the final size and margin of the element.
|
||||
mainContentDim += getDimWithMargin(child, mainAxis);
|
||||
nextContentDim = getDimWithMargin(child, mainAxis);
|
||||
}
|
||||
}
|
||||
|
||||
// The element we are about to add would make us go to the next line
|
||||
if (isFlexWrap(node) &&
|
||||
!isUndefined(node.layout[dim[mainAxis]]) &&
|
||||
mainContentDim + nextContentDim > definedMainDim) {
|
||||
nextLine = i + 1;
|
||||
break;
|
||||
}
|
||||
nextLine = 0;
|
||||
mainContentDim += nextContentDim;
|
||||
endLine = i + 1;
|
||||
}
|
||||
|
||||
// <Loop B> Layout flexible children and allocate empty space
|
||||
@@ -360,13 +392,13 @@ var computeLayout = (function() {
|
||||
var/*float*/ leadingMainDim = 0;
|
||||
var/*float*/ betweenMainDim = 0;
|
||||
|
||||
var/*float*/ definedMainDim = fmaxf(mainContentDim, 0);
|
||||
if (!isUndefined(node.layout[dim[mainAxis]])) {
|
||||
definedMainDim = node.layout[dim[mainAxis]] -
|
||||
getPaddingAndBorderAxis(node, mainAxis);
|
||||
}
|
||||
// The remaining available space that needs to be allocated
|
||||
var/*float*/ remainingMainDim = definedMainDim - mainContentDim;
|
||||
var/*float*/ remainingMainDim = 0;
|
||||
if (!isUndefined(node.layout[dim[mainAxis]])) {
|
||||
remainingMainDim = definedMainDim - mainContentDim;
|
||||
} else {
|
||||
remainingMainDim = fmaxf(mainContentDim, 0) - mainContentDim;
|
||||
}
|
||||
|
||||
// If there are flexible children in the mix, they are going to fill the
|
||||
// remaining space
|
||||
@@ -381,7 +413,7 @@ var computeLayout = (function() {
|
||||
// We iterate over the full array and only apply the action on flexible
|
||||
// children. This is faster than actually allocating a new array that
|
||||
// contains only flexible children.
|
||||
for (var/*int*/ i = 0; i < node.children.length; ++i) {
|
||||
for (var/*int*/ i = startLine; i < endLine; ++i) {
|
||||
var/*css_node_t**/ child = node.children[i];
|
||||
if (isFlex(child)) {
|
||||
// At this point we know the final size of the element in the main
|
||||
@@ -441,7 +473,8 @@ var computeLayout = (function() {
|
||||
var/*float*/ crossDim = 0;
|
||||
var/*float*/ mainDim = leadingMainDim +
|
||||
getPaddingAndBorder(node, leading[mainAxis]);
|
||||
for (var/*int*/ i = 0; i < node.children.length; ++i) {
|
||||
|
||||
for (var/*int*/ i = startLine; i < endLine; ++i) {
|
||||
var/*css_node_t**/ child = node.children[i];
|
||||
|
||||
if (getPositionType(child) === CSS_POSITION_ABSOLUTE &&
|
||||
@@ -471,10 +504,11 @@ var computeLayout = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
var/*float*/ containerMainAxis = node.layout[dim[mainAxis]];
|
||||
// If the user didn't specify a width or height, and it has not been set
|
||||
// by the container, then we set it via the children.
|
||||
if (isUndefined(node.layout[dim[mainAxis]])) {
|
||||
node.layout[dim[mainAxis]] = fmaxf(
|
||||
containerMainAxis = fmaxf(
|
||||
// We're missing the last padding at this point to get the final
|
||||
// dimension
|
||||
mainDim + getPaddingAndBorder(node, trailing[mainAxis]),
|
||||
@@ -483,8 +517,9 @@ var computeLayout = (function() {
|
||||
);
|
||||
}
|
||||
|
||||
var/*float*/ containerCrossAxis = node.layout[dim[crossAxis]];
|
||||
if (isUndefined(node.layout[dim[crossAxis]])) {
|
||||
node.layout[dim[crossAxis]] = fmaxf(
|
||||
containerCrossAxis = fmaxf(
|
||||
// For the cross dim, we add both sides at the end because the value
|
||||
// is aggregate via a max function. Intermediate negative values
|
||||
// can mess this computation otherwise
|
||||
@@ -493,10 +528,9 @@ var computeLayout = (function() {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// <Loop D> Position elements in the cross axis
|
||||
|
||||
for (var/*int*/ i = 0; i < node.children.length; ++i) {
|
||||
for (var/*int*/ i = startLine; i < endLine; ++i) {
|
||||
var/*css_node_t**/ child = node.children[i];
|
||||
|
||||
if (getPositionType(child) === CSS_POSITION_ABSOLUTE &&
|
||||
@@ -522,7 +556,7 @@ var computeLayout = (function() {
|
||||
// previously.
|
||||
if (!isDimDefined(child, crossAxis)) {
|
||||
child.layout[dim[crossAxis]] = fmaxf(
|
||||
node.layout[dim[crossAxis]] -
|
||||
containerCrossAxis -
|
||||
getPaddingAndBorderAxis(node, crossAxis) -
|
||||
getMarginAxis(child, crossAxis),
|
||||
// You never want to go smaller than padding
|
||||
@@ -532,7 +566,7 @@ var computeLayout = (function() {
|
||||
} else {
|
||||
// The remaining space between the parent dimensions+padding and child
|
||||
// dimensions+margin.
|
||||
var/*float*/ remainingCrossDim = node.layout[dim[crossAxis]] -
|
||||
var/*float*/ remainingCrossDim = containerCrossAxis -
|
||||
getPaddingAndBorderAxis(node, crossAxis) -
|
||||
getDimWithMargin(child, crossAxis);
|
||||
|
||||
@@ -545,10 +579,37 @@ var computeLayout = (function() {
|
||||
}
|
||||
|
||||
// And we apply the position
|
||||
child.layout[pos[crossAxis]] += leadingCrossDim;
|
||||
child.layout[pos[crossAxis]] += linesCrossDim + leadingCrossDim;
|
||||
}
|
||||
}
|
||||
|
||||
linesCrossDim += crossDim;
|
||||
linesMainDim = fmaxf(linesMainDim, mainDim);
|
||||
startLine = endLine;
|
||||
}
|
||||
|
||||
// If the user didn't specify a width or height, and it has not been set
|
||||
// by the container, then we set it via the children.
|
||||
if (isUndefined(node.layout[dim[mainAxis]])) {
|
||||
node.layout[dim[mainAxis]] = fmaxf(
|
||||
// We're missing the last padding at this point to get the final
|
||||
// dimension
|
||||
linesMainDim + getPaddingAndBorder(node, trailing[mainAxis]),
|
||||
// We can never assign a width smaller than the padding and borders
|
||||
getPaddingAndBorderAxis(node, mainAxis)
|
||||
);
|
||||
}
|
||||
|
||||
if (isUndefined(node.layout[dim[crossAxis]])) {
|
||||
node.layout[dim[crossAxis]] = fmaxf(
|
||||
// For the cross dim, we add both sides at the end because the value
|
||||
// is aggregate via a max function. Intermediate negative values
|
||||
// can mess this computation otherwise
|
||||
linesCrossDim + getPaddingAndBorderAxis(node, crossAxis),
|
||||
getPaddingAndBorderAxis(node, crossAxis)
|
||||
);
|
||||
}
|
||||
|
||||
// <Loop E> Calculate dimensions for absolutely positioned elements
|
||||
|
||||
for (var/*int*/ i = 0; i < node.children.length; ++i) {
|
||||
@@ -557,7 +618,7 @@ var computeLayout = (function() {
|
||||
// Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both
|
||||
// left and right or top and bottom).
|
||||
for (var/*int*/ ii = 0; ii < 2; ii++) {
|
||||
var/*css_flex_direction_t*/ axis = (ii != 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN;
|
||||
var/*css_flex_direction_t*/ axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN;
|
||||
if (!isUndefined(node.layout[dim[axis]]) &&
|
||||
!isDimDefined(child, axis) &&
|
||||
isPosDefined(child, leading[axis]) &&
|
||||
@@ -574,7 +635,7 @@ var computeLayout = (function() {
|
||||
}
|
||||
}
|
||||
for (var/*int*/ ii = 0; ii < 2; ii++) {
|
||||
var/*css_flex_direction_t*/ axis = (ii != 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN;
|
||||
var/*css_flex_direction_t*/ axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN;
|
||||
if (isPosDefined(child, trailing[axis]) &&
|
||||
!isPosDefined(child, leading[axis])) {
|
||||
child.layout[leading[axis]] =
|
||||
|
@@ -3671,6 +3671,59 @@ int main()
|
||||
|
||||
test("should layout with children of a contain with left", root_node, root_layout);
|
||||
}
|
||||
|
||||
{
|
||||
css_node_t *root_node = new_test_css_node();
|
||||
{
|
||||
css_node_t *node_0 = root_node;
|
||||
node_0->style.flex_direction = CSS_FLEX_DIRECTION_ROW;
|
||||
node_0->style.flex_wrap = CSS_WRAP;
|
||||
node_0->style.dimensions[CSS_WIDTH] = 100;
|
||||
init_css_node_children(node_0, 3);
|
||||
{
|
||||
css_node_t *node_1;
|
||||
node_1 = node_0->get_child(node_0->context, 0);
|
||||
node_1->style.dimensions[CSS_WIDTH] = 40;
|
||||
node_1->style.dimensions[CSS_HEIGHT] = 10;
|
||||
node_1 = node_0->get_child(node_0->context, 1);
|
||||
node_1->style.dimensions[CSS_WIDTH] = 40;
|
||||
node_1->style.dimensions[CSS_HEIGHT] = 10;
|
||||
node_1 = node_0->get_child(node_0->context, 2);
|
||||
node_1->style.dimensions[CSS_WIDTH] = 40;
|
||||
node_1->style.dimensions[CSS_HEIGHT] = 10;
|
||||
}
|
||||
}
|
||||
|
||||
css_node_t *root_layout = new_test_css_node();
|
||||
{
|
||||
css_node_t *node_0 = root_layout;
|
||||
node_0->layout.position[CSS_TOP] = 0;
|
||||
node_0->layout.position[CSS_LEFT] = 0;
|
||||
node_0->layout.dimensions[CSS_WIDTH] = 100;
|
||||
node_0->layout.dimensions[CSS_HEIGHT] = 20;
|
||||
init_css_node_children(node_0, 3);
|
||||
{
|
||||
css_node_t *node_1;
|
||||
node_1 = node_0->get_child(node_0->context, 0);
|
||||
node_1->layout.position[CSS_TOP] = 0;
|
||||
node_1->layout.position[CSS_LEFT] = 0;
|
||||
node_1->layout.dimensions[CSS_WIDTH] = 40;
|
||||
node_1->layout.dimensions[CSS_HEIGHT] = 10;
|
||||
node_1 = node_0->get_child(node_0->context, 1);
|
||||
node_1->layout.position[CSS_TOP] = 0;
|
||||
node_1->layout.position[CSS_LEFT] = 40;
|
||||
node_1->layout.dimensions[CSS_WIDTH] = 40;
|
||||
node_1->layout.dimensions[CSS_HEIGHT] = 10;
|
||||
node_1 = node_0->get_child(node_0->context, 2);
|
||||
node_1->layout.position[CSS_TOP] = 10;
|
||||
node_1->layout.position[CSS_LEFT] = 0;
|
||||
node_1->layout.dimensions[CSS_WIDTH] = 40;
|
||||
node_1->layout.dimensions[CSS_HEIGHT] = 10;
|
||||
}
|
||||
}
|
||||
|
||||
test("should layout flex-wrap", root_node, root_layout);
|
||||
}
|
||||
/** END_GENERATED **/
|
||||
return tests_finished();
|
||||
}
|
||||
|
@@ -1144,12 +1144,11 @@ describe('Layout', function() {
|
||||
);
|
||||
});
|
||||
|
||||
xit('should layout flex-wrap', function() {
|
||||
it('should layout flex-wrap', function() {
|
||||
testLayout(
|
||||
{style: {flexWrap: 'wrap', flexDirection: 'row', width: 100}, children: [
|
||||
{style: {width: 40, height: 10}},
|
||||
{style: {width: 40, height: 10}},
|
||||
{style: {flex: 1}},
|
||||
{style: {width: 40, height: 10}},
|
||||
]},
|
||||
{width: 100, height: 20, top: 0, left: 0, children: [
|
||||
|
@@ -287,6 +287,13 @@ public class CSSNode {
|
||||
}
|
||||
}
|
||||
|
||||
public void setWrap(CSSWrap flexWrap) {
|
||||
if (!valuesEqual(style.flexWrap, flexWrap)) {
|
||||
style.flexWrap = flexWrap;
|
||||
dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public void setFlex(float flex) {
|
||||
if (!valuesEqual(style.flex, flex)) {
|
||||
style.flex = flex;
|
||||
|
@@ -23,6 +23,7 @@ public class CSSStyle {
|
||||
public CSSAlign alignItems = CSSAlign.STRETCH;
|
||||
public CSSAlign alignSelf = CSSAlign.AUTO;
|
||||
public CSSPositionType positionType = CSSPositionType.RELATIVE;
|
||||
public CSSWrap flexWrap = CSSWrap.NOWRAP;
|
||||
public float flex;
|
||||
|
||||
public float[] margin = new float[4];
|
||||
|
14
src/java/src/com/facebook/csslayout/CSSWrap.java
Normal file
14
src/java/src/com/facebook/csslayout/CSSWrap.java
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Copyright (c) 2014, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
package com.facebook.csslayout;
|
||||
|
||||
public enum CSSWrap {
|
||||
NOWRAP,
|
||||
WRAP,
|
||||
}
|
@@ -235,6 +235,10 @@ public class LayoutEngine {
|
||||
return node.style.justifyContent;
|
||||
}
|
||||
|
||||
private static boolean isFlexWrap(CSSNode node) {
|
||||
return node.style.flexWrap == CSSWrap.WRAP;
|
||||
}
|
||||
|
||||
private static boolean isFlex(CSSNode node) {
|
||||
return getPositionType(node) == CSSPositionType.RELATIVE && getFlex(node) > 0;
|
||||
}
|
||||
@@ -371,6 +375,20 @@ public class LayoutEngine {
|
||||
}
|
||||
}
|
||||
|
||||
float definedMainDim = CSSConstants.UNDEFINED;
|
||||
if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) {
|
||||
definedMainDim = getLayoutDimension(node, getDim(mainAxis)) -
|
||||
getPaddingAndBorderAxis(node, mainAxis);
|
||||
}
|
||||
|
||||
// We want to execute the next two loops one per line with flex-wrap
|
||||
int startLine = 0;
|
||||
int endLine = 0;
|
||||
int nextLine = 0;
|
||||
// We aggregate the total dimensions of the container in those two variables
|
||||
float linesCrossDim = 0;
|
||||
float linesMainDim = 0;
|
||||
while (endLine != node.getChildCount()) {
|
||||
// <Loop A> Layout non flexible children and count children by type
|
||||
|
||||
// mainContentDim is accumulation of the dimensions and margin of all the
|
||||
@@ -384,8 +402,9 @@ public class LayoutEngine {
|
||||
int flexibleChildrenCount = 0;
|
||||
float totalFlexible = 0;
|
||||
int nonFlexibleChildrenCount = 0;
|
||||
for (int i = 0; i < node.getChildCount(); ++i) {
|
||||
for (int i = startLine; i < node.getChildCount(); ++i) {
|
||||
CSSNode child = node.getChildAt(i);
|
||||
float nextContentDim = 0;
|
||||
|
||||
// It only makes sense to consider a child flexible if we have a computed
|
||||
// dimension for the node.
|
||||
@@ -396,7 +415,7 @@ public class LayoutEngine {
|
||||
// Even if we don't know its exact size yet, we already know the padding,
|
||||
// border and margin. We'll use this partial information to compute the
|
||||
// remaining space.
|
||||
mainContentDim = mainContentDim + getPaddingAndBorderAxis(child, mainAxis) +
|
||||
nextContentDim = getPaddingAndBorderAxis(child, mainAxis) +
|
||||
getMarginAxis(child, mainAxis);
|
||||
|
||||
} else {
|
||||
@@ -413,16 +432,29 @@ public class LayoutEngine {
|
||||
}
|
||||
|
||||
// This is the main recursive call. We layout non flexible children.
|
||||
if (nextLine == 0) {
|
||||
layoutNode(child, maxWidth);
|
||||
}
|
||||
|
||||
// Absolute positioned elements do not take part of the layout, so we
|
||||
// don't use them to compute mainContentDim
|
||||
if (getPositionType(child) == CSSPositionType.RELATIVE) {
|
||||
nonFlexibleChildrenCount++;
|
||||
// At this point we know the final size and margin of the element.
|
||||
mainContentDim = mainContentDim + getDimWithMargin(child, mainAxis);
|
||||
nextContentDim = getDimWithMargin(child, mainAxis);
|
||||
}
|
||||
}
|
||||
|
||||
// The element we are about to add would make us go to the next line
|
||||
if (isFlexWrap(node) &&
|
||||
!CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis))) &&
|
||||
mainContentDim + nextContentDim > definedMainDim) {
|
||||
nextLine = i + 1;
|
||||
break;
|
||||
}
|
||||
nextLine = 0;
|
||||
mainContentDim = mainContentDim + nextContentDim;
|
||||
endLine = i + 1;
|
||||
}
|
||||
|
||||
// <Loop B> Layout flexible children and allocate empty space
|
||||
@@ -433,13 +465,13 @@ public class LayoutEngine {
|
||||
float leadingMainDim = 0;
|
||||
float betweenMainDim = 0;
|
||||
|
||||
float definedMainDim = Math.max(mainContentDim, 0);
|
||||
if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) {
|
||||
definedMainDim = getLayoutDimension(node, getDim(mainAxis)) -
|
||||
getPaddingAndBorderAxis(node, mainAxis);
|
||||
}
|
||||
// The remaining available space that needs to be allocated
|
||||
float remainingMainDim = definedMainDim - mainContentDim;
|
||||
float remainingMainDim = 0;
|
||||
if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) {
|
||||
remainingMainDim = definedMainDim - mainContentDim;
|
||||
} else {
|
||||
remainingMainDim = Math.max(mainContentDim, 0) - mainContentDim;
|
||||
}
|
||||
|
||||
// If there are flexible children in the mix, they are going to fill the
|
||||
// remaining space
|
||||
@@ -454,7 +486,7 @@ public class LayoutEngine {
|
||||
// We iterate over the full array and only apply the action on flexible
|
||||
// children. This is faster than actually allocating a new array that
|
||||
// contains only flexible children.
|
||||
for (int i = 0; i < node.getChildCount(); ++i) {
|
||||
for (int i = startLine; i < endLine; ++i) {
|
||||
CSSNode child = node.getChildAt(i);
|
||||
if (isFlex(child)) {
|
||||
// At this point we know the final size of the element in the main
|
||||
@@ -514,7 +546,8 @@ public class LayoutEngine {
|
||||
float crossDim = 0;
|
||||
float mainDim = leadingMainDim +
|
||||
getPaddingAndBorder(node, getLeading(mainAxis));
|
||||
for (int i = 0; i < node.getChildCount(); ++i) {
|
||||
|
||||
for (int i = startLine; i < endLine; ++i) {
|
||||
CSSNode child = node.getChildAt(i);
|
||||
|
||||
if (getPositionType(child) == CSSPositionType.ABSOLUTE &&
|
||||
@@ -544,32 +577,33 @@ public class LayoutEngine {
|
||||
}
|
||||
}
|
||||
|
||||
float containerMainAxis = getLayoutDimension(node, getDim(mainAxis));
|
||||
// If the user didn't specify a width or height, and it has not been set
|
||||
// by the container, then we set it via the children.
|
||||
if (CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) {
|
||||
setLayoutDimension(node, getDim(mainAxis), Math.max(
|
||||
containerMainAxis = Math.max(
|
||||
// We're missing the last padding at this point to get the final
|
||||
// dimension
|
||||
mainDim + getPaddingAndBorder(node, getTrailing(mainAxis)),
|
||||
// We can never assign a width smaller than the padding and borders
|
||||
getPaddingAndBorderAxis(node, mainAxis)
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
float containerCrossAxis = getLayoutDimension(node, getDim(crossAxis));
|
||||
if (CSSConstants.isUndefined(getLayoutDimension(node, getDim(crossAxis)))) {
|
||||
setLayoutDimension(node, getDim(crossAxis), Math.max(
|
||||
containerCrossAxis = Math.max(
|
||||
// For the cross dim, we add both sides at the end because the value
|
||||
// is aggregate via a max function. Intermediate negative values
|
||||
// can mess this computation otherwise
|
||||
crossDim + getPaddingAndBorderAxis(node, crossAxis),
|
||||
getPaddingAndBorderAxis(node, crossAxis)
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// <Loop D> Position elements in the cross axis
|
||||
|
||||
for (int i = 0; i < node.getChildCount(); ++i) {
|
||||
for (int i = startLine; i < endLine; ++i) {
|
||||
CSSNode child = node.getChildAt(i);
|
||||
|
||||
if (getPositionType(child) == CSSPositionType.ABSOLUTE &&
|
||||
@@ -595,7 +629,7 @@ public class LayoutEngine {
|
||||
// previously.
|
||||
if (!isDimDefined(child, crossAxis)) {
|
||||
setLayoutDimension(child, getDim(crossAxis), Math.max(
|
||||
getLayoutDimension(node, getDim(crossAxis)) -
|
||||
containerCrossAxis -
|
||||
getPaddingAndBorderAxis(node, crossAxis) -
|
||||
getMarginAxis(child, crossAxis),
|
||||
// You never want to go smaller than padding
|
||||
@@ -605,7 +639,7 @@ public class LayoutEngine {
|
||||
} else {
|
||||
// The remaining space between the parent dimensions+padding and child
|
||||
// dimensions+margin.
|
||||
float remainingCrossDim = getLayoutDimension(node, getDim(crossAxis)) -
|
||||
float remainingCrossDim = containerCrossAxis -
|
||||
getPaddingAndBorderAxis(node, crossAxis) -
|
||||
getDimWithMargin(child, crossAxis);
|
||||
|
||||
@@ -618,10 +652,37 @@ public class LayoutEngine {
|
||||
}
|
||||
|
||||
// And we apply the position
|
||||
setLayoutPosition(child, getPos(crossAxis), getLayoutPosition(child, getPos(crossAxis)) + leadingCrossDim);
|
||||
setLayoutPosition(child, getPos(crossAxis), getLayoutPosition(child, getPos(crossAxis)) + linesCrossDim + leadingCrossDim);
|
||||
}
|
||||
}
|
||||
|
||||
linesCrossDim = linesCrossDim + crossDim;
|
||||
linesMainDim = Math.max(linesMainDim, mainDim);
|
||||
startLine = endLine;
|
||||
}
|
||||
|
||||
// If the user didn't specify a width or height, and it has not been set
|
||||
// by the container, then we set it via the children.
|
||||
if (CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) {
|
||||
setLayoutDimension(node, getDim(mainAxis), Math.max(
|
||||
// We're missing the last padding at this point to get the final
|
||||
// dimension
|
||||
linesMainDim + getPaddingAndBorder(node, getTrailing(mainAxis)),
|
||||
// We can never assign a width smaller than the padding and borders
|
||||
getPaddingAndBorderAxis(node, mainAxis)
|
||||
));
|
||||
}
|
||||
|
||||
if (CSSConstants.isUndefined(getLayoutDimension(node, getDim(crossAxis)))) {
|
||||
setLayoutDimension(node, getDim(crossAxis), Math.max(
|
||||
// For the cross dim, we add both sides at the end because the value
|
||||
// is aggregate via a max function. Intermediate negative values
|
||||
// can mess this computation otherwise
|
||||
linesCrossDim + getPaddingAndBorderAxis(node, crossAxis),
|
||||
getPaddingAndBorderAxis(node, crossAxis)
|
||||
));
|
||||
}
|
||||
|
||||
// <Loop E> Calculate dimensions for absolutely positioned elements
|
||||
|
||||
for (int i = 0; i < node.getChildCount(); ++i) {
|
||||
|
@@ -3931,5 +3931,60 @@ public class LayoutEngineTest {
|
||||
|
||||
test("should layout with children of a contain with left", root_node, root_layout);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase93()
|
||||
{
|
||||
TestCSSNode root_node = new TestCSSNode();
|
||||
{
|
||||
TestCSSNode node_0 = root_node;
|
||||
node_0.style.flexDirection = CSSFlexDirection.ROW;
|
||||
node_0.style.flexWrap = CSSWrap.WRAP;
|
||||
node_0.style.width = 100;
|
||||
addChildren(node_0, 3);
|
||||
{
|
||||
TestCSSNode node_1;
|
||||
node_1 = node_0.getChildAt(0);
|
||||
node_1.style.width = 40;
|
||||
node_1.style.height = 10;
|
||||
node_1 = node_0.getChildAt(1);
|
||||
node_1.style.width = 40;
|
||||
node_1.style.height = 10;
|
||||
node_1 = node_0.getChildAt(2);
|
||||
node_1.style.width = 40;
|
||||
node_1.style.height = 10;
|
||||
}
|
||||
}
|
||||
|
||||
TestCSSNode root_layout = new TestCSSNode();
|
||||
{
|
||||
TestCSSNode node_0 = root_layout;
|
||||
node_0.layout.y = 0;
|
||||
node_0.layout.x = 0;
|
||||
node_0.layout.width = 100;
|
||||
node_0.layout.height = 20;
|
||||
addChildren(node_0, 3);
|
||||
{
|
||||
TestCSSNode node_1;
|
||||
node_1 = node_0.getChildAt(0);
|
||||
node_1.layout.y = 0;
|
||||
node_1.layout.x = 0;
|
||||
node_1.layout.width = 40;
|
||||
node_1.layout.height = 10;
|
||||
node_1 = node_0.getChildAt(1);
|
||||
node_1.layout.y = 0;
|
||||
node_1.layout.x = 40;
|
||||
node_1.layout.width = 40;
|
||||
node_1.layout.height = 10;
|
||||
node_1 = node_0.getChildAt(2);
|
||||
node_1.layout.y = 10;
|
||||
node_1.layout.x = 0;
|
||||
node_1.layout.width = 40;
|
||||
node_1.layout.height = 10;
|
||||
}
|
||||
}
|
||||
|
||||
test("should layout flex-wrap", root_node, root_layout);
|
||||
}
|
||||
/** END_GENERATED **/
|
||||
}
|
||||
|
@@ -141,6 +141,10 @@ function printLayout(test) {
|
||||
'relative': 'CSS_POSITION_RELATIVE',
|
||||
'absolute': 'CSS_POSITION_ABSOLUTE'
|
||||
});
|
||||
addEnum(node, 'flexWrap', 'flex_wrap', {
|
||||
'nowrap': 'CSS_NOWRAP',
|
||||
'wrap': 'CSS_WRAP'
|
||||
});
|
||||
addFloat('positive', node, 'flex', 'flex');
|
||||
addFloat('positive', node, 'width', 'dimensions[CSS_WIDTH]');
|
||||
addFloat('positive', node, 'height', 'dimensions[CSS_HEIGHT]');
|
||||
|
Reference in New Issue
Block a user