mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
feat(css): add gap, row-gap and column-gap support
Add the CSS gap shorthand and its row-gap/column-gap longhands for flex
containers.
- Register row-gap/column-gap as longhand properties and gap as a
shorthand accepting [ normal | <length-percentage> ]{1,2}.
- Add css_gap_t (CSS_GAP_INHERIT/SET/NORMAL) and corresponding
type/unit/value fields on css_computed_style_t.
- Implement cascade logic in lib/css/src/properties/gap.c, resolving
normal to zero and rejecting auto.
- Apply gaps in ui_flexbox_layout: main-axis gap participates in line
size, wrap detection, justify-content space and per-item positioning;
cross-axis gap participates in cross-size accumulation and line
positioning. The last item/line never gets a trailing gap. Percentage
gaps resolve against the container content-box on the corresponding
axis.
- Cover gap parsing/cascading in lib/css/tests (single/two values,
axis-only, normal, percentage) and add a flex layout integration test
exercising column-gap, row-gap, wrapped two-value gap and normal.
This commit is contained in:
+596
-586
File diff suppressed because it is too large
Load Diff
@@ -21,9 +21,9 @@
|
||||
css_register_property_with_key(css_prop_##PROP_KEY, PROP_NAME, VALDEF, \
|
||||
INIT, css_cascade_##PROP_KEY)
|
||||
|
||||
#define DEFINE_SHORTHAND_PROP(PROP_KEY, NAME, VALDEF) \
|
||||
#define DEFINE_SHORTHAND_PROP(PROP_KEY, NAME, VALDEF) \
|
||||
extern int css_parse_##PROP_KEY(const css_propdef_t *, const char *, \
|
||||
css_style_decl_t *); \
|
||||
css_style_decl_t *); \
|
||||
css_register_shorthand_property(NAME, VALDEF, css_parse_##PROP_KEY)
|
||||
|
||||
static struct css_properties_module {
|
||||
@@ -61,7 +61,8 @@ static void css_propdef_destroy(css_propdef_t *prop)
|
||||
}
|
||||
|
||||
int css_register_shorthand_property(const char *name, const char *syntax,
|
||||
int (*parse)(const css_propdef_t *, const char *,
|
||||
int (*parse)(const css_propdef_t *,
|
||||
const char *,
|
||||
css_style_decl_t *))
|
||||
{
|
||||
css_propdef_t *prop;
|
||||
@@ -233,8 +234,8 @@ void css_init_properties(void)
|
||||
"auto | min-content | <length> | <percentage>", "auto");
|
||||
|
||||
/** @see https://developer.mozilla.org/en-US/docs/Web/CSS/min-height */
|
||||
DEFINE_PROP(min_height, "min-height", "auto | min-content | <length> | <percentage>",
|
||||
"auto");
|
||||
DEFINE_PROP(min_height, "min-height",
|
||||
"auto | min-content | <length> | <percentage>", "auto");
|
||||
|
||||
/** @see https://developer.mozilla.org/en-US/docs/Web/CSS/max-width */
|
||||
DEFINE_PROP(max_width, "max-width", "auto | <length> | <percentage>",
|
||||
@@ -547,6 +548,18 @@ void css_init_properties(void)
|
||||
"normal | stretch | flex-start | flex-end | center",
|
||||
"normal");
|
||||
|
||||
/** @see https://developer.mozilla.org/en-US/docs/Web/CSS/row-gap */
|
||||
DEFINE_PROP(row_gap, "row-gap", "normal | <length-percentage>",
|
||||
"normal");
|
||||
|
||||
/** @see https://developer.mozilla.org/en-US/docs/Web/CSS/column-gap */
|
||||
DEFINE_PROP(column_gap, "column-gap", "normal | <length-percentage>",
|
||||
"normal");
|
||||
|
||||
/** @see https://developer.mozilla.org/en-US/docs/Web/CSS/gap */
|
||||
DEFINE_SHORTHAND_PROP(gap, "gap",
|
||||
"[ normal | <length-percentage> ]{1,2}");
|
||||
|
||||
/** @see https://developer.mozilla.org/en-US/docs/Web/CSS/color */
|
||||
DEFINE_PROP(color, "color", "<color>", "#000");
|
||||
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* lib/css/src/properties/gap.c
|
||||
*
|
||||
* Copyright (c) 2023-2025, Liu Chao <i@lc-soft.io> All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* This file is part of LCUI, distributed under the MIT License found in the
|
||||
* LICENSE.TXT file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "../properties.h"
|
||||
#include "./helpers.h"
|
||||
|
||||
static void set_row_gap(css_computed_style_t *computed, uint8_t keyword,
|
||||
css_numeric_value_t value, css_unit_t unit)
|
||||
{
|
||||
computed->row_gap = value;
|
||||
computed->unit_bits.row_gap = unit;
|
||||
computed->type_bits.row_gap = keyword;
|
||||
}
|
||||
|
||||
static void set_column_gap(css_computed_style_t *computed, uint8_t keyword,
|
||||
css_numeric_value_t value, css_unit_t unit)
|
||||
{
|
||||
computed->column_gap = value;
|
||||
computed->unit_bits.column_gap = unit;
|
||||
computed->type_bits.column_gap = keyword;
|
||||
}
|
||||
|
||||
/* 仅接受 <length-percentage> 与 normal 关键字;不接受 auto */
|
||||
static int cascade_gap_length(const css_style_array_value_t input,
|
||||
css_computed_style_t *computed,
|
||||
void (*setter)(css_computed_style_t *, uint8_t,
|
||||
css_numeric_value_t, css_unit_t))
|
||||
{
|
||||
switch (input[0].type) {
|
||||
case CSS_UNIT_VALUE:
|
||||
setter(computed, CSS_GAP_SET, input[0].unit_value.value,
|
||||
input[0].unit_value.unit);
|
||||
break;
|
||||
case CSS_KEYWORD_VALUE:
|
||||
if (input[0].keyword_value == CSS_KEYWORD_NORMAL) {
|
||||
setter(computed, CSS_GAP_NORMAL, 0, CSS_UNIT_PX);
|
||||
break;
|
||||
}
|
||||
return -1;
|
||||
case CSS_NUMERIC_VALUE:
|
||||
if (input[0].numeric_value == 0) {
|
||||
setter(computed, CSS_GAP_SET, 0, CSS_UNIT_PX);
|
||||
break;
|
||||
}
|
||||
return -1;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int css_cascade_row_gap(const css_style_array_value_t input,
|
||||
css_computed_style_t *computed)
|
||||
{
|
||||
return cascade_gap_length(input, computed, set_row_gap);
|
||||
}
|
||||
|
||||
int css_cascade_column_gap(const css_style_array_value_t input,
|
||||
css_computed_style_t *computed)
|
||||
{
|
||||
return cascade_gap_length(input, computed, set_column_gap);
|
||||
}
|
||||
|
||||
int css_parse_gap(const css_propdef_t *propdef, const char *input,
|
||||
css_style_decl_t *s)
|
||||
{
|
||||
css_style_value_t value;
|
||||
int len = css_parse_value(propdef->valdef, input, &value);
|
||||
|
||||
if (len <= 0) {
|
||||
return len;
|
||||
}
|
||||
switch (css_style_value_get_array_length(&value)) {
|
||||
case 1:
|
||||
css_style_decl_add(s, css_prop_row_gap, value.array_value);
|
||||
css_style_decl_add(s, css_prop_column_gap, value.array_value);
|
||||
break;
|
||||
case 2:
|
||||
css_style_decl_add(s, css_prop_row_gap, value.array_value);
|
||||
css_style_decl_add(s, css_prop_column_gap,
|
||||
value.array_value + 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
css_style_value_destroy(&value);
|
||||
return len;
|
||||
}
|
||||
@@ -150,6 +150,122 @@ void test_button_borderless_style(void)
|
||||
css_computed_style_destroy(&computed);
|
||||
}
|
||||
|
||||
void test_flex_gap_shorthand_one_value(void)
|
||||
{
|
||||
css_selector_t *selector;
|
||||
css_style_decl_t *result;
|
||||
css_computed_style_t computed;
|
||||
|
||||
selector = css_selector_create(".flex-gap");
|
||||
result = css_select_style(selector);
|
||||
css_cascade_style(result, &computed);
|
||||
|
||||
ctest_equal_int("row-gap type", computed.type_bits.row_gap,
|
||||
CSS_GAP_SET);
|
||||
ctest_equal_int("column-gap type", computed.type_bits.column_gap,
|
||||
CSS_GAP_SET);
|
||||
ctest_equal_int("row-gap unit", computed.unit_bits.row_gap,
|
||||
CSS_UNIT_PX);
|
||||
ctest_equal_int("column-gap unit", computed.unit_bits.column_gap,
|
||||
CSS_UNIT_PX);
|
||||
check_length("row-gap", computed.row_gap, 12);
|
||||
check_length("column-gap", computed.column_gap, 12);
|
||||
|
||||
css_style_decl_destroy(result);
|
||||
css_selector_destroy(selector);
|
||||
css_computed_style_destroy(&computed);
|
||||
}
|
||||
|
||||
void test_flex_gap_shorthand_two_values(void)
|
||||
{
|
||||
css_selector_t *selector;
|
||||
css_style_decl_t *result;
|
||||
css_computed_style_t computed;
|
||||
|
||||
selector = css_selector_create(".flex-gap-two-values");
|
||||
result = css_select_style(selector);
|
||||
css_cascade_style(result, &computed);
|
||||
|
||||
ctest_equal_int("row-gap type", computed.type_bits.row_gap,
|
||||
CSS_GAP_SET);
|
||||
ctest_equal_int("column-gap type", computed.type_bits.column_gap,
|
||||
CSS_GAP_SET);
|
||||
check_length("row-gap", computed.row_gap, 8);
|
||||
check_length("column-gap", computed.column_gap, 16);
|
||||
|
||||
css_style_decl_destroy(result);
|
||||
css_selector_destroy(selector);
|
||||
css_computed_style_destroy(&computed);
|
||||
}
|
||||
|
||||
void test_flex_gap_axis(void)
|
||||
{
|
||||
css_selector_t *selector;
|
||||
css_style_decl_t *result;
|
||||
css_computed_style_t computed;
|
||||
|
||||
selector = css_selector_create(".flex-gap-axis");
|
||||
result = css_select_style(selector);
|
||||
css_cascade_style(result, &computed);
|
||||
|
||||
ctest_equal_int("row-gap type", computed.type_bits.row_gap,
|
||||
CSS_GAP_SET);
|
||||
ctest_equal_int("column-gap type", computed.type_bits.column_gap,
|
||||
CSS_GAP_SET);
|
||||
check_length("row-gap", computed.row_gap, 4);
|
||||
check_length("column-gap", computed.column_gap, 20);
|
||||
|
||||
css_style_decl_destroy(result);
|
||||
css_selector_destroy(selector);
|
||||
css_computed_style_destroy(&computed);
|
||||
}
|
||||
|
||||
void test_flex_gap_normal(void)
|
||||
{
|
||||
css_selector_t *selector;
|
||||
css_style_decl_t *result;
|
||||
css_computed_style_t computed;
|
||||
|
||||
selector = css_selector_create(".flex-gap-normal");
|
||||
result = css_select_style(selector);
|
||||
css_cascade_style(result, &computed);
|
||||
|
||||
ctest_equal_int("row-gap type", computed.type_bits.row_gap,
|
||||
CSS_GAP_NORMAL);
|
||||
ctest_equal_int("column-gap type", computed.type_bits.column_gap,
|
||||
CSS_GAP_NORMAL);
|
||||
|
||||
css_style_decl_destroy(result);
|
||||
css_selector_destroy(selector);
|
||||
css_computed_style_destroy(&computed);
|
||||
}
|
||||
|
||||
void test_flex_gap_percentage(void)
|
||||
{
|
||||
css_selector_t *selector;
|
||||
css_style_decl_t *result;
|
||||
css_computed_style_t computed;
|
||||
|
||||
selector = css_selector_create(".flex-gap-percentage");
|
||||
result = css_select_style(selector);
|
||||
css_cascade_style(result, &computed);
|
||||
|
||||
ctest_equal_int("row-gap type", computed.type_bits.row_gap,
|
||||
CSS_GAP_SET);
|
||||
ctest_equal_int("column-gap type", computed.type_bits.column_gap,
|
||||
CSS_GAP_SET);
|
||||
ctest_equal_int("row-gap unit", computed.unit_bits.row_gap,
|
||||
CSS_UNIT_PERCENT);
|
||||
ctest_equal_int("column-gap unit", computed.unit_bits.column_gap,
|
||||
CSS_UNIT_PERCENT);
|
||||
check_length("row-gap", computed.row_gap, 25);
|
||||
check_length("column-gap", computed.column_gap, 50);
|
||||
|
||||
css_style_decl_destroy(result);
|
||||
css_selector_destroy(selector);
|
||||
css_computed_style_destroy(&computed);
|
||||
}
|
||||
|
||||
void test_css_computed(void)
|
||||
{
|
||||
size_t n;
|
||||
@@ -176,6 +292,13 @@ void test_css_computed(void)
|
||||
ctest_describe(".container", test_container_style);
|
||||
ctest_describe(".button.borderless", test_button_borderless_style);
|
||||
ctest_describe(".w-[360px]", test_w360px_style);
|
||||
ctest_describe(".flex-gap (one value)",
|
||||
test_flex_gap_shorthand_one_value);
|
||||
ctest_describe(".flex-gap-two-values",
|
||||
test_flex_gap_shorthand_two_values);
|
||||
ctest_describe(".flex-gap-axis", test_flex_gap_axis);
|
||||
ctest_describe(".flex-gap-normal", test_flex_gap_normal);
|
||||
ctest_describe(".flex-gap-percentage", test_flex_gap_percentage);
|
||||
|
||||
css_destroy();
|
||||
}
|
||||
|
||||
@@ -34,3 +34,29 @@
|
||||
.w-\[360px\] {
|
||||
width: 360px;
|
||||
}
|
||||
|
||||
.flex-gap {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.flex-gap-two-values {
|
||||
display: flex;
|
||||
gap: 8px 16px;
|
||||
}
|
||||
|
||||
.flex-gap-axis {
|
||||
display: flex;
|
||||
row-gap: 4px;
|
||||
column-gap: 20px;
|
||||
}
|
||||
|
||||
.flex-gap-normal {
|
||||
display: flex;
|
||||
gap: normal;
|
||||
}
|
||||
|
||||
.flex-gap-percentage {
|
||||
display: flex;
|
||||
gap: 25% 50%;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ typedef struct ui_flexbox_layout_context {
|
||||
bool column_direction;
|
||||
ui_resizer_t *resizer;
|
||||
float cross_size;
|
||||
float main_gap;
|
||||
float cross_gap;
|
||||
|
||||
/** list_t<ui_flexbox_line_t*> lines */
|
||||
list_t lines;
|
||||
@@ -91,6 +93,19 @@ static ui_flexbox_line_t *ui_flexbox_layout_next_line(
|
||||
return next_line;
|
||||
}
|
||||
|
||||
/* 将 gap 值解析为像素长度,percentage 引用容器对应轴上的内容尺寸 */
|
||||
static float ui_flexbox_resolve_gap(uint8_t type, css_numeric_value_t value,
|
||||
css_unit_t unit, float reference)
|
||||
{
|
||||
if (type != CSS_GAP_SET) {
|
||||
return 0;
|
||||
}
|
||||
if (unit == CSS_UNIT_PERCENT) {
|
||||
return value * reference / 100.f;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static void ui_reset_row_item_flex_basis(ui_widget_t *item)
|
||||
{
|
||||
css_computed_style_t *s = &item->computed_style;
|
||||
@@ -289,6 +304,7 @@ static void ui_flexbox_layout_load_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
css_computed_style_t *cs;
|
||||
float main_size, min_main_size;
|
||||
unsigned child_index = 0;
|
||||
unsigned in_flow_count = 0;
|
||||
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_DEBUG_MSG(
|
||||
@@ -309,8 +325,13 @@ static void ui_flexbox_layout_load_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
min_main_size = cs->flex_shrink > 0
|
||||
? ui_flexbox_item_min_main_size(ctx, child)
|
||||
: main_size;
|
||||
if (in_flow_count > 0 && ctx->main_gap > 0) {
|
||||
ui_resizer_load_item_main_size(
|
||||
ctx->resizer, ctx->main_gap, ctx->main_gap);
|
||||
}
|
||||
ui_resizer_load_item_main_size(ctx->resizer, main_size,
|
||||
min_main_size);
|
||||
in_flow_count++;
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_WIDGET_STR(child, str);
|
||||
UI_WIDGET_SIZE_STR(child, size_str);
|
||||
@@ -340,6 +361,7 @@ static void ui_flexbox_layout_apply_line(ui_flexbox_layout_context_t *ctx,
|
||||
ui_widget_t *child;
|
||||
float item_main_size, item_cross_size;
|
||||
unsigned child_index = 0;
|
||||
unsigned in_flow_count = 0;
|
||||
|
||||
line->main_size = 0;
|
||||
line->cross_size = 0;
|
||||
@@ -382,7 +404,11 @@ static void ui_flexbox_layout_apply_line(ui_flexbox_layout_context_t *ctx,
|
||||
if (line->cross_size < item_cross_size) {
|
||||
line->cross_size = item_cross_size;
|
||||
}
|
||||
if (in_flow_count > 0) {
|
||||
line->main_size += ctx->main_gap;
|
||||
}
|
||||
line->main_size += item_main_size;
|
||||
in_flow_count++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,9 +461,13 @@ static void ui_flexbox_layout_apply_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
UI_DEBUG_END;
|
||||
if (s->type_bits.flex_wrap == CSS_FLEX_WRAP_WRAP &&
|
||||
line->main_size > 0 &&
|
||||
line->main_size + main_size > max_main_size) {
|
||||
line->main_size + ctx->main_gap + main_size >
|
||||
max_main_size) {
|
||||
line = ui_flexbox_layout_next_line(ctx);
|
||||
}
|
||||
if (line->items.length > 0) {
|
||||
line->main_size += ctx->main_gap;
|
||||
}
|
||||
line->main_size += main_size;
|
||||
if (column) {
|
||||
if (cs->type_bits.margin_top == CSS_MARGIN_AUTO) {
|
||||
@@ -489,6 +519,9 @@ static void ui_flexbox_layout_apply_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
UI_DEBUG_MSG("line[%zu]: cross_size=%g", line->index,
|
||||
line->cross_size);
|
||||
UI_DEBUG_END;
|
||||
if (line->index > 0) {
|
||||
ctx->cross_size += ctx->cross_gap;
|
||||
}
|
||||
ctx->cross_size += line->cross_size;
|
||||
}
|
||||
if (ctx->cross_size < hint_cross_size) {
|
||||
@@ -585,14 +618,16 @@ static void ui_flexbox_layout_reflow_lines(ui_flexbox_layout_context_t *ctx)
|
||||
ui_flexbox_line_t *line;
|
||||
css_computed_style_t *s = &ctx->widget->computed_style;
|
||||
bool column = ctx->column_direction;
|
||||
unsigned item_index;
|
||||
|
||||
float space;
|
||||
float main_axis;
|
||||
float cross_axis = column ? s->padding_left : s->padding_top;
|
||||
float cross_space = ((column ? ctx->widget->content_box.width
|
||||
: ctx->widget->content_box.height) -
|
||||
ctx->cross_size) /
|
||||
ctx->lines.length;
|
||||
float cross_free_space = (column ? ctx->widget->content_box.width
|
||||
: ctx->widget->content_box.height) -
|
||||
ctx->cross_size;
|
||||
float cross_space =
|
||||
ctx->lines.length > 0 ? cross_free_space / ctx->lines.length : 0;
|
||||
float line_max_cross_size;
|
||||
|
||||
UI_DEBUG_BEGIN;
|
||||
@@ -602,6 +637,9 @@ static void ui_flexbox_layout_reflow_lines(ui_flexbox_layout_context_t *ctx)
|
||||
UI_DEBUG_END;
|
||||
for (list_each(line_node, &ctx->lines)) {
|
||||
line = line_node->data;
|
||||
if (line->index > 0) {
|
||||
cross_axis += ctx->cross_gap;
|
||||
}
|
||||
line_max_cross_size = line->cross_size + cross_space;
|
||||
ui_flexbox_layout_compute_justify_content(ctx, line, &main_axis,
|
||||
&space);
|
||||
@@ -612,12 +650,19 @@ static void ui_flexbox_layout_reflow_lines(ui_flexbox_layout_context_t *ctx)
|
||||
line_max_cross_size);
|
||||
UI_DEBUG_INDENT_INC;
|
||||
UI_DEBUG_END;
|
||||
item_index = 0;
|
||||
for (list_each(node, &line->items)) {
|
||||
child = node->data;
|
||||
main_axis += space;
|
||||
if (item_index > 0 && ui_widget_in_layout_flow(child)) {
|
||||
main_axis += ctx->main_gap;
|
||||
}
|
||||
main_axis += ui_flexbox_compute_item_layout(
|
||||
ctx, child, main_axis, cross_axis,
|
||||
s->type_bits.align_items, line_max_cross_size);
|
||||
if (ui_widget_in_layout_flow(child)) {
|
||||
item_index++;
|
||||
}
|
||||
}
|
||||
cross_axis += line_max_cross_size;
|
||||
UI_DEBUG_BEGIN;
|
||||
@@ -639,6 +684,22 @@ void ui_flexbox_layout_reflow(ui_widget_t *w, ui_resizer_t *resizer)
|
||||
ctx.column_direction = ui_widget_has_flex_column_direction(w);
|
||||
list_create(&ctx.lines);
|
||||
ui_resizer_init(resizer, w);
|
||||
{
|
||||
css_computed_style_t *s = &w->computed_style;
|
||||
float row_gap = ui_flexbox_resolve_gap(
|
||||
s->type_bits.row_gap, s->row_gap, s->unit_bits.row_gap,
|
||||
w->content_box.height);
|
||||
float column_gap = ui_flexbox_resolve_gap(
|
||||
s->type_bits.column_gap, s->column_gap,
|
||||
s->unit_bits.column_gap, w->content_box.width);
|
||||
if (ctx.column_direction) {
|
||||
ctx.main_gap = row_gap;
|
||||
ctx.cross_gap = column_gap;
|
||||
} else {
|
||||
ctx.main_gap = column_gap;
|
||||
ctx.cross_gap = row_gap;
|
||||
}
|
||||
}
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_WIDGET_STR(w, str);
|
||||
UI_WIDGET_SIZE_STR(w, size_str);
|
||||
|
||||
@@ -774,6 +774,209 @@ static void test_flex_layout_320(void)
|
||||
&w->border_box, &rect);
|
||||
}
|
||||
|
||||
static void test_flex_layout_gap(void)
|
||||
{
|
||||
ui_widget_t *root;
|
||||
ui_widget_t *container;
|
||||
ui_widget_t *child;
|
||||
ui_rect_t rect;
|
||||
|
||||
root = ui_root();
|
||||
ui_widget_resize(root, 800, 600);
|
||||
|
||||
/* (1) row direction with column-gap shorthand */
|
||||
|
||||
container = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(container, "display", "flex");
|
||||
ui_widget_set_style_string(container, "width", "300px");
|
||||
ui_widget_set_style_string(container, "height", "60px");
|
||||
ui_widget_set_style_string(container, "gap", "10px");
|
||||
ui_widget_append(root, container);
|
||||
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
ui_update();
|
||||
|
||||
rect.y = 0;
|
||||
rect.width = 50;
|
||||
rect.height = 50;
|
||||
|
||||
child = ui_widget_get_child(container, 0);
|
||||
rect.x = 0;
|
||||
ctest_equal_ui_rect("row gap: child[0] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
child = ui_widget_get_child(container, 1);
|
||||
rect.x = 60;
|
||||
ctest_equal_ui_rect("row gap: child[1] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
child = ui_widget_get_child(container, 2);
|
||||
rect.x = 120;
|
||||
ctest_equal_ui_rect("row gap: child[2] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
child = ui_widget_get_child(container, 3);
|
||||
rect.x = 180;
|
||||
ctest_equal_ui_rect("row gap: child[3] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
ui_widget_remove(container);
|
||||
|
||||
/* (2) column direction with row-gap */
|
||||
|
||||
container = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(container, "display", "flex");
|
||||
ui_widget_set_style_string(container, "flex-direction", "column");
|
||||
ui_widget_set_style_string(container, "width", "100px");
|
||||
ui_widget_set_style_string(container, "height", "300px");
|
||||
ui_widget_set_style_string(container, "row-gap", "20px");
|
||||
ui_widget_append(root, container);
|
||||
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
ui_update();
|
||||
|
||||
rect.x = 0;
|
||||
rect.width = 50;
|
||||
rect.height = 50;
|
||||
|
||||
child = ui_widget_get_child(container, 0);
|
||||
rect.y = 0;
|
||||
ctest_equal_ui_rect("column gap: child[0] border_box",
|
||||
&child->border_box, &rect);
|
||||
|
||||
child = ui_widget_get_child(container, 1);
|
||||
rect.y = 70;
|
||||
ctest_equal_ui_rect("column gap: child[1] border_box",
|
||||
&child->border_box, &rect);
|
||||
|
||||
child = ui_widget_get_child(container, 2);
|
||||
rect.y = 140;
|
||||
ctest_equal_ui_rect("column gap: child[2] border_box",
|
||||
&child->border_box, &rect);
|
||||
|
||||
ui_widget_remove(container);
|
||||
|
||||
/* (3) row + wrap with two-value gap */
|
||||
|
||||
container = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(container, "display", "flex");
|
||||
ui_widget_set_style_string(container, "flex-wrap", "wrap");
|
||||
ui_widget_set_style_string(container, "width", "200px");
|
||||
ui_widget_set_style_string(container, "height", "85px");
|
||||
ui_widget_set_style_string(container, "gap", "5px 10px");
|
||||
ui_widget_append(root, container);
|
||||
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "80px");
|
||||
ui_widget_set_style_string(child, "height", "40px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "80px");
|
||||
ui_widget_set_style_string(child, "height", "40px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "80px");
|
||||
ui_widget_set_style_string(child, "height", "40px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "80px");
|
||||
ui_widget_set_style_string(child, "height", "40px");
|
||||
ui_widget_append(container, child);
|
||||
ui_update();
|
||||
|
||||
rect.width = 80;
|
||||
rect.height = 40;
|
||||
|
||||
child = ui_widget_get_child(container, 0);
|
||||
rect.x = 0;
|
||||
rect.y = 0;
|
||||
ctest_equal_ui_rect("wrap gap: child[0] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
child = ui_widget_get_child(container, 1);
|
||||
rect.x = 90;
|
||||
rect.y = 0;
|
||||
ctest_equal_ui_rect("wrap gap: child[1] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
child = ui_widget_get_child(container, 2);
|
||||
rect.x = 0;
|
||||
rect.y = 45;
|
||||
ctest_equal_ui_rect("wrap gap: child[2] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
child = ui_widget_get_child(container, 3);
|
||||
rect.x = 90;
|
||||
rect.y = 45;
|
||||
ctest_equal_ui_rect("wrap gap: child[3] border_box", &child->border_box,
|
||||
&rect);
|
||||
|
||||
ui_widget_remove(container);
|
||||
|
||||
/* (4) gap: normal should fall back to 0 */
|
||||
|
||||
container = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(container, "display", "flex");
|
||||
ui_widget_set_style_string(container, "width", "300px");
|
||||
ui_widget_set_style_string(container, "height", "60px");
|
||||
ui_widget_set_style_string(container, "gap", "normal");
|
||||
ui_widget_append(root, container);
|
||||
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
child = ui_create_widget(NULL);
|
||||
ui_widget_set_style_string(child, "width", "50px");
|
||||
ui_widget_set_style_string(child, "height", "50px");
|
||||
ui_widget_append(container, child);
|
||||
ui_update();
|
||||
|
||||
rect.width = 50;
|
||||
rect.height = 50;
|
||||
|
||||
child = ui_widget_get_child(container, 0);
|
||||
rect.x = 0;
|
||||
rect.y = 0;
|
||||
ctest_equal_ui_rect("normal gap: child[0] border_box",
|
||||
&child->border_box, &rect);
|
||||
|
||||
child = ui_widget_get_child(container, 1);
|
||||
rect.x = 50;
|
||||
rect.y = 0;
|
||||
ctest_equal_ui_rect("normal gap: child[1] border_box",
|
||||
&child->border_box, &rect);
|
||||
|
||||
ui_widget_remove(container);
|
||||
ui_update();
|
||||
}
|
||||
|
||||
void test_flex_layout(void)
|
||||
{
|
||||
lcui_init();
|
||||
@@ -784,6 +987,7 @@ void test_flex_layout(void)
|
||||
ctest_describe("root width 1280px", test_flex_layout_1280);
|
||||
ctest_describe("root width 600px", test_flex_layout_600);
|
||||
ctest_describe("root width 320px", test_flex_layout_320);
|
||||
ctest_describe("flex gap", test_flex_layout_gap);
|
||||
|
||||
lcui_destroy();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user