Skip to content

Commit

Permalink
Combined row and column layout methods (#3061)
Browse files Browse the repository at this point in the history
The flexbox algorithm is almost identical between the row and column layout, the implementations can be shared with a very small amount of parameterization. This PR converges the two direction implementations into a single parameterised implementation.
  • Loading branch information
HalfWhitt authored Jan 3, 2025
1 parent 08e7d38 commit 4d0a179
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 545 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions changes/3061.misc.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pack.layout() now has a single argument -- the viewport -- instead of (node, viewport).
1 change: 1 addition & 0 deletions changes/3061.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pack's layout logic for rows and columns has been combined into one direction-agnostic method.
855 changes: 349 additions & 506 deletions core/src/toga/style/pack.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/tests/style/pack/layout/test_beeliza.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_beeliza():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(160, 125),
Expand Down
10 changes: 5 additions & 5 deletions core/tests/style/pack/layout/test_column_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_left():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(540, 300),
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_center():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(540, 300),
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_right():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(540, 300),
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_no_margin():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(540, 300),
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_column_box():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(430, 310),
Expand Down
8 changes: 4 additions & 4 deletions core/tests/style/pack/layout/test_fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_row_expanding_intrinsic():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(200, 100),
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_row_fixed_intrinsic():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(150, 100),
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_column_expanding_intrinsic():
)

# Normal size
root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 200),
Expand Down Expand Up @@ -216,7 +216,7 @@ def test_column_fixed_intrinsic():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 150),
Expand Down
20 changes: 10 additions & 10 deletions core/tests/style/pack/layout/test_flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_row_flex_no_hints():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(50, 50),
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_row_flex():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(300, 100),
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_row_flex_insufficient_space():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(200, 100),
Expand Down Expand Up @@ -249,7 +249,7 @@ def test_row_flex_insufficient_space_no_flex():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(220, 100),
Expand Down Expand Up @@ -302,7 +302,7 @@ def test_row_flex_grandchild_min_size():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(120, 100),
Expand Down Expand Up @@ -351,7 +351,7 @@ def test_column_flex_no_hints():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(50, 50),
Expand Down Expand Up @@ -426,7 +426,7 @@ def test_column_flex():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 300),
Expand Down Expand Up @@ -507,7 +507,7 @@ def test_column_flex_insufficient_space():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 200),
Expand Down Expand Up @@ -573,7 +573,7 @@ def test_column_flex_insufficient_space_no_flex():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 220),
Expand Down Expand Up @@ -627,7 +627,7 @@ def test_column_flex_grandchild_min_size():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 120),
Expand Down
8 changes: 4 additions & 4 deletions core/tests/style/pack/layout/test_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_gap():
)

# No gap
root.style.layout(root, viewport)
root.style.layout(viewport)
assert_layout(
root,
(130, 100),
Expand All @@ -45,7 +45,7 @@ def test_gap():

# Row, LTR
root.style.update(direction="row", text_direction="ltr")
root.style.layout(root, viewport)
root.style.layout(viewport)
assert_layout(
root,
(170, 100),
Expand All @@ -63,7 +63,7 @@ def test_gap():

# Row, RTL
root.style.update(direction="row", text_direction="rtl")
root.style.layout(root, viewport)
root.style.layout(viewport)
assert_layout(
root,
(170, 100),
Expand All @@ -81,7 +81,7 @@ def test_gap():

# Column
root.style.update(direction="column")
root.style.layout(root, viewport)
root.style.layout(viewport)
assert_layout(
root,
(100, 170),
Expand Down
10 changes: 5 additions & 5 deletions core/tests/style/pack/layout/test_row_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_top():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(300, 540),
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_center():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(300, 540),
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_bottom():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(300, 540),
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_no_margin():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(300, 540),
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_row_box():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(310, 430),
Expand Down
12 changes: 6 additions & 6 deletions core/tests/style/pack/layout/test_rtl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_row_box_child_layout():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(210, 100),
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_column_box_child_layout():
)

# Normal size
root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 210),
Expand All @@ -78,7 +78,7 @@ def test_align_items_top():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(60, 100),
Expand All @@ -104,7 +104,7 @@ def test_align_items_bottom():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(60, 100),
Expand All @@ -130,7 +130,7 @@ def test_align_items_left():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 60),
Expand All @@ -156,7 +156,7 @@ def test_align_items_right():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(100, 60),
Expand Down
4 changes: 2 additions & 2 deletions core/tests/style/pack/layout/test_tutorial0.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_tutorial_0():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(220, 130),
Expand All @@ -41,7 +41,7 @@ def test_vertical():
],
)

root.style.layout(root, ExampleViewport(480, 640))
root.style.layout(ExampleViewport(480, 640))
assert_layout(
root,
(130, 220),
Expand Down
2 changes: 1 addition & 1 deletion core/tests/style/pack/layout/test_tutorial1.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_tutorial_1():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(380, 120),
Expand Down
2 changes: 1 addition & 1 deletion core/tests/style/pack/layout/test_tutorial3.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_tutorial_3():
],
)

root.style.layout(root, ExampleViewport(640, 480))
root.style.layout(ExampleViewport(640, 480))
assert_layout(
root,
(170, 125),
Expand Down

0 comments on commit 4d0a179

Please sign in to comment.