|
| 1 | +import pytest |
| 2 | + |
| 3 | +from arcade.gui import UIBoxLayout, UIWidget |
| 4 | +from arcade.types import LBWH |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize("vertical", [False, True]) |
| 8 | +@pytest.mark.parametrize("count", [0, 1, 3]) |
| 9 | +def test_all_collapsed_children_preserve_only_padding_and_border(vertical, count): |
| 10 | + children = [UIWidget(width=100, height=50) for _ in range(count)] |
| 11 | + for child in children: |
| 12 | + child.visible = None |
| 13 | + original_rects = [child.rect for child in children] |
| 14 | + box = UIBoxLayout(vertical=vertical, children=children, space_between=10) |
| 15 | + box.with_padding(left=2, right=3, top=5, bottom=7).with_border(width=4) |
| 16 | + |
| 17 | + box.fit_content() |
| 18 | + box.do_layout() |
| 19 | + |
| 20 | + assert box.size_hint_min == (13, 20) |
| 21 | + assert box.size == (13, 20) |
| 22 | + assert [child.rect for child in children] == original_rects |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize("vertical", [False, True]) |
| 26 | +@pytest.mark.parametrize("collapsed_index", [0, 1, 2]) |
| 27 | +def test_collapsed_children_do_not_reserve_gaps(vertical, collapsed_index): |
| 28 | + first = UIWidget(size_hint=(1, 1), size_hint_min=(20, 30)) |
| 29 | + second = UIWidget(size_hint=(1, 1), size_hint_min=(20, 30)) |
| 30 | + collapsed = UIWidget(width=100, height=100) |
| 31 | + collapsed.visible = None |
| 32 | + original_rect = collapsed.rect |
| 33 | + children = [first, second] |
| 34 | + children.insert(collapsed_index, collapsed) |
| 35 | + box = UIBoxLayout(vertical=vertical, children=children, space_between=10) |
| 36 | + |
| 37 | + assert box.size_hint_min == ((20, 70) if vertical else (50, 30)) |
| 38 | + box.rect = LBWH(0, 0, 120, 120) |
| 39 | + box.do_layout() |
| 40 | + |
| 41 | + if vertical: |
| 42 | + assert first.rect == LBWH(0, 65, 120, 55) |
| 43 | + assert second.rect == LBWH(0, 0, 120, 55) |
| 44 | + else: |
| 45 | + assert first.rect == LBWH(0, 0, 55, 120) |
| 46 | + assert second.rect == LBWH(65, 0, 55, 120) |
| 47 | + assert collapsed.rect == original_rect |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("vertical", [False, True]) |
| 51 | +@pytest.mark.parametrize("visible", [False, True]) |
| 52 | +def test_hidden_children_still_participate_in_layout(vertical, visible): |
| 53 | + first = UIWidget(width=20, height=30) |
| 54 | + first.visible = visible |
| 55 | + second = UIWidget(width=40, height=50) |
| 56 | + box = UIBoxLayout(vertical=vertical, children=[first, second], space_between=10) |
| 57 | + |
| 58 | + box.fit_content() |
| 59 | + box.do_layout() |
| 60 | + |
| 61 | + assert box.size_hint_min == ((40, 90) if vertical else (70, 50)) |
| 62 | + if vertical: |
| 63 | + assert first.top == box.top |
| 64 | + assert first.bottom - second.top == 10 |
| 65 | + assert second.bottom == box.bottom |
| 66 | + else: |
| 67 | + assert first.left == box.left |
| 68 | + assert second.left - first.right == 10 |
| 69 | + assert second.right == box.right |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("vertical", [False, True]) |
| 73 | +def test_visibility_changes_update_the_minimum_size(vertical): |
| 74 | + first = UIWidget(width=20, height=30) |
| 75 | + second = UIWidget(width=40, height=50) |
| 76 | + box = UIBoxLayout(vertical=vertical, children=[first, second], space_between=10) |
| 77 | + |
| 78 | + first.visible = None |
| 79 | + box._prepare_layout() |
| 80 | + assert box.size_hint_min == (40, 50) |
| 81 | + |
| 82 | + second.visible = None |
| 83 | + box._prepare_layout() |
| 84 | + assert box.size_hint_min == (0, 0) |
| 85 | + |
| 86 | + first.visible = False |
| 87 | + box._prepare_layout() |
| 88 | + assert box.size_hint_min == (20, 30) |
| 89 | + |
| 90 | + second.visible = True |
| 91 | + box._prepare_layout() |
| 92 | + assert box.size_hint_min == ((40, 90) if vertical else (70, 50)) |
| 93 | + |
| 94 | + |
| 95 | +def test_collapse_and_restore_through_ui_manager(window, ui): |
| 96 | + first = UIWidget(width=20, height=30) |
| 97 | + second = UIWidget(width=40, height=50) |
| 98 | + box = UIBoxLayout(children=[first, second], space_between=10) |
| 99 | + ui.add(box) |
| 100 | + ui.execute_layout() |
| 101 | + assert box.size == (40, 90) |
| 102 | + |
| 103 | + first.visible = None |
| 104 | + ui.execute_layout() |
| 105 | + assert box.size == (40, 50) |
| 106 | + |
| 107 | + second.visible = None |
| 108 | + ui.execute_layout() |
| 109 | + assert box.size == (0, 0) |
| 110 | + |
| 111 | + first.visible = False |
| 112 | + ui.execute_layout() |
| 113 | + assert box.size == (20, 30) |
0 commit comments