diff --git a/docs/internals/layout-engine.md b/docs/internals/layout-engine.md index 562fd159..05ae75d1 100644 --- a/docs/internals/layout-engine.md +++ b/docs/internals/layout-engine.md @@ -87,26 +87,31 @@ updater 先递归处理子元素(②),再做父元素自身的重排(③ | 布局约束 | `layout_constraint.{has_definite_width, available_width, source, generation}` | 父布局写入;`ui_widget_get_available_width` 校验 `source == parent` 且 `generation == parent->layout_generation` | | used size | `content_box` / `border_box` / `outer_box` | 每轮布局重算 | | 内在缓存 | `max_content_*`、`min_content_*`、`unconstrained_content_*` | 子项 `update_size` 写入;有有效宽度约束时不更新 `unconstrained_*`,避免 used size 污染 | -| 测量缓存 | `measured_content_width/height` + `has_measured` | 在给定宽度下测折行高时缓存;`should_reflow` 或宽度变化时失效 | +| 测量缓存 | `measured_content_width/height` + `has_measured` | 在给定宽度下测折行高时缓存;`request_reflow` 按依赖链使自身/祖先缓存失效,宽度变化也会失效 | 规则: - **约束必须在子项 `reset_layout` 之后记录**(`ui_flexbox_record_width_constraint`)。否则会读到上一轮布局写入的 `margin: auto` 实际值,算出过窄的可用宽。 -- **column 子项的主轴 basis 必须在父级可用宽下测**。纯 `unconstrained_content_height`(无限宽单行高)会让文本退回单行,实测 5 个折行用例失败,因此 `measured_content_*` 缓存不能删。 +- **百分比宽度在包含块宽度不确定时按 `auto` 处理**:块级流内元素交给父布局分配宽度,行内/绝对定位元素退回 fit-content。CSS 计算层不得把未解析的百分数(如 `100`)当作像素写进 `width`。 +- **column 子项的主轴 basis 必须在父级可用宽下测**。auto/百分比宽度的 block/flex 容器都遵守此规则;纯 `unconstrained_content_height`(无限宽单行高)会让文本退回单行,因此 `measured_content_*` 缓存不能删。`ui_flexbox_item_max_main_size()` 在同宽测量有效时也必须用该结果,否则内容收起会被旧的大高度阻止。 +- **测量宽只解析一次**。`ui_flexbox_resolve_measure_width()` 按 stretch/fit-content 或百分比得到 CSS 宽,经 `ui_widget_fix_width()` 应用 min/max 后换算为内容宽。组合测量、嵌套 reflow、used width 和 `measured_content_width` 共用该值;auto 宽的同轮提前返回也要校验缓存宽。 +- **组合测量必须累加子项 outer height**。固定高子项的 sizehint 仍可能只是文本行高,必须优先采用声明高度并做 min/max 与盒模型换算;auto 高子项才直接使用内容 sizehint。快路径与 reflow 路径都返回容器 content-box 高度,不能重复加入容器 padding/border。 +- **测量缓存按依赖失效,而非按更新标志失效**。`request_reflow` 清除自身与祖先的 `has_measured`;同轮后续 basis 趟即使 `should_reflow` 仍置位,也能复用刚测得的同宽结果。 - **row 主轴分配结果也要记录为宽度约束**(`ui_apply_row_item_main_size`),否则 `flex: 1` 子项单独 reflow 时会退回 max-content 宽。 - **sizehint 是否实现看 `proto->has_sizehint`**,不看返回数值是否为零。零宽/零高是合法测量结果;自定义 widget 覆盖 `sizehint` 时必须置 `has_sizehint = true`。对应查询是 `ui_widget_has_sizehint()`。 - **sizehint 只负责自有内容层**:`ui_widget_get_sizehint()` 始终可调用(默认原型是空实现,返回 0)。fit-content 与 column basis 都按 `max(自有 hint, 子项布局尺寸)` 合并;两者在 LCUI 中是同一内容区的不同表达,因此取 max 而非相加。 - **sizehint 内在宽缓存刷新**:自动宽叶子部件用 `ui_widget_get_unconstrained_sizehint()`(临时清除 used width 与父约束)刷新 `unconstrained_*`;auto 宽 flex/inline-block 容器用自身 `content_box` 刷新。否则内容变化后会继续复用旧约束下折行的宽度。 - **带流内子部件的容器不参与继承来的 sizehint 快路径**:`button`、`label`、`badge` 等从 `text` 继承原型的容器也带文本层 sizehint 实现,但自身文本层通常为空。`ui_widget_sizehint_is_exact()` 在无流内子项或原型标记 `sizehint_exact`(`text`、`canvas`、`textinput`)时为 true,只用于 `ui_flexbox_try_measure_container_height()` 这类组合测量快路径。 -- **`ui_flexbox_try_measure_container_height` 测量前先 `ui_widget_reset_width(child)`**,否则 `text` 会复用上一轮布局的固定宽,不折行。 +- **组合测量前必须恢复完整声明状态**:先对每个子项调用 `ui_widget_reset_width()` 和 `ui_widget_reset_height()`,再读取 margin、盒模型或 sizehint。否则 shrink 后的 computed height 和上一轮 auto margin 会被误当作内在尺寸。 +- **`text` 的 sizehint 不得把绘制层留在测量态**:测量可临时把 TextLayer 的 fixed size 设为 0、修改 max size 并重新排版。paint 前必须按**当前渲染 `style->content_box` 的物理尺寸**重新设置 fixed/max size 并排版,不能用上一轮布局记录的物理尺寸。否则 hover 重排会按文本实际宽居中,文字左右跳动;shimmer/spinner 等离屏捕获提升 device scale 时,则会把长文本按旧 scale 的宽度裁掉尾部。 - **每次 reflow 递增 `layout_generation`**,旧约束自动失效。flex column 单独 reflow 时,只有父级仍是 flex 容器且 `align-items: center` 才从约束重推导交叉轴宽,避免 flex→block 后使用过期约束。 - fit-content 仍会把结果写回 `computed_style.width`(已知临时例外,见项目 `docs/layout-engine-rules.md`)。 flex column 的入口分为测量 / 布局两段:`ui_flexbox_measure()` 收集并解析 basis,`ui_flexbox_layout()` 分行、分配主轴/交叉轴并定位。 -相关回归测试:`flex layout` → `measure cache`、`measure cache invalidates on content change`、`zero height text ignores stale cache`、`field checkbox description stable`。 +相关回归测试:`flex layout` → `measure cache`、`measure cache invalidates on content change`、`zero height text ignores stale cache`、`fixed height intrinsic restoration`、`auto margin intrinsic height`、`measurement path width consistency`、`percentage measurement constraints`;`gallery layout` → `gallery empty hover` 覆盖百分比宽父级、折行文本、固定高按钮和 `clip-safe` padding;`browser layout` 覆盖百分比 min-height 的父高变化重算。 -项目内速查:`docs/layout-engine-rules.md`(值模型、I1–I8 不变量、已知缺口);设计文档:`docs/superpowers/specs/2026-10-11-flex-column-text-wrap-design.md`、`2026-10-25-layout-two-phase-design.md`。 +项目内速查:`docs/layout-engine-rules.md`(值模型、I1–I10 不变量、已知缺口);设计文档:`docs/superpowers/specs/2026-10-11-flex-column-text-wrap-design.md`、`2026-10-25-layout-two-phase-design.md`。 ## 更新周期 @@ -192,6 +197,7 @@ ui_block_layout_reflow(w, resizer): - 计算容器总高度 ④ update(ctx) — 计算每个子元素的位置 - 遍历行列表,累加 x(同行内)和 y(行间) + - 百分比 height/min-height/max-height 在父高变化后重置并条件 reflow - 对 inline 元素做 vertical-align(middle/bottom) - 设置 child->layout_x, layout_y → ui_widget_update_box_position() ⑤ w->proto->resize(w, w, w->content_box.*) @@ -210,9 +216,11 @@ ui_flexbox_reflow(w, resizer): ③ ui_flexbox_layout(&ctx) - break_into_lines(非内在路径) - resolve_main_sizes:grow/shrink 分配并落到子项 - - resolve_cross_sizes:提交容器交叉尺寸、定位;流外子项按文档顺序 - 在本趟逐项布局 - ④ w->proto->resize(w, w->content_box.*) + - resolve_cross_sizes:提交容器交叉尺寸、定位 + ④ ui_flexbox_layout_out_of_flow(&ctx) + - 主布局结束后按“唯一 flex item”规则定位绝对定位子项 + - display:none 子项在收集阶段跳过,不进入 line->items + ⑤ w->proto->resize(w, w->content_box.*) ``` 约束、缓存与 sizehint 的细节见[约束模型与测量缓存](#约束模型与测量缓存flex-column)。 diff --git a/docs/layout-engine-rules.md b/docs/layout-engine-rules.md index 8f5ac2a6..3b253ec4 100644 --- a/docs/layout-engine-rules.md +++ b/docs/layout-engine-rules.md @@ -20,13 +20,22 @@ `max_content_*` 每轮都会被 used size 覆盖;有有效宽度约束时不更新 `unconstrained_content_*`,它是布局可用的可信内在尺寸。 5. **测量缓存**:`measured_content_width/height` + `has_measured`。用于“在给定宽度下 - 测折行高”的快路径;`should_reflow` 或缓存宽度变化时失效。 + 测折行高”的快路径;缓存宽度变化时失效。`ui_widget_request_reflow()` 会清空部件 + 自身及所有祖先的 `has_measured`,因此同一轮 flex basis 各趟可以复用新结果,而 + 内容或样式变化后又必须重新测量;不能用更新期间持续置位的 `should_reflow` + 充当缓存失效条件。 规则: - 父级分配的可用宽只能写入布局约束,不能依赖 `computed_style` 承载—— `reset_size()` 会把它擦除。 - `computed_style` 只承载 CSS 语义;布局结果不应永久写回。 +- 父级宽度不确定时,块级流内元素的百分比 `width` 计算为 `auto`(由父布局 + 分配),行内/绝对定位元素为 fit-content。不得保留原始百分数并把 `100` + 当像素使用,否则整棵子树会按 100px 排版。 +- 在 column flex 容器里,`width:auto` 与百分比宽度的 block/flex 容器子项都会在 + 父宽确定时记录内容宽约束,并在该宽度下重新测量 auto 高度。父宽不确定时百分比 + 仍按既有规则退回 auto/fit-content,不能提前把百分数当像素使用。 > 现状例外:居中 auto 宽子项的 fit-content 钳制、以及居中列容器的交叉轴重推导, > 仍会把 used width 写入 `computed_style.width`。它们每次都从约束重算、不依赖 @@ -37,17 +46,36 @@ ### 写入 - 时机:flex column 容器在每个子项 `reset_layout` 之后、主轴测量之前,由 - `ui_flexbox_record_width_constraint()` 为 in-flow 且 `width:auto` 的子项记录。 - 晚于 `reset_layout` 是为了避免读到上一轮布局写入的 `margin: auto` 实际值。 -- 值:`available_width = 容器 content_box.width − margin − border − padding` - (不小于 0)。 + `ui_flexbox_record_width_constraint()` 为 in-flow 且 `width:auto` 或百分比宽度的 + 子项记录。晚于 `reset_layout` 是为了避免读到上一轮布局写入的 `margin:auto` + 实际值。 +- 值:由 `ui_flexbox_resolve_measure_width()` 只解析一次。auto 按有效 align 与 + auto-margin 规则选择 stretch 可用宽或 fit-content;百分比先由 `compute_style` + 解析成固定 CSS 宽。两者随后都经 `ui_widget_fix_width()` 应用 min/max,再换算成 + 非负的子项内容宽。该值同时用于组合测量、嵌套 reflow、used width 落地和 + `measured_content_width`,不得在后续路径另算另一套宽度。 - 门控:仅当容器交叉轴(宽)为确定值(`IS_CSS_FIXED_LENGTH(width)`)。 - 版本:`generation = 容器 layout_generation`(每次 reflow 递增)。 ### 读取 - 文本 `sizehint` 的宽优先级:自身固定宽 > 有效约束 > `max-width` > 不限。 -- `ui_flexbox_compute_column_item_flex_basis()` 用有效约束宽现算折行高。 +- `ui_flexbox_compute_column_item_flex_basis()` 用有效约束宽现算折行高,适用于 + `width:auto` 和百分比宽度的 flex 容器子项。按 max-content 宽度得到的 + `unconstrained_content_height` 可能少算折行,固定高子项的文本 sizehint 也可能小于 + 声明高度;两者都不能直接当作 stacked children 的完整 outer height,否则父容器 + 高度偏小、子项被 flex shrink 压扁。 +- 无回流的组合测量快路径先对每个子项调用 `ui_widget_reset_width()` 和 + `ui_widget_reset_height()`,再读取 margin、盒模型或 sizehint。这样固定高使用恢复 + 后的声明高度而不是上一轮 shrink 结果,auto margin 也不会混入内在高度。 +- 快路径按子项 outer height 累加:声明固定高时经 `fix_height` 和盒模型换算, + auto 高时才使用内容 sizehint。返回值与 reflow 路径一致,都是容器的 content-box + 高度;容器自身 padding/border 在 flex basis 换算时统一加入。 +- auto 宽子项的“已测量”提前返回也必须比较当前约束宽与缓存宽;只有同宽结果 + 才能复用,不能在宽度解析变化后继续返回旧高度。 +- `ui_flexbox_item_max_main_size()` 在同宽测量有效时同样使用 + `measured_content_height`,不得在内容收起后再用更大的旧 + `unconstrained_content_height` 充当主轴尺寸下限。 ### 失效 @@ -78,8 +106,11 @@ 2. **`ui_flexbox_layout`**: - `break_into_lines`:按主轴可用尺寸分行(非内在路径)。 - `resolve_main_sizes`:分配主轴空间(grow/shrink/auto margin),只写尺寸。 - - `resolve_cross_sizes`:固化交叉尺寸、定位;把宽高合并为一次条件 reflow; - 流外子项按文档顺序在各趟逐项布局。 + - `resolve_cross_sizes`:固化交叉尺寸、定位;把宽高合并为一次条件 reflow。 + - `ui_flexbox_layout_out_of_flow`:主布局结束后定位绝对定位子项;静态位置按 + “容器中唯一 flex item”计算(`justify-content` + `align-items`)。 + - `display:none` 子项在收集阶段直接跳过,不进入 `line->items`,gap、justify + 和分行都只统计流内子项。 ### 3.3 reflow 传播 @@ -87,6 +118,8 @@ (经 `ui_style_diff_end` 的 `inner_changed`)。 - 挂载(`ui_widget_append`):只 `request_reflow(parent)` + 递归刷新子孙**样式**; 子孙 reflow 依赖样式差异间接触发,或父级重排时被动布局。 +- block 定位阶段遇到百分比 `height`、`min-height` 或 `max-height` 时都会重置高度 + 并条件 reflow,确保父级内容高变化后,百分比最小/最大高度也重新解析。 ## 4. 不变量清单 @@ -100,6 +133,8 @@ | I6 | 居中 auto 宽子项按 fit-content 钳制 | `ui_flexbox_fit_content_width` | 单行溢出容器 | | I7 | fit-content 合并:自有 `sizehint` 与子项布局尺寸按轴取 `max`;`sizehint` 始终可调用,零值合法;`sizehint_exact` 仅用于组合测量快路径 | `ui_flexbox_fit_content_width`、`ui_widget_sizehint_is_exact` | button 等 flex 容器被填满;零宽内容读到过期缓存 | | I8 | 独立 reflow 时从约束重推导交叉轴宽 | `ui_flexbox_reflow` 入口 | hover/路由切换后塌成单行 | +| I9 | 测量前恢复子项声明的宽高和双向 margin | `ui_flexbox_try_measure_container_height` | shrink 高度、旧 auto margin 污染内在高度 | +| I10 | 测量宽只解析一次,快路径、reflow、缓存键共用 | `ui_flexbox_resolve_measure_width` | 同结构因路径不同而折行/父高不一致 | ## 5. 已知缺口 / deferred @@ -117,6 +152,7 @@ | 规则 | 测试 | |---|---| | 换行 / 约束 | `flex layout` → `flex column text wrap`、`constraint refresh` | +| 百分比 / auto 宽度 | `css computed` → `percentage width with indefinite parent`;`home layout` | | 零尺寸内容 | `zero height text ignores stale cache` | | 单独重排稳定 | `standalone reflow stays wrapped`、`empty description reflow stable` | | 重新挂载 / row grow 宽 | `field checkbox description stable`(remount、field-content standalone reflow) | @@ -128,3 +164,12 @@ | sizehint 能力 | `sizehint`(`sizehint capability`、`text single line hint`、`nested container height`) | | flex 语义契约 | `flex contract`(`F01`–`F19`、`C02`–`C10`) | | widget 场景 | `empty layout`(`empty basic`)、`field layout`(`field choice card`、`field checkbox row`) | +| 百分比父级 / 固定高子项 | `gallery layout`(`gallery empty hover`)、`card`(`card basic header`) | +| shrink / auto margin 重测 | `flex layout`(`fixed height intrinsic restoration`、`auto margin intrinsic height`) | +| 测量路径与盒模型一致 | `flex layout`(`measurement path width consistency`、`percentage measurement constraints`) | +| 百分比高度失效 | `flex layout`(`browser layout`)、`block layout` | +| Home clip-safe 间距 | `gallery layout`(`clip-safe body should keep ... padding`) | +| block 内容展开/收起 | `gallery layout`(`gallery accordion resize`) | +| text sizehint / hover 绘制稳定 | `gallery layout`(`production description hover`) | +| 捕获 scale / 文本完整性 | `shimmer`(`shimmer text capture`) | +| Home 首挂与重挂布局 | `gallery layout`(`gallery initial mount`、`gallery remount`)、`production empty icon` | diff --git a/lib/css/src/computed.c b/lib/css/src/computed.c index 8319becb..14e14df2 100644 --- a/lib/css/src/computed.c +++ b/lib/css/src/computed.c @@ -1188,11 +1188,25 @@ static void compute_absolute_width(const css_computed_style_t *parent, if (unit != CSS_UNIT_PERCENT) { break; } - if (!parent || - !compute_content_box_fixed_width(parent, &parent_value)) { + if (!parent) { s->type_bits.width = CSS_WIDTH_FIT_CONTENT; break; } + if (!compute_content_box_fixed_width(parent, &parent_value)) { + /* A percentage width against an indefinite containing + * block behaves like auto for in-flow block boxes: + * the parent layout resolves it. Inline and + * out-of-flow boxes shrink to fit instead. Leaving + * the raw percentage number as pixels would size the + * box to e.g. 100px and make the subtree flicker. */ + if (is_css_display_inline(s) || + css_computed_position(s) > CSS_POSITION_RELATIVE) { + s->type_bits.width = CSS_WIDTH_FIT_CONTENT; + } else { + s->type_bits.width = CSS_WIDTH_AUTO; + } + break; + } if (css_computed_position(s) > CSS_POSITION_RELATIVE) { parent_value += css_padding_x(parent); } diff --git a/lib/css/tests/test_css_computed.c b/lib/css/tests/test_css_computed.c index b35ad2ca..d7b3cc41 100644 --- a/lib/css/tests/test_css_computed.c +++ b/lib/css/tests/test_css_computed.c @@ -10,6 +10,7 @@ */ #include +#include #include "test.h" #include "ctest.h" #include "../include/css.h" @@ -266,6 +267,34 @@ void test_flex_gap_percentage(void) css_computed_style_destroy(&computed); } +static void test_percentage_width_indefinite_parent(void) +{ + css_computed_style_t parent; + css_computed_style_t child; + css_metrics_t metrics; + + memset(&parent, 0, sizeof(parent)); + memset(&metrics, 0, sizeof(metrics)); + parent.type_bits.display = CSS_DISPLAY_BLOCK; + parent.type_bits.width = CSS_WIDTH_AUTO; + + memset(&child, 0, sizeof(child)); + child.type_bits.display = CSS_DISPLAY_BLOCK; + child.type_bits.width = CSS_WIDTH_SET; + child.unit_bits.width = CSS_UNIT_PERCENT; + child.width = 100; + + css_compute_absolute_values(&parent, &child, &metrics); + ctest_equal_int("percentage against an auto parent becomes auto", + child.type_bits.width, CSS_WIDTH_AUTO); + + child.type_bits.width = CSS_WIDTH_SET; + child.type_bits.display = CSS_DISPLAY_INLINE; + css_compute_absolute_values(&parent, &child, &metrics); + ctest_equal_int("inline percentage becomes fit-content", + child.type_bits.width, CSS_WIDTH_FIT_CONTENT); +} + void test_css_computed(void) { size_t n; @@ -299,6 +328,8 @@ void test_css_computed(void) 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); + ctest_describe("percentage width with indefinite parent", + test_percentage_width_indefinite_parent); css_destroy(); } diff --git a/lib/ui/src/ui_block_layout.c b/lib/ui/src/ui_block_layout.c index 0bc5f803..e3c0525d 100644 --- a/lib/ui/src/ui_block_layout.c +++ b/lib/ui/src/ui_block_layout.c @@ -345,7 +345,11 @@ static void ui_block_layout_update(ui_block_layout_context_t *ctx) continue; } if (IS_CSS_PERCENTAGE(&child->specified_style, - height)) { + height) || + IS_CSS_PERCENTAGE(&child->specified_style, + min_height) || + IS_CSS_PERCENTAGE(&child->specified_style, + max_height)) { ui_widget_reset_height(child); ui_widget_reflow_if_height_changed(child); } diff --git a/lib/ui/src/ui_diff.c b/lib/ui/src/ui_diff.c index afcb10e1..46d00fa9 100644 --- a/lib/ui/src/ui_diff.c +++ b/lib/ui/src/ui_diff.c @@ -188,6 +188,92 @@ static void ui_style_diff_update_dependent_children(ui_style_diff_t *diff, diff->has_style_snapshot = false; } +bool ui_style_diff_has_layout_change(ui_style_diff_t *diff, ui_widget_t *w) +{ + css_computed_style_t *a = &diff->style; + css_computed_style_t *b = &w->computed_style; + + return a->type_bits.display != b->type_bits.display || + a->type_bits.position != b->type_bits.position || + a->type_bits.box_sizing != b->type_bits.box_sizing || + a->type_bits.width != b->type_bits.width || + a->unit_bits.width != b->unit_bits.width || + a->width != b->width || + a->type_bits.height != b->type_bits.height || + a->unit_bits.height != b->unit_bits.height || + a->height != b->height || + a->type_bits.min_width != b->type_bits.min_width || + a->unit_bits.min_width != b->unit_bits.min_width || + a->min_width != b->min_width || + a->type_bits.min_height != b->type_bits.min_height || + a->unit_bits.min_height != b->unit_bits.min_height || + a->min_height != b->min_height || + a->type_bits.max_width != b->type_bits.max_width || + a->unit_bits.max_width != b->unit_bits.max_width || + a->max_width != b->max_width || + a->type_bits.max_height != b->type_bits.max_height || + a->unit_bits.max_height != b->unit_bits.max_height || + a->max_height != b->max_height || + a->type_bits.margin_left != b->type_bits.margin_left || + a->unit_bits.margin_left != b->unit_bits.margin_left || + a->margin_left != b->margin_left || + a->type_bits.margin_right != b->type_bits.margin_right || + a->unit_bits.margin_right != b->unit_bits.margin_right || + a->margin_right != b->margin_right || + a->type_bits.margin_top != b->type_bits.margin_top || + a->unit_bits.margin_top != b->unit_bits.margin_top || + a->margin_top != b->margin_top || + a->type_bits.margin_bottom != b->type_bits.margin_bottom || + a->unit_bits.margin_bottom != b->unit_bits.margin_bottom || + a->margin_bottom != b->margin_bottom || + a->type_bits.padding_left != b->type_bits.padding_left || + a->unit_bits.padding_left != b->unit_bits.padding_left || + a->padding_left != b->padding_left || + a->type_bits.padding_right != b->type_bits.padding_right || + a->unit_bits.padding_right != b->unit_bits.padding_right || + a->padding_right != b->padding_right || + a->type_bits.padding_top != b->type_bits.padding_top || + a->unit_bits.padding_top != b->unit_bits.padding_top || + a->padding_top != b->padding_top || + a->type_bits.padding_bottom != b->type_bits.padding_bottom || + a->unit_bits.padding_bottom != b->unit_bits.padding_bottom || + a->padding_bottom != b->padding_bottom || + a->type_bits.border_top_style != b->type_bits.border_top_style || + a->unit_bits.border_top_width != b->unit_bits.border_top_width || + a->border_top_width != b->border_top_width || + a->type_bits.border_right_style != + b->type_bits.border_right_style || + a->unit_bits.border_right_width != + b->unit_bits.border_right_width || + a->border_right_width != b->border_right_width || + a->type_bits.border_bottom_style != + b->type_bits.border_bottom_style || + a->unit_bits.border_bottom_width != + b->unit_bits.border_bottom_width || + a->border_bottom_width != b->border_bottom_width || + a->type_bits.border_left_style != + b->type_bits.border_left_style || + a->unit_bits.border_left_width != + b->unit_bits.border_left_width || + a->border_left_width != b->border_left_width || + a->type_bits.flex_direction != b->type_bits.flex_direction || + a->type_bits.flex_wrap != b->type_bits.flex_wrap || + a->type_bits.justify_content != b->type_bits.justify_content || + a->type_bits.align_content != b->type_bits.align_content || + a->type_bits.align_items != b->type_bits.align_items || + a->flex_grow != b->flex_grow || + a->flex_shrink != b->flex_shrink || + a->type_bits.flex_basis != b->type_bits.flex_basis || + a->unit_bits.flex_basis != b->unit_bits.flex_basis || + a->flex_basis != b->flex_basis || + a->type_bits.row_gap != b->type_bits.row_gap || + a->unit_bits.row_gap != b->unit_bits.row_gap || + a->row_gap != b->row_gap || + a->type_bits.column_gap != b->type_bits.column_gap || + a->unit_bits.column_gap != b->unit_bits.column_gap || + a->column_gap != b->column_gap; +} + void ui_style_diff_end(ui_style_diff_t *diff, ui_widget_t *w) { ui_mutation_record_t *record; diff --git a/lib/ui/src/ui_diff.h b/lib/ui/src/ui_diff.h index 8ce32dc4..155e24ca 100644 --- a/lib/ui/src/ui_diff.h +++ b/lib/ui/src/ui_diff.h @@ -34,4 +34,5 @@ typedef struct ui_widget_style_diff_t_ { void ui_style_diff_init(ui_style_diff_t *diff, ui_widget_t *w); void ui_style_diff_begin(ui_style_diff_t *diff, ui_widget_t *w); +bool ui_style_diff_has_layout_change(ui_style_diff_t *diff, ui_widget_t *w); void ui_style_diff_end(ui_style_diff_t *diff, ui_widget_t *w); diff --git a/lib/ui/src/ui_flexbox_layout.c b/lib/ui/src/ui_flexbox_layout.c index 43f357a5..a02f1612 100644 --- a/lib/ui/src/ui_flexbox_layout.c +++ b/lib/ui/src/ui_flexbox_layout.c @@ -66,6 +66,8 @@ typedef struct ui_flexbox_context { /** list_t lines */ list_t lines; + /** list_t absolute children */ + list_t out_of_flow_items; } ui_flexbox_context_t; typedef struct ui_flexbox_main_contribution { @@ -274,6 +276,7 @@ static float ui_flexbox_try_measure_container_height(ui_widget_t *item, css_computed_style_t *s = &item->computed_style; ui_sizehint_t child_hint; float child_avail; + float child_height; float height = 0; unsigned count = 0; @@ -286,6 +289,10 @@ static float ui_flexbox_try_measure_container_height(ui_widget_t *item, if (!ui_widget_in_layout_flow(child)) { continue; } + /* 先清除上一轮布局写入的宽高与 auto margin,再读取 + * 盒模型和 sizehint,避免把 used size 当作内在尺寸。 */ + ui_widget_reset_width(child); + ui_widget_reset_height(child); child_avail = avail_width - css_margin_x(&child->computed_style) - css_border_x(&child->computed_style) - @@ -298,19 +305,33 @@ static float ui_flexbox_try_measure_container_height(ui_widget_t *item, } ui_widget_set_available_width_constraint( child, child_avail, item, item->layout_generation); - /* 清除上次布局写入的固定宽,让 sizehint 使用新约束。 */ - ui_widget_reset_width(child); ui_widget_get_sizehint(child, &child_hint); + if (IS_CSS_FIXED_LENGTH(&child->specified_style, height)) { + /* sizehint 描述文本等内容层,不包含声明的固定 + * height。固定高子项必须按 used outer height 计入, + * 否则 32px 按钮会被误算成 20px 文本行。 */ + child_height = css_obox_height( + &child->computed_style, + ui_widget_fix_height(child, + child->computed_style.height)); + } else { + child_height = css_obox_height( + &child->computed_style, + css_height_from_cbox(&child->computed_style, + child_hint.max_height)); + } if (count > 0) { height += s->row_gap; } - height += child_hint.max_height; + height += child_height; count++; } if (count == 0) { return -1; } - height += css_padding_y(s) + css_border_y(s); + /* 与 reflow 路径的 measured_content_height 一样,这里返回容器 + * content-box 高度;父级的 padding/border 由 flex basis 的盒模型 + * 换算统一加入,不能在此重复计算。 */ return height; } @@ -319,30 +340,36 @@ static float ui_flexbox_try_measure_container_height(ui_widget_t *item, * * 普通容器(如 empty-header)的原型 sizehint 为空操作,其高度取决于子内容 * 在约束宽度下的折行;在 definite 宽度下回流一次即可让其子内容收到约束并 - * 折行,从而得到正确的高度。宽度按 fit-content 钳制并写入 computed_style, - * 后续交叉趟复用该宽度、不再重复测量。 */ + * 折行,从而得到正确的高度。avail_width 已在约束记录阶段按最终交叉轴 + * 规则解析,快路径与 reflow 共用该宽度。 */ static float ui_flexbox_measure_item_height_at_width(ui_widget_t *item, float avail_width) { css_computed_style_t *s = &item->computed_style; float content_width; - /* 本次父布局已在早前一趟测量过(宽度已固定):直接复用结果, - * 避免对后代再触发一次回流,维持“每次父布局至多回流一次”。 */ - if (IS_CSS_FIXED_LENGTH(s, width)) { - return item->content_box.height; + /* Auto 宽度只会在本函数中被写成固定值,因此固定宽可作为 + * “本次父布局已测量”的标记,避免后续 basis 趟再次回流。 */ + if (item->specified_style.type_bits.width == CSS_WIDTH_AUTO && + IS_CSS_FIXED_LENGTH(s, width) && item->has_measured && + fabsf(item->measured_content_width - avail_width) < 0.01f) { + return item->measured_content_height; } - content_width = ui_flexbox_fit_content_width(item, avail_width); - /* 约束宽未变、且无待重排:复用缓存(含同轮与跨轮), - * 避免从根到叶逐级重复测量子孙。 */ - if (!item->update.should_reflow && item->has_measured && + /* 约束记录阶段已经按最终交叉轴规则解析内容宽,快路径、 + * reflow 与缓存键都直接使用同一个值。 */ + content_width = avail_width; + /* 请求重排时已按依赖链清除缓存,因此这里只按测量宽度命中。 + * 不能检查 should_reflow:该标志在父级嵌套测量的各 basis 趟之间 + * 仍然置位,会把刚得到的同轮结果误判为过期。 */ + if (item->has_measured && fabsf(item->measured_content_width - content_width) < 0.01f) { return item->measured_content_height; } /* 优先用 sizehint 组合测量,避免嵌套 reflow。 */ { float no_reflow_height = - ui_flexbox_try_measure_container_height(item, avail_width); + ui_flexbox_try_measure_container_height(item, + content_width); if (no_reflow_height >= 0) { item->measured_content_width = content_width; @@ -385,14 +412,16 @@ static void ui_compute_column_item_flex_basis(ui_widget_t *item) own_height = hint.max_height; if (has_constraint && ui_widget_sizehint_is_exact(item)) { intrinsic = own_height; - } else if (has_constraint && ui_widget_has_flex_display(item) && - item->specified_style.type_bits.width == - CSS_WIDTH_AUTO && - item->parent && - item->parent->computed_style.type_bits.align_items == - CSS_ALIGN_ITEMS_CENTER) { - /* 居中的 flex 容器子项会被钳制到可用宽度,其高度 - * 需在该宽度下测量,使折行高度向上传播。 */ + } else if (has_constraint && + (ui_widget_has_flex_display(item) || + ui_widget_has_block_display(item)) && + (item->specified_style.type_bits.width == + CSS_WIDTH_AUTO || + IS_CSS_PERCENTAGE(&item->specified_style, width))) { + /* Auto/percentage-width block/flex children have a + * definite cross size in this column. Measure their + * stacked children at that width instead of reusing + * the unconstrained height from an earlier layout. */ intrinsic = y_max( own_height, ui_flexbox_measure_item_height_at_width( item, avail_width)); @@ -444,6 +473,17 @@ static float ui_flexbox_item_max_main_size(ui_flexbox_context_t *ctx, css_computed_style_t *cs = &item->computed_style; if (ctx->column_direction) { + float avail_width; + + if (item->has_measured && + ui_widget_get_available_width(item, &avail_width) && + fabsf(item->measured_content_width - avail_width) < 0.01f) { + /* 同宽测量是当前的内在高度;旧的无约束缓存可能 + * 更大,不能在内容收起后再次把它作为下限。 */ + return css_obox_height( + cs, css_height_from_cbox( + cs, item->measured_content_height)); + } return css_obox_height( cs, css_height_from_cbox( cs, item->unconstrained_content_height)); @@ -712,26 +752,59 @@ static void ui_apply_column_item_main_size(ui_widget_t *item, float flex_space, * 先记录宽度约束,再按文档顺序测量各流内子项;内在分行路径由 * ui_flexbox_collect_intrinsic_lines 单独处理。 */ -/* 为单个 in-flow 且宽度 auto 的子项记录父级可用宽约束。 +/* 解析子项用于内在测高的内容宽。百分比先由 compute_style 解析为 + * CSS width;auto 则按交叉轴规则选择 stretch 或 fit-content。随后与 + * used width 一样应用 min/max,最后换算为内容宽,确保后续各测量路径 + * 与实际盒模型提交使用同一输入。 */ +static float ui_flexbox_resolve_measure_width(ui_flexbox_context_t *ctx, + ui_widget_t *item) +{ + css_computed_style_t *s = &item->computed_style; + css_computed_style_t *specified = &item->specified_style; + float avail; + float width; + + if (IS_CSS_PERCENTAGE(specified, width)) { + width = s->width; + } else { + avail = ctx->widget->content_box.width - css_margin_x(s) - + css_border_x(s) - css_padding_x(s); + if (avail < 0) { + avail = 0; + } + if (ctx->widget->computed_style.type_bits.align_items == + CSS_ALIGN_ITEMS_STRETCH && + s->type_bits.margin_left != CSS_MARGIN_AUTO && + s->type_bits.margin_right != CSS_MARGIN_AUTO) { + width = css_width_from_cbox(s, avail); + } else { + width = css_width_from_cbox( + s, ui_flexbox_fit_content_width(item, avail)); + } + } + width = ui_widget_fix_width(item, width); + width = css_width_to_content_box_width(s, width); + return y_max(0, width); +} + +/* 为单个 in-flow 且宽度 auto/percentage 的子项记录父级可用宽约束。 * 必须在 ui_widget_reset_layout(child) 之后调用,避免读到上一次布局 * 写入的 auto margin。 */ static void ui_flexbox_record_width_constraint(ui_flexbox_context_t *ctx, ui_widget_t *child) { - css_computed_style_t *cs; float avail; + bool auto_width = + child->specified_style.type_bits.width == CSS_WIDTH_AUTO; + bool percentage_width = + IS_CSS_PERCENTAGE(&child->specified_style, width); if (!ctx->column_direction || !IS_CSS_FIXED_LENGTH(&ctx->widget->computed_style, width) || - child->specified_style.type_bits.width != CSS_WIDTH_AUTO) { + (!auto_width && !percentage_width)) { return; } - cs = &child->computed_style; - avail = ctx->widget->content_box.width - css_margin_x(cs) - - css_border_x(cs) - css_padding_x(cs); - if (avail < 0) { - avail = 0; - } + avail = ui_flexbox_resolve_measure_width(ctx, child); ui_widget_set_available_width_constraint( child, avail, ctx->widget, ctx->widget->layout_generation); } @@ -750,11 +823,11 @@ static void ui_flexbox_collect_intrinsic_lines(ui_flexbox_context_t *ctx) for (list_each(node, &ctx->widget->children)) { child = node->data; cs = &child->computed_style; - if (!ui_widget_in_layout_flow(child)) { - if (!line) { - line = ui_flexbox_next_line(ctx); - } - list_append(&line->items, child); + if (ui_widget_is_display_none(child)) { + continue; + } + if (ui_widget_has_absolute_position(child)) { + list_append(&ctx->out_of_flow_items, child); continue; } ui_widget_reset_layout(child); @@ -1046,14 +1119,18 @@ static void ui_flexbox_break_into_lines(ui_flexbox_context_t *ctx) line = ui_flexbox_next_line(ctx); for (list_each(node, &ctx->widget->children)) { child = node->data; - if (!ui_widget_in_layout_flow(child)) { + if (ui_widget_is_display_none(child)) { + child_index++; + continue; + } + if (ui_widget_has_absolute_position(child)) { UI_DEBUG_BEGIN; UI_WIDGET_STR(child, str); - UI_DEBUG_MSG("line[%zu]: children[%u]=%s, skip", - line->index, child_index, str); + UI_DEBUG_MSG("children[%u]=%s, out-of-flow", + child_index, str); child_index++; UI_DEBUG_END; - list_append(&line->items, child); + list_append(&ctx->out_of_flow_items, child); continue; } cs = &child->computed_style; @@ -1074,10 +1151,11 @@ static void ui_flexbox_break_into_lines(ui_flexbox_context_t *ctx) ctx->max_main_size) { line = ui_flexbox_next_line(ctx); } - if (line->items.length > 0) { + if (line->count_of_in_flow_items > 0) { line->main_size += ctx->main_gap; } line->main_size += main_size; + line->count_of_in_flow_items++; ui_flexbox_line_add_auto_margin_count(ctx, line, child); ui_flexbox_line_load_item(line, child); } @@ -1393,6 +1471,86 @@ static void ui_flexbox_measure(ui_flexbox_context_t *ctx) ui_flexbox_resolve_flex_basis(ctx); } +/* 第六趟:layout_out_of_flow,绝对定位子项的静态位置。 + * + * CSS Flexbox §4.1:绝对定位子项不参与 flex 布局,其静态位置按“容器中唯一 + * flex item”计算,使用容器的 justify-content / align-items 和自身 used + * size。这里在主布局结束后统一处理,避免污染 line->items 的 gap/justify + * 计数,也避免在多趟遍历里重复访问。 */ +static float ui_flexbox_static_main_offset(ui_flexbox_context_t *ctx, + float item_size) +{ + css_computed_style_t *s = &ctx->widget->computed_style; + float start = ctx->column_direction ? s->padding_top : s->padding_left; + float free = (ctx->column_direction ? ctx->widget->content_box.height + : ctx->widget->content_box.width) - + item_size; + + if (free < 0) { + free = 0; + } + switch (s->type_bits.justify_content) { + case CSS_JUSTIFY_CONTENT_FLEX_END: + return start + free; + case CSS_JUSTIFY_CONTENT_CENTER: + case CSS_JUSTIFY_CONTENT_SPACE_AROUND: + case CSS_JUSTIFY_CONTENT_SPACE_EVENLY: + return start + free * 0.5f; + default: + /* flex-start, and space-between with a single item. */ + return start; + } +} + +static float ui_flexbox_static_cross_offset(ui_flexbox_context_t *ctx, + float item_size) +{ + css_computed_style_t *s = &ctx->widget->computed_style; + float start = ctx->column_direction ? s->padding_left : s->padding_top; + float free = (ctx->column_direction ? ctx->widget->content_box.width + : ctx->widget->content_box.height) - + item_size; + + if (free < 0) { + free = 0; + } + switch (s->type_bits.align_items) { + case CSS_ALIGN_ITEMS_FLEX_END: + return start + free; + case CSS_ALIGN_ITEMS_CENTER: + return start + free * 0.5f; + default: + /* stretch / baseline / flex-start. */ + return start; + } +} + +static void ui_flexbox_layout_out_of_flow(ui_flexbox_context_t *ctx) +{ + list_node_t *node; + ui_widget_t *item; + float main_size; + float cross_size; + float main_axis; + float cross_axis; + + for (list_each(node, &ctx->out_of_flow_items)) { + item = node->data; + ui_widget_reset_layout(item); + ui_widget_reflow_if_width_changed(item); + ui_widget_reflow_if_height_changed(item); + main_size = ctx->column_direction ? item->outer_box.height + : item->outer_box.width; + cross_size = ctx->column_direction ? item->outer_box.width + : item->outer_box.height; + main_axis = ui_flexbox_static_main_offset(ctx, main_size); + cross_axis = ui_flexbox_static_cross_offset(ctx, cross_size); + item->layout_x = ctx->column_direction ? cross_axis : main_axis; + item->layout_y = ctx->column_direction ? main_axis : cross_axis; + ui_widget_update_box_position(item); + } +} + /* 布局阶段:分行、分配主轴、分配交叉并定位。 */ static void ui_flexbox_layout(ui_flexbox_context_t *ctx) { @@ -1413,6 +1571,7 @@ void ui_flexbox_reflow(ui_widget_t *w, ui_resizer_t *resizer) ctx.resizer = resizer; ctx.column_direction = ui_widget_has_flex_column_direction(w); list_create(&ctx.lines); + list_create(&ctx.out_of_flow_items); /* 每次回流递增布局代数,使旧一轮记录的可用宽度约束失效, * 本轮的约束记录以新代数为准。 */ w->layout_generation++; @@ -1477,7 +1636,9 @@ void ui_flexbox_reflow(ui_widget_t *w, ui_resizer_t *resizer) ui_flexbox_measure(&ctx); /* 布局阶段:分行、分配主轴、分配交叉并定位。 */ ui_flexbox_layout(&ctx); + ui_flexbox_layout_out_of_flow(&ctx); w->proto->resize(w, w->content_box.width, w->content_box.height); + list_destroy(&ctx.out_of_flow_items, NULL); list_destroy(&ctx.lines, ui_flexbox_line_destroy); UI_DEBUG_BEGIN; UI_DEBUG_INDENT_DEC; diff --git a/lib/ui/src/ui_updater.c b/lib/ui/src/ui_updater.c index e582da45..49e007a7 100644 --- a/lib/ui/src/ui_updater.c +++ b/lib/ui/src/ui_updater.c @@ -90,8 +90,19 @@ void ui_widget_request_update(ui_widget_t *w) void ui_widget_request_reflow(ui_widget_t *w) { + ui_widget_t *ancestor; + w->update.should_reflow = true; ui_widget_request_update(w); + /* A reflow request can change the intrinsic height cached on the + * widget itself or on any ancestor that contains it. Invalidate by + * dependency rather than by the transient should_reflow flag: the + * flag remains set while the parent performs its nested measurement, + * but a fresh same-width result must still be reusable in later flex + * basis passes. */ + for (ancestor = w; ancestor; ancestor = ancestor->parent) { + ancestor->has_measured = false; + } } void ui_widget_set_rules(ui_widget_t *w, const ui_widget_rules_t *rules) @@ -442,7 +453,10 @@ size_t ui_updater_update_widget(ui_updater_t *updater, ui_widget_t *w) } #endif ui_widget_update_style(w); - ui_widget_update_box_size(w); + if (updater->refresh_all || + ui_style_diff_has_layout_change(&style_diff, w)) { + ui_widget_update_box_size(w); + } ui_widget_update_box_position(w); ui_style_diff_end(&style_diff, w); if (w->proto && w->proto->update) { diff --git a/lib/ui/src/ui_widget.c b/lib/ui/src/ui_widget.c index cefcb342..72a85522 100644 --- a/lib/ui/src/ui_widget.c +++ b/lib/ui/src/ui_widget.c @@ -326,8 +326,6 @@ void ui_widget_update_box_position(ui_widget_t *w) w->border_box.width; } x -= s->right; - } else { - x = 0; } if (IS_CSS_FIXED_LENGTH(s, top)) { y = s->top; @@ -337,8 +335,6 @@ void ui_widget_update_box_position(ui_widget_t *w) w->border_box.height; } y -= s->bottom; - } else { - y = 0; } break; case CSS_POSITION_RELATIVE: diff --git a/lib/ui/src/ui_widget_layout.c b/lib/ui/src/ui_widget_layout.c index f66c2beb..99df8c7f 100644 --- a/lib/ui/src/ui_widget_layout.c +++ b/lib/ui/src/ui_widget_layout.c @@ -203,6 +203,7 @@ void ui_widget_reflow_with_resizer(ui_widget_t *w, ui_resizer_t *resizer) str, w->max_content_width, w->max_content_height); } #endif + ui_widget_resolve_background(w); ui_widget_post_event(w, &ev, NULL, NULL); ui_widget_add_state(w, UI_WIDGET_STATE_LAYOUTED); } diff --git a/lib/ui/src/ui_widget_style.c b/lib/ui/src/ui_widget_style.c index a03b3652..7cc9e7c5 100644 --- a/lib/ui/src/ui_widget_style.c +++ b/lib/ui/src/ui_widget_style.c @@ -9,6 +9,8 @@ * in the LICENSE.TXT file in the root directory of this source tree. */ +#include +#include #include #include #include @@ -320,14 +322,32 @@ void ui_widget_set_style_keyword_value(ui_widget_t *w, int key, ui_widget_set_style(w, key, &v); } -static void ui_widget_on_image_load(ui_image_event_t *e) +void ui_widget_resolve_background(ui_widget_t *w) { float scale; - ui_widget_t *w = e->data; - pd_canvas_t *img = &e->image->data; + float old_width; + float old_height; + float old_x; + float old_y; + ui_image_t *image; + pd_canvas_t *img; css_computed_style_t *s = &w->computed_style; ui_rect_t *box; + if (!s->background_image) { + return; + } + image = ui_get_image(s->background_image); + if (!image || image->state != UI_IMAGE_STATE_COMPLETE || + image->error != PD_OK) { + return; + } + img = &image->data; + old_width = s->background_width; + old_height = s->background_height; + old_x = s->background_position_x; + old_y = s->background_position_y; + switch (s->type_bits.background_clip) { case CSS_BACKGROUND_CLIP_PADDING_BOX: box = &w->padding_box; @@ -392,7 +412,7 @@ static void ui_widget_on_image_load(ui_image_event_t *e) s->background_width * img->height / img->width); } // See more: - // https://developer.mozilla.org/en-US/docs/Web/CSS/background-position#regarding_percentages + // https://developer.mozilla.org/en-US/docs/Web/CSS/background-position#regarding-percentages if (IS_CSS_PERCENTAGE(s, background_position_x)) { CSS_SET_FIXED_LENGTH(s, background_position_x, (box->width - s->background_width) * @@ -403,7 +423,17 @@ static void ui_widget_on_image_load(ui_image_event_t *e) (box->height - s->background_height) * s->background_position_y / 100.f); } - ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_BORDER_BOX); + if (s->background_width != old_width || + s->background_height != old_height || + s->background_position_x != old_x || + s->background_position_y != old_y) { + ui_widget_mark_dirty_rect(w, NULL, UI_BOX_TYPE_BORDER_BOX); + } +} + +static void ui_widget_on_image_load(ui_image_event_t *e) +{ + ui_widget_resolve_background(e->data); } static void ui_widget_on_image_load_start(ui_image_event_t *e) @@ -459,11 +489,14 @@ void ui_widget_compute_style(ui_widget_t *w) void ui_widget_update_style(ui_widget_t *w) { ui_image_t *image; - css_computed_style_t *s = &w->specified_style; + css_computed_style_t new_style; css_style_decl_t *style; + char *old_background = NULL; + bool reuse_background; - ui_widget_destroy_background_style(w); - css_computed_style_destroy(s); + if (w->computed_style.background_image) { + old_background = y_strdup(w->computed_style.background_image); + } style = css_style_decl_create(); if (w->custom_style) { css_style_decl_merge(style, w->custom_style); @@ -471,16 +504,32 @@ void ui_widget_update_style(ui_widget_t *w) css_style_decl_merge(style, w->matched_style); ui_widget_update_style_dependencies(w, style); css_cascade_style_with_parent( - style, w->parent ? &w->parent->specified_style : NULL, s); + style, w->parent ? &w->parent->specified_style : NULL, &new_style); css_style_decl_destroy(style); - w->computed_style = *s; - // 增加 UIImage 的引用次数,让它在部件销毁和背景图改变前一直可用 - if (s->background_image) { - image = ui_image_create(s->background_image); + + /* Rebuild the background image only when the URL changed. Recreating + * it would fire the load handler, which resolves cover geometry from + * the box current at that moment; a style refresh is not a reason to + * throw the crop away. */ + reuse_background = + old_background && new_style.background_image && + strcmp(old_background, new_style.background_image) == 0; + if (!reuse_background) { + ui_widget_destroy_background_style(w); + } + css_computed_style_destroy(&w->specified_style); + w->specified_style = new_style; + w->computed_style = new_style; + if (reuse_background) { + ui_widget_resolve_background(w); + } else if (new_style.background_image) { + // 增加 UIImage 的引用次数,让它在部件销毁和背景图改变前一直可用 + image = ui_image_create(new_style.background_image); ui_image_on_load(image, ui_widget_on_image_load, w); ui_image_on_progress(image, ui_widget_on_image_load_start, w); ui_image_on_progress(image, ui_widget_on_image_progress, w); } + free(old_background); ui_widget_compute_style(w); } diff --git a/lib/ui/src/ui_widget_style.h b/lib/ui/src/ui_widget_style.h index 1eaad87c..e75e96fb 100644 --- a/lib/ui/src/ui_widget_style.h +++ b/lib/ui/src/ui_widget_style.h @@ -66,9 +66,14 @@ LIBUI_INLINE bool ui_widget_has_absolute_position(ui_widget_t *w) CSS_POSITION_ABSOLUTE; } +LIBUI_INLINE bool ui_widget_is_display_none(ui_widget_t *w) +{ + return css_computed_display(&w->computed_style) == CSS_DISPLAY_NONE; +} + LIBUI_INLINE bool ui_widget_in_layout_flow(ui_widget_t *w) { - return css_computed_display(&w->computed_style) != CSS_DISPLAY_NONE && + return !ui_widget_is_display_none(w) && !ui_widget_has_absolute_position(w); } @@ -95,4 +100,6 @@ LIBUI_INLINE bool ui_widget_has_flex_column_direction(ui_widget_t *w) size_t ui_widget_get_children_style_changes(ui_widget_t *w, int type, const char *name); +void ui_widget_resolve_background(ui_widget_t *w); + void ui_widget_destroy_style(ui_widget_t *w); diff --git a/src/widgets/text.c b/src/widgets/text.c index e65f8300..d29057f3 100644 --- a/src/widgets/text.c +++ b/src/widgets/text.c @@ -232,6 +232,23 @@ static void ui_text_on_destroy(ui_widget_t *w) } } +static void ui_text_restore_paint_size(ui_text_t *txt, int width, int height) +{ + list_t rects; + + if (txt->layer->fixed_width == width && + txt->layer->fixed_height == height && + txt->layer->max_width == width && + txt->layer->max_height == height) { + return; + } + pd_text_set_fixed_size(txt->layer, width, height); + pd_text_set_max_size(txt->layer, width, height); + list_create(&rects); + pd_text_update(txt->layer, &rects); + pd_rects_clear(&rects); +} + static void ui_text_on_sizehint(ui_widget_t *w, ui_sizehint_t *hint) { float max_width = 0, max_height = 0, avail_width = 0; @@ -369,6 +386,8 @@ static void ui_text_on_paint(ui_widget_t *w, pd_context_t *paint, ui_text_t *txt = ui_widget_get_data(w, ui_text.prototype); ui_text_sync_layer_scale(w, style); + ui_text_restore_paint_size(txt, (int)style->content_box.width, + (int)style->content_box.height); content_rect.width = style->content_box.width; content_rect.height = style->content_box.height; content_rect.x = style->content_box.x - style->canvas_box.x; diff --git a/tests/fixtures/test_background.css b/tests/fixtures/test_background.css new file mode 100644 index 00000000..4668ee8d --- /dev/null +++ b/tests/fixtures/test_background.css @@ -0,0 +1,25 @@ +/* + * tests/fixtures/test_background.css + * + * Background image geometry: a fixed box with a cover/center image and a + * resizable parent used to check that layout changes re-resolve the crop. + */ + +.test-background-parent { + display: block; + width: 200px; +} + +.test-background-parent-narrow { + width: 100px; +} + +.test-background { + display: block; + width: 100%; + height: 100px; + background-image: url("test_background.png"); + background-size: cover; + background-position: center; + background-repeat: no-repeat; +} diff --git a/tests/fixtures/test_background.png b/tests/fixtures/test_background.png new file mode 100644 index 00000000..cf247fb6 Binary files /dev/null and b/tests/fixtures/test_background.png differ diff --git a/tests/fixtures/test_card.css b/tests/fixtures/test_card.css new file mode 100644 index 00000000..29c9610f --- /dev/null +++ b/tests/fixtures/test_card.css @@ -0,0 +1,32 @@ +/* + * tests/fixtures/test_card.css + * + * The card-basic demo layout. Mirrors the rules that + * examples/doc-viewer/app/global.css scopes under `.card-basic-demo`. + */ + +.test-card-form { + display: flex; + flex-direction: column; + gap: 24px; +} + +.test-card-field { + display: flex; + flex-direction: column; + gap: 8px; +} + +.test-card-label-row { + display: flex; + align-items: center; +} + +.test-card-footer { + flex-direction: column; + gap: 8px; +} + +.test-card-footer button { + width: 100%; +} diff --git a/tests/fixtures/test_empty_layout.css b/tests/fixtures/test_empty_layout.css index b8506bbc..22c4222e 100644 --- a/tests/fixtures/test_empty_layout.css +++ b/tests/fixtures/test_empty_layout.css @@ -70,6 +70,37 @@ gap: 8px; } +.test-empty-hover-box { + display: flex; + flex-direction: column; + width: 300px; +} + +.test-empty-hover-box empty, +.test-empty-hover-box empty-header, +.test-empty-hover-box empty-content, +.test-empty-hover-box button { + flex-shrink: 0; +} + +.test-empty-hover-box empty { + width: 100%; +} + +.test-empty-hover-box empty-header { + width: 100%; +} + +.test-empty-hover-box empty-content { + width: 100%; + align-items: center; +} + +.test-empty-hover-box empty-description { + width: 100%; + text-align: center; +} + .test-action-primary { display: flex; width: 108px; diff --git a/tests/fixtures/test_flex_hidden_gap.css b/tests/fixtures/test_flex_hidden_gap.css new file mode 100644 index 00000000..745af966 --- /dev/null +++ b/tests/fixtures/test_flex_hidden_gap.css @@ -0,0 +1,24 @@ +/* + * tests/fixtures/test_flex_hidden_gap.css + * + * A row with one hidden sibling and one fixed-size visible item. The gap + * must only be inserted between in-flow items. + */ + +.test-hg-row { + display: inline-flex; + flex-direction: row; + gap: 6px; +} + +.test-hg-hidden { + display: none; + width: 33px; + height: 20px; +} + +.test-hg-visible { + width: 28px; + height: 20px; + flex-shrink: 1; +} diff --git a/tests/fixtures/test_home_layout.css b/tests/fixtures/test_home_layout.css new file mode 100644 index 00000000..d1c79e4b --- /dev/null +++ b/tests/fixtures/test_home_layout.css @@ -0,0 +1,76 @@ +/* + * tests/fixtures/test_home_layout.css + * + * Mirrors the doc-viewer home page structure: a scroll area, a centered + * max-width container with padding, and a four-column card gallery. The + * `test-*` classes keep the scenario independent from style-nova. + */ + +.test-home-scroll { + width: 100%; + height: 100%; +} + +.test-home-page { + display: flex; + flex-direction: column; + width: 100%; +} + +.test-home-inner { + display: flex; + flex-direction: column; + width: 100%; + max-width: 1560px; + margin-left: auto; + margin-right: auto; + padding: 72px 48px 96px; +} + +.test-home-grid { + display: flex; + flex-direction: row; + align-items: flex-start; + gap: 24px; + width: 100%; + margin-top: 56px; +} + +.test-home-column { + display: flex; + flex-direction: column; + flex-grow: 0; + flex-shrink: 0; + gap: 24px; + min-width: 0; +} + +.test-home-card { + display: flex; + flex-direction: column; + box-sizing: border-box; + width: 100%; + padding: 24px; + border: 1px solid #e5e5e5; + border-radius: 12px; +} + +.test-home-input { + height: 36px; + border: 1px solid #e5e5e5; + border-radius: 8px; +} + +.test-home-input:focus { + border-color: #111111; +} + +.test-home-page-active { + background: #ffffff; +} + +.test-home-filler { + display: block; + height: 900px; + background: #f7f7f7; +} diff --git a/tests/fixtures/test_style_refresh.css b/tests/fixtures/test_style_refresh.css new file mode 100644 index 00000000..6a5ddc11 --- /dev/null +++ b/tests/fixtures/test_style_refresh.css @@ -0,0 +1,23 @@ +/* + * tests/fixtures/test_style_refresh.css + * + * A plain block with an auto width/height used to check that status-driven + * style refreshes do not temporarily collapse the used box. + */ + +.test-refresh-parent { + display: block; + width: 400px; +} + +.test-refresh-box { + display: block; + padding: 10px; + border: 1px solid #dddddd; +} + +.test-refresh-text { + display: block; + font-size: 14px; + line-height: 20px; +} diff --git a/tests/integration/test_background_style.c b/tests/integration/test_background_style.c new file mode 100644 index 00000000..ee0011d8 --- /dev/null +++ b/tests/integration/test_background_style.c @@ -0,0 +1,145 @@ +/* + * tests/integration/test_background_style.c + * + * Copyright (c) 2026, Liu Chao 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. + */ + +/* + * Background image geometry. `cover` resolves to concrete pixels using the + * widget's used box, so the result must survive style refreshes and be + * re-resolved when layout changes the box without a style refresh. + */ + +#include +#include +#include + +static void settle(void) +{ + int i; + + for (i = 0; i < 40; ++i) { + ui_update(); + ui_load_images(); + } +} + +static ui_widget_t *create_background(ui_widget_t **parent_out) +{ + ui_widget_t *parent = ui_create_widget(NULL); + ui_widget_t *w = ui_create_widget(NULL); + + ui_widget_add_class(parent, "test-background-parent"); + ui_widget_add_class(w, "test-background"); + ui_widget_append(parent, w); + ui_widget_append(ui_root(), parent); + if (parent_out) { + *parent_out = parent; + } + return w; +} + +static void destroy_background(ui_widget_t *w) +{ + ui_widget_destroy(w->parent); + ui_clear_trash(); +} + +static void test_cover_geometry(void) +{ + ui_widget_t *w = create_background(NULL); + + ui_widget_resize(ui_root(), 800, 600); + settle(); + + /* 30x20 image in a 200x100 box: cover scales by max(200/30, 100/20) + * and centers the overflow vertically. */ + ctest_equal_bool( + "cover width fills the box", + fabsf(w->computed_style.background_width - 200.f) < 0.5f, true); + ctest_equal_bool( + "cover height keeps the image aspect", + fabsf(w->computed_style.background_height - 133.33f) < 0.5f, true); + ctest_equal_bool("cover centers vertically", + w->computed_style.background_position_y < -16.f, true); + + destroy_background(w); +} + +static void test_hover_keeps_geometry(void) +{ + ui_widget_t *w = create_background(NULL); + float width; + float height; + float x; + float y; + + ui_widget_resize(ui_root(), 800, 600); + settle(); + width = w->computed_style.background_width; + height = w->computed_style.background_height; + x = w->computed_style.background_position_x; + y = w->computed_style.background_position_y; + + ui_widget_add_status(w, "hover"); + settle(); + + ctest_equal_float("hover keeps the cover width", + w->computed_style.background_width, width); + ctest_equal_float("hover keeps the cover height", + w->computed_style.background_height, height); + ctest_equal_float("hover keeps the horizontal offset", + w->computed_style.background_position_x, x); + ctest_equal_float("hover keeps the vertical offset", + w->computed_style.background_position_y, y); + + destroy_background(w); +} + +static void test_layout_resize_resolves_cover(void) +{ + ui_widget_t *parent = NULL; + ui_widget_t *w = create_background(&parent); + + ui_widget_resize(ui_root(), 800, 600); + settle(); + ui_widget_add_class(parent, "test-background-parent-narrow"); + settle(); + + /* 100x100 box: cover is max(100/30, 100/20) = 5, so the image becomes + * 150x100 and is centered horizontally. Only the parent style + * changed, so this relies on layout re-resolving the background. */ + ctest_equal_bool( + "layout resize recomputes the cover width", + fabsf(w->computed_style.background_width - 150.f) < 0.5f, true); + ctest_equal_bool( + "layout resize recomputes the cover height", + fabsf(w->computed_style.background_height - 100.f) < 0.5f, true); + ctest_equal_bool( + "layout resize recenters horizontally", + fabsf(w->computed_style.background_position_x + 25.f) < 0.5f, true); + ctest_equal_bool("layout resize clears the vertical offset", + fabsf(w->computed_style.background_position_y) < 0.5f, + true); + + destroy_background(w); +} + +void test_background_style(void) +{ + lcui_init(); + ui_load_css_file("test_background.css"); + ui_widget_set_title(ui_root(), L"test background style"); + + ctest_describe("background cover geometry", test_cover_geometry); + ctest_describe("background hover stability", test_hover_keeps_geometry); + ctest_describe("background layout resize", + test_layout_resize_resolves_cover); + + lcui_destroy(); +} diff --git a/tests/integration/test_card.c b/tests/integration/test_card.c index 9199f172..ddc95b6f 100644 --- a/tests/integration/test_card.c +++ b/tests/integration/test_card.c @@ -131,6 +131,107 @@ static void test_layout(void) ui_widget_destroy(card); } +/* Reproduce the card-basic documentation example: the header action shrinks + * the title row to 278px, so the description wraps. The title, description + * and header must keep their line boxes instead of being compressed by the + * card's column layout. The doc-viewer runs at 1.5x, which is where the + * stale card height used to squeeze the header. */ +static ui_widget_t *create_input(const char *id, const char *placeholder, + bool password) +{ + ui_widget_t *input = ui_create_widget("textinput"); + + ui_widget_set_id(input, id); + ui_textinput_set_placeholder(input, placeholder); + if (password) { + ui_textinput_set_password_char(input, L'*'); + } + return input; +} + +static void test_basic_header_metrics(void) +{ + ui_widget_t *card = ui_create_card(); + ui_widget_t *header = ui_create_card_header(); + ui_widget_t *title = ui_create_card_title(); + ui_widget_t *description = ui_create_card_description(); + ui_widget_t *action = ui_create_card_action(); + ui_widget_t *sign_up = ui_create_button(); + ui_widget_t *content = ui_create_card_content(); + ui_widget_t *form = ui_create_widget(NULL); + ui_widget_t *email_field = ui_create_field(); + ui_widget_t *email_label = ui_create_field_label(); + ui_widget_t *password_field = ui_create_field(); + ui_widget_t *password_row = ui_create_widget(NULL); + ui_widget_t *password_label = ui_create_field_label(); + ui_widget_t *forgot = ui_create_button(); + ui_widget_t *footer = ui_create_card_footer(); + ui_widget_t *login = ui_create_button(); + ui_widget_t *google = ui_create_button(); + + ui_load_css_file("test_card.css"); + ui_widget_resize(ui_root(), 1280, 693); + ui_widget_set_style_string(card, "width", "384px"); + + ui_text_set_content(title, "Login to your account"); + ui_text_set_content(description, + "Enter your email below to login to your account"); + ui_widget_append(header, title); + ui_widget_append(header, description); + ui_button_set_text(sign_up, "Sign Up"); + ui_button_set_variant(sign_up, UI_BUTTON_VARIANT_LINK); + ui_widget_append(action, sign_up); + ui_widget_append(header, action); + + ui_widget_add_class(form, "test-card-form"); + ui_widget_add_class(form, "clip-safe"); + ui_field_set_orientation(email_field, "vertical"); + ui_widget_add_class(email_field, "test-card-field"); + ui_text_set_content(email_label, "Email"); + ui_widget_append(email_field, email_label); + ui_widget_append(email_field, create_input("test-card-email", + "m@example.com", false)); + ui_widget_append(form, email_field); + + ui_field_set_orientation(password_field, "vertical"); + ui_widget_add_class(password_field, "test-card-field"); + ui_widget_add_class(password_row, "test-card-label-row"); + ui_text_set_content(password_label, "Password"); + ui_button_set_text(forgot, "Forgot your password?"); + ui_button_set_variant(forgot, UI_BUTTON_VARIANT_LINK); + ui_button_set_size(forgot, UI_BUTTON_SIZE_SM); + ui_widget_append(password_row, password_label); + ui_widget_append(password_row, forgot); + ui_widget_append(password_field, password_row); + ui_widget_append(password_field, + create_input("test-card-password", "", true)); + ui_widget_append(form, password_field); + ui_widget_append(content, form); + ui_widget_add_class(content, "clip-safe"); + + ui_widget_add_class(footer, "test-card-footer"); + ui_button_set_text(login, "Login"); + ui_button_set_variant(login, UI_BUTTON_VARIANT_PRIMARY); + ui_button_set_text(google, "Login with Google"); + ui_button_set_variant(google, UI_BUTTON_VARIANT_OUTLINE); + ui_widget_append(footer, login); + ui_widget_append(footer, google); + + ui_widget_append(card, header); + ui_widget_append(card, content); + ui_widget_append(card, footer); + ui_widget_append(ui_root(), card); + ui_update(); + + ctest_equal_float("title should keep its line box", + title->border_box.height, 22.0f); + ctest_equal_float("description should wrap and keep two line boxes", + description->border_box.height, 40.0f); + ctest_equal_float("header should stack the full line boxes", + header->border_box.height, 66.0f); + + ui_widget_destroy(card); +} void test_card(void) { lcui_init(); @@ -138,6 +239,7 @@ void test_card(void) ctest_describe("card structure", test_structure); ctest_describe("card size", test_size); ctest_describe("card layout", test_layout); + ctest_describe("card basic header", test_basic_header_metrics); lcui_destroy(); } diff --git a/tests/integration/test_empty_layout.c b/tests/integration/test_empty_layout.c index 2e6d44bf..5810277d 100644 --- a/tests/integration/test_empty_layout.c +++ b/tests/integration/test_empty_layout.c @@ -136,6 +136,77 @@ static void test_empty_basic(void) ui_widget_destroy(ref_learn->parent); } +/* The gallery uses the real empty widget inside a column. Its description + * height must come from the wrapped line boxes, and a hover-triggered reflow + * must not change the card height. */ +static void test_empty_hover_stability(void) +{ + static const float widths[] = { 340, 320, 300, 290, 280, 270, 260, + 250, 240, 230, 220, 210, 200 }; + ui_widget_t *box = create_box("test-empty-hover-box"); + ui_widget_t *empty = ui_create_empty(); + ui_widget_t *header = ui_create_empty_header(); + ui_widget_t *description = ui_create_empty_description(); + ui_widget_t *content = ui_create_empty_content(); + ui_widget_t *button = ui_create_button(); + ui_widget_t *w; + float height; + float card_height; + int i, j; + + ui_text_set_content( + description, + "Upload your first master to start reaching listeners on " + "Spotify, Apple Music, and more."); + ui_button_set_text(button, "Create Release"); + ui_button_set_variant(button, UI_BUTTON_VARIANT_PRIMARY); + ui_widget_append(header, description); + ui_widget_append(content, button); + ui_widget_append(empty, header); + ui_widget_append(empty, content); + ui_widget_append(box, empty); + ui_widget_append(ui_root(), box); + ui_widget_resize(ui_root(), 1280, 720); + ui_update(); + ui_update(); + + for (j = 0; j < (int)(sizeof(widths) / sizeof(widths[0])); ++j) { + ui_widget_set_style_unit_value(box, css_prop_width, widths[j], + CSS_UNIT_PX); + for (i = 0; i < 3; ++i) { + ui_update(); + } + height = description->border_box.height; + card_height = box->border_box.height; + ctest_equal_bool("empty description should have a height", + height > 0.0f, true); + + for (w = button; w; w = w->parent) { + ui_widget_add_status(w, "hover"); + } + for (i = 0; i < 3; ++i) { + ui_update(); + } + ctest_equal_float( + "hover should keep the empty description height", + description->border_box.height, height); + ctest_equal_float("hover should keep the empty card height", + box->border_box.height, card_height); + + for (w = button; w; w = w->parent) { + ui_widget_remove_status(w, "hover"); + } + for (i = 0; i < 3; ++i) { + ui_update(); + } + ctest_equal_float( + "leaving hover should keep the empty description height", + description->border_box.height, height); + } + + ui_widget_destroy(box); +} + void test_empty_layout(void) { lcui_init(); @@ -143,6 +214,7 @@ void test_empty_layout(void) ui_widget_set_title(ui_root(), L"test empty layout"); ctest_describe("empty basic", test_empty_basic); + ctest_describe("empty hover stability", test_empty_hover_stability); lcui_destroy(); } diff --git a/tests/integration/test_flex_contract.c b/tests/integration/test_flex_contract.c index bcb46008..239a9673 100644 --- a/tests/integration/test_flex_contract.c +++ b/tests/integration/test_flex_contract.c @@ -715,6 +715,46 @@ static void test_out_of_flow_children(void) ui_widget_destroy(row); } +/* F15: absolute static position follows justify-content / align-items. */ +static void test_out_of_flow_static_position(void) +{ + ui_widget_t *row = create_flex("row", 200, 100); + ui_widget_t *visible = create_block(50, 20); + ui_widget_t *absolute = create_block(20, 10); + + set_style(row, "justify-content", "flex-end"); + set_style(row, "align-items", "center"); + set_style(absolute, "position", "absolute"); + ui_widget_append(row, visible); + ui_widget_append(row, absolute); + append_root(row); + ui_update(); + + ctest_equal_bool("F15 static x follows justify-content flex-end", + close_to(absolute->border_box.x, 180, TOL), true); + ctest_equal_bool("F15 static y follows align-items center", + close_to(absolute->border_box.y, 45, TOL), true); + + set_style(row, "justify-content", "flex-start"); + set_style(row, "align-items", "flex-start"); + ui_update(); + ctest_equal_bool("F15 flex-start static x", + close_to(absolute->border_box.x, 0, TOL), true); + ctest_equal_bool("F15 flex-start static y", + close_to(absolute->border_box.y, 0, TOL), true); + + set_px(absolute, "left", 12); + set_px(absolute, "top", 8); + ui_update(); + ui_update(); + ctest_equal_float("F15 left inset overrides static x", + absolute->border_box.x, 12.f); + ctest_equal_float("F15 top inset overrides static y", + absolute->border_box.y, 8.f); + + ui_widget_destroy(row); +} + /* F16: unset flex-direction inherits the parent direction. */ static void test_direction_inheritance(void) { @@ -1153,6 +1193,8 @@ void test_flex_contract(void) ctest_describe("F13 box-sizing border-box", test_box_sizing_border_box); ctest_describe("F14 inline-flex", test_inline_flex); ctest_describe("F15 out-of-flow children", test_out_of_flow_children); + ctest_describe("F15 absolute static position", + test_out_of_flow_static_position); ctest_describe("F16 direction inheritance", test_direction_inheritance); ctest_describe("F17 repeated updates stable", test_repeated_updates_stable); diff --git a/tests/integration/test_flex_hidden_gap.c b/tests/integration/test_flex_hidden_gap.c new file mode 100644 index 00000000..5fd6c639 --- /dev/null +++ b/tests/integration/test_flex_hidden_gap.c @@ -0,0 +1,86 @@ +/* + * tests/integration/test_flex_hidden_gap.c + * + * Copyright (c) 2026, Liu Chao 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. + */ + +/* + * Hidden children (display: none) must not contribute a flex gap. A row + * that only exposes one fixed-size item must keep that item at its own + * width instead of shrinking it by the gap that would precede a hidden + * sibling. + */ + +#include +#include + +static ui_widget_t *create_box(const char *class_name) +{ + ui_widget_t *w = ui_create_widget(NULL); + + ui_widget_add_class(w, class_name); + return w; +} + +static void test_hidden_sibling_gap(void) +{ + ui_widget_t *row = create_box("test-hg-row"); + ui_widget_t *hidden = create_box("test-hg-hidden"); + ui_widget_t *visible = create_box("test-hg-visible"); + + ui_widget_append(row, hidden); + ui_widget_append(row, visible); + ui_widget_append(ui_root(), row); + ui_widget_resize(ui_root(), 800, 600); + ui_update(); + ui_update(); + ui_update(); + + ctest_equal_float("visible item keeps its own width", + visible->border_box.width, 28.f); + ctest_equal_float("row width excludes the hidden sibling", + row->content_box.width, 28.f); + + ui_widget_destroy(row); + ui_clear_trash(); +} + +static void test_visible_first(void) +{ + ui_widget_t *row = create_box("test-hg-row"); + ui_widget_t *visible = create_box("test-hg-visible"); + ui_widget_t *hidden = create_box("test-hg-hidden"); + + ui_widget_append(row, visible); + ui_widget_append(row, hidden); + ui_widget_append(ui_root(), row); + ui_widget_resize(ui_root(), 800, 600); + ui_update(); + ui_update(); + ui_update(); + + ctest_equal_float("visible first keeps its own width", + visible->border_box.width, 28.f); + ctest_equal_float("visible first row width", row->content_box.width, + 28.f); + + ui_widget_destroy(row); + ui_clear_trash(); +} + +void test_flex_hidden_gap(void) +{ + lcui_init(); + ui_load_css_file("test_flex_hidden_gap.css"); + ui_widget_set_title(ui_root(), L"test flex hidden gap"); + + ctest_describe("flex hidden gap", test_hidden_sibling_gap); + ctest_describe("flex visible first", test_visible_first); + + lcui_destroy(); +} diff --git a/tests/integration/test_flex_layout.c b/tests/integration/test_flex_layout.c index 3578597c..d7b7c868 100644 --- a/tests/integration/test_flex_layout.c +++ b/tests/integration/test_flex_layout.c @@ -23,6 +23,8 @@ void ui_widget_set_available_width_constraint(ui_widget_t *child, ui_widget_t *source, unsigned generation); bool ui_widget_get_available_width(ui_widget_t *w, float *available_width); +void ui_widget_reset_height(ui_widget_t *w); +void ui_widget_update_style(ui_widget_t *w); #define SCROLLBAR_WIDTH 10 @@ -1459,6 +1461,195 @@ static void test_fixed_width_child_wraps_own_width(void) ui_widget_destroy(ref); } +static ui_widget_t *build_fixed_height_column(ui_widget_t **item, + ui_widget_t **button) +{ + ui_widget_t *outer = ui_create_widget(NULL); + + *item = ui_create_widget(NULL); + *button = ui_create_button(); + ui_widget_set_style_string(outer, "display", "flex"); + ui_widget_set_style_string(outer, "flex-direction", "column"); + ui_widget_set_style_string(outer, "width", "300px"); + ui_widget_set_style_string(*item, "display", "flex"); + ui_widget_set_style_string(*item, "flex-direction", "column"); + ui_widget_set_style_string(*item, "width", "100%"); + ui_widget_set_style_string(*button, "height", "32px"); + ui_widget_set_style_string(*button, "flex-shrink", "1"); + ui_button_set_text(*button, "Fixed height"); + ui_widget_append(*item, *button); + ui_widget_append(outer, *item); + ui_widget_append(ui_root(), outer); + return outer; +} + +static void test_fixed_height_child_restores_intrinsic_size(void) +{ + ui_widget_t *item; + ui_widget_t *button; + ui_widget_t *outer = build_fixed_height_column(&item, &button); + + ui_widget_set_style_string(outer, "height", "20px"); + ui_update(); + ui_update(); + ctest_equal_bool( + "fixed-height child should shrink in a definite parent", + button->border_box.height < 32.0f, true); + + ui_widget_set_style_string(outer, "height", "auto"); + ui_widget_update_style(outer); + ui_widget_reset_height(outer); + ui_widget_request_reflow(item); + ui_widget_reflow(outer); + ctest_equal_float( + "fixed-height child should restore its declared height", + button->border_box.height, 32.0f); + ctest_equal_bool( + "auto parent should contain the restored child", + item->content_box.height + 0.5f >= button->border_box.height, true); + + ui_widget_destroy(outer); +} + +static void test_auto_margins_excluded_from_intrinsic_height(void) +{ + ui_widget_t *item; + ui_widget_t *button; + ui_widget_t *outer = build_fixed_height_column(&item, &button); + + ui_widget_set_style_string(outer, "height", "64px"); + ui_widget_set_style_string(item, "flex-grow", "1"); + ui_widget_set_style_string(button, "margin-top", "auto"); + ui_update(); + ui_update(); + ctest_equal_bool("auto margin should be resolved in a definite parent", + button->computed_style.margin_top > 1.0f, true); + + ui_widget_set_style_string(outer, "height", "auto"); + ui_widget_update_style(outer); + ui_widget_reset_height(outer); + ui_widget_request_reflow(item); + ui_widget_reflow(outer); + ctest_equal_float("auto parent should exclude the old auto margin", + item->content_box.height, 32.0f); + ctest_equal_float( + "auto margin should reset during intrinsic measurement", + button->computed_style.margin_top, 0.0f); + + ui_widget_destroy(outer); +} + +static ui_widget_t *create_measure_column(void) +{ + ui_widget_t *w = ui_create_widget(NULL); + + ui_widget_set_style_string(w, "display", "flex"); + ui_widget_set_style_string(w, "flex-direction", "column"); + return w; +} + +static ui_widget_t *create_measure_text(void) +{ + ui_widget_t *w = ui_create_widget("text"); + + ui_widget_set_style_string(w, "white-space", "normal"); + ui_text_set_content(w, LONG_TEXT); + return w; +} + +static void test_measurement_paths_share_resolved_width(void) +{ + ui_widget_t *fast_parent = create_measure_column(); + ui_widget_t *reflow_parent = create_measure_column(); + ui_widget_t *fast_item = create_measure_column(); + ui_widget_t *reflow_item = create_measure_column(); + ui_widget_t *wrapper = create_measure_column(); + ui_widget_t *fast_text = create_measure_text(); + ui_widget_t *reflow_text = create_measure_text(); + + ui_widget_set_style_string(fast_parent, "align-items", "center"); + ui_widget_set_style_string(reflow_parent, "align-items", "center"); + ui_widget_set_style_string(fast_parent, "width", "400px"); + ui_widget_set_style_string(reflow_parent, "width", "400px"); + ui_widget_set_style_string(fast_parent, "height", "auto"); + ui_widget_set_style_string(reflow_parent, "height", "auto"); + ui_widget_set_style_string(fast_item, "max-width", "200px"); + ui_widget_set_style_string(reflow_item, "max-width", "200px"); + ui_widget_append(fast_item, fast_text); + ui_widget_append(wrapper, reflow_text); + ui_widget_append(reflow_item, wrapper); + ui_widget_append(fast_parent, fast_item); + ui_widget_append(reflow_parent, reflow_item); + ui_widget_append(ui_root(), fast_parent); + ui_widget_append(ui_root(), reflow_parent); + ui_update(); + ui_update(); + + ctest_equal_float("fast and reflow paths should use the same width", + fast_item->content_box.width, + reflow_item->content_box.width); + ctest_equal_float( + "fast and reflow paths should produce the same text height", + fast_text->content_box.height, reflow_text->content_box.height); + ctest_equal_float( + "fast and reflow paths should produce the same parent height", + fast_parent->content_box.height, reflow_parent->content_box.height); + ctest_equal_float("fast-path cache should record the resolved width", + fast_item->measured_content_width, + fast_item->content_box.width); + ctest_equal_float("reflow cache should record the resolved width", + reflow_item->measured_content_width, + reflow_item->content_box.width); + + ui_widget_destroy(fast_parent); + ui_widget_destroy(reflow_parent); +} + +static void test_percentage_measurement_honors_min_max_and_box_sizing(void) +{ + static const char *box_sizing[] = { "content-box", "border-box" }; + static const float expected_width[] = { 200.0f, 178.0f }; + int i; + + for (i = 0; i < 2; ++i) { + ui_widget_t *outer = create_measure_column(); + ui_widget_t *item = create_measure_column(); + ui_widget_t *text = create_measure_text(); + ui_widget_t *reference = + build_column_with_long_text(expected_width[i]); + ui_widget_t *reference_text = ui_widget_get_child(reference, 1); + + ui_widget_set_style_string(outer, "width", "400px"); + ui_widget_set_style_string(item, "width", "100%"); + ui_widget_set_style_string(item, "max-width", "200px"); + ui_widget_set_style_string(item, "box-sizing", box_sizing[i]); + ui_widget_set_style_string(item, "padding", "10px"); + ui_widget_set_style_string(item, "border", "1px solid #000000"); + ui_widget_append(item, text); + ui_widget_append(outer, item); + ui_widget_append(ui_root(), outer); + ui_update(); + + ctest_equal_float( + "percentage item should apply min max before box sizing", + item->content_box.width, expected_width[i]); + ctest_equal_float( + "percentage cache should use the clamped content width", + item->measured_content_width, expected_width[i]); + ctest_equal_float( + "percentage item should wrap at its resolved width", + text->content_box.height, + reference_text->content_box.height); + ctest_equal_bool( + "percentage parent should contain all wrapped lines", + item->content_box.height + 0.5f >= text->border_box.height, + true); + + ui_widget_destroy(outer); + ui_widget_destroy(reference); + } +} + /* A flex-grow child's main size is allocated from remaining space and must * not diverge across repeated updates. */ static void test_grow_child_main_size_stable(void) @@ -1542,6 +1733,15 @@ void test_flex_layout(void) test_indefinite_width_keeps_single_line); ctest_describe("fixed width child wrap", test_fixed_width_child_wraps_own_width); + ctest_describe("fixed height intrinsic restoration", + test_fixed_height_child_restores_intrinsic_size); + ctest_describe("auto margin intrinsic height", + test_auto_margins_excluded_from_intrinsic_height); + ctest_describe("measurement path width consistency", + test_measurement_paths_share_resolved_width); + ctest_describe( + "percentage measurement constraints", + test_percentage_measurement_honors_min_max_and_box_sizing); ctest_describe("grow child main size", test_grow_child_main_size_stable); ctest_describe("constraint refresh", diff --git a/tests/integration/test_home_layout.c b/tests/integration/test_home_layout.c new file mode 100644 index 00000000..19da1a43 --- /dev/null +++ b/tests/integration/test_home_layout.c @@ -0,0 +1,244 @@ +/* + * tests/integration/test_home_layout.c + * + * Copyright (c) 2026, Liu Chao 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. + */ + +/* + * The doc-viewer home page structure: scroll area -> page -> centered + * max-width container -> four-column card gallery with a text input inside + * a card. These tests pin down that a focus-driven style refresh must not + * collapse the percentage-sized containers between layout passes, and that + * the columns can be sized from the container width without oscillating. + */ + +#include +#include +#include +#include +#include +#include + +#define HOME_COLUMNS 4 +#define HOME_GAP 24.0f + +typedef struct { + ui_widget_t *scroll; + ui_widget_t *page; + ui_widget_t *inner; + ui_widget_t *grid; + ui_widget_t *columns[HOME_COLUMNS]; + ui_widget_t *input; + float min_inner_width; + float max_inner_width; + float min_column_width; + float max_column_width; + int layout_passes; +} home_scene_t; + +static home_scene_t *home_scene; + +static ui_widget_t *create_box(const char *class_name) +{ + ui_widget_t *w = ui_create_widget(NULL); + + ui_widget_add_class(w, class_name); + return w; +} + +static void home_scene_record(ui_widget_t *w, ui_event_t *e, void *emit_arg) +{ + home_scene_t *scene = home_scene; + float width = w->content_box.width; + + if (!scene || width <= 0) { + return; + } + scene->layout_passes++; + if (width < scene->min_inner_width) { + scene->min_inner_width = width; + } + if (width > scene->max_inner_width) { + scene->max_inner_width = width; + } +} + +/* Mirrors `home_grid_update_visibility()`: size each column from the + * container width in pixels, like the app does. */ +static void home_scene_sync_columns(ui_widget_t *w, ui_event_t *e, void *arg) +{ + home_scene_t *scene = home_scene; + float available; + float width; + int i; + + if (!scene) { + return; + } + available = scene->inner->content_box.width; + if (available <= 100.0f) { + return; + } + width = (available - HOME_GAP * (HOME_COLUMNS - 1)) / HOME_COLUMNS; + if (width < scene->min_column_width) { + scene->min_column_width = width; + } + if (width > scene->max_column_width) { + scene->max_column_width = width; + } + for (i = 0; i < HOME_COLUMNS; ++i) { + ui_widget_set_style_unit_value( + scene->columns[i], css_prop_width, width, CSS_UNIT_PX); + } +} + +static void home_scene_build(void) +{ + ui_widget_t *content = ui_create_scrollarea_content(); + ui_widget_t *column; + ui_widget_t *card; + ui_widget_t *filler; + ui_widget_t *input; + int i; + + home_scene = calloc(1, sizeof(*home_scene)); + home_scene->scroll = ui_create_scrollarea(); + home_scene->page = create_box("test-home-page"); + home_scene->inner = create_box("test-home-inner"); + home_scene->grid = create_box("test-home-grid"); + + ui_widget_add_class(home_scene->scroll, "test-home-scroll"); + ui_widget_append(home_scene->scroll, content); + ui_widget_append(home_scene->scroll, ui_create_scrollbar()); + ui_widget_append(content, home_scene->page); + ui_widget_append(home_scene->page, home_scene->inner); + ui_widget_append(home_scene->inner, home_scene->grid); + + for (i = 0; i < HOME_COLUMNS; ++i) { + column = create_box("test-home-column"); + home_scene->columns[i] = column; + ui_widget_append(home_scene->grid, column); + card = create_box("test-home-card"); + ui_widget_append(column, card); + if (i == 0) { + ui_widget_t *group = ui_create_input_group(); + + input = ui_create_widget("textinput"); + ui_widget_add_class(input, "test-home-input"); + ui_widget_append(group, input); + ui_widget_append(card, group); + home_scene->input = input; + } else { + filler = create_box("test-home-filler"); + ui_widget_append(card, filler); + } + } + ui_widget_append(ui_root(), home_scene->scroll); + + ui_widget_on(home_scene->grid, "afterlayout", home_scene_record, + home_scene); + ui_widget_on(home_scene->grid, "afterlayout", home_scene_sync_columns, + home_scene); +} + +static void home_scene_resize(float width, float height) +{ + ui_widget_resize(ui_root(), width, height); + ui_update(); + ui_update(); + ui_update(); +} + +static void home_scene_destroy(void) +{ + ui_widget_destroy(home_scene->scroll); + ui_clear_trash(); + free(home_scene); + home_scene = NULL; +} + +static void test_structure(void) +{ + home_scene_resize(1400, 900); + + ctest_equal_bool("inner keeps the page width", + home_scene->inner->content_box.width > 1000.0f, true); + ctest_equal_bool("page fills the scroll area", + home_scene->page->content_box.width > 1000.0f, true); + ctest_equal_bool("grid fills the inner container", + fabsf(home_scene->grid->content_box.width - + home_scene->inner->content_box.width) < 1.0f, + true); +} + +static void test_columns_converge(void) +{ + float spread; + + home_scene->min_column_width = FLT_MAX; + home_scene->max_column_width = 0.0f; + home_scene_resize(1200, 800); + ui_update(); + ui_update(); + spread = home_scene->max_column_width - home_scene->min_column_width; + ctest_equal_bool("column width settles after resizing", spread < 1.0f, + true); + ctest_equal_bool("column stays inside the grid", + home_scene->columns[0]->border_box.width < + home_scene->grid->content_box.width, + true); +} + +static void test_focus_keeps_ancestor_widths(void) +{ + float inner_width; + float page_width; + + home_scene_resize(1400, 900); + inner_width = home_scene->inner->content_box.width; + page_width = home_scene->page->content_box.width; + home_scene->min_inner_width = FLT_MAX; + home_scene->max_inner_width = 0.0f; + home_scene->layout_passes = 0; + + ui_set_focus(home_scene->input); + ui_widget_add_class(home_scene->page, "test-home-page-active"); + ui_scrollarea_set_scroll_top(home_scene->scroll, 120); + ui_update(); + ui_update(); + ui_update(); + + ctest_equal_bool( + "focus keeps the inner width", + fabsf(home_scene->inner->content_box.width - inner_width) < 0.5f, + true); + ctest_equal_bool( + "focus keeps the page width", + fabsf(home_scene->page->content_box.width - page_width) < 0.5f, + true); + ctest_equal_bool("layout passes observe the stable inner width", + home_scene->layout_passes > 0 && + home_scene->min_inner_width > inner_width * 0.9f, + true); +} + +void test_home_layout(void) +{ + lcui_init(); + ui_load_css_file("test_home_layout.css"); + ui_widget_set_title(ui_root(), L"test home layout"); + home_scene_build(); + + ctest_describe("home page structure", test_structure); + ctest_describe("home columns converge", test_columns_converge); + ctest_describe("home focus stability", + test_focus_keeps_ancestor_widths); + + home_scene_destroy(); + lcui_destroy(); +} diff --git a/tests/integration/test_shimmer.c b/tests/integration/test_shimmer.c index 7630dce2..9193af08 100644 --- a/tests/integration/test_shimmer.c +++ b/tests/integration/test_shimmer.c @@ -760,6 +760,93 @@ static void check_content_cache_invalidates(void) ui_widget_destroy(sh); } +static void render_alpha_into(ui_widget_t *w, pd_canvas_t *out, int cw, int ch) +{ + pd_rect_t rect = { 0, 0, cw, ch }; + pd_context_t paint; + + make_argb(out, cw, ch); + pd_canvas_fill(out, pd_argb(0, 0, 0, 0)); + paint.with_alpha = true; + paint.rect = rect; + pd_canvas_init(&paint.canvas); + pd_canvas_quote(&paint.canvas, out, &rect); + ui_widget_render(w, &paint); +} + +static int rightmost_alpha(pd_canvas_t *out) +{ + int x; + int y; + + for (x = (int)out->width - 1; x >= 0; --x) { + for (y = 0; y < (int)out->height; ++y) { + if (pd_canvas_get_pixel(out, (unsigned)x, (unsigned)y) + .a > 20) { + return x; + } + } + } + return -1; +} + +/* The shimmer capture temporarily boosts the device scale. Text must be + * retypeset at that scale, not restored to the previous layout pass's fixed + * size, otherwise the right side of a long label is clipped. */ +static void check_text_capture_full_width(void) +{ + const char *content_text = "design-system.zip"; + ui_widget_t *sh = ui_create_shimmer(); + ui_widget_t *content = ui_create_widget("shimmer-content"); + ui_widget_t *text = ui_create_widget("text"); + ui_widget_t *reference = ui_create_widget("text"); + pd_canvas_t captured; + pd_canvas_t direct; + int captured_right; + int direct_right; + + ui_metrics.dpi = 144; + ui_widget_set_style_string(text, "font-size", "14px"); + ui_widget_set_style_string(text, "line-height", "20px"); + ui_widget_set_style_string(text, "white-space", "nowrap"); + ui_widget_set_style_string(text, "color", "#ffffff"); + ui_text_set_content(text, content_text); + ui_widget_append(content, text); + ui_shimmer_set_content(sh, content); + ui_widget_append(ui_root(), sh); + + ui_widget_set_style_string(reference, "width", "240px"); + ui_widget_set_style_string(reference, "height", "20px"); + ui_widget_set_style_string(reference, "font-size", "14px"); + ui_widget_set_style_string(reference, "line-height", "20px"); + ui_widget_set_style_string(reference, "white-space", "nowrap"); + ui_widget_set_style_string(reference, "color", "#ffffff"); + ui_text_set_content(reference, content_text); + ui_widget_append(ui_root(), reference); + ui_widget_resize(reference, 240, 20); + + prime_shimmer(sh, 240, 20, 0.0f, 0.0f); + ui_update(); + render_alpha_into(sh, &captured, 360, 30); + render_alpha_into(reference, &direct, 360, 30); + captured_right = rightmost_alpha(&captured); + direct_right = rightmost_alpha(&direct); + + ctest_equal_bool("shimmer text capture should render text", + captured_right > 0, true); + ctest_equal_bool("reference text should render text", direct_right > 0, + true); + ctest_equal_bool( + "shimmer capture should not clip the end of a long text label", + abs(captured_right - direct_right) <= 6, true); + + pd_canvas_destroy(&captured); + pd_canvas_destroy(&direct); + ui_widget_destroy(sh); + ui_widget_destroy(reference); + ui_metrics.dpi = 96; +} + void test_shimmer(void) { lcui_init(); @@ -781,6 +868,7 @@ void test_shimmer(void) check_sheen_preserves_alpha); ctest_describe("shimmer content cache", check_content_cache_invalidates); + ctest_describe("shimmer text capture", check_text_capture_full_width); ui_metrics.dpi = 96; lcui_destroy(); diff --git a/tests/integration/test_style_refresh.c b/tests/integration/test_style_refresh.c new file mode 100644 index 00000000..3fb63dc7 --- /dev/null +++ b/tests/integration/test_style_refresh.c @@ -0,0 +1,102 @@ +/* + * tests/integration/test_style_refresh.c + * + * Copyright (c) 2026, Liu Chao 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. + */ + +/* + * A status change (hover/focus) only refreshes the matched style. It must + * not collapse the used box of auto-sized widgets for one layout pass, or + * cards resize and text reflows whenever the pointer moves over them. + */ + +#include +#include +#include + +typedef struct { + float min_width; + float min_height; +} refresh_probe_t; + +static refresh_probe_t probe; + +static void record_box(ui_widget_t *w, ui_event_t *e, void *arg) +{ + if (w->content_box.width < probe.min_width) { + probe.min_width = w->content_box.width; + } + if (w->content_box.height < probe.min_height) { + probe.min_height = w->content_box.height; + } +} + +static ui_widget_t *create_box(const char *class_name) +{ + ui_widget_t *w = ui_create_widget(NULL); + + ui_widget_add_class(w, class_name); + return w; +} + +static void settle(void) +{ + ui_update(); + ui_update(); + ui_update(); +} + +static void test_hover_keeps_box(void) +{ + ui_widget_t *parent = create_box("test-refresh-parent"); + ui_widget_t *box = create_box("test-refresh-box"); + ui_widget_t *text = ui_create_widget("text"); + float width; + float height; + + ui_widget_add_class(text, "test-refresh-text"); + ui_text_set_content(text, + "Hovering the card must keep its box stable."); + ui_widget_append(box, text); + ui_widget_append(parent, box); + ui_widget_append(ui_root(), parent); + ui_widget_resize(ui_root(), 800, 600); + settle(); + + width = box->content_box.width; + height = box->content_box.height; + probe.min_width = FLT_MAX; + probe.min_height = FLT_MAX; + ui_widget_on(box, "afterlayout", record_box, NULL); + + ui_widget_add_status(box, "hover"); + settle(); + + ctest_equal_float("hover keeps the box width", box->content_box.width, + width); + ctest_equal_float("hover keeps the box height", box->content_box.height, + height); + ctest_equal_bool("hover never observes a collapsed width", + probe.min_width > width * 0.9f, true); + ctest_equal_bool("hover never observes a collapsed height", + probe.min_height > height * 0.9f, true); + + ui_widget_destroy(parent); + ui_clear_trash(); +} + +void test_style_refresh(void) +{ + lcui_init(); + ui_load_css_file("test_style_refresh.css"); + ui_widget_set_title(ui_root(), L"test style refresh"); + + ctest_describe("style refresh hover", test_hover_keeps_box); + + lcui_destroy(); +} diff --git a/tests/main.c b/tests/main.c index a3516e0c..5953ec5c 100644 --- a/tests/main.c +++ b/tests/main.c @@ -31,6 +31,10 @@ extern void test_flex_layout_percentage_wrap(void); extern void test_flex_contract(void); extern void test_empty_layout(void); extern void test_field_layout(void); +extern void test_home_layout(void); +extern void test_background_style(void); +extern void test_style_refresh(void); +extern void test_flex_hidden_gap(void); extern void test_sizehint(void); extern void test_scrollbar(void); extern void test_scrollarea(void); @@ -87,6 +91,10 @@ static const ctest_suite_t suites[] = { { "flex contract", test_flex_contract }, { "empty layout", test_empty_layout }, { "field layout", test_field_layout }, + { "home layout", test_home_layout }, + { "background style", test_background_style }, + { "style refresh", test_style_refresh }, + { "flex hidden gap", test_flex_hidden_gap }, { "sizehint", test_sizehint }, { "scrollbar", test_scrollbar }, { "scrollarea", test_scrollarea },