Skip to content

Commit 625ee90

Browse files
committed
fix randrew#15 and randrew#20, add some test case.
1 parent b0c0282 commit 625ee90

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

layout.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ void lay_arrange_overlay(lay_context *ctx, lay_id item, int dim)
10661066

10671067
switch (b_flags & LAY_HFILL) {
10681068
case LAY_HCENTER:
1069-
child_rect[dim] += (space - child_rect[2 + dim]) / 2 - child_margins[wdim];
1069+
child_rect[dim] += lay_scalar_max(0, (space - child_rect[2 + dim]) / 2 - child_margins[wdim]);
10701070
break;
10711071
case LAY_RIGHT:
10721072
child_rect[dim] += space - child_rect[2 + dim] - child_margins[dim] - child_margins[wdim];
@@ -1101,7 +1101,7 @@ void lay_arrange_overlay_squeezed_range(
11011101
switch (b_flags & LAY_HFILL) {
11021102
case LAY_HCENTER:
11031103
rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size);
1104-
rect[dim] += (space - rect[2 + dim]) / 2 - margins[wdim];
1104+
rect[dim] += lay_scalar_max(0, (space - rect[2 + dim]) / 2 - margins[wdim]);
11051105
break;
11061106
case LAY_RIGHT:
11071107
rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size);
@@ -1159,8 +1159,19 @@ static void lay_arrange(lay_context *ctx, lay_id item, int dim)
11591159
lay_arrange_stacked(ctx, item, 1, true);
11601160
lay_scalar offset = lay_arrange_wrapped_overlay_squeezed(ctx, item, 0);
11611161
ctx->rects[item][2 + 0] = offset - ctx->rects[item][0];
1162+
1163+
// The X coordinates are calculated above, and all children nodes need to be updated here
1164+
lay_id child = pitem->first_child;
1165+
while (child != LAY_INVALID_ID) {
1166+
lay_arrange(ctx, child, 0);
1167+
lay_item_t *pchild = lay_get_item(ctx, child);
1168+
child = pchild->next_sibling;
1169+
}
1170+
break;
1171+
} else {
1172+
// To prevent repeated calculations, it should be returned directly here
1173+
return;
11621174
}
1163-
break;
11641175
case LAY_ROW | LAY_WRAP:
11651176
if (dim == 0)
11661177
lay_arrange_stacked(ctx, item, 0, true);

test_layout.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,89 @@ LTEST_DECLARE(anchor_right_margin2)
920920
LTEST_VEC4EQ(lay_get_rect(ctx, child), 40, 40, 50, 50);
921921
}
922922

923+
// issue #15
924+
LTEST_DECLARE(child_expands_container1)
925+
{
926+
lay_id root = lay_item(ctx);
927+
lay_set_size_xy(ctx, root, 1, 100);
928+
929+
lay_id row = lay_item(ctx);
930+
lay_set_contain(ctx, row, LAY_ROW);
931+
lay_insert(ctx, root, row);
932+
933+
lay_id child = lay_item(ctx);
934+
lay_set_size_xy(ctx, child, 1, 50);
935+
lay_set_margins_ltrb(ctx, child, 0, 0, 0, 10);
936+
lay_insert(ctx, row, child);
937+
938+
lay_run_context(ctx);
939+
lay_vec4 root_rect = lay_get_rect(ctx, root);
940+
lay_vec4 row_rect = lay_get_rect(ctx, row);
941+
lay_vec4 child_rect = lay_get_rect(ctx, child);
942+
943+
LTEST_VEC4EQ(root_rect, 0, 0, 1, 100);
944+
LTEST_VEC4EQ(row_rect, 0, 20, 1, 60);
945+
LTEST_VEC4EQ(child_rect, 0, 20, 1, 50);
946+
}
947+
948+
// issue #15
949+
LTEST_DECLARE(child_expands_container2)
950+
{
951+
lay_id root = lay_item(ctx);
952+
lay_set_size_xy(ctx, root, 400, 400);
953+
lay_set_margins_ltrb(ctx, root, 50, 50, 50, 50);
954+
lay_set_contain(ctx, root, LAY_COLUMN | LAY_WRAP);
955+
956+
lay_id child1 = lay_item(ctx);
957+
lay_set_size_xy(ctx, child1 , 50, 50);
958+
lay_set_margins_ltrb(ctx, child1 , 5, 5, 5, 5);
959+
lay_insert(ctx, root, child1);
960+
961+
lay_id child2 = lay_item(ctx);
962+
lay_set_size_xy(ctx, child2, 50, 50);
963+
lay_set_margins_ltrb(ctx, child2, 5, 5, 5, 5);
964+
lay_insert(ctx, root, child2);
965+
966+
lay_run_context(ctx);
967+
968+
lay_vec4 root_rect = lay_get_rect(ctx, root);
969+
lay_vec4 child1_rect = lay_get_rect(ctx, child1);
970+
lay_vec4 child2_rect = lay_get_rect(ctx, child2);
971+
972+
LTEST_VEC4EQ(root_rect, 50, 50, 60, 400);
973+
LTEST_VEC4EQ(child1_rect, 55, 195, 50, 50);
974+
LTEST_VEC4EQ(child2_rect, 55, 255, 50, 50);
975+
}
976+
977+
// issue #20
978+
LTEST_DECLARE(column_wrap_grandson)
979+
{
980+
lay_id root = lay_item(ctx);
981+
lay_set_size_xy(ctx, root, 200, 200);
982+
lay_set_margins_ltrb(ctx, root, 50, 50, 50, 50);
983+
lay_set_contain(ctx, root, LAY_WRAP | LAY_COLUMN);
984+
985+
lay_id child1 = lay_item(ctx);
986+
lay_set_size_xy(ctx, child1, 50, 50);
987+
lay_set_margins_ltrb(ctx, child1, 5, 5, 5, 5);
988+
lay_insert(ctx, root, child1);
989+
990+
lay_id child2 = lay_item(ctx);
991+
lay_set_size_xy(ctx, child2, 50, 50);
992+
lay_set_margins_ltrb(ctx, child2, 5, 5, 5, 5);
993+
lay_insert(ctx, child1, child2);
994+
995+
lay_run_context(ctx);
996+
997+
lay_vec4 root_rect = lay_get_rect(ctx, root);
998+
lay_vec4 child1_rect = lay_get_rect(ctx, child1);
999+
lay_vec4 child2_rect = lay_get_rect(ctx, child2);
1000+
1001+
LTEST_VEC4EQ(root_rect, 50, 50, 60, 200);
1002+
LTEST_VEC4EQ(child1_rect, 55, 125, 50, 50);
1003+
LTEST_VEC4EQ(child2_rect, 60, 130, 50, 50);
1004+
}
1005+
9231006
// Call in main to run a test by name
9241007
//
9251008
// Resets string buffer and lay context before running test
@@ -966,6 +1049,9 @@ int main(int argc, char** argv)
9661049
LTEST_RUN(wrap_column_4);
9671050
LTEST_RUN(anchor_right_margin1);
9681051
LTEST_RUN(anchor_right_margin2);
1052+
LTEST_RUN(child_expands_container1);
1053+
LTEST_RUN(child_expands_container2);
1054+
LTEST_RUN(column_wrap_grandson);
9691055

9701056
printf("Finished tests\n");
9711057

0 commit comments

Comments
 (0)