From 3db737b7e10b980b8e3ad0efa1524aa46c45078f Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 6 Nov 2025 13:41:46 +0000 Subject: [PATCH 1/3] expose grid-size --- src/textual/layouts/grid.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/textual/layouts/grid.py b/src/textual/layouts/grid.py index c6f518360b..a765934f33 100644 --- a/src/textual/layouts/grid.py +++ b/src/textual/layouts/grid.py @@ -29,6 +29,17 @@ def __init__(self) -> None: """Shrink the grid to fit the container if it is larger.""" self.auto_minimum: bool = False """If self.shrink is `True`, auto-detect and limit the width.""" + self._grid_size: tuple[int, int] | None = None + """Grid size after last arrange call.""" + + @property + def grid_size(self) -> tuple[int, int] | None: + """The grid size after the last arrange call. + + Returns: + A tuple of (WIDTH, HEIGHT) or `None` prior to the first `arrange` + """ + return self._grid_size def arrange( self, parent: Widget, children: list[Widget], size: Size, greedy: bool = True @@ -60,6 +71,9 @@ def arrange( table_size_columns -= 1 table_size_rows = styles.grid_size_rows + + self._grid_size = (table_size_columns, table_size_rows) + viewport = parent.app.viewport_size keyline_style, _keyline_color = styles.keyline offset = (0, 0) From 0b27ffc4eff826a1c325951f08618c6e824cacfd Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 6 Nov 2025 13:56:34 +0000 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 4 ++++ src/textual/layouts/grid.py | 8 +++----- tests/layouts/test_grid.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/layouts/test_grid.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 64025599d8..5dae36477b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed `TextArea` cursor display on wrapped lines https://github.com/Textualize/textual/pull/6196 - Fixed `remove_children` not refreshing layout https://github.com/Textualize/textual/pull/6206 +### Added + +- Added `grid_size` property to `GridLayout` https://github.com/Textualize/textual/pull/6210 + ## [6.5.0] - 2025-10-31 ### Added diff --git a/src/textual/layouts/grid.py b/src/textual/layouts/grid.py index a765934f33..fac6aee6e9 100644 --- a/src/textual/layouts/grid.py +++ b/src/textual/layouts/grid.py @@ -72,8 +72,6 @@ def arrange( table_size_rows = styles.grid_size_rows - self._grid_size = (table_size_columns, table_size_rows) - viewport = parent.app.viewport_size keyline_style, _keyline_color = styles.keyline offset = (0, 0) @@ -171,9 +169,9 @@ def repeat_scalars(scalars: Iterable[Scalar], count: int) -> list[Scalar]: cell_coord = next_coord() column_scalars = repeat_scalars(column_scalars, table_size_columns) - row_scalars = repeat_scalars( - row_scalars, table_size_rows if table_size_rows else row + 1 - ) + table_size_rows = table_size_rows if table_size_rows else row + 1 + row_scalars = repeat_scalars(row_scalars, table_size_rows) + self._grid_size = (table_size_columns, table_size_rows) def apply_width_limits(widget: Widget, width: int) -> int: """Apply min and max widths to dimension. diff --git a/tests/layouts/test_grid.py b/tests/layouts/test_grid.py new file mode 100644 index 0000000000..e781249ab7 --- /dev/null +++ b/tests/layouts/test_grid.py @@ -0,0 +1,34 @@ +from textual import containers, widgets +from textual.app import App, ComposeResult +from textual.layouts.grid import GridLayout + + +async def test_grid_size(): + """Test the `grid_size` property on GridLayout.""" + + class GridApp(App): + CSS = """ + Grid { + grid-size: 3; + grid-columns: auto; + height: auto; + Label { + padding: 2 4; + border: blue; + } + } + """ + + def compose(self) -> ComposeResult: + with containers.VerticalScroll(): + with containers.Grid(): + for _ in range(7): + yield widgets.Label("Hello, World!") + + app = GridApp() + async with app.run_test() as pilot: + await pilot.pause() + await app.wait_for_refresh() + grid_layout = app.query_one(containers.Grid).layout + assert isinstance(grid_layout, GridLayout) + assert grid_layout.grid_size == (3, 3) From 6e728c77eacce1af45df80283172ea4e3b385913 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 6 Nov 2025 13:57:15 +0000 Subject: [PATCH 3/3] docstring --- src/textual/layouts/grid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/textual/layouts/grid.py b/src/textual/layouts/grid.py index fac6aee6e9..d8f389f38c 100644 --- a/src/textual/layouts/grid.py +++ b/src/textual/layouts/grid.py @@ -37,7 +37,7 @@ def grid_size(self) -> tuple[int, int] | None: """The grid size after the last arrange call. Returns: - A tuple of (WIDTH, HEIGHT) or `None` prior to the first `arrange` + A tuple of (WIDTH, HEIGHT) or `None` prior to the first `arrange`. """ return self._grid_size