Support ROW_REVERSE, COLUMN_REVERSE and RTL direction
This commit is contained in:
163
src/Layout.c
163
src/Layout.c
@@ -36,6 +36,9 @@ static bool eq(float a, float b) {
|
||||
void init_css_node(css_node_t *node) {
|
||||
node->style.align_items = CSS_ALIGN_STRETCH;
|
||||
|
||||
node->style.direction = CSS_DIRECTION_INHERIT;
|
||||
node->style.flex_direction = CSS_FLEX_DIRECTION_COLUMN;
|
||||
|
||||
// Some of the fields default to undefined and not 0
|
||||
node->style.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
|
||||
node->style.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
|
||||
@@ -58,6 +61,7 @@ void init_css_node(css_node_t *node) {
|
||||
node->layout.last_requested_dimensions[CSS_WIDTH] = -1;
|
||||
node->layout.last_requested_dimensions[CSS_HEIGHT] = -1;
|
||||
node->layout.last_parent_max_width = -1;
|
||||
node->layout.last_direction = -1;
|
||||
node->layout.should_update = true;
|
||||
}
|
||||
|
||||
@@ -119,8 +123,14 @@ static void print_css_node_rec(
|
||||
}
|
||||
|
||||
if (options & CSS_PRINT_STYLE) {
|
||||
if (node->style.flex_direction == CSS_FLEX_DIRECTION_ROW) {
|
||||
if (node->style.flex_direction == CSS_FLEX_DIRECTION_COLUMN) {
|
||||
printf("flexDirection: 'column', ");
|
||||
} else if (node->style.flex_direction == CSS_FLEX_DIRECTION_COLUMN_REVERSE) {
|
||||
printf("flexDirection: 'columnReverse', ");
|
||||
} else if (node->style.flex_direction == CSS_FLEX_DIRECTION_ROW) {
|
||||
printf("flexDirection: 'row', ");
|
||||
} else if (node->style.flex_direction == CSS_FLEX_DIRECTION_ROW_REVERSE) {
|
||||
printf("flexDirection: 'rowReverse', ");
|
||||
}
|
||||
|
||||
if (node->style.justify_content == CSS_JUSTIFY_CENTER) {
|
||||
@@ -210,25 +220,32 @@ void print_css_node(css_node_t *node, css_print_options_t options) {
|
||||
}
|
||||
|
||||
|
||||
static css_position_t leading[2] = {
|
||||
static css_position_t leading[4] = {
|
||||
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_TOP,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_LEFT
|
||||
/* CSS_FLEX_DIRECTION_COLUMN_REVERSE = */ CSS_BOTTOM,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_LEFT,
|
||||
/* CSS_FLEX_DIRECTION_ROW_REVERSE = */ CSS_RIGHT
|
||||
};
|
||||
static css_position_t trailing[2] = {
|
||||
static css_position_t trailing[4] = {
|
||||
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_BOTTOM,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_RIGHT
|
||||
/* CSS_FLEX_DIRECTION_COLUMN_REVERSE = */ CSS_TOP,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_RIGHT,
|
||||
/* CSS_FLEX_DIRECTION_ROW_REVERSE = */ CSS_LEFT
|
||||
};
|
||||
static css_position_t pos[2] = {
|
||||
static css_position_t pos[4] = {
|
||||
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_TOP,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_LEFT
|
||||
/* CSS_FLEX_DIRECTION_COLUMN_REVERSE = */ CSS_BOTTOM,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_LEFT,
|
||||
/* CSS_FLEX_DIRECTION_ROW_REVERSE = */ CSS_RIGHT
|
||||
};
|
||||
static css_dimension_t dim[2] = {
|
||||
static css_dimension_t dim[4] = {
|
||||
/* CSS_FLEX_DIRECTION_COLUMN = */ CSS_HEIGHT,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_WIDTH
|
||||
/* CSS_FLEX_DIRECTION_COLUMN_REVERSE = */ CSS_HEIGHT,
|
||||
/* CSS_FLEX_DIRECTION_ROW = */ CSS_WIDTH,
|
||||
/* CSS_FLEX_DIRECTION_ROW_REVERSE = */ CSS_WIDTH
|
||||
};
|
||||
|
||||
|
||||
|
||||
static float getMargin(css_node_t *node, int location) {
|
||||
return node->style.margin[location];
|
||||
}
|
||||
@@ -278,10 +295,50 @@ static css_align_t getAlignItem(css_node_t *node, css_node_t *child) {
|
||||
return node->style.align_items;
|
||||
}
|
||||
|
||||
static bool isRowDirection(css_flex_direction_t flex_direction) {
|
||||
return flex_direction == CSS_FLEX_DIRECTION_ROW ||
|
||||
flex_direction == CSS_FLEX_DIRECTION_ROW_REVERSE;
|
||||
}
|
||||
|
||||
static bool isColumnDirection(css_flex_direction_t flex_direction) {
|
||||
return flex_direction == CSS_FLEX_DIRECTION_COLUMN ||
|
||||
flex_direction == CSS_FLEX_DIRECTION_COLUMN_REVERSE;
|
||||
}
|
||||
|
||||
static css_direction_t resolveDirection(css_node_t *node, css_direction_t parentDirection) {
|
||||
css_direction_t direction = node->style.direction;
|
||||
|
||||
if (direction == CSS_DIRECTION_INHERIT) {
|
||||
direction = parentDirection > CSS_DIRECTION_INHERIT ? parentDirection : CSS_DIRECTION_LTR;
|
||||
}
|
||||
|
||||
return direction;
|
||||
}
|
||||
|
||||
static css_flex_direction_t getFlexDirection(css_node_t *node) {
|
||||
return node->style.flex_direction;
|
||||
}
|
||||
|
||||
static css_flex_direction_t resolveAxis(css_flex_direction_t flex_direction, css_direction_t direction) {
|
||||
if (direction == CSS_DIRECTION_RTL) {
|
||||
if (flex_direction == CSS_FLEX_DIRECTION_ROW) {
|
||||
return CSS_FLEX_DIRECTION_ROW_REVERSE;
|
||||
} else if (flex_direction == CSS_FLEX_DIRECTION_ROW_REVERSE) {
|
||||
return CSS_FLEX_DIRECTION_ROW;
|
||||
}
|
||||
}
|
||||
|
||||
return flex_direction;
|
||||
}
|
||||
|
||||
static css_flex_direction_t getCrossFlexDirection(css_flex_direction_t flex_direction, css_direction_t direction) {
|
||||
if (isColumnDirection(flex_direction)) {
|
||||
return resolveAxis(CSS_FLEX_DIRECTION_ROW, direction);
|
||||
} else {
|
||||
return CSS_FLEX_DIRECTION_COLUMN;
|
||||
}
|
||||
}
|
||||
|
||||
static float getFlex(css_node_t *node) {
|
||||
return node->style.flex;
|
||||
}
|
||||
@@ -328,10 +385,10 @@ static float boundAxis(css_node_t *node, css_flex_direction_t axis, float value)
|
||||
float min = CSS_UNDEFINED;
|
||||
float max = CSS_UNDEFINED;
|
||||
|
||||
if (axis == CSS_FLEX_DIRECTION_COLUMN) {
|
||||
if (isColumnDirection(axis)) {
|
||||
min = node->style.minDimensions[CSS_HEIGHT];
|
||||
max = node->style.maxDimensions[CSS_HEIGHT];
|
||||
} else if (axis == CSS_FLEX_DIRECTION_ROW) {
|
||||
} else if (isRowDirection(axis)) {
|
||||
min = node->style.minDimensions[CSS_WIDTH];
|
||||
max = node->style.maxDimensions[CSS_WIDTH];
|
||||
}
|
||||
@@ -366,6 +423,11 @@ static void setDimensionFromStyle(css_node_t *node, css_flex_direction_t axis) {
|
||||
);
|
||||
}
|
||||
|
||||
static void setTrailingPosition(css_node_t *node, css_node_t *child, css_flex_direction_t axis) {
|
||||
child->layout.position[trailing[axis]] = node->layout.dimensions[dim[axis]] -
|
||||
child->layout.dimensions[dim[axis]] - child->layout.position[pos[axis]];
|
||||
}
|
||||
|
||||
// If both left and right are defined, then use left. Otherwise return
|
||||
// +left or -right depending on which is defined.
|
||||
static float getRelativePosition(css_node_t *node, css_flex_direction_t axis) {
|
||||
@@ -376,13 +438,12 @@ static float getRelativePosition(css_node_t *node, css_flex_direction_t axis) {
|
||||
return -getPosition(node, trailing[axis]);
|
||||
}
|
||||
|
||||
static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
static void layoutNodeImpl(css_node_t *node, float parentMaxWidth, css_direction_t parentDirection) {
|
||||
/** START_GENERATED **/
|
||||
|
||||
css_flex_direction_t mainAxis = getFlexDirection(node);
|
||||
css_flex_direction_t crossAxis = mainAxis == CSS_FLEX_DIRECTION_ROW ?
|
||||
CSS_FLEX_DIRECTION_COLUMN :
|
||||
CSS_FLEX_DIRECTION_ROW;
|
||||
css_direction_t direction = resolveDirection(node, parentDirection);
|
||||
css_flex_direction_t mainAxis = resolveAxis(getFlexDirection(node), direction);
|
||||
css_flex_direction_t crossAxis = getCrossFlexDirection(mainAxis, direction);
|
||||
css_flex_direction_t resolvedRowAxis = resolveAxis(CSS_FLEX_DIRECTION_ROW, direction);
|
||||
|
||||
// Handle width and height style attributes
|
||||
setDimensionFromStyle(node, mainAxis);
|
||||
@@ -392,26 +453,30 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
// delta composed of the margin and left/top/right/bottom
|
||||
node->layout.position[leading[mainAxis]] += getMargin(node, leading[mainAxis]) +
|
||||
getRelativePosition(node, mainAxis);
|
||||
node->layout.position[trailing[mainAxis]] += getMargin(node, trailing[mainAxis]) +
|
||||
getRelativePosition(node, mainAxis);
|
||||
node->layout.position[leading[crossAxis]] += getMargin(node, leading[crossAxis]) +
|
||||
getRelativePosition(node, crossAxis);
|
||||
node->layout.position[trailing[crossAxis]] += getMargin(node, trailing[crossAxis]) +
|
||||
getRelativePosition(node, crossAxis);
|
||||
|
||||
if (isMeasureDefined(node)) {
|
||||
float width = CSS_UNDEFINED;
|
||||
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
||||
if (isDimDefined(node, resolvedRowAxis)) {
|
||||
width = node->style.dimensions[CSS_WIDTH];
|
||||
} else if (!isUndefined(node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]])) {
|
||||
width = node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]];
|
||||
} else if (!isUndefined(node->layout.dimensions[dim[resolvedRowAxis]])) {
|
||||
width = node->layout.dimensions[dim[resolvedRowAxis]];
|
||||
} else {
|
||||
width = parentMaxWidth -
|
||||
getMarginAxis(node, CSS_FLEX_DIRECTION_ROW);
|
||||
getMarginAxis(node, resolvedRowAxis);
|
||||
}
|
||||
width -= getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
||||
width -= getPaddingAndBorderAxis(node, resolvedRowAxis);
|
||||
|
||||
// We only need to give a dimension for the text if we haven't got any
|
||||
// for it computed yet. It can either be from the style attribute or because
|
||||
// the element is flexible.
|
||||
bool isRowUndefined = !isDimDefined(node, CSS_FLEX_DIRECTION_ROW) &&
|
||||
isUndefined(node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]]);
|
||||
bool isRowUndefined = !isDimDefined(node, resolvedRowAxis) &&
|
||||
isUndefined(node->layout.dimensions[dim[resolvedRowAxis]]);
|
||||
bool isColumnUndefined = !isDimDefined(node, CSS_FLEX_DIRECTION_COLUMN) &&
|
||||
isUndefined(node->layout.dimensions[dim[CSS_FLEX_DIRECTION_COLUMN]]);
|
||||
|
||||
@@ -424,7 +489,7 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
);
|
||||
if (isRowUndefined) {
|
||||
node->layout.dimensions[CSS_WIDTH] = measureDim.dimensions[CSS_WIDTH] +
|
||||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
||||
getPaddingAndBorderAxis(node, resolvedRowAxis);
|
||||
}
|
||||
if (isColumnUndefined) {
|
||||
node->layout.dimensions[CSS_HEIGHT] = measureDim.dimensions[CSS_HEIGHT] +
|
||||
@@ -527,20 +592,20 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
|
||||
} else {
|
||||
maxWidth = CSS_UNDEFINED;
|
||||
if (mainAxis != CSS_FLEX_DIRECTION_ROW) {
|
||||
if (!isRowDirection(mainAxis)) {
|
||||
maxWidth = parentMaxWidth -
|
||||
getMarginAxis(node, CSS_FLEX_DIRECTION_ROW) -
|
||||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
||||
getMarginAxis(node, resolvedRowAxis) -
|
||||
getPaddingAndBorderAxis(node, resolvedRowAxis);
|
||||
|
||||
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
||||
maxWidth = node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]] -
|
||||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
||||
if (isDimDefined(node, resolvedRowAxis)) {
|
||||
maxWidth = node->layout.dimensions[dim[resolvedRowAxis]] -
|
||||
getPaddingAndBorderAxis(node, resolvedRowAxis);
|
||||
}
|
||||
}
|
||||
|
||||
// This is the main recursive call. We layout non flexible children.
|
||||
if (alreadyComputedNextLayout == 0) {
|
||||
layoutNode(child, maxWidth);
|
||||
layoutNode(child, maxWidth, direction);
|
||||
}
|
||||
|
||||
// Absolute positioned elements do not take part of the layout, so we
|
||||
@@ -626,17 +691,17 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
);
|
||||
|
||||
maxWidth = CSS_UNDEFINED;
|
||||
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) {
|
||||
maxWidth = node->layout.dimensions[dim[CSS_FLEX_DIRECTION_ROW]] -
|
||||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
||||
} else if (mainAxis != CSS_FLEX_DIRECTION_ROW) {
|
||||
if (isDimDefined(node, resolvedRowAxis)) {
|
||||
maxWidth = node->layout.dimensions[dim[resolvedRowAxis]] -
|
||||
getPaddingAndBorderAxis(node, resolvedRowAxis);
|
||||
} else if (!isRowDirection(mainAxis)) {
|
||||
maxWidth = parentMaxWidth -
|
||||
getMarginAxis(node, CSS_FLEX_DIRECTION_ROW) -
|
||||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW);
|
||||
getMarginAxis(node, resolvedRowAxis) -
|
||||
getPaddingAndBorderAxis(node, resolvedRowAxis);
|
||||
}
|
||||
|
||||
// And we recursively call the layout algorithm for this child
|
||||
layoutNode(child, maxWidth);
|
||||
layoutNode(child, maxWidth, direction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -689,6 +754,11 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
// If the child is position absolute (without top/left) or relative,
|
||||
// we put it at the current accumulated offset.
|
||||
child->layout.position[pos[mainAxis]] += mainDim;
|
||||
|
||||
// Define the trailing position accordingly.
|
||||
if (!isUndefined(node->layout.dimensions[dim[mainAxis]])) {
|
||||
setTrailingPosition(node, child, mainAxis);
|
||||
}
|
||||
}
|
||||
|
||||
// Now that we placed the element, we need to update the variables
|
||||
@@ -783,6 +853,12 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
// We can never assign a width smaller than the padding and borders
|
||||
getPaddingAndBorderAxis(node, mainAxis)
|
||||
);
|
||||
|
||||
// Now that the width is defined, we should update the trailing
|
||||
// positions for the children.
|
||||
for (i = 0; i < node->children_count; ++i) {
|
||||
setTrailingPosition(node, node->get_child(node->context, i), mainAxis);
|
||||
}
|
||||
}
|
||||
|
||||
if (isUndefined(node->layout.dimensions[dim[crossAxis]])) {
|
||||
@@ -835,8 +911,9 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
|
||||
/** END_GENERATED **/
|
||||
}
|
||||
|
||||
void layoutNode(css_node_t *node, float parentMaxWidth) {
|
||||
void layoutNode(css_node_t *node, float parentMaxWidth, css_direction_t parentDirection) {
|
||||
css_layout_t *layout = &node->layout;
|
||||
css_direction_t direction = node->style.direction;
|
||||
layout->should_update = true;
|
||||
|
||||
bool skipLayout =
|
||||
@@ -844,6 +921,7 @@ void layoutNode(css_node_t *node, float parentMaxWidth) {
|
||||
eq(layout->last_requested_dimensions[CSS_WIDTH], layout->dimensions[CSS_WIDTH]) &&
|
||||
eq(layout->last_requested_dimensions[CSS_HEIGHT], layout->dimensions[CSS_HEIGHT]) &&
|
||||
eq(layout->last_parent_max_width, parentMaxWidth);
|
||||
eq(layout->last_direction, direction);
|
||||
|
||||
if (skipLayout) {
|
||||
layout->dimensions[CSS_WIDTH] = layout->last_dimensions[CSS_WIDTH];
|
||||
@@ -854,8 +932,9 @@ void layoutNode(css_node_t *node, float parentMaxWidth) {
|
||||
layout->last_requested_dimensions[CSS_WIDTH] = layout->dimensions[CSS_WIDTH];
|
||||
layout->last_requested_dimensions[CSS_HEIGHT] = layout->dimensions[CSS_HEIGHT];
|
||||
layout->last_parent_max_width = parentMaxWidth;
|
||||
layout->last_direction = direction;
|
||||
|
||||
layoutNodeImpl(node, parentMaxWidth);
|
||||
layoutNodeImpl(node, parentMaxWidth, parentDirection);
|
||||
|
||||
layout->last_dimensions[CSS_WIDTH] = layout->dimensions[CSS_WIDTH];
|
||||
layout->last_dimensions[CSS_HEIGHT] = layout->dimensions[CSS_HEIGHT];
|
||||
|
Reference in New Issue
Block a user