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 preference_row_at(
13 &mut self,
14 id: u64,
15 bounds: Rect,
16 label: &str,
17 role: Role,
18 ink: Color,
19 content: impl FnOnce(&mut Self, Response),
20 ) -> Response {
21 let response = self.target(id, bounds, label, true, role);
22 self.state_layer(response, 0.0, ink);
23 let press = self.animate_geometry(id, 221, f32::from(response.pressed), 0.0);
24 let center = [
25 bounds.x + bounds.width * 0.5,
26 bounds.y + bounds.height * 0.5,
27 ];
28 self.with_scale(center, 1.0 - 0.025 * press.clamp(0.0, 1.0), |frame| {
29 content(frame, response);
30 });
31 response
32 }
33
34 pub fn list_item(&mut self, id: u64, label: &str, selected: bool, enabled: bool) -> Response {
35 let b = self.allocate(48.0);
36 let r = self.interact(id, b, enabled);
37 let t = self.ui.theme;
38 let amount = self.animate(
39 id,
40 3,
41 if selected { 1.0 } else { 0.0 },
42 if selected { 1.0 } else { 0.0 },
43 );
44 let base = t.background.mix(t.secondary, amount);
45 let feedback = if self.ui.motion_preference == MotionPreference::Full {
46 Response {
47 pressed: false,
48 ..r
49 }
50 } else {
51 r
52 };
53 let color = self.control_color(feedback, base);
54 self.rect(b, 0.0, color);
55 self.ink(r, 0.0, t.text);
56 self.styled_text(
57 b,
58 label,
59 t.text.opacity(if r.enabled { 1.0 } else { 0.38 }),
60 TextStyle {
61 padding: 16.0,
62 letter_spacing: 0.5,
63 ..TextStyle::default()
64 },
65 );
66 self.focus_ring(r);
67 self.semantic(r, Role::ListItem, label, selected, None, None, None);
68 r
69 }
70 pub fn selection_item(
71 &mut self,
72 id: u64,
73 label: &str,
74 selected: bool,
75 enabled: bool,
76 ) -> Response {
77 let r = self.list_item(id, label, false, enabled);
78 let t = self.ui.theme;
79 let b = Rect {
80 x: r.bounds.x + r.bounds.width - 38.0,
81 y: r.bounds.y + (r.bounds.height - 20.0) * 0.5,
82 width: 20.0,
83 height: 20.0,
84 };
85 let color = if selected {
86 t.primary
87 } else {
88 t.on_surface_variant
89 }
90 .opacity(if r.enabled { 1.0 } else { 0.38 });
91 self.rect(b, 10.0, color);
92 self.rect(b.inset(2.0), 8.0, t.background);
93 if selected {
94 self.rect(b.inset(5.0), 5.0, color);
95 }
96 if let Some(node) = self.ui.semantics.last_mut() {
97 node.selected = selected;
98 }
99 r
100 }
101 pub fn virtual_list(
103 &mut self,
104 id: u64,
105 height: f32,
106 count: usize,
107 state: &mut ListState,
108 build: impl FnMut(&mut Self, usize, bool) -> Response,
109 ) -> Response {
110 self.virtual_list_inner(id, height, count, state, 0.0, 40.0, build)
111 }
112 pub fn virtual_list_padded(
113 &mut self,
114 id: u64,
115 height: f32,
116 count: usize,
117 state: &mut ListState,
118 build: impl FnMut(&mut Self, usize, bool) -> Response,
119 ) -> Response {
120 self.virtual_list_inner(id, height, count, state, 8.0, 40.0, build)
121 }
122 pub fn virtual_list_rows(
124 &mut self,
125 id: u64,
126 height: f32,
127 count: usize,
128 state: &mut ListState,
129 row_height: f32,
130 build: impl FnMut(&mut Self, usize, bool) -> Response,
131 ) -> Response {
132 self.virtual_list_inner(id, height, count, state, 0.0, row_height.max(1.0), build)
133 }
134 #[allow(clippy::too_many_arguments)]
135 fn virtual_list_inner(
136 &mut self,
137 id: u64,
138 height: f32,
139 count: usize,
140 state: &mut ListState,
141 padding: f32,
142 row_height: f32,
143 mut build: impl FnMut(&mut Self, usize, bool) -> Response,
144 ) -> Response {
145 let b = self.allocate(height.max(0.0));
146 if !state.offset.is_finite() {
147 state.offset = 0.0;
148 }
149 let old_input = self.input;
150 self.input.pressed = false;
151 self.input.released = false;
152 let mut r = self.interact(id, b, true);
153 self.input = old_input;
154 let previous_selection = state.selected;
155 let max_offset = (count as f32 * row_height + 2.0 * padding - b.height).max(0.0);
156 self.scroll_viewport(id, b, &mut state.offset, max_offset);
157 if let Some(key) = self.key_for(r)
158 && count > 0
159 {
160 let index = state.selected.unwrap_or(0).min(count - 1);
161 state.selected = Some(match key {
162 Key::Down => (index + 1).min(count - 1),
163 Key::Up => index.saturating_sub(1),
164 Key::Home => 0,
165 Key::End => count - 1,
166 _ => index,
167 });
168 if let Some(selected) = state.selected {
169 let top = padding + selected as f32 * row_height;
170 if top < state.offset {
171 state.offset = top;
172 } else if top + row_height > state.offset + b.height {
173 state.offset = top + row_height - b.height;
174 }
175 }
176 }
177 if state.selected.is_some_and(|i| i >= count) {
179 state.selected = count.checked_sub(1);
180 }
181 self.semantic(r, Role::ListBox, "List", false, None, None, None);
182 let first = ((state.offset - padding).max(0.0) / row_height).floor() as usize;
183 let last = ((state.offset + b.height - padding).max(0.0) / row_height).ceil() as usize;
184 let old_gap = self.gap;
185 self.gap = 0.0;
186 self.within(b, |frame| {
188 let rows_start = frame.ui.semantics.len();
189 for index in first..last.min(count) {
190 frame.cursor = b.y + padding + index as f32 * row_height - state.offset;
191 frame.bounds = Rect {
192 y: frame.cursor,
193 height: row_height,
194 ..b
195 };
196 let row_start = frame.ui.semantics.len();
197 let row = build(frame, index, state.selected == Some(index));
198 if row.clicked {
199 state.selected = Some(index);
200 r.clicked = true;
201 frame.ui.continuous = true;
202 for node in &mut frame.ui.semantics[rows_start..row_start] {
203 if node.role == Role::ListItem {
204 node.selected = false;
205 }
206 }
207 }
208 if let Some(node) = frame.ui.semantics[row_start..]
209 .iter_mut()
210 .find(|node| node.id == row.id && node.role == Role::ListItem)
211 {
212 node.selected = state.selected == Some(index);
213 }
214 }
215 });
216 self.gap = old_gap;
217 r.changed = state.selected != previous_selection;
218 r
219 }
220}
221
222impl Frame<'_> {
223 pub fn virtual_grid(
225 &mut self,
226 id: u64,
227 height: f32,
228 columns: usize,
229 count: usize,
230 state: &mut ListState,
231 mut build: impl FnMut(&mut Self, usize, bool) -> Response,
232 ) -> Response {
233 assert!(columns > 0);
234 let b = self.allocate(height.max(0.0));
235 let cell_height = 64.0;
236 let rows = count.div_ceil(columns);
237 let max = (rows as f32 * cell_height - b.height).max(0.0);
238 let old = self.input;
239 self.input.pressed = false;
240 self.input.released = false;
241 let mut r = self.interact(id, b, true);
242 self.input = old;
243 if r.enabled {
244 self.scroll_viewport(id, b, &mut state.offset, max);
245 }
246 let previous = state.selected;
247 if count > 0
248 && let Some(key) = self.key_for(r)
249 {
250 let i = state.selected.unwrap_or(0).min(count - 1);
251 state.selected = Some(match key {
252 Key::Down => (i + columns).min(count - 1),
253 Key::Up => i.saturating_sub(columns),
254 Key::Right => (i + 1).min(count - 1),
255 Key::Left => i.saturating_sub(1),
256 Key::Home => 0,
257 Key::End => count - 1,
258 _ => i,
259 });
260 if let Some(i) = state.selected {
261 let y = (i / columns) as f32 * cell_height;
262 if y < state.offset {
263 state.offset = y;
264 } else if y + cell_height > state.offset + b.height {
265 state.offset = y + cell_height - b.height;
266 }
267 }
268 }
269 state.offset = if state.offset.is_finite() {
270 state.offset.clamp(-b.height * 0.12, max + b.height * 0.12)
271 } else {
272 0.0
273 };
274 if state.selected.is_some_and(|i| i >= count) {
275 state.selected = count.checked_sub(1);
276 }
277 self.semantic(
278 r,
279 Role::ListBox,
280 "Application grid",
281 false,
282 None,
283 None,
284 None,
285 );
286 let first = (state.offset / cell_height).floor() as usize;
287 let last = ((state.offset + b.height) / cell_height).ceil() as usize;
288 self.within(b, |f| {
289 for row in first..last.min(rows) {
290 for col in 0..columns {
291 let i = row * columns + col;
292 if i >= count {
293 break;
294 }
295 let cell = Rect {
296 x: b.x + col as f32 * b.width / columns as f32,
297 y: b.y + row as f32 * cell_height - state.offset,
298 width: b.width / columns as f32,
299 height: cell_height,
300 };
301 f.within(cell.inset(3.0), |f| {
302 if build(f, i, state.selected == Some(i)).clicked {
303 state.selected = Some(i);
304 f.ui.continuous = true;
305 }
306 });
307 }
308 }
309 });
310 r.changed = state.selected != previous;
311 r
312 }
313
314 pub fn list_item_two_line(
317 &mut self,
318 id: u64,
319 icon: Option<MaterialIcon>,
320 title: &str,
321 subtitle: &str,
322 show_chevron: bool,
323 enabled: bool,
324 ) -> Response {
325 let b = self.allocate(if subtitle.is_empty() { 56.0 } else { 68.0 });
326 let r = self.target(id, b, title, enabled, Role::ListItem);
327 let t = self.theme();
328 self.state_layer(r, 8.0, t.text);
329 self.focus_ring(r);
330 let fg = if r.enabled {
331 t.text
332 } else {
333 t.text.opacity(0.38)
334 };
335 let sub_fg = if r.enabled {
336 t.on_surface_variant
337 } else {
338 t.text.opacity(0.38)
339 };
340 let mut x = b.x + 16.0;
341 if let Some(ic) = icon {
342 let icon_rect = Rect {
343 x,
344 y: b.y + (b.height - 24.0) * 0.5,
345 width: 24.0,
346 height: 24.0,
347 };
348 self.paint_material_icon(icon_rect, ic, sub_fg);
349 x += 40.0;
350 }
351 let trail = if show_chevron { 36.0 } else { 16.0 };
352 let text_w = (b.x + b.width - x - trail).max(0.0);
353 if subtitle.is_empty() {
354 self.styled_text(
355 Rect {
356 x,
357 width: text_w,
358 ..b
359 },
360 title,
361 fg,
362 TextStyle {
363 size: 16.0,
364 ..TextStyle::default()
365 },
366 );
367 } else {
368 self.styled_text(
369 Rect {
370 x,
371 y: b.y + 12.0,
372 width: text_w,
373 height: 22.0,
374 },
375 title,
376 fg,
377 TextStyle {
378 size: 16.0,
379 weight: 500,
380 ..TextStyle::default()
381 },
382 );
383 self.styled_text(
384 Rect {
385 x,
386 y: b.y + 36.0,
387 width: text_w,
388 height: 20.0,
389 },
390 subtitle,
391 sub_fg,
392 TextStyle {
393 size: 13.5,
394 ..TextStyle::default()
395 },
396 );
397 }
398 if show_chevron {
399 let cx = b.x + b.width - 20.0;
400 let cy = b.y + b.height * 0.5;
401 self.paint_line([cx - 2.5, cy - 4.5], [cx + 2.0, cy], 1.8, sub_fg);
402 self.paint_line([cx + 2.0, cy], [cx - 2.5, cy + 4.5], 1.8, sub_fg);
403 }
404 r
405 }
406
407 pub fn preference_switch_row(
409 &mut self,
410 id: u64,
411 title: &str,
412 subtitle: &str,
413 value: &mut bool,
414 enabled: bool,
415 ) -> Response {
416 let b = self.allocate(if subtitle.is_empty() { 56.0 } else { 68.0 });
417 let mut r = self.interact(id, b, enabled);
418 if r.clicked {
419 *value = !*value;
420 r.changed = true;
421 }
422 let t = self.theme();
423 self.state_layer(r, 8.0, t.text);
424 self.focus_ring(r);
425 let fg = if r.enabled {
426 t.text
427 } else {
428 t.text.opacity(0.38)
429 };
430 let sub_fg = if r.enabled {
431 t.on_surface_variant
432 } else {
433 t.text.opacity(0.38)
434 };
435 let sw_w = 52.0;
436 let sw_h = 32.0;
437 let text_w = (b.width - sw_w - 40.0).max(0.0);
438 if subtitle.is_empty() {
439 self.styled_text(
440 Rect {
441 x: b.x + 16.0,
442 width: text_w,
443 ..b
444 },
445 title,
446 fg,
447 TextStyle {
448 size: 16.0,
449 ..TextStyle::default()
450 },
451 );
452 } else {
453 self.styled_text(
454 Rect {
455 x: b.x + 16.0,
456 y: b.y + 12.0,
457 width: text_w,
458 height: 22.0,
459 },
460 title,
461 fg,
462 TextStyle {
463 size: 16.0,
464 weight: 500,
465 ..TextStyle::default()
466 },
467 );
468 self.styled_text(
469 Rect {
470 x: b.x + 16.0,
471 y: b.y + 36.0,
472 width: text_w,
473 height: 20.0,
474 },
475 subtitle,
476 sub_fg,
477 TextStyle {
478 size: 13.5,
479 ..TextStyle::default()
480 },
481 );
482 }
483 let pos = self
484 .animate_geometry(id, 4, f32::from(*value), f32::from(*value))
485 .clamp(0.0, 1.0);
486 let track = Rect {
487 x: b.x + b.width - sw_w - 16.0,
488 y: b.y + (b.height - sw_h) * 0.5,
489 width: sw_w,
490 height: sw_h,
491 };
492 let track_col = if !r.enabled {
493 t.text.opacity(0.12)
494 } else {
495 t.surface_highest.mix(t.primary, pos)
496 };
497 self.paint_rect(track, sw_h * 0.5, track_col);
498 if !*value {
499 self.commands.push(DrawCommand::Outline {
500 bounds: track,
501 radius: sw_h * 0.5,
502 width: 2.0,
503 color: if r.enabled {
504 t.outline
505 } else {
506 t.text.opacity(0.12)
507 },
508 });
509 }
510 let thumb_d = 16.0 + 8.0 * pos;
511 let thumb_cx = track.x + 16.0 + pos * (sw_w - 32.0);
512 let thumb = Rect {
513 x: thumb_cx - thumb_d * 0.5,
514 y: track.y + (sw_h - thumb_d) * 0.5,
515 width: thumb_d,
516 height: thumb_d,
517 };
518 let thumb_col = if !r.enabled {
519 t.text.opacity(0.38)
520 } else {
521 t.outline.mix(t.on_primary, pos)
522 };
523 self.paint_rect(thumb, thumb_d * 0.5, thumb_col);
524 self.semantic(
525 r,
526 Role::Switch,
527 title,
528 *value,
529 Some(if *value {
530 CheckState::Checked
531 } else {
532 CheckState::Unchecked
533 }),
534 None,
535 None,
536 );
537 r
538 }
539}
540
541#[cfg(test)]
542#[path = "list.spec.rs"]
543mod tests;