From d4520d187e87a7c2c616f1eeb357e32244fa980a Mon Sep 17 00:00:00 2001 From: fan wu <336798967+FanWu-ai@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:36:57 -0700 Subject: [PATCH] Preserve maximum sizes of zero-preferred dimensions Do not discard a finite maximum merely because a child prefers zero. Keep hidden zero-size dimensions ignored and add real layout regressions. AI assistance: OpenAI Codex. --- src/prompt_toolkit/layout/dimension.py | 2 +- tests/test_layout.py | 28 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/prompt_toolkit/layout/dimension.py b/src/prompt_toolkit/layout/dimension.py index 28fdc9b95..08bbc4ef5 100644 --- a/src/prompt_toolkit/layout/dimension.py +++ b/src/prompt_toolkit/layout/dimension.py @@ -140,7 +140,7 @@ def max_layout_dimensions(dimensions: list[Dimension]) -> Dimension: return Dimension.zero() # Ignore empty dimensions. (They should not reduce the size of others.) - dimensions = [d for d in dimensions if d.preferred != 0 and d.max != 0] + dimensions = [d for d in dimensions if d.max != 0] if dimensions: # Take the highest minimum dimension. diff --git a/tests/test_layout.py b/tests/test_layout.py index cbbbcd0bd..572b65990 100644 --- a/tests/test_layout.py +++ b/tests/test_layout.py @@ -5,6 +5,7 @@ from prompt_toolkit.layout import InvalidLayoutError, Layout from prompt_toolkit.layout.containers import HSplit, VSplit, Window from prompt_toolkit.layout.controls import BufferControl +from prompt_toolkit.layout.dimension import Dimension, max_layout_dimensions def test_layout_class(): @@ -51,3 +52,30 @@ def test_layout_class(): def test_create_invalid_layout(): with pytest.raises(InvalidLayoutError): Layout(HSplit([])) + + +@pytest.mark.parametrize( + "dimensions, expected", + [ + ([Dimension(max=10)], (0, 10, 0)), + ([Dimension(max=10, preferred=0)], (0, 10, 0)), + ([Dimension.zero(), Dimension(max=10)], (0, 10, 0)), + ([Dimension(), Dimension(max=10)], (0, 10, 0)), + ([Dimension(max=10), Dimension(max=20)], (0, 10, 0)), + ([Dimension(max=10), Dimension(max=20, preferred=5)], (0, 10, 5)), + ([Dimension(max=10), Dimension(max=20, preferred=12)], (0, 12, 12)), + ([], (0, 0, 0)), + ([Dimension.zero()], (0, 0, 0)), + ([Dimension.zero(), Dimension.exact(8)], (8, 8, 8)), + ], +) +def test_max_layout_dimensions_preserves_zero_preferred_max(dimensions, expected): + dimension = max_layout_dimensions(dimensions) + assert (dimension.min, dimension.max, dimension.preferred) == expected + + +def test_nested_split_preserves_zero_preferred_width_limit(): + child = HSplit([Window(width=Dimension(max=10, preferred=0))]) + + assert child.preferred_width(80).max == 10 + assert VSplit([child, Window()])._divide_widths(80) == [10, 0, 70]