mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
fix(ui): percentage flex items should wrap within intrinsic size
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
# Used size 污染 intrinsic contribution
|
||||
|
||||
- 状态:已知问题 / 暂缓
|
||||
- 严重程度:Medium(中等)。该问题会造成布局无法随内容收缩和出现多余留白,但不会导致崩溃或内存安全问题,且以固定父尺寸为主的普通空间分配通常不受影响。
|
||||
- 发现日期:2026-07-27
|
||||
|
||||
## 现象
|
||||
|
||||
在 auto 或 fit-content 父级内放置百分比 flex item,并由固定宽度子元素提供 intrinsic contribution。初始子元素宽度为 `100px` 时,布局结果为:
|
||||
|
||||
- group content width:`333.344px`
|
||||
- item border width:`100.003px`
|
||||
- child border width:`100px`
|
||||
|
||||
将 child 宽度从 `100px` 改为 `50px` 并执行更新后,child 会变为 `50px`,但 group 仍为 `333.344px`,item 仍为 `100.003px`,二者均不收缩。
|
||||
|
||||
## 根因
|
||||
|
||||
`ui_widget_update_size()` 将 `content_box` 的 used size 写入 `max_content_width` 和 `max_content_height`。后续 fit-content 和 percentage intrinsic 计算会读取这些 `max_content_*` 值,因此上一轮布局得到的 used size 被误当成新的 intrinsic contribution。内容缩小后,历史尺寸继续参与计算,阻止父级和百分比 item 收缩。
|
||||
|
||||
## 影响范围
|
||||
|
||||
已确认或直接相关的场景包括:
|
||||
|
||||
- auto 或 fit-content 父级;
|
||||
- 百分比 flex item;
|
||||
- 子内容缩小或移除;
|
||||
- 局部 style refresh 后重新布局。
|
||||
|
||||
以固定父尺寸为主的普通空间分配通常不受影响。Block、flex 以及其他读取 `max_content_*` 的路径也可能受影响,仍需进一步调查。
|
||||
|
||||
## 风险
|
||||
|
||||
- 容器和 item 无法随内容收缩;
|
||||
- 页面产生多余留白;
|
||||
- 最终布局依赖历史 used size,可能因更新顺序不同而不同;
|
||||
- 该问题不属于崩溃或内存安全风险。
|
||||
|
||||
## 当前修复为何不包含此问题
|
||||
|
||||
当前 percentage wrap 修复处理百分比 flex item 的 intrinsic 宽度求解、换行和 gap 分配。used size 污染 intrinsic contribution 属于尺寸状态建模和更新生命周期问题,修复需要重新划分 `ui_widget_update_size()` 及其调用方维护的尺寸语义,并审计 block、flex 等共享读取路径。将其并入当前修复会显著扩大范围和回归风险,因此暂缓处理。当前修复不应被视为已经解决此问题。
|
||||
|
||||
## 未来修复方向
|
||||
|
||||
1. 区分 pure intrinsic contribution 与布局产生的 used size,避免共用 `max_content_width` / `max_content_height` 状态。
|
||||
2. 审计所有 `max_content_*` 的写入点和读取点,明确每条路径需要 intrinsic size 还是 used size。
|
||||
3. 保持 load 阶段为纯读取,禁止在 load 阶段触发 child reflow。
|
||||
4. 使用 TDD 重新添加 child 从 `100px` 缩小到 `50px` 后 group 和 item 同步收缩的回归测试。
|
||||
|
||||
## 复现步骤
|
||||
|
||||
伪代码:
|
||||
|
||||
```c
|
||||
group = create_fit_content_flex_group();
|
||||
item = append_percentage_flex_item(group, 30_percent);
|
||||
child = append_fixed_width_child(item, 100_px);
|
||||
ui_update();
|
||||
|
||||
assert(group->content_box.width == 333.344_px);
|
||||
assert(item->border_box.width == 100.003_px);
|
||||
|
||||
set_width(child, 50_px);
|
||||
ui_update();
|
||||
|
||||
// 当前已知问题:child 已缩至 50px,但以下两个尺寸保持不变。
|
||||
assert(group->content_box.width == 333.344_px);
|
||||
assert(item->border_box.width == 100.003_px);
|
||||
```
|
||||
|
||||
可使用 `tests/integration/test_flex_layout_percentage_wrap.c` 中的 `intrinsic percentage flex item` 场景作为初始边界覆盖;重新加入动态收缩断言前,应先完成尺寸语义修复。
|
||||
|
||||
## 验收标准
|
||||
|
||||
- child 从 `100px` 缩小到 `50px` 后,group 和百分比 item 在同一次正常更新流程中收缩到新的 intrinsic 解;
|
||||
- item 的 content box 仍能容纳 child;
|
||||
- 结果不依赖此前布局得到的 used size,也不依赖额外的全树 style refresh;
|
||||
- 修复不在 load 阶段触发 child reflow;
|
||||
- percentage wrap、固定父尺寸分配以及 block/flex 相关布局测试全部通过;
|
||||
- 回归测试采用 TDD 恢复并覆盖内容缩小和移除场景。
|
||||
|
||||
## 相关文件
|
||||
|
||||
- `tests/integration/test_flex_layout_percentage_wrap.c`
|
||||
- `tests/fixtures/test_flex_layout_percentage_wrap.xml`
|
||||
- `tests/fixtures/test_flex_layout_percentage_wrap.css`
|
||||
- `lib/ui/src/ui_updater.c`
|
||||
- `lib/ui/src/ui_widget_layout.c`
|
||||
- `lib/ui/src/ui_flexbox_layout.c`
|
||||
- `lib/ui/src/ui_block_layout.c`
|
||||
- `lib/ui/src/ui_resizer.c`
|
||||
+405
-47
@@ -19,6 +19,8 @@
|
||||
#include <ui.h>
|
||||
#include <ui/style.h>
|
||||
#include <css/computed.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include "ui_diff.h"
|
||||
#include "ui_debug.h"
|
||||
#include "ui_widget_style.h"
|
||||
@@ -28,10 +30,17 @@
|
||||
typedef struct ui_flexbox_line {
|
||||
size_t index;
|
||||
float main_size;
|
||||
float intrinsic_non_percentage_size;
|
||||
float percentage_non_percentage_size;
|
||||
float intrinsic_percentage;
|
||||
float intrinsic_gap_percentage;
|
||||
float intrinsic_min_size;
|
||||
float intrinsic_container_min_size;
|
||||
float cross_axis;
|
||||
float cross_size;
|
||||
float sum_of_grow_value;
|
||||
float sum_of_shrink_value;
|
||||
size_t count_of_in_flow_items;
|
||||
size_t count_of_auto_margin_items;
|
||||
|
||||
/** list_t<ui_widget_t*> items */
|
||||
@@ -45,20 +54,47 @@ typedef struct ui_flexbox_layout_context {
|
||||
float cross_size;
|
||||
float main_gap;
|
||||
float cross_gap;
|
||||
uint8_t main_gap_type;
|
||||
css_numeric_value_t main_gap_value;
|
||||
css_unit_t main_gap_unit;
|
||||
bool has_intrinsic_lines;
|
||||
|
||||
/** list_t<ui_flexbox_line_t*> lines */
|
||||
list_t lines;
|
||||
} ui_flexbox_layout_context_t;
|
||||
|
||||
typedef struct ui_flexbox_main_contribution {
|
||||
float non_percentage_size;
|
||||
float percentage;
|
||||
float min_size;
|
||||
float container_min_size;
|
||||
bool has_percentage_size;
|
||||
} ui_flexbox_main_contribution_t;
|
||||
|
||||
static float ui_ceil_layout_unit(float value)
|
||||
{
|
||||
if (value > FLT_MAX / 64.f || value < -FLT_MAX / 64.f) {
|
||||
return value;
|
||||
}
|
||||
return ceilf(value * 64.f) / 64.f;
|
||||
}
|
||||
|
||||
static ui_flexbox_line_t *ui_flexbox_line_create(void)
|
||||
{
|
||||
ui_flexbox_line_t *line;
|
||||
|
||||
line = malloc(sizeof(ui_flexbox_line_t));
|
||||
line->main_size = 0;
|
||||
line->intrinsic_non_percentage_size = 0;
|
||||
line->percentage_non_percentage_size = 0;
|
||||
line->intrinsic_percentage = 0;
|
||||
line->intrinsic_gap_percentage = 0;
|
||||
line->intrinsic_min_size = 0;
|
||||
line->intrinsic_container_min_size = 0;
|
||||
line->cross_size = 0;
|
||||
line->sum_of_grow_value = 0;
|
||||
line->sum_of_shrink_value = 0;
|
||||
line->count_of_in_flow_items = 0;
|
||||
line->count_of_auto_margin_items = 0;
|
||||
list_create(&line->items);
|
||||
return line;
|
||||
@@ -81,6 +117,7 @@ static void ui_flexbox_line_load_item(ui_flexbox_line_t *line,
|
||||
if (item->computed_style.flex_shrink > 0) {
|
||||
line->sum_of_shrink_value += item->computed_style.flex_shrink;
|
||||
}
|
||||
line->count_of_in_flow_items++;
|
||||
list_append(&line->items, item);
|
||||
}
|
||||
|
||||
@@ -106,6 +143,53 @@ static float ui_flexbox_resolve_gap(uint8_t type, css_numeric_value_t value,
|
||||
return value;
|
||||
}
|
||||
|
||||
static float ui_flexbox_layout_get_main_gap_percentage(
|
||||
ui_flexbox_layout_context_t *ctx)
|
||||
{
|
||||
if (ctx->main_gap_type != CSS_GAP_SET ||
|
||||
ctx->main_gap_unit != CSS_UNIT_PERCENT) {
|
||||
return 0;
|
||||
}
|
||||
return ctx->main_gap_value / 100.f;
|
||||
}
|
||||
|
||||
static void ui_flexbox_layout_resolve_main_gap(ui_flexbox_layout_context_t *ctx)
|
||||
{
|
||||
float reference = ctx->column_direction
|
||||
? ctx->widget->content_box.height
|
||||
: ctx->widget->content_box.width;
|
||||
|
||||
ctx->main_gap =
|
||||
ui_flexbox_resolve_gap(ctx->main_gap_type, ctx->main_gap_value,
|
||||
ctx->main_gap_unit, reference);
|
||||
}
|
||||
|
||||
static bool ui_flexbox_item_get_main_percentage(
|
||||
ui_flexbox_layout_context_t *ctx, ui_widget_t *item, float *percentage)
|
||||
{
|
||||
css_computed_style_t *s = &item->specified_style;
|
||||
|
||||
if (IS_CSS_PERCENTAGE(s, flex_basis)) {
|
||||
*percentage = s->flex_basis / 100.f;
|
||||
return true;
|
||||
}
|
||||
if (s->type_bits.flex_basis != CSS_FLEX_BASIS_AUTO) {
|
||||
return false;
|
||||
}
|
||||
if (ctx->column_direction) {
|
||||
if (!IS_CSS_PERCENTAGE(s, height)) {
|
||||
return false;
|
||||
}
|
||||
*percentage = s->height / 100.f;
|
||||
return true;
|
||||
}
|
||||
if (!IS_CSS_PERCENTAGE(s, width)) {
|
||||
return false;
|
||||
}
|
||||
*percentage = s->width / 100.f;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void ui_reset_row_item_flex_basis(ui_widget_t *item)
|
||||
{
|
||||
css_computed_style_t *s = &item->computed_style;
|
||||
@@ -219,6 +303,162 @@ static float ui_flexbox_item_max_main_size(ui_flexbox_layout_context_t *ctx,
|
||||
css_width_from_cbox(cs, item->max_content_width));
|
||||
}
|
||||
|
||||
static ui_flexbox_main_contribution_t ui_flexbox_item_get_main_contribution(
|
||||
ui_flexbox_layout_context_t *ctx, ui_widget_t *item)
|
||||
{
|
||||
ui_flexbox_main_contribution_t contribution = { 0 };
|
||||
css_computed_style_t *cs = &item->computed_style;
|
||||
float intrinsic_size;
|
||||
float required_size;
|
||||
float fixed_size;
|
||||
|
||||
if (ui_flexbox_item_get_main_percentage(ctx, item,
|
||||
&contribution.percentage)) {
|
||||
contribution.has_percentage_size = true;
|
||||
intrinsic_size = ui_flexbox_item_max_main_size(ctx, item);
|
||||
if (ctx->column_direction) {
|
||||
contribution.non_percentage_size =
|
||||
css_obox_height(cs, 0);
|
||||
fixed_size = ui_widget_fix_height(item, 0);
|
||||
contribution.min_size = css_obox_height(cs, fixed_size);
|
||||
required_size = css_height_from_bbox(
|
||||
cs, intrinsic_size - css_margin_y(cs));
|
||||
fixed_size = ui_widget_fix_height(item, required_size);
|
||||
fixed_size = css_obox_height(cs, fixed_size);
|
||||
} else {
|
||||
contribution.non_percentage_size =
|
||||
css_obox_width(cs, 0);
|
||||
fixed_size = ui_widget_fix_width(item, 0);
|
||||
contribution.min_size = css_obox_width(cs, fixed_size);
|
||||
required_size = css_width_from_bbox(
|
||||
cs, intrinsic_size - css_margin_x(cs));
|
||||
fixed_size = ui_widget_fix_width(item, required_size);
|
||||
fixed_size = css_obox_width(cs, fixed_size);
|
||||
}
|
||||
if (contribution.percentage > 0 &&
|
||||
contribution.min_size + 0.0001f < intrinsic_size &&
|
||||
fixed_size + 0.0001f >= intrinsic_size) {
|
||||
contribution.container_min_size = ui_ceil_layout_unit(
|
||||
required_size / contribution.percentage);
|
||||
}
|
||||
return contribution;
|
||||
}
|
||||
contribution.min_size = ui_flexbox_item_min_main_size(ctx, item);
|
||||
contribution.non_percentage_size =
|
||||
ui_flexbox_item_reset_main_size(ctx, item);
|
||||
contribution.non_percentage_size =
|
||||
y_max(contribution.non_percentage_size,
|
||||
ui_flexbox_item_max_main_size(ctx, item));
|
||||
return contribution;
|
||||
}
|
||||
|
||||
static bool ui_flexbox_line_should_wrap_percentage(
|
||||
ui_flexbox_layout_context_t *ctx, ui_flexbox_line_t *line,
|
||||
ui_flexbox_main_contribution_t contribution)
|
||||
{
|
||||
float gap_percentage = ui_flexbox_layout_get_main_gap_percentage(ctx);
|
||||
float percentage;
|
||||
float non_percentage_size = line->intrinsic_non_percentage_size +
|
||||
line->percentage_non_percentage_size +
|
||||
contribution.non_percentage_size;
|
||||
|
||||
if (ctx->widget->computed_style.type_bits.flex_wrap !=
|
||||
CSS_FLEX_WRAP_WRAP ||
|
||||
line->count_of_in_flow_items == 0) {
|
||||
return false;
|
||||
}
|
||||
percentage = line->intrinsic_percentage + contribution.percentage +
|
||||
gap_percentage;
|
||||
if (percentage > 1.0001f) {
|
||||
return true;
|
||||
}
|
||||
if (ctx->main_gap_unit != CSS_UNIT_PERCENT) {
|
||||
non_percentage_size += ctx->main_gap;
|
||||
}
|
||||
return 1.f - percentage <= 0.0001f && non_percentage_size > 0.0001f;
|
||||
}
|
||||
|
||||
static void ui_flexbox_line_load_main_contribution(
|
||||
ui_flexbox_layout_context_t *ctx, ui_flexbox_line_t *line,
|
||||
ui_widget_t *item, ui_flexbox_main_contribution_t contribution)
|
||||
{
|
||||
if (line->count_of_in_flow_items > 0) {
|
||||
if (ctx->main_gap_unit == CSS_UNIT_PERCENT) {
|
||||
float percentage =
|
||||
ui_flexbox_layout_get_main_gap_percentage(ctx);
|
||||
|
||||
line->intrinsic_percentage += percentage;
|
||||
line->intrinsic_gap_percentage += percentage;
|
||||
} else {
|
||||
line->intrinsic_non_percentage_size += ctx->main_gap;
|
||||
line->intrinsic_min_size += ctx->main_gap;
|
||||
}
|
||||
}
|
||||
if (contribution.has_percentage_size) {
|
||||
line->percentage_non_percentage_size +=
|
||||
contribution.non_percentage_size;
|
||||
} else {
|
||||
line->intrinsic_non_percentage_size +=
|
||||
contribution.non_percentage_size;
|
||||
}
|
||||
line->intrinsic_percentage += contribution.percentage;
|
||||
line->intrinsic_min_size += contribution.min_size;
|
||||
line->intrinsic_container_min_size =
|
||||
y_max(line->intrinsic_container_min_size,
|
||||
contribution.container_min_size);
|
||||
ui_flexbox_line_load_item(line, item);
|
||||
}
|
||||
|
||||
static float ui_flexbox_line_intrinsic_main_size(
|
||||
ui_flexbox_layout_context_t *ctx, ui_flexbox_line_t *line)
|
||||
{
|
||||
list_node_t *node;
|
||||
ui_widget_t *item;
|
||||
ui_flexbox_main_contribution_t contribution;
|
||||
float denominator;
|
||||
float main_size;
|
||||
float next_size;
|
||||
size_t i;
|
||||
|
||||
main_size =
|
||||
y_max(line->intrinsic_min_size, line->intrinsic_container_min_size);
|
||||
for (i = 0; i <= line->count_of_in_flow_items; ++i) {
|
||||
denominator = 1.f - line->intrinsic_gap_percentage;
|
||||
next_size = line->intrinsic_non_percentage_size;
|
||||
for (list_each(node, &line->items)) {
|
||||
item = node->data;
|
||||
if (!ui_widget_in_layout_flow(item) ||
|
||||
!ui_flexbox_item_get_main_percentage(
|
||||
ctx, item, &contribution.percentage)) {
|
||||
continue;
|
||||
}
|
||||
contribution =
|
||||
ui_flexbox_item_get_main_contribution(ctx, item);
|
||||
if (contribution.percentage * main_size +
|
||||
contribution.non_percentage_size >=
|
||||
contribution.min_size) {
|
||||
denominator -= contribution.percentage;
|
||||
next_size += contribution.non_percentage_size;
|
||||
} else {
|
||||
next_size += contribution.min_size;
|
||||
}
|
||||
}
|
||||
if (denominator > 0.0001f) {
|
||||
next_size /= denominator;
|
||||
} else {
|
||||
next_size = main_size;
|
||||
}
|
||||
next_size = y_max(next_size, line->intrinsic_min_size);
|
||||
next_size =
|
||||
y_max(next_size, line->intrinsic_container_min_size);
|
||||
if (next_size <= main_size + 0.0001f) {
|
||||
break;
|
||||
}
|
||||
main_size = next_size;
|
||||
}
|
||||
return main_size;
|
||||
}
|
||||
|
||||
static void ui_flexbox_layout_compute_justify_content(
|
||||
ui_flexbox_layout_context_t *ctx, ui_flexbox_line_t *line,
|
||||
float *start_axis, float *space)
|
||||
@@ -314,26 +554,64 @@ static void ui_flexbox_layout_load_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
{
|
||||
list_node_t *node;
|
||||
ui_widget_t *child;
|
||||
ui_flexbox_line_t *line = NULL;
|
||||
ui_flexbox_main_contribution_t contribution;
|
||||
css_computed_style_t *cs;
|
||||
float main_size, min_main_size;
|
||||
float max_item_min_size = 0;
|
||||
bool main_size_fixed;
|
||||
bool load_intrinsic_lines;
|
||||
unsigned child_index = 0;
|
||||
unsigned in_flow_count = 0;
|
||||
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_DEBUG_MSG(
|
||||
"%s: begin, main_size_fixed?=%d", __FUNCTION__,
|
||||
main_size_fixed =
|
||||
ctx->column_direction
|
||||
? IS_CSS_FIXED_LENGTH(&ctx->widget->computed_style, height)
|
||||
: IS_CSS_FIXED_LENGTH(&ctx->widget->computed_style, width));
|
||||
: IS_CSS_FIXED_LENGTH(&ctx->widget->computed_style, width);
|
||||
load_intrinsic_lines =
|
||||
!main_size_fixed &&
|
||||
ctx->widget->computed_style.type_bits.flex_wrap ==
|
||||
CSS_FLEX_WRAP_WRAP;
|
||||
ctx->has_intrinsic_lines = load_intrinsic_lines;
|
||||
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_DEBUG_MSG("%s: begin, main_size_fixed?=%d", __FUNCTION__,
|
||||
main_size_fixed);
|
||||
UI_DEBUG_INDENT_INC;
|
||||
UI_DEBUG_END;
|
||||
for (list_each(node, &ctx->widget->children)) {
|
||||
child = node->data;
|
||||
cs = &child->computed_style;
|
||||
if (!ui_widget_in_layout_flow(child)) {
|
||||
if (load_intrinsic_lines) {
|
||||
if (!line) {
|
||||
line = ui_flexbox_layout_next_line(ctx);
|
||||
}
|
||||
list_append(&line->items, child);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ui_widget_reset_layout(child);
|
||||
if (load_intrinsic_lines) {
|
||||
contribution =
|
||||
ui_flexbox_item_get_main_contribution(ctx, child);
|
||||
min_main_size = contribution.min_size;
|
||||
if (cs->flex_shrink <= 0 &&
|
||||
!contribution.has_percentage_size) {
|
||||
min_main_size =
|
||||
y_max(min_main_size,
|
||||
contribution.non_percentage_size);
|
||||
}
|
||||
max_item_min_size =
|
||||
y_max(max_item_min_size, min_main_size);
|
||||
if (!line || ui_flexbox_line_should_wrap_percentage(
|
||||
ctx, line, contribution)) {
|
||||
line = ui_flexbox_layout_next_line(ctx);
|
||||
}
|
||||
ui_flexbox_line_load_main_contribution(ctx, line, child,
|
||||
contribution);
|
||||
continue;
|
||||
}
|
||||
main_size = ui_flexbox_item_reset_main_size(ctx, child);
|
||||
main_size =
|
||||
y_max(main_size, ui_flexbox_item_max_main_size(ctx, child));
|
||||
@@ -356,6 +634,18 @@ static void ui_flexbox_layout_load_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
child_index++;
|
||||
UI_DEBUG_END;
|
||||
}
|
||||
if (load_intrinsic_lines) {
|
||||
for (list_each(node, &ctx->lines)) {
|
||||
line = node->data;
|
||||
main_size =
|
||||
ui_flexbox_line_intrinsic_main_size(ctx, line);
|
||||
ui_resizer_load_item_main_size(
|
||||
ctx->resizer, main_size,
|
||||
y_max(max_item_min_size,
|
||||
line->intrinsic_container_min_size));
|
||||
ui_resizer_update(ctx->resizer);
|
||||
}
|
||||
}
|
||||
if (ctx->column_direction) {
|
||||
ui_resizer_commit_column_main_size(ctx->resizer);
|
||||
} else {
|
||||
@@ -368,6 +658,56 @@ static void ui_flexbox_layout_load_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
UI_DEBUG_END;
|
||||
}
|
||||
|
||||
static void ui_flexbox_line_reset_main_size(ui_flexbox_layout_context_t *ctx,
|
||||
ui_flexbox_line_t *line)
|
||||
{
|
||||
list_node_t *node;
|
||||
ui_widget_t *child;
|
||||
css_computed_style_t *cs;
|
||||
float main_size;
|
||||
|
||||
line->main_size = 0;
|
||||
line->cross_size = 0;
|
||||
line->sum_of_grow_value = 0;
|
||||
line->sum_of_shrink_value = 0;
|
||||
line->count_of_in_flow_items = 0;
|
||||
line->count_of_auto_margin_items = 0;
|
||||
for (list_each(node, &line->items)) {
|
||||
child = node->data;
|
||||
if (!ui_widget_in_layout_flow(child)) {
|
||||
continue;
|
||||
}
|
||||
cs = &child->computed_style;
|
||||
main_size = ui_flexbox_item_reset_main_size(ctx, child);
|
||||
if (line->count_of_in_flow_items > 0) {
|
||||
line->main_size += ctx->main_gap;
|
||||
}
|
||||
line->main_size += main_size;
|
||||
if (cs->flex_grow > 0) {
|
||||
line->sum_of_grow_value += cs->flex_grow;
|
||||
}
|
||||
if (cs->flex_shrink > 0) {
|
||||
line->sum_of_shrink_value += cs->flex_shrink;
|
||||
}
|
||||
if (ctx->column_direction) {
|
||||
if (cs->type_bits.margin_top == CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
if (cs->type_bits.margin_bottom == CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
} else {
|
||||
if (cs->type_bits.margin_left == CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
if (cs->type_bits.margin_right == CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
}
|
||||
line->count_of_in_flow_items++;
|
||||
}
|
||||
}
|
||||
|
||||
static void ui_flexbox_layout_apply_line(ui_flexbox_layout_context_t *ctx,
|
||||
ui_flexbox_line_t *line,
|
||||
float flex_space, float margin_space)
|
||||
@@ -458,57 +798,68 @@ static void ui_flexbox_layout_apply_main_size(ui_flexbox_layout_context_t *ctx)
|
||||
max_main_size);
|
||||
UI_DEBUG_INDENT_INC;
|
||||
UI_DEBUG_END;
|
||||
line = ui_flexbox_layout_next_line(ctx);
|
||||
for (list_each(node, &ctx->widget->children)) {
|
||||
child = node->data;
|
||||
if (!ui_widget_in_layout_flow(child)) {
|
||||
if (ctx->has_intrinsic_lines) {
|
||||
for (list_each(line_node, &ctx->lines)) {
|
||||
line = line_node->data;
|
||||
ui_flexbox_line_reset_main_size(ctx, line);
|
||||
}
|
||||
} else {
|
||||
line = ui_flexbox_layout_next_line(ctx);
|
||||
for (list_each(node, &ctx->widget->children)) {
|
||||
child = node->data;
|
||||
if (!ui_widget_in_layout_flow(child)) {
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_WIDGET_STR(child, str);
|
||||
UI_DEBUG_MSG("line[%zu]: children[%u]=%s, skip",
|
||||
line->index, child_index, str);
|
||||
child_index++;
|
||||
UI_DEBUG_END;
|
||||
list_append(&line->items, child);
|
||||
continue;
|
||||
}
|
||||
cs = &child->computed_style;
|
||||
main_size = ui_flexbox_item_reset_main_size(ctx, child);
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_WIDGET_STR(child, str);
|
||||
UI_DEBUG_MSG("line[%zu]: children[%u]=%s, skip",
|
||||
line->index, child_index, str);
|
||||
UI_WIDGET_SIZE_STR(child, size_str);
|
||||
UI_DEBUG_MSG("line[%zu]: children[%u]=%s, size=%s, "
|
||||
"flex_basis(%d)=%g, main_size=%g",
|
||||
line->index, child_index, str, size_str,
|
||||
cs->type_bits.flex_basis, cs->flex_basis,
|
||||
main_size);
|
||||
child_index++;
|
||||
UI_DEBUG_END;
|
||||
list_append(&line->items, child);
|
||||
continue;
|
||||
}
|
||||
cs = &child->computed_style;
|
||||
main_size = ui_flexbox_item_reset_main_size(ctx, child);
|
||||
UI_DEBUG_BEGIN;
|
||||
UI_WIDGET_STR(child, str);
|
||||
UI_WIDGET_SIZE_STR(child, size_str);
|
||||
UI_DEBUG_MSG("line[%zu]: children[%u]=%s, size=%s, "
|
||||
"flex_basis(%d)=%g, main_size=%g",
|
||||
line->index, child_index, str, size_str,
|
||||
cs->type_bits.flex_basis, cs->flex_basis,
|
||||
main_size);
|
||||
child_index++;
|
||||
UI_DEBUG_END;
|
||||
if (s->type_bits.flex_wrap == CSS_FLEX_WRAP_WRAP &&
|
||||
line->main_size > 0 &&
|
||||
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) {
|
||||
line->count_of_auto_margin_items++;
|
||||
if (s->type_bits.flex_wrap == CSS_FLEX_WRAP_WRAP &&
|
||||
line->main_size > 0 &&
|
||||
line->main_size + ctx->main_gap + main_size >
|
||||
max_main_size) {
|
||||
line = ui_flexbox_layout_next_line(ctx);
|
||||
}
|
||||
if (cs->type_bits.margin_bottom == CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
if (line->items.length > 0) {
|
||||
line->main_size += ctx->main_gap;
|
||||
}
|
||||
} else {
|
||||
if (cs->type_bits.margin_left == CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
if (cs->type_bits.margin_right == CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
line->main_size += main_size;
|
||||
if (column) {
|
||||
if (cs->type_bits.margin_top ==
|
||||
CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
if (cs->type_bits.margin_bottom ==
|
||||
CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
} else {
|
||||
if (cs->type_bits.margin_left ==
|
||||
CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
if (cs->type_bits.margin_right ==
|
||||
CSS_MARGIN_AUTO) {
|
||||
line->count_of_auto_margin_items++;
|
||||
}
|
||||
}
|
||||
ui_flexbox_line_load_item(line, child);
|
||||
}
|
||||
ui_flexbox_line_load_item(line, child);
|
||||
}
|
||||
ctx->cross_size = 0;
|
||||
UI_DEBUG_BEGIN;
|
||||
@@ -717,9 +1068,15 @@ void ui_flexbox_layout_reflow(ui_widget_t *w, ui_resizer_t *resizer)
|
||||
s->type_bits.column_gap, s->column_gap,
|
||||
s->unit_bits.column_gap, w->content_box.width);
|
||||
if (ctx.column_direction) {
|
||||
ctx.main_gap_type = s->type_bits.row_gap;
|
||||
ctx.main_gap_value = s->row_gap;
|
||||
ctx.main_gap_unit = s->unit_bits.row_gap;
|
||||
ctx.main_gap = row_gap;
|
||||
ctx.cross_gap = column_gap;
|
||||
} else {
|
||||
ctx.main_gap_type = s->type_bits.column_gap;
|
||||
ctx.main_gap_value = s->column_gap;
|
||||
ctx.main_gap_unit = s->unit_bits.column_gap;
|
||||
ctx.main_gap = column_gap;
|
||||
ctx.cross_gap = row_gap;
|
||||
}
|
||||
@@ -739,6 +1096,7 @@ void ui_flexbox_layout_reflow(ui_widget_t *w, ui_resizer_t *resizer)
|
||||
ui_resizer_load_row_minmaxinfo(resizer);
|
||||
}
|
||||
ui_flexbox_layout_load_main_size(&ctx);
|
||||
ui_flexbox_layout_resolve_main_gap(&ctx);
|
||||
ui_flexbox_layout_apply_main_size(&ctx);
|
||||
ui_flexbox_layout_reflow_lines(&ctx);
|
||||
w->proto->resize(w, w->content_box.width, w->content_box.height);
|
||||
|
||||
+146
@@ -96,6 +96,10 @@ scrollarea-content {
|
||||
width: fit-content;
|
||||
}
|
||||
|
||||
.percentage-wrap-box {
|
||||
background-color: #f3b64c;
|
||||
}
|
||||
|
||||
.percent-30-item {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
@@ -110,3 +114,145 @@ scrollarea-content {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.percent-100-item {
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.percent-100-item-30 {
|
||||
width: 30%;
|
||||
}
|
||||
|
||||
.percent-100-item-40 {
|
||||
width: 40%;
|
||||
}
|
||||
|
||||
.mixed-gap-group {
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.mixed-gap-percent-item {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.mixed-gap-fixed-item {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.full-width-item {
|
||||
width: 100%;
|
||||
height: 20px;
|
||||
padding: 4px;
|
||||
border: 2px solid #333;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.intrinsic-percent-item {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.intrinsic-percent-child {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.decorated-percent-group {
|
||||
border: 1px solid #4f72be;
|
||||
}
|
||||
|
||||
.decorated-percent-item {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
padding-left: 10px;
|
||||
padding-right: 10px;
|
||||
border: 1px solid #333;
|
||||
box-sizing: content-box;
|
||||
}
|
||||
|
||||
.decorated-percent-child {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
background-color: #6ca86c;
|
||||
}
|
||||
|
||||
.min-percent-item {
|
||||
width: 30%;
|
||||
min-width: 100px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.review-fixed-item {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.percent-gap-group {
|
||||
gap: 10%;
|
||||
}
|
||||
|
||||
.percent-gap-item {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.decorated-empty-percent-item {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
padding-left: 10px;
|
||||
padding-right: 10px;
|
||||
border: 1px solid #333;
|
||||
box-sizing: content-box;
|
||||
}
|
||||
|
||||
.decorated-empty-fixed-item {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.full-percent-wrap-item {
|
||||
width: 100%;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.percentage-wrap-fixed-item {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.percent-row-wrap-item-30 {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.percent-row-wrap-item-70 {
|
||||
width: 70%;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.no-flex-fixed-item {
|
||||
width: 100px;
|
||||
height: 20px;
|
||||
flex: none;
|
||||
}
|
||||
|
||||
.full-percent-no-flex-item {
|
||||
width: 100%;
|
||||
height: 20px;
|
||||
flex: none;
|
||||
}
|
||||
|
||||
.decorated-before-percent-item {
|
||||
width: 30%;
|
||||
height: 20px;
|
||||
padding-left: 10px;
|
||||
padding-right: 10px;
|
||||
border: 1px solid #333;
|
||||
box-sizing: content-box;
|
||||
}
|
||||
|
||||
.fixed-wrap-box {
|
||||
background-color: #4f72be;
|
||||
}
|
||||
|
||||
+126
-6
@@ -35,10 +35,10 @@
|
||||
<text class="example__title">Four 30 percent flex items</text>
|
||||
<w class="example__content">
|
||||
<w id="percent-30-group" class="percent-wrap-group percent-30-group">
|
||||
<w id="percent-30-item-1" class="percent-30-item"></w>
|
||||
<w id="percent-30-item-2" class="percent-30-item"></w>
|
||||
<w id="percent-30-item-3" class="percent-30-item"></w>
|
||||
<w id="percent-30-item-4" class="percent-30-item"></w>
|
||||
<w id="percent-30-item-1" class="percent-30-item percentage-wrap-box"></w>
|
||||
<w id="percent-30-item-2" class="percent-30-item percentage-wrap-box"></w>
|
||||
<w id="percent-30-item-3" class="percent-30-item percentage-wrap-box"></w>
|
||||
<w id="percent-30-item-4" class="percent-30-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
@@ -46,8 +46,128 @@
|
||||
<text class="example__title">Percentage and fixed flex items</text>
|
||||
<w class="example__content">
|
||||
<w id="mixed-size-group" class="percent-wrap-group mixed-size-group">
|
||||
<w id="mixed-percent-item" class="mixed-percent-item"></w>
|
||||
<w id="mixed-fixed-item" class="mixed-fixed-item"></w>
|
||||
<w id="mixed-percent-item" class="mixed-percent-item percentage-wrap-box"></w>
|
||||
<w id="mixed-fixed-item" class="mixed-fixed-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Thirty thirty forty percent flex items</text>
|
||||
<w class="example__content">
|
||||
<w id="percent-100-group" class="percent-wrap-group percent-100-group">
|
||||
<w id="percent-100-item-1" class="percent-100-item percent-100-item-30 percentage-wrap-box"></w>
|
||||
<w id="percent-100-item-2" class="percent-100-item percent-100-item-30 percentage-wrap-box"></w>
|
||||
<w id="percent-100-item-3" class="percent-100-item percent-100-item-40 percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Percentage and fixed flex items with gap</text>
|
||||
<w class="example__content">
|
||||
<w id="mixed-gap-group" class="percent-wrap-group mixed-gap-group">
|
||||
<w id="mixed-gap-percent-item" class="mixed-gap-percent-item percentage-wrap-box"></w>
|
||||
<w id="mixed-gap-fixed-item" class="mixed-gap-fixed-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Full width bordered flex item</text>
|
||||
<w class="example__content">
|
||||
<w id="full-width-group" class="percent-wrap-group full-width-group">
|
||||
<w id="full-width-item" class="full-width-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Intrinsic percentage flex item</text>
|
||||
<w class="example__content">
|
||||
<w id="intrinsic-percent-group" class="percent-wrap-group">
|
||||
<w id="intrinsic-percent-item" class="intrinsic-percent-item percentage-wrap-box">
|
||||
<w id="intrinsic-percent-child" class="intrinsic-percent-child percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Decorated intrinsic percentage flex item</text>
|
||||
<w class="example__content">
|
||||
<w id="decorated-percent-group" class="percent-wrap-group decorated-percent-group percentage-wrap-box">
|
||||
<w id="decorated-percent-item" class="decorated-percent-item percentage-wrap-box">
|
||||
<w id="decorated-percent-child" class="decorated-percent-child percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Minimum percentage flex item</text>
|
||||
<w class="example__content">
|
||||
<w id="min-percent-group" class="percent-wrap-group">
|
||||
<w id="min-percent-item" class="min-percent-item percentage-wrap-box"></w>
|
||||
<w id="min-percent-fixed-item" class="review-fixed-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Percentage flex gap</text>
|
||||
<w class="example__content">
|
||||
<w id="percent-gap-group" class="percent-wrap-group percent-gap-group">
|
||||
<w id="percent-gap-item" class="percent-gap-item percentage-wrap-box"></w>
|
||||
<w id="percent-gap-fixed-item" class="review-fixed-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Fit-content percentage flex item with decorations</text>
|
||||
<w class="example__content">
|
||||
<w id="decorated-empty-percent-group" class="percent-wrap-group">
|
||||
<w id="decorated-empty-percent-item" class="decorated-empty-percent-item percentage-wrap-box"></w>
|
||||
<w id="decorated-empty-fixed-item" class="decorated-empty-fixed-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Fit-content full percentage row wrap</text>
|
||||
<w class="example__content">
|
||||
<w id="full-percent-wrap-group" class="percent-wrap-group">
|
||||
<w id="full-percent-wrap-item" class="full-percent-wrap-item percentage-wrap-box"></w>
|
||||
<w id="full-percent-wrap-fixed" class="percentage-wrap-fixed-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Fit-content percentage row totaling one hundred wrap</text>
|
||||
<w class="example__content">
|
||||
<w id="percent-row-wrap-group" class="percent-wrap-group">
|
||||
<w id="percent-row-wrap-item-30" class="percent-row-wrap-item-30 percentage-wrap-box"></w>
|
||||
<w id="percent-row-wrap-item-70" class="percent-row-wrap-item-70 percentage-wrap-box"></w>
|
||||
<w id="percent-row-wrap-fixed" class="percentage-wrap-fixed-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Fixed item before full percentage item</text>
|
||||
<w class="example__content">
|
||||
<w id="fixed-before-percent-group" class="percent-wrap-group">
|
||||
<w id="fixed-before-percent-fixed" class="no-flex-fixed-item fixed-wrap-box"></w>
|
||||
<w id="fixed-before-percent-item" class="full-percent-no-flex-item percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Decorated percentage item before percentage item</text>
|
||||
<w class="example__content">
|
||||
<w id="decorated-before-percent-group" class="percent-wrap-group">
|
||||
<w id="decorated-before-percent-item" class="decorated-before-percent-item percentage-wrap-box"></w>
|
||||
<w id="decorated-before-percent-next" class="percent-row-wrap-item-70 percentage-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
<w class="example">
|
||||
<text class="example__title">Fit-content contribution of fixed wrap items</text>
|
||||
<w class="example__content">
|
||||
<w id="fixed-contribution-group" class="percent-wrap-group">
|
||||
<w id="fixed-contribution-item-1" class="no-flex-fixed-item fixed-wrap-box"></w>
|
||||
<w id="fixed-contribution-item-2" class="no-flex-fixed-item fixed-wrap-box"></w>
|
||||
</w>
|
||||
</w>
|
||||
</w>
|
||||
|
||||
@@ -87,6 +87,249 @@ static void test_mixed_percentage_and_fixed_width(void)
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_percentages_totaling_one_hundred(void)
|
||||
{
|
||||
ui_widget_t *item1 = ui_get_widget("percent-100-item-1");
|
||||
ui_widget_t *item2 = ui_get_widget("percent-100-item-2");
|
||||
ui_widget_t *item3 = ui_get_widget("percent-100-item-3");
|
||||
|
||||
ctest_equal_float("should keep second item on first line",
|
||||
item2->border_box.y, item1->border_box.y);
|
||||
ctest_equal_float("should keep third item on first line",
|
||||
item3->border_box.y, item1->border_box.y);
|
||||
}
|
||||
|
||||
static void test_mixed_percentage_fixed_width_and_gap(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("mixed-gap-group");
|
||||
ui_widget_t *percent = ui_get_widget("mixed-gap-percent-item");
|
||||
ui_widget_t *fixed = ui_get_widget("mixed-gap-fixed-item");
|
||||
|
||||
ctest_equal_float("should keep gapped mixed items on one line",
|
||||
percent->border_box.y, fixed->border_box.y);
|
||||
ctest_equal_bool("should solve gapped mixed line width near 157px",
|
||||
group->content_box.width > 156.f &&
|
||||
group->content_box.width < 158.f,
|
||||
true);
|
||||
ctest_equal_bool("should resolve gapped percentage item near 47px",
|
||||
percent->border_box.width > 46.f &&
|
||||
percent->border_box.width < 48.f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_full_width_bordered_item_stability(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("full-width-group");
|
||||
ui_widget_t *item = ui_get_widget("full-width-item");
|
||||
float group_width = group->border_box.width;
|
||||
float item_width = item->border_box.width;
|
||||
|
||||
ctest_equal_bool("should keep full width group finite and positive",
|
||||
group_width > 0.f && group_width < 600.f, true);
|
||||
ctest_equal_bool("should keep full width item finite and positive",
|
||||
item_width > 0.f && item_width < 600.f, true);
|
||||
ui_widget_request_refresh_style(item);
|
||||
ui_update();
|
||||
ctest_equal_float(
|
||||
"should keep full width group stable after style refresh",
|
||||
group->border_box.width, group_width);
|
||||
ctest_equal_float(
|
||||
"should keep full width item stable after style refresh",
|
||||
item->border_box.width, item_width);
|
||||
}
|
||||
|
||||
static void test_intrinsic_percentage_item(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("intrinsic-percent-group");
|
||||
ui_widget_t *item = ui_get_widget("intrinsic-percent-item");
|
||||
ui_widget_t *child = ui_get_widget("intrinsic-percent-child");
|
||||
|
||||
ctest_equal_bool("should contain fixed intrinsic child",
|
||||
child->border_box.width <= item->content_box.width,
|
||||
true);
|
||||
ctest_equal_bool("should round percentage group width up to 1/64px",
|
||||
group->content_box.width >= 333.3f &&
|
||||
group->content_box.width <= 333.5f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_decorated_intrinsic_percentage_item(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("decorated-percent-group");
|
||||
ui_widget_t *item = ui_get_widget("decorated-percent-item");
|
||||
ui_widget_t *child = ui_get_widget("decorated-percent-child");
|
||||
|
||||
ctest_equal_bool(
|
||||
"should solve decorated percentage group near 333.34375px",
|
||||
group->content_box.width >= 333.3f &&
|
||||
group->content_box.width <= 333.5f,
|
||||
true);
|
||||
ctest_equal_bool("should contain fixed child in percentage content box",
|
||||
child->border_box.width <= item->content_box.width,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_minimum_percentage_item(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("min-percent-group");
|
||||
ui_widget_t *percent = ui_get_widget("min-percent-item");
|
||||
ui_widget_t *fixed = ui_get_widget("min-percent-fixed-item");
|
||||
|
||||
ctest_equal_float("should keep minimum and fixed items on one line",
|
||||
percent->border_box.y, fixed->border_box.y);
|
||||
ctest_equal_bool("should contain both minimum and fixed outer widths",
|
||||
percent->outer_box.width + fixed->outer_box.width <=
|
||||
group->content_box.width,
|
||||
true);
|
||||
ctest_equal_bool("should solve minimum group width near 200px",
|
||||
group->content_box.width >= 199.f &&
|
||||
group->content_box.width <= 201.f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_percentage_gap(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("percent-gap-group");
|
||||
ui_widget_t *percent = ui_get_widget("percent-gap-item");
|
||||
ui_widget_t *fixed = ui_get_widget("percent-gap-fixed-item");
|
||||
float group_width = group->content_box.width;
|
||||
float percent_width = percent->border_box.width;
|
||||
float fixed_x = fixed->border_box.x;
|
||||
float gap = fixed_x - percent->border_box.x - percent_width;
|
||||
int i;
|
||||
|
||||
ctest_equal_float("should keep percentage gap items on one line",
|
||||
percent->border_box.y, fixed->border_box.y);
|
||||
ctest_equal_bool("should solve percentage gap group near 166.667px",
|
||||
group_width >= 166.f && group_width <= 168.f, true);
|
||||
ctest_equal_bool("should resolve percentage gap near 16.667px",
|
||||
gap >= 16.f && gap <= 17.f, true);
|
||||
for (i = 0; i < 3; ++i) {
|
||||
ui_widget_request_refresh_style(group);
|
||||
ui_update();
|
||||
}
|
||||
ctest_equal_float("should keep percentage gap group stable",
|
||||
group->content_box.width, group_width);
|
||||
ctest_equal_float("should keep percentage gap item stable",
|
||||
percent->border_box.width, percent_width);
|
||||
ctest_equal_float("should keep percentage gap position stable",
|
||||
fixed->border_box.x, fixed_x);
|
||||
}
|
||||
|
||||
static void test_decorated_empty_percentage_item(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("decorated-empty-percent-group");
|
||||
ui_widget_t *percent = ui_get_widget("decorated-empty-percent-item");
|
||||
ui_widget_t *fixed = ui_get_widget("decorated-empty-fixed-item");
|
||||
|
||||
ctest_equal_float("should keep decorated empty items on one line",
|
||||
percent->border_box.y, fixed->border_box.y);
|
||||
ctest_equal_bool(
|
||||
"should solve decorated empty percentage group near 174.296875px",
|
||||
group->content_box.width >= 174.28f &&
|
||||
group->content_box.width <= 174.31f,
|
||||
true);
|
||||
ctest_equal_bool(
|
||||
"should resolve decorated percentage outer width near 74.286px",
|
||||
percent->outer_box.width >= 74.28f &&
|
||||
percent->outer_box.width <= 74.31f,
|
||||
true);
|
||||
ctest_equal_bool("should preserve fixed item width near 100px",
|
||||
fixed->outer_box.width >= 99.99f &&
|
||||
fixed->outer_box.width <= 100.01f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_full_percentage_row_wrap(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("full-percent-wrap-group");
|
||||
ui_widget_t *percent = ui_get_widget("full-percent-wrap-item");
|
||||
ui_widget_t *fixed = ui_get_widget("full-percent-wrap-fixed");
|
||||
|
||||
ctest_equal_bool("should wrap fixed item after full percentage row",
|
||||
fixed->border_box.y > percent->border_box.y, true);
|
||||
ctest_equal_bool(
|
||||
"should keep full percentage group width finite and positive",
|
||||
group->content_box.width > 0.f && group->content_box.width < 600.f,
|
||||
true);
|
||||
ctest_equal_bool("should derive full percentage group near 100px",
|
||||
group->content_box.width >= 99.f &&
|
||||
group->content_box.width <= 101.f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_percentage_row_wrap(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("percent-row-wrap-group");
|
||||
ui_widget_t *item30 = ui_get_widget("percent-row-wrap-item-30");
|
||||
ui_widget_t *item70 = ui_get_widget("percent-row-wrap-item-70");
|
||||
ui_widget_t *fixed = ui_get_widget("percent-row-wrap-fixed");
|
||||
|
||||
ctest_equal_float("should keep 30 and 70 percent items on first row",
|
||||
item70->border_box.y, item30->border_box.y);
|
||||
ctest_equal_bool("should wrap fixed item after percentage row",
|
||||
fixed->border_box.y > item30->border_box.y, true);
|
||||
ctest_equal_bool(
|
||||
"should keep percentage row group width finite and positive",
|
||||
group->content_box.width > 0.f && group->content_box.width < 600.f,
|
||||
true);
|
||||
ctest_equal_bool("should derive percentage row group near 100px",
|
||||
group->content_box.width >= 99.f &&
|
||||
group->content_box.width <= 101.f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_fixed_before_full_percentage_wrap(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("fixed-before-percent-group");
|
||||
ui_widget_t *fixed = ui_get_widget("fixed-before-percent-fixed");
|
||||
ui_widget_t *percent = ui_get_widget("fixed-before-percent-item");
|
||||
|
||||
ctest_equal_bool("should wrap full percentage item after fixed item",
|
||||
percent->border_box.y > fixed->border_box.y, true);
|
||||
ctest_equal_bool("should keep wrapped group width near 100px",
|
||||
group->content_box.width >= 99.f &&
|
||||
group->content_box.width <= 101.f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_decorated_percentage_before_percentage_wrap(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("decorated-before-percent-group");
|
||||
ui_widget_t *decorated = ui_get_widget("decorated-before-percent-item");
|
||||
ui_widget_t *next = ui_get_widget("decorated-before-percent-next");
|
||||
|
||||
ctest_equal_bool(
|
||||
"should wrap percentage item after decorated percentage item",
|
||||
next->border_box.y > decorated->border_box.y, true);
|
||||
ctest_equal_bool(
|
||||
"should keep decorated percentage group finite and positive",
|
||||
group->content_box.width > 0.f && group->content_box.width < 600.f,
|
||||
true);
|
||||
}
|
||||
|
||||
static void test_fixed_wrap_content_contributions(void)
|
||||
{
|
||||
ui_widget_t *group = ui_get_widget("fixed-contribution-group");
|
||||
ui_widget_t *item1 = ui_get_widget("fixed-contribution-item-1");
|
||||
ui_widget_t *item2 = ui_get_widget("fixed-contribution-item-2");
|
||||
|
||||
ctest_equal_bool("should have max content width near 200px",
|
||||
group->max_content_width >= 199.f &&
|
||||
group->max_content_width <= 201.f,
|
||||
true);
|
||||
ctest_equal_bool("should have min content width near 100px",
|
||||
group->min_content_width >= 99.f &&
|
||||
group->min_content_width <= 101.f,
|
||||
true);
|
||||
ctest_equal_bool("should allow both fixed items on the used row",
|
||||
item2->border_box.y == item1->border_box.y, true);
|
||||
ctest_equal_bool("should allow used group width near 200px",
|
||||
group->content_box.width >= 199.f &&
|
||||
group->content_box.width <= 201.f,
|
||||
true);
|
||||
}
|
||||
|
||||
void test_flex_layout_percentage_wrap(void)
|
||||
{
|
||||
lcui_init();
|
||||
@@ -99,6 +342,31 @@ void test_flex_layout_percentage_wrap(void)
|
||||
test_four_thirty_percent_items);
|
||||
ctest_describe("mixed percentage and fixed flex items",
|
||||
test_mixed_percentage_and_fixed_width);
|
||||
ctest_describe("percentages totaling one hundred",
|
||||
test_percentages_totaling_one_hundred);
|
||||
ctest_describe("mixed percentage fixed width and gap",
|
||||
test_mixed_percentage_fixed_width_and_gap);
|
||||
ctest_describe("full width bordered item stability",
|
||||
test_full_width_bordered_item_stability);
|
||||
ctest_describe("intrinsic percentage flex item",
|
||||
test_intrinsic_percentage_item);
|
||||
ctest_describe("decorated intrinsic percentage flex item",
|
||||
test_decorated_intrinsic_percentage_item);
|
||||
ctest_describe("minimum percentage flex item",
|
||||
test_minimum_percentage_item);
|
||||
ctest_describe("percentage flex gap", test_percentage_gap);
|
||||
ctest_describe("decorated empty percentage flex item",
|
||||
test_decorated_empty_percentage_item);
|
||||
ctest_describe("full percentage row wrap",
|
||||
test_full_percentage_row_wrap);
|
||||
ctest_describe("percentage row totaling one hundred wrap",
|
||||
test_percentage_row_wrap);
|
||||
ctest_describe("fixed before full percentage wrap",
|
||||
test_fixed_before_full_percentage_wrap);
|
||||
ctest_describe("decorated percentage before percentage wrap",
|
||||
test_decorated_percentage_before_percentage_wrap);
|
||||
ctest_describe("fixed wrap content contributions",
|
||||
test_fixed_wrap_content_contributions);
|
||||
|
||||
#ifndef LCUI_FLEX_LAYOUT_PERCENTAGE_WRAP_PREVIEW
|
||||
lcui_destroy();
|
||||
|
||||
Reference in New Issue
Block a user