1use gpui::{
2 AnyElement, App, Axis, DefiniteLength, IntoElement, ParentElement, RenderOnce, SharedString,
3 Styled, Window, div, prelude::FluentBuilder as _, px, relative,
4};
5use gpui_base::LogicalStyledExt as _;
6
7use crate::{ActiveTheme as _, AxisExt, Sizable, Size, h_flex, text::Text, v_flex};
8
9#[derive(IntoElement)]
11pub struct DescriptionList {
12 items: Vec<DescriptionItem>,
13 size: Size,
14 layout: Axis,
15 label_width: DefiniteLength,
16 bordered: bool,
17 columns: usize,
18}
19
20pub enum DescriptionItem {
22 Item {
23 label: DescriptionText,
24 value: DescriptionText,
25 span: usize,
26 },
27 Separator,
28}
29
30#[derive(IntoElement)]
32pub enum DescriptionText {
33 String(SharedString),
34 Text(Text),
35 AnyElement(AnyElement),
36}
37
38impl From<&str> for DescriptionText {
39 fn from(text: &str) -> Self {
40 DescriptionText::String(SharedString::from(text.to_string()))
41 }
42}
43
44impl From<Text> for DescriptionText {
45 fn from(text: Text) -> Self {
46 DescriptionText::Text(text)
47 }
48}
49
50impl From<AnyElement> for DescriptionText {
51 fn from(element: AnyElement) -> Self {
52 DescriptionText::AnyElement(element)
53 }
54}
55
56impl From<SharedString> for DescriptionText {
57 fn from(text: SharedString) -> Self {
58 DescriptionText::String(text)
59 }
60}
61
62impl From<String> for DescriptionText {
63 fn from(text: String) -> Self {
64 DescriptionText::String(SharedString::from(text))
65 }
66}
67
68impl RenderOnce for DescriptionText {
69 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
70 match self {
71 DescriptionText::String(text) => div().child(text).into_any_element(),
72 DescriptionText::Text(text) => text.into_any_element(),
73 DescriptionText::AnyElement(element) => element,
74 }
75 }
76}
77
78impl DescriptionItem {
79 pub fn new(label: impl Into<DescriptionText>) -> Self {
83 DescriptionItem::Item {
84 label: label.into(),
85 value: "".into(),
86 span: 1,
87 }
88 }
89
90 pub fn value(mut self, value: impl Into<DescriptionText>) -> Self {
92 let new_value = value.into();
93 if let DescriptionItem::Item { value, .. } = &mut self {
94 *value = new_value;
95 }
96 self
97 }
98
99 pub fn span(mut self, span: usize) -> Self {
103 let val = span;
104 if let DescriptionItem::Item { span, .. } = &mut self {
105 *span = val;
106 }
107 self
108 }
109
110 fn _label(&self) -> Option<&DescriptionText> {
111 match self {
112 DescriptionItem::Item { label, .. } => Some(label),
113 _ => None,
114 }
115 }
116
117 fn _span(&self) -> Option<usize> {
118 match self {
119 DescriptionItem::Item { span, .. } => Some(*span),
120 _ => None,
121 }
122 }
123}
124
125impl DescriptionList {
126 pub fn new() -> Self {
128 Self {
129 items: Vec::new(),
130 layout: Axis::Horizontal,
131 label_width: px(120.).into(),
132 size: Size::default(),
133 bordered: true,
134 columns: 3,
135 }
136 }
137
138 pub fn vertical() -> Self {
140 Self::new().layout(Axis::Vertical)
141 }
142
143 pub fn horizontal() -> Self {
145 Self::new().layout(Axis::Horizontal)
146 }
147
148 pub fn label_width(mut self, label_width: impl Into<DefiniteLength>) -> Self {
152 self.label_width = label_width.into();
153 self
154 }
155
156 pub fn layout(mut self, layout: Axis) -> Self {
158 self.layout = layout;
159 self
160 }
161
162 pub fn bordered(mut self, bordered: bool) -> Self {
166 self.bordered = bordered;
167 self
168 }
169
170 pub fn columns(mut self, columns: usize) -> Self {
174 self.columns = columns.clamp(1, 10);
175 self
176 }
177
178 pub fn item(
180 mut self,
181 label: impl Into<DescriptionText>,
182 value: impl Into<DescriptionText>,
183 span: usize,
184 ) -> Self {
185 self.items.push(DescriptionItem::Item {
186 label: label.into(),
187 value: value.into(),
188 span,
189 });
190 self
191 }
192
193 pub fn child(mut self, child: impl Into<DescriptionItem>) -> Self {
195 self.items.push(child.into());
196 self
197 }
198
199 pub fn children(
201 mut self,
202 children: impl IntoIterator<Item = impl Into<DescriptionItem>>,
203 ) -> Self {
204 self.items
205 .extend(children.into_iter().map(Into::into).collect::<Vec<_>>());
206 self
207 }
208
209 pub fn separator(mut self) -> Self {
211 self.items.push(DescriptionItem::Separator);
212 self
213 }
214
215 fn group_item_rows(items: Vec<DescriptionItem>, columns: usize) -> Vec<Vec<DescriptionItem>> {
216 let mut rows = vec![];
217 let mut current_span = 0;
218 for item in items.into_iter() {
219 let span = item._span().unwrap_or(columns);
220 if rows.is_empty() {
221 rows.push(vec![]);
222 }
223 if current_span + span > columns {
224 rows.push(vec![]);
225 current_span = 0;
226 }
227 let last_group = rows.last_mut().unwrap();
228 last_group.push(item);
229 current_span += span;
230 }
231 while let Some(last_group) = rows.last() {
233 if !last_group.is_empty() {
234 break;
235 }
236
237 rows.pop();
238 }
239
240 rows
241 }
242}
243
244impl Sizable for DescriptionList {
245 fn with_size(mut self, size: impl Into<Size>) -> Self {
246 self.size = size.into();
247 self
248 }
249}
250
251impl RenderOnce for DescriptionList {
252 fn render(self, _: &mut Window, cx: &mut gpui::App) -> impl gpui::IntoElement {
253 let base_gap = match self.size {
254 Size::XSmall | Size::Small => px(2.),
255 Size::Medium => px(4.),
256 Size::Large => px(8.),
257 _ => px(4.),
258 };
259
260 let (mut padding_x, mut padding_y) = match self.size {
262 Size::XSmall | Size::Small => (px(4.), px(2.)),
263 Size::Medium => (px(8.), px(4.)),
264 Size::Large => (px(12.), px(6.)),
265 _ => (px(8.), px(4.)),
266 };
267
268 let label_width = if self.layout.is_horizontal() {
269 Some(self.label_width)
270 } else {
271 None
272 };
273 if !self.bordered {
274 padding_x = px(0.);
275 padding_y = px(0.);
276 }
277 let gap = if self.bordered { px(0.) } else { base_gap };
278
279 let rows = Self::group_item_rows(self.items, self.columns);
281 let rows_len = rows.len();
282
283 v_flex()
284 .gap(gap)
285 .overflow_hidden()
286 .when(self.bordered, |this| {
287 this.rounded(cx.theme().radius)
288 .border_1()
289 .border_color(cx.theme().border)
290 })
291 .children(rows.into_iter().enumerate().map(|(ix, items)| {
292 let is_last = ix == rows_len - 1;
293 h_flex()
294 .flex_row_dir(cx)
295 .when(self.bordered && !is_last, |this| {
296 this.border_b_1().border_color(cx.theme().border)
297 })
298 .children({
299 items.into_iter().enumerate().map(|(item_ix, item)| {
300 let is_first_col = item_ix == 0;
301
302 match item {
303 DescriptionItem::Item { label, value, span } => {
304 let el = if self.layout.is_vertical() {
305 v_flex()
306 } else {
307 div().flex_row_dir(cx).h_full()
308 };
309
310 el.flex_1()
311 .flex_basis(relative((span as f32) / (self.columns as f32)))
312 .overflow_x_hidden()
313 .child(
314 div()
315 .when(self.layout.is_horizontal(), |this| {
316 this.h_full()
317 })
318 .text_color(
319 cx.theme().description_list_label_foreground,
320 )
321 .text_sm()
322 .px(padding_x)
323 .py(padding_y)
324 .when(self.bordered, |this| {
325 this.when(self.layout.is_horizontal(), |this| {
326 this.border_e(px(1.), cx)
327 .when(!is_first_col, |this| {
328 this.border_s(px(1.), cx)
329 })
330 })
331 .when(self.layout.is_vertical(), |this| {
332 this.border_b_1()
333 })
334 .border_color(cx.theme().border)
335 .bg(cx.theme().tokens.description_list_label)
336 })
337 .map(|this| match label_width {
338 Some(label_width) => {
339 this.w(label_width).flex_shrink_0()
340 }
341 None => this,
342 })
343 .child(label),
344 )
345 .child(
346 div()
347 .flex_1()
348 .px(padding_x)
349 .py(padding_y)
350 .overflow_hidden()
351 .child(value),
352 )
353 }
354 _ => div().h_2().w_full().when(self.bordered, |this| {
355 this.bg(cx.theme().tokens.description_list_label)
356 }),
357 }
358 })
359 })
360 }))
361 }
362}
363
364#[cfg(test)]
365mod tests {
366 use super::DescriptionItem;
367
368 #[test]
369 fn test_group_item_rows() {
370 let items = vec![
371 DescriptionItem::new("test1"),
372 DescriptionItem::new("test2").span(2),
373 DescriptionItem::new("test3"),
374 DescriptionItem::new("test4"),
375 DescriptionItem::new("test5"),
376 DescriptionItem::new("test6").span(3),
377 DescriptionItem::new("test7"),
378 ];
379 let rows = super::DescriptionList::group_item_rows(items, 3);
380 assert_eq!(rows.len(), 4);
381 assert_eq!(rows[0].len(), 2);
382 assert_eq!(rows[1].len(), 3);
383 assert_eq!(rows[2].len(), 1);
384 assert_eq!(rows[3].len(), 1);
385 }
386}