@@ -20,9 +20,13 @@ class GridLayout(Layout):
2020
2121 def __init__ (self ) -> None :
2222 self .min_column_width : int | None = None
23+ """Maintain a minimum column width, or `None` for no minimum."""
24+ self .max_column_width : int | None = None
25+ """Maintain a maximum column width, or `None` for no maximum."""
2326 self .stretch_height : bool = False
2427 """Stretch the height of cells to be equal in each row."""
2528 self .regular : bool = False
29+ """Grid should be regular (no remainder in last row)."""
2630 self .expand : bool = False
2731 """Expand the grid to fit the container if it is smaller."""
2832 self .shrink : bool = False
@@ -57,14 +61,23 @@ def arrange(
5761
5862 table_size_columns = max (1 , styles .grid_size_columns )
5963 min_column_width = self .min_column_width
64+ max_column_width = self .max_column_width
65+
66+ container_width = size .width
67+ if max_column_width is not None :
68+ container_width = (
69+ max (1 , min (len (children ), (container_width // max_column_width )))
70+ * max_column_width
71+ )
72+ size = Size (container_width , size .height )
6073
6174 if min_column_width is not None :
62- container_width = size .width
6375 table_size_columns = max (
6476 1 ,
6577 (container_width + gutter_horizontal )
6678 // (min_column_width + gutter_horizontal ),
6779 )
80+
6881 table_size_columns = min (table_size_columns , len (children ))
6982 if self .regular :
7083 while len (children ) % table_size_columns and table_size_columns > 1 :
@@ -139,8 +152,7 @@ def repeat_scalars(scalars: Iterable[Scalar], count: int) -> list[Scalar]:
139152 cell_map : dict [tuple [int , int ], tuple [Widget , bool ]] = {}
140153 cell_size_map : dict [Widget , tuple [int , int , int , int ]] = {}
141154
142- column_count = table_size_columns
143- next_coord = iter (cell_coords (column_count )).__next__
155+ next_coord = iter (cell_coords (table_size_columns )).__next__
144156 cell_coord = (0 , 0 )
145157 column = row = 0
146158
0 commit comments