Skip to content

Commit

Permalink
fix randrew#15 and randrew#20, add some test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
haoyu234 committed Oct 26, 2024
1 parent b0c0282 commit 625ee90
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
17 changes: 14 additions & 3 deletions layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ void lay_arrange_overlay(lay_context *ctx, lay_id item, int dim)

switch (b_flags & LAY_HFILL) {
case LAY_HCENTER:
child_rect[dim] += (space - child_rect[2 + dim]) / 2 - child_margins[wdim];
child_rect[dim] += lay_scalar_max(0, (space - child_rect[2 + dim]) / 2 - child_margins[wdim]);
break;
case LAY_RIGHT:
child_rect[dim] += space - child_rect[2 + dim] - child_margins[dim] - child_margins[wdim];
Expand Down Expand Up @@ -1101,7 +1101,7 @@ void lay_arrange_overlay_squeezed_range(
switch (b_flags & LAY_HFILL) {
case LAY_HCENTER:
rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size);
rect[dim] += (space - rect[2 + dim]) / 2 - margins[wdim];
rect[dim] += lay_scalar_max(0, (space - rect[2 + dim]) / 2 - margins[wdim]);
break;
case LAY_RIGHT:
rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size);
Expand Down Expand Up @@ -1159,8 +1159,19 @@ static void lay_arrange(lay_context *ctx, lay_id item, int dim)
lay_arrange_stacked(ctx, item, 1, true);
lay_scalar offset = lay_arrange_wrapped_overlay_squeezed(ctx, item, 0);
ctx->rects[item][2 + 0] = offset - ctx->rects[item][0];

// The X coordinates are calculated above, and all children nodes need to be updated here
lay_id child = pitem->first_child;
while (child != LAY_INVALID_ID) {
lay_arrange(ctx, child, 0);
lay_item_t *pchild = lay_get_item(ctx, child);
child = pchild->next_sibling;
}
break;
} else {
// To prevent repeated calculations, it should be returned directly here
return;
}
break;
case LAY_ROW | LAY_WRAP:
if (dim == 0)
lay_arrange_stacked(ctx, item, 0, true);
Expand Down
86 changes: 86 additions & 0 deletions test_layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,89 @@ LTEST_DECLARE(anchor_right_margin2)
LTEST_VEC4EQ(lay_get_rect(ctx, child), 40, 40, 50, 50);
}

// issue #15
LTEST_DECLARE(child_expands_container1)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 1, 100);

lay_id row = lay_item(ctx);
lay_set_contain(ctx, row, LAY_ROW);
lay_insert(ctx, root, row);

lay_id child = lay_item(ctx);
lay_set_size_xy(ctx, child, 1, 50);
lay_set_margins_ltrb(ctx, child, 0, 0, 0, 10);
lay_insert(ctx, row, child);

lay_run_context(ctx);
lay_vec4 root_rect = lay_get_rect(ctx, root);
lay_vec4 row_rect = lay_get_rect(ctx, row);
lay_vec4 child_rect = lay_get_rect(ctx, child);

LTEST_VEC4EQ(root_rect, 0, 0, 1, 100);
LTEST_VEC4EQ(row_rect, 0, 20, 1, 60);
LTEST_VEC4EQ(child_rect, 0, 20, 1, 50);
}

// issue #15
LTEST_DECLARE(child_expands_container2)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 400, 400);
lay_set_margins_ltrb(ctx, root, 50, 50, 50, 50);
lay_set_contain(ctx, root, LAY_COLUMN | LAY_WRAP);

lay_id child1 = lay_item(ctx);
lay_set_size_xy(ctx, child1 , 50, 50);
lay_set_margins_ltrb(ctx, child1 , 5, 5, 5, 5);
lay_insert(ctx, root, child1);

lay_id child2 = lay_item(ctx);
lay_set_size_xy(ctx, child2, 50, 50);
lay_set_margins_ltrb(ctx, child2, 5, 5, 5, 5);
lay_insert(ctx, root, child2);

lay_run_context(ctx);

lay_vec4 root_rect = lay_get_rect(ctx, root);
lay_vec4 child1_rect = lay_get_rect(ctx, child1);
lay_vec4 child2_rect = lay_get_rect(ctx, child2);

LTEST_VEC4EQ(root_rect, 50, 50, 60, 400);
LTEST_VEC4EQ(child1_rect, 55, 195, 50, 50);
LTEST_VEC4EQ(child2_rect, 55, 255, 50, 50);
}

// issue #20
LTEST_DECLARE(column_wrap_grandson)
{
lay_id root = lay_item(ctx);
lay_set_size_xy(ctx, root, 200, 200);
lay_set_margins_ltrb(ctx, root, 50, 50, 50, 50);
lay_set_contain(ctx, root, LAY_WRAP | LAY_COLUMN);

lay_id child1 = lay_item(ctx);
lay_set_size_xy(ctx, child1, 50, 50);
lay_set_margins_ltrb(ctx, child1, 5, 5, 5, 5);
lay_insert(ctx, root, child1);

lay_id child2 = lay_item(ctx);
lay_set_size_xy(ctx, child2, 50, 50);
lay_set_margins_ltrb(ctx, child2, 5, 5, 5, 5);
lay_insert(ctx, child1, child2);

lay_run_context(ctx);

lay_vec4 root_rect = lay_get_rect(ctx, root);
lay_vec4 child1_rect = lay_get_rect(ctx, child1);
lay_vec4 child2_rect = lay_get_rect(ctx, child2);

LTEST_VEC4EQ(root_rect, 50, 50, 60, 200);
LTEST_VEC4EQ(child1_rect, 55, 125, 50, 50);
LTEST_VEC4EQ(child2_rect, 60, 130, 50, 50);
}

// Call in main to run a test by name
//
// Resets string buffer and lay context before running test
Expand Down Expand Up @@ -966,6 +1049,9 @@ int main(int argc, char** argv)
LTEST_RUN(wrap_column_4);
LTEST_RUN(anchor_right_margin1);
LTEST_RUN(anchor_right_margin2);
LTEST_RUN(child_expands_container1);
LTEST_RUN(child_expands_container2);
LTEST_RUN(column_wrap_grandson);

printf("Finished tests\n");

Expand Down

0 comments on commit 625ee90

Please sign in to comment.