From f358b9531a3311b449c3fde41a04f7bfc0c574f2 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Tue, 16 Dec 2025 17:09:11 +0530 Subject: [PATCH] gpui: Add grid repeat min content API (#44973) Required for https://github.com/zed-industries/zed/pull/44712 We started using `grid` for Markdown tables instead of flex. This resulted in tables having a width of 0 inside popovers, since popovers are laid out using `AvailableSpace::MinContent`. One way to fix this is to lay out popovers using `MaxContent` instead. But that would affect all Markdown rendered in popovers and could change how popovers look, or regress things. The other option is to fix it where the problem actually is: `repeat(count, vec![minmax(length(0.0), fr(1.0))])`. Since the minimum width here is `0`, laying things out with `MinContent` causes the Markdown table to shrink completely. What we want instead is for the minimum width to be the min-content size, but only for Markdown rendered inside popovers. This PR does exactly that, without interfering with the `grid_cols` API, which intentionally follows a TailwindCSS-like convention. See https://github.com/zed-industries/zed/pull/44368 for context. Release Notes: - N/A --- crates/gpui/src/style.rs | 5 +++++ crates/gpui/src/styled.rs | 7 +++++++ crates/gpui/src/taffy.rs | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/gpui/src/style.rs b/crates/gpui/src/style.rs index 446c3ad2a325681a39689577a261ed1ffdde6d5b..4d6e6f490d81d967692a3e9d8316af75a7a4d306 100644 --- a/crates/gpui/src/style.rs +++ b/crates/gpui/src/style.rs @@ -265,6 +265,10 @@ pub struct Style { /// Equivalent to the Tailwind `grid-cols-` pub grid_cols: Option, + /// The grid columns with min-content minimum sizing. + /// Unlike grid_cols, it won't shrink to width 0 in AvailableSpace::MinContent constraints. + pub grid_cols_min_content: Option, + /// The row span of this element /// Equivalent to the Tailwind `grid-rows-` pub grid_rows: Option, @@ -772,6 +776,7 @@ impl Default for Style { opacity: None, grid_rows: None, grid_cols: None, + grid_cols_min_content: None, grid_location: None, #[cfg(debug_assertions)] diff --git a/crates/gpui/src/styled.rs b/crates/gpui/src/styled.rs index e01649be481e27f89643db2ffb3a9ccd294b9b73..e8088a84d7fc141d0a320988c6399afe2b93ce07 100644 --- a/crates/gpui/src/styled.rs +++ b/crates/gpui/src/styled.rs @@ -637,6 +637,13 @@ pub trait Styled: Sized { self } + /// Sets the grid columns with min-content minimum sizing. + /// Unlike grid_cols, it won't shrink to width 0 in AvailableSpace::MinContent constraints. + fn grid_cols_min_content(mut self, cols: u16) -> Self { + self.style().grid_cols_min_content = Some(cols); + self + } + /// Sets the grid rows of this element. fn grid_rows(mut self, rows: u16) -> Self { self.style().grid_rows = Some(rows); diff --git a/crates/gpui/src/taffy.rs b/crates/gpui/src/taffy.rs index 11cb0872861321c3c06c3f8a5bf79fdd30eb2275..99a50b87c8aa9f40a7694f1c2084b10f6d0a9315 100644 --- a/crates/gpui/src/taffy.rs +++ b/crates/gpui/src/taffy.rs @@ -8,6 +8,7 @@ use std::{fmt::Debug, ops::Range}; use taffy::{ TaffyTree, TraversePartialTree as _, geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize}, + prelude::min_content, style::AvailableSpace as TaffyAvailableSpace, tree::NodeId, }; @@ -314,6 +315,14 @@ impl ToTaffy for Style { .unwrap_or_default() } + fn to_grid_repeat_min_content( + unit: &Option, + ) -> Vec> { + // grid-template-columns: repeat(, minmax(min-content, 1fr)); + unit.map(|count| vec![repeat(count, vec![minmax(min_content(), fr(1.0))])]) + .unwrap_or_default() + } + taffy::style::Style { display: self.display.into(), overflow: self.overflow.into(), @@ -338,7 +347,11 @@ impl ToTaffy for Style { flex_grow: self.flex_grow, flex_shrink: self.flex_shrink, grid_template_rows: to_grid_repeat(&self.grid_rows), - grid_template_columns: to_grid_repeat(&self.grid_cols), + grid_template_columns: if self.grid_cols_min_content.is_some() { + to_grid_repeat_min_content(&self.grid_cols_min_content) + } else { + to_grid_repeat(&self.grid_cols) + }, grid_row: self .grid_location .as_ref()