From 0231c152fcbbe11a82a897902d46673dcc057f99 Mon Sep 17 00:00:00 2001 From: Johann Tuffe Date: Fri, 25 Apr 2025 07:00:10 +0800 Subject: [PATCH] fix number of rows in grid when evenly distributed (#2896) * fix number of rows in grid when evenly distributed * use div_ceil --------- Co-authored-by: Johann Tuffe --- widget/src/grid.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/widget/src/grid.rs b/widget/src/grid.rs index 62471cfe..da827007 100644 --- a/widget/src/grid.rs +++ b/widget/src/grid.rs @@ -193,8 +193,6 @@ where Constraint::Amount(amount) => amount, }; - let total_rows = self.children.len() / cells_per_row; - let cell_width = (available.width - self.spacing * (cells_per_row - 1) as f32) / cells_per_row as f32; @@ -202,10 +200,13 @@ where let cell_height = match self.height { Sizing::AspectRatio(ratio) => Some(cell_width / ratio), Sizing::EvenlyDistribute(Length::Shrink) => None, - Sizing::EvenlyDistribute(_) => Some( - (available.height - self.spacing * (total_rows - 1) as f32) - / total_rows as f32, - ), + Sizing::EvenlyDistribute(_) => { + let total_rows = self.children.len().div_ceil(cells_per_row); + Some( + (available.height - self.spacing * (total_rows - 1) as f32) + / total_rows as f32, + ) + } }; let cell_limits = layout::Limits::new( @@ -213,7 +214,7 @@ where Size::new(cell_width, cell_height.unwrap_or(available.height)), ); - let mut nodes = Vec::new(); + let mut nodes = Vec::with_capacity(self.children.len()); let mut x = 0.0; let mut y = 0.0; let mut row_height = 0.0f32;