From 3395ec049b12bd78df07ed70d7ac20f0751cea2c Mon Sep 17 00:00:00 2001 From: Liu Date: Mon, 7 Oct 2024 23:13:51 +0800 Subject: [PATCH] fix(ui): correct width calculation --- lib/css/src/computed.c | 8 +++++--- lib/ui/src/ui_flexbox_layout.c | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/css/src/computed.c b/lib/css/src/computed.c index e2245c196..a5a3751d0 100644 --- a/lib/css/src/computed.c +++ b/lib/css/src/computed.c @@ -1079,7 +1079,10 @@ static void compute_absolute_width(const css_computed_style_t *parent, s->type_bits.width = CSS_WIDTH_FIT_CONTENT; break; } - if (compute_content_box_fixed_width(parent, &parent_value)) { + // 当父元素是块级元素且 display 为 block 时,width 为父元素的 + // content box 的宽度 + if (is_css_display_block(parent) && + compute_content_box_fixed_width(parent, &parent_value)) { value = parent_value - s->margin_left - s->margin_right; if (s->type_bits.box_sizing == CSS_BOX_SIZING_CONTENT_BOX) { @@ -1147,8 +1150,7 @@ static void compute_absolute_height(const css_computed_style_t *parent, if (css_computed_position(s) > CSS_POSITION_RELATIVE) { parent_value += css_padding_y(parent); } - CSS_SET_FIXED_LENGTH(s, height, - parent_value * value / 100.0f); + CSS_SET_FIXED_LENGTH(s, height, parent_value * value / 100.0f); break; default: break; diff --git a/lib/ui/src/ui_flexbox_layout.c b/lib/ui/src/ui_flexbox_layout.c index 4b4d0b97c..cfc64a949 100644 --- a/lib/ui/src/ui_flexbox_layout.c +++ b/lib/ui/src/ui_flexbox_layout.c @@ -428,6 +428,13 @@ static void ui_flexbox_layout_update_row(ui_flexbox_layout_context_t *ctx) // 根据 justify-content 和 align-items,更新每个项目的位置和尺寸 ui_flexbox_layout_compute_justify_content(ctx, &main_axis, &space); +#ifdef UI_DEBUG_ENABLED + { + UI_WIDGET_STR(ctx->widget, str); + UI_DEBUG_MSG("%s: main_axis_start = %g, free_space = %g, justify_content = %d", + str, main_axis, space, ctx->widget->computed_style.type_bits.justify_content); + } +#endif for (list_each(node, &ctx->line->items)) { main_axis += space; child = node->data;