1use crate::*;
3
4#[derive(Clone, Copy)]
6pub struct GridListItem<'a> {
7 pub label: &'a str,
8 pub subtitle: Option<&'a str>,
9 pub icon: Option<MaterialIcon>,
10 pub image: Option<&'a Image>,
11}
12
13#[derive(Clone, Copy, Debug)]
15pub struct GridListOptions {
16 pub columns: usize,
17 pub tile_height: f32,
18 pub gap: f32,
19 pub radius: f32,
20 pub show_caption_scrim: bool,
21}
22
23impl Default for GridListOptions {
24 fn default() -> Self {
25 Self {
26 columns: 2,
27 tile_height: 120.0,
28 gap: 12.0,
29 radius: 12.0,
30 show_caption_scrim: true,
31 }
32 }
33}
34
35impl Frame<'_> {
36 pub fn grid_list(
40 &mut self,
41 id: u64,
42 items: &[GridListItem<'_>],
43 selected: Option<usize>,
44 options: GridListOptions,
45 ) -> Option<usize> {
46 if items.is_empty() {
47 return None;
48 }
49 let cols = options.columns.max(1);
50 let rows = items.len().div_ceil(cols);
51 let tile_h = options.tile_height.max(48.0);
52 let gap = options.gap.max(0.0);
53 let t = self.theme();
54 let mut clicked = None;
55
56 for row in 0..rows {
57 let row_rect = self.reserve(tile_h);
58 let tile_w = ((row_rect.width - gap * (cols - 1) as f32) / cols as f32).max(0.0);
59 for col in 0..cols {
60 let idx = row * cols + col;
61 let Some(item) = items.get(idx) else {
62 break;
63 };
64 let tile = Rect {
65 x: row_rect.x + col as f32 * (tile_w + gap),
66 y: row_rect.y,
67 width: tile_w,
68 height: tile_h,
69 };
70 let is_sel = selected == Some(idx);
71 let r = self.interact(id.wrapping_add(idx as u64), tile, true);
72 let bg = if is_sel { t.primary_container } else { t.surface_high };
73 let fg = if is_sel { t.on_primary_container } else { t.text };
74 self.paint_rect(tile, options.radius, bg);
75 if let Some(img) = item.image {
76 self.paint_image(tile, img, ImageFit::Cover, options.radius);
77 } else if let Some(icon) = item.icon {
78 let icon_sz = 32.0_f32.min(tile.width * 0.4).min(tile.height * 0.4);
79 let icon_rect = Rect {
80 x: tile.x + (tile.width - icon_sz) * 0.5,
81 y: tile.y + (tile.height - 36.0 - icon_sz).max(8.0) * 0.5,
82 width: icon_sz,
83 height: icon_sz,
84 };
85 self.paint_material_icon(icon_rect, icon, fg);
86 }
87 let caption_h = (if item.subtitle.is_some() { 48.0_f32 } else { 36.0 }).min(tile.height);
88 let caption = Rect {
89 x: tile.x,
90 y: tile.y + tile.height - caption_h,
91 width: tile.width,
92 height: caption_h,
93 };
94 let (cap_fg, sub_fg) = if item.image.is_some() && options.show_caption_scrim {
95 self.paint_asym_rect(caption, 0.0, options.radius, Color(0, 0, 0, 148));
96 (Color(255, 255, 255, 255), Color(255, 255, 255, 200))
97 } else {
98 (fg, if is_sel { t.on_primary_container.opacity(0.8) } else { t.on_surface_variant })
99 };
100 if let Some(sub) = item.subtitle {
101 self.styled_text(
102 Rect { x: caption.x + 12.0, y: caption.y + 6.0, width: (caption.width - 24.0).max(0.0), height: 18.0 },
103 item.label,
104 cap_fg,
105 TextStyle { size: 14.0, weight: 500, ..TextStyle::default() },
106 );
107 self.styled_text(
108 Rect { x: caption.x + 12.0, y: caption.y + 24.0, width: (caption.width - 24.0).max(0.0), height: 16.0 },
109 sub,
110 sub_fg,
111 TextStyle { size: 12.0, ..TextStyle::default() },
112 );
113 } else {
114 self.styled_text(
115 Rect { x: caption.x + 12.0, width: (caption.width - 24.0).max(0.0), ..caption },
116 item.label,
117 cap_fg,
118 TextStyle { size: 14.0, weight: 500, align: if item.image.is_none() { 1 } else { 0 }, ..TextStyle::default() },
119 );
120 }
121 if is_sel {
122 self.commands.push(DrawCommand::Outline {
123 bounds: tile,
124 radius: options.radius,
125 width: 2.0,
126 color: t.primary,
127 });
128 }
129 self.state_layer(r, options.radius, fg);
130 self.focus_ring(r);
131 self.semantic(r, Role::Button, item.label, is_sel, None, None, None);
132 if r.clicked {
133 clicked = Some(idx);
134 }
135 }
136 }
137 clicked
138 }
139}
140
141#[cfg(test)]
142#[path = "grid_list.spec.rs"]
143mod tests;