Skip to content

Commit

Permalink
Merge pull request #5444 from Textualize/grid-sizing
Browse files Browse the repository at this point in the history
fix grid sizing
  • Loading branch information
willmcgugan authored Dec 29, 2024
2 parents d34f4a4 + cb48fa9 commit 6bc9501
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/textual/layouts/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def apply_height_limits(widget: Widget, height: int) -> int:
+ gutter_height,
)
height = max(height, widget_height)

row_scalars[row] = Scalar.from_number(height)

rows = resolve(row_scalars, size.height, gutter_horizontal, size, viewport)
Expand All @@ -271,12 +272,15 @@ def apply_height_limits(widget: Widget, height: int) -> int:
x2, cell_width = columns[min(max_column, column + column_span)]
y2, cell_height = rows[min(max_row, row + row_span)]
cell_size = Size(cell_width + x2 - x, cell_height + y2 - y)

box_width, box_height, margin = widget._get_box_model(
cell_size,
viewport,
Fraction(cell_size.width),
Fraction(cell_size.height),
constrain_width=True,
)

if self.stretch_height and len(children) > 1:
if box_height <= cell_size.height:
box_height = Fraction(cell_size.height)
Expand Down
7 changes: 6 additions & 1 deletion src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,14 +1455,16 @@ def _get_box_model(
viewport: Size,
width_fraction: Fraction,
height_fraction: Fraction,
constrain_width: bool = False,
) -> BoxModel:
"""Process the box model for this widget.
Args:
container: The size of the container widget (with a layout)
container: The size of the container widget (with a layout).
viewport: The viewport size.
width_fraction: A fraction used for 1 `fr` unit on the width dimension.
height_fraction: A fraction used for 1 `fr` unit on the height dimension.
constrain_width: Restrict the width to the container width.
Returns:
The size and margin for this widget.
Expand Down Expand Up @@ -1534,6 +1536,9 @@ def _get_box_model(

content_width = max(Fraction(0), content_width)

if constrain_width:
content_width = min(Fraction(container.width - gutter.width), content_width)

if styles.height is None:
# No height specified, fill the available space
content_height = Fraction(content_container.height - margin.height)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Grid,
Middle,
Vertical,
VerticalGroup,
VerticalScroll,
HorizontalGroup,
)
Expand Down Expand Up @@ -3193,3 +3194,39 @@ def compose(self):
yield MyListView()

snap_compare(TUI(), press=["down", "enter", "down", "down", "enter"])


def test_widgets_in_grid(snap_compare):
"""You should see a 3x3 grid of labels where the text is wrapped, and there is no superfluous space."""
TEXT = """I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.
And when it has gone past, I will turn the inner eye to see its path.
Where the fear has gone there will be nothing. Only I will remain."""

class MyApp(App):
CSS = """
VerticalGroup {
layout: grid;
grid-size: 3 3;
grid-columns: 1fr;
grid-rows: auto;
height: auto;
background: blue;
}
Label {
border: heavy red;
text-align: left;
}
"""

def compose(self) -> ComposeResult:
with VerticalGroup():
for n in range(9):
label = Label(TEXT, id=f"label{n}")
label.border_title = str(n)
yield label

snap_compare(MyApp(), terminal_size=(100, 50))

0 comments on commit 6bc9501

Please sign in to comment.