1use crate::*;
2
3#[derive(Clone, Debug, Default)]
4pub struct ListState {
5 pub offset: f32,
6 pub selected: Option<usize>,
7}
8
9impl Frame<'_> {
10 pub fn list_item(&mut self, id: u64, label: &str, selected: bool, enabled: bool) -> Response {
11 let b = self.allocate(48.0);
12 let r = self.interact(id, b, enabled);
13 let t = self.ui.theme;
14 let amount = self.animate(
15 id,
16 3,
17 if selected { 1.0 } else { 0.0 },
18 if selected { 1.0 } else { 0.0 },
19 );
20 let base = t.background.mix(t.secondary, amount);
21 let feedback = if self.ui.motion_preference == MotionPreference::Full {
22 Response {
23 pressed: false,
24 ..r
25 }
26 } else {
27 r
28 };
29 let color = self.control_color(feedback, base);
30 self.rect(b, 0.0, color);
31 self.ink(r, 0.0, t.text);
32 self.styled_text(
33 b,
34 label,
35 t.text.opacity(if r.enabled { 1.0 } else { 0.38 }),
36 TextStyle {
37 padding: 16.0,
38 letter_spacing: 0.5,
39 ..TextStyle::default()
40 },
41 );
42 self.focus_ring(r);
43 self.semantic(r, Role::ListItem, label, selected, None, None, None);
44 r
45 }
46 pub fn selection_item(
47 &mut self,
48 id: u64,
49 label: &str,
50 selected: bool,
51 enabled: bool,
52 ) -> Response {
53 let r = self.list_item(id, label, false, enabled);
54 let t = self.ui.theme;
55 let b = Rect {
56 x: r.bounds.x + r.bounds.width - 38.0,
57 y: r.bounds.y + (r.bounds.height - 20.0) * 0.5,
58 width: 20.0,
59 height: 20.0,
60 };
61 let color = if selected {
62 t.primary
63 } else {
64 t.on_surface_variant
65 }
66 .opacity(if r.enabled { 1.0 } else { 0.38 });
67 self.rect(b, 10.0, color);
68 self.rect(b.inset(2.0), 8.0, t.background);
69 if selected {
70 self.rect(b.inset(5.0), 5.0, color);
71 }
72 if let Some(node) = self.ui.semantics.last_mut() {
73 node.selected = selected;
74 }
75 r
76 }
77 pub fn virtual_list(
79 &mut self,
80 id: u64,
81 height: f32,
82 count: usize,
83 state: &mut ListState,
84 build: impl FnMut(&mut Self, usize, bool) -> Response,
85 ) -> Response {
86 self.virtual_list_inner(id, height, count, state, 0.0, 40.0, build)
87 }
88 pub fn virtual_list_padded(
89 &mut self,
90 id: u64,
91 height: f32,
92 count: usize,
93 state: &mut ListState,
94 build: impl FnMut(&mut Self, usize, bool) -> Response,
95 ) -> Response {
96 self.virtual_list_inner(id, height, count, state, 8.0, 40.0, build)
97 }
98 pub fn virtual_list_rows(
100 &mut self,
101 id: u64,
102 height: f32,
103 count: usize,
104 state: &mut ListState,
105 row_height: f32,
106 build: impl FnMut(&mut Self, usize, bool) -> Response,
107 ) -> Response {
108 self.virtual_list_inner(id, height, count, state, 0.0, row_height.max(1.0), build)
109 }
110 #[allow(clippy::too_many_arguments)]
111 fn virtual_list_inner(
112 &mut self,
113 id: u64,
114 height: f32,
115 count: usize,
116 state: &mut ListState,
117 padding: f32,
118 row_height: f32,
119 mut build: impl FnMut(&mut Self, usize, bool) -> Response,
120 ) -> Response {
121 let b = self.allocate(height.max(0.0));
122 if !state.offset.is_finite() {
123 state.offset = 0.0;
124 }
125 let old_input = self.input;
126 self.input.pressed = false;
127 self.input.released = false;
128 let mut r = self.interact(id, b, true);
129 self.input = old_input;
130 let previous_selection = state.selected;
131 let max_offset = (count as f32 * row_height + 2.0 * padding - b.height).max(0.0);
132 self.scroll_viewport(id, b, &mut state.offset, max_offset);
133 if let Some(key) = self.key_for(r)
134 && count > 0
135 {
136 let index = state.selected.unwrap_or(0).min(count - 1);
137 state.selected = Some(match key {
138 Key::Down => (index + 1).min(count - 1),
139 Key::Up => index.saturating_sub(1),
140 Key::Home => 0,
141 Key::End => count - 1,
142 _ => index,
143 });
144 if let Some(selected) = state.selected {
145 let top = padding + selected as f32 * row_height;
146 if top < state.offset {
147 state.offset = top;
148 } else if top + row_height > state.offset + b.height {
149 state.offset = top + row_height - b.height;
150 }
151 }
152 }
153 if state.selected.is_some_and(|i| i >= count) {
155 state.selected = count.checked_sub(1);
156 }
157 self.semantic(r, Role::ListBox, "List", false, None, None, None);
158 let first = ((state.offset - padding).max(0.0) / row_height).floor() as usize;
159 let last = ((state.offset + b.height - padding).max(0.0) / row_height).ceil() as usize;
160 let old_gap = self.gap;
161 self.gap = 0.0;
162 self.within(b, |frame| {
164 let rows_start = frame.ui.semantics.len();
165 for index in first..last.min(count) {
166 frame.cursor = b.y + padding + index as f32 * row_height - state.offset;
167 frame.bounds = Rect {
168 y: frame.cursor,
169 height: row_height,
170 ..b
171 };
172 let row_start = frame.ui.semantics.len();
173 let row = build(frame, index, state.selected == Some(index));
174 if row.clicked {
175 state.selected = Some(index);
176 r.clicked = true;
177 frame.ui.continuous = true;
178 for node in &mut frame.ui.semantics[rows_start..row_start] {
179 if node.role == Role::ListItem {
180 node.selected = false;
181 }
182 }
183 }
184 if let Some(node) = frame.ui.semantics[row_start..]
185 .iter_mut()
186 .find(|node| node.id == row.id && node.role == Role::ListItem)
187 {
188 node.selected = state.selected == Some(index);
189 }
190 }
191 });
192 self.gap = old_gap;
193 r.changed = state.selected != previous_selection;
194 r
195 }
196}
197
198impl Frame<'_> {
199 pub fn virtual_grid(
201 &mut self,
202 id: u64,
203 height: f32,
204 columns: usize,
205 count: usize,
206 state: &mut ListState,
207 mut build: impl FnMut(&mut Self, usize, bool) -> Response,
208 ) -> Response {
209 assert!(columns > 0);
210 let b = self.allocate(height.max(0.0));
211 let cell_height = 64.0;
212 let rows = count.div_ceil(columns);
213 let max = (rows as f32 * cell_height - b.height).max(0.0);
214 let old = self.input;
215 self.input.pressed = false;
216 self.input.released = false;
217 let mut r = self.interact(id, b, true);
218 self.input = old;
219 if r.enabled {
220 self.scroll_viewport(id, b, &mut state.offset, max);
221 }
222 let previous = state.selected;
223 if count > 0
224 && let Some(key) = self.key_for(r)
225 {
226 let i = state.selected.unwrap_or(0).min(count - 1);
227 state.selected = Some(match key {
228 Key::Down => (i + columns).min(count - 1),
229 Key::Up => i.saturating_sub(columns),
230 Key::Right => (i + 1).min(count - 1),
231 Key::Left => i.saturating_sub(1),
232 Key::Home => 0,
233 Key::End => count - 1,
234 _ => i,
235 });
236 if let Some(i) = state.selected {
237 let y = (i / columns) as f32 * cell_height;
238 if y < state.offset {
239 state.offset = y;
240 } else if y + cell_height > state.offset + b.height {
241 state.offset = y + cell_height - b.height;
242 }
243 }
244 }
245 state.offset = if state.offset.is_finite() {
246 state.offset.clamp(-b.height*0.12, max+b.height*0.12)
247 } else {
248 0.0
249 };
250 if state.selected.is_some_and(|i| i >= count) {
251 state.selected = count.checked_sub(1);
252 }
253 self.semantic(
254 r,
255 Role::ListBox,
256 "Application grid",
257 false,
258 None,
259 None,
260 None,
261 );
262 let first = (state.offset / cell_height).floor() as usize;
263 let last = ((state.offset + b.height) / cell_height).ceil() as usize;
264 self.within(b, |f| {
265 for row in first..last.min(rows) {
266 for col in 0..columns {
267 let i = row * columns + col;
268 if i >= count {
269 break;
270 }
271 let cell = Rect {
272 x: b.x + col as f32 * b.width / columns as f32,
273 y: b.y + row as f32 * cell_height - state.offset,
274 width: b.width / columns as f32,
275 height: cell_height,
276 };
277 f.within(cell.inset(3.0), |f| {
278 if build(f, i, state.selected == Some(i)).clicked {
279 state.selected = Some(i);
280 f.ui.continuous = true;
281 }
282 });
283 }
284 }
285 });
286 r.changed = state.selected != previous;
287 r
288 }
289
290 pub fn list_item_two_line(
293 &mut self,
294 id: u64,
295 icon: Option<MaterialIcon>,
296 title: &str,
297 subtitle: &str,
298 show_chevron: bool,
299 enabled: bool,
300 ) -> Response {
301 let b = self.allocate(if subtitle.is_empty() { 56.0 } else { 68.0 });
302 let r = self.target(id, b, title, enabled, Role::ListItem);
303 let t = self.theme();
304 self.state_layer(r, 8.0, t.text);
305 self.focus_ring(r);
306 let fg = if r.enabled { t.text } else { t.text.opacity(0.38) };
307 let sub_fg = if r.enabled { t.on_surface_variant } else { t.text.opacity(0.38) };
308 let mut x = b.x + 16.0;
309 if let Some(ic) = icon {
310 let icon_rect = Rect { x, y: b.y + (b.height - 24.0) * 0.5, width: 24.0, height: 24.0 };
311 self.paint_material_icon(icon_rect, ic, sub_fg);
312 x += 40.0;
313 }
314 let trail = if show_chevron { 36.0 } else { 16.0 };
315 let text_w = (b.x + b.width - x - trail).max(0.0);
316 if subtitle.is_empty() {
317 self.styled_text(Rect { x, width: text_w, ..b }, title, fg, TextStyle { size: 16.0, ..TextStyle::default() });
318 } else {
319 self.styled_text(Rect { x, y: b.y + 12.0, width: text_w, height: 22.0 }, title, fg, TextStyle { size: 16.0, weight: 500, ..TextStyle::default() });
320 self.styled_text(Rect { x, y: b.y + 36.0, width: text_w, height: 20.0 }, subtitle, sub_fg, TextStyle { size: 13.5, ..TextStyle::default() });
321 }
322 if show_chevron {
323 let cx = b.x + b.width - 20.0;
324 let cy = b.y + b.height * 0.5;
325 self.paint_line([cx - 2.5, cy - 4.5], [cx + 2.0, cy], 1.8, sub_fg);
326 self.paint_line([cx + 2.0, cy], [cx - 2.5, cy + 4.5], 1.8, sub_fg);
327 }
328 r
329 }
330
331 pub fn preference_switch_row(
333 &mut self,
334 id: u64,
335 title: &str,
336 subtitle: &str,
337 value: &mut bool,
338 enabled: bool,
339 ) -> Response {
340 let b = self.allocate(if subtitle.is_empty() { 56.0 } else { 68.0 });
341 let mut r = self.interact(id, b, enabled);
342 if r.clicked {
343 *value = !*value;
344 r.changed = true;
345 }
346 let t = self.theme();
347 self.state_layer(r, 8.0, t.text);
348 self.focus_ring(r);
349 let fg = if r.enabled { t.text } else { t.text.opacity(0.38) };
350 let sub_fg = if r.enabled { t.on_surface_variant } else { t.text.opacity(0.38) };
351 let sw_w = 52.0;
352 let sw_h = 32.0;
353 let text_w = (b.width - sw_w - 40.0).max(0.0);
354 if subtitle.is_empty() {
355 self.styled_text(Rect { x: b.x + 16.0, width: text_w, ..b }, title, fg, TextStyle { size: 16.0, ..TextStyle::default() });
356 } else {
357 self.styled_text(Rect { x: b.x + 16.0, y: b.y + 12.0, width: text_w, height: 22.0 }, title, fg, TextStyle { size: 16.0, weight: 500, ..TextStyle::default() });
358 self.styled_text(Rect { x: b.x + 16.0, y: b.y + 36.0, width: text_w, height: 20.0 }, subtitle, sub_fg, TextStyle { size: 13.5, ..TextStyle::default() });
359 }
360 let pos = self.animate_geometry(id, 4, f32::from(*value), f32::from(*value)).clamp(0.0, 1.0);
361 let track = Rect { x: b.x + b.width - sw_w - 16.0, y: b.y + (b.height - sw_h) * 0.5, width: sw_w, height: sw_h };
362 let track_col = if !r.enabled {
363 t.text.opacity(0.12)
364 } else {
365 t.surface_highest.mix(t.primary, pos)
366 };
367 self.paint_rect(track, sw_h * 0.5, track_col);
368 if !*value {
369 self.commands.push(DrawCommand::Outline {
370 bounds: track,
371 radius: sw_h * 0.5,
372 width: 2.0,
373 color: if r.enabled { t.outline } else { t.text.opacity(0.12) },
374 });
375 }
376 let thumb_d = 16.0 + 8.0 * pos;
377 let thumb_cx = track.x + 16.0 + pos * (sw_w - 32.0);
378 let thumb = Rect {
379 x: thumb_cx - thumb_d * 0.5,
380 y: track.y + (sw_h - thumb_d) * 0.5,
381 width: thumb_d,
382 height: thumb_d,
383 };
384 let thumb_col = if !r.enabled {
385 t.text.opacity(0.38)
386 } else {
387 t.outline.mix(t.on_primary, pos)
388 };
389 self.paint_rect(thumb, thumb_d * 0.5, thumb_col);
390 self.semantic(
391 r,
392 Role::Switch,
393 title,
394 *value,
395 Some(if *value { CheckState::Checked } else { CheckState::Unchecked }),
396 None,
397 None,
398 );
399 r
400 }
401}
402
403#[cfg(test)]
404#[path = "list.spec.rs"]
405mod tests;