bhoo/systems/material/
command_palette.rs1use crate::*;
3
4#[derive(Clone, Copy, Debug)]
6pub struct CommandItem<'a> {
7 pub label: &'a str,
8 pub shortcut: Option<&'a str>,
9 pub icon: Option<MaterialIcon>,
10}
11
12impl Frame<'_> {
13 pub fn command_palette_inline(
16 &mut self,
17 id: u64,
18 query: &mut String,
19 items: &[CommandItem<'_>],
20 placeholder: &str,
21 ) -> Option<usize> {
22 let t = self.theme();
23 let r = self.text_field(id, placeholder, 56.0, !query.is_empty(), false, false);
24 if !query.is_empty() {
25 self.styled_text(
26 Rect { x: r.bounds.x + 16.0, width: (r.bounds.width - 32.0).max(0.0), ..r.bounds },
27 query,
28 t.text,
29 TextStyle { size: 16.0, ..TextStyle::default() },
30 );
31 }
32 let needle = query.to_lowercase();
33 let mut chosen = None;
34 self.with_gap(0.0, |f| {
35 for (i, item) in items.iter().enumerate() {
36 if !needle.is_empty() && !item.label.to_lowercase().contains(&needle) {
37 continue;
38 }
39 let row = f.reserve(48.0);
40 let resp = f.target(id.wrapping_add(100 + i as u64), row, item.label, true, Role::MenuItem);
41 f.state_layer(resp, 8.0, t.text);
42 f.focus_ring(resp);
43 let mut text_x = row.x + 16.0;
44 if let Some(icon) = item.icon {
45 f.paint_material_icon(
46 Rect { x: row.x + 12.0, y: row.y + 12.0, width: 24.0, height: 24.0 },
47 icon,
48 t.on_surface_variant,
49 );
50 text_x = row.x + 48.0;
51 }
52 let right_pad = if let Some(sc) = item.shortcut {
53 let sc_w = (f.text_width(sc, TextStyle { size: 12.0, ..TextStyle::default() }) + 16.0).max(36.0);
54 let badge = Rect {
55 x: (row.x + row.width - sc_w - 12.0).max(text_x),
56 y: row.y + 11.0,
57 width: sc_w,
58 height: 26.0,
59 };
60 f.paint_rect(badge, 6.0, t.surface_high);
61 f.styled_text(
62 badge,
63 sc,
64 t.on_surface_variant,
65 TextStyle { size: 12.0, weight: 500, align: 1, ..TextStyle::default() },
66 );
67 sc_w + 24.0
68 } else {
69 16.0
70 };
71 f.styled_text(
72 Rect { x: text_x, width: (row.x + row.width - text_x - right_pad).max(0.0), ..row },
73 item.label,
74 t.text,
75 TextStyle { size: 14.0, ..TextStyle::default() },
76 );
77 if resp.clicked {
78 chosen = Some(i);
79 }
80 }
81 });
82 if !query.is_empty() && self.button(id.wrapping_add(50), "Clear search").clicked {
83 query.clear();
84 }
85 chosen
86 }
87
88 pub fn command_palette(
91 &mut self,
92 id: u64,
93 overlay: &mut OverlayState,
94 query: &mut String,
95 items: &[CommandItem<'_>],
96 placeholder: &str,
97 ) -> Option<usize> {
98 let vp = self.viewport();
99 let w = 520.0_f32.min((vp.width - 32.0).max(200.0));
100 let h = (items.len() as f32 * 48.0 + 140.0).min((vp.height - 80.0).max(160.0));
101 let panel = Rect {
102 x: vp.x + (vp.width - w) * 0.5,
103 y: vp.y + 48.0_f32.min((vp.height - h).max(0.0) * 0.5),
104 width: w,
105 height: h,
106 };
107 overlay.kind = OverlayKind::Dialog;
108 overlay.dismiss_policy = OverlayDismissPolicy::EscapeOrOutside;
109 let mut chosen = None;
110 self.overlay(id, overlay, panel, true, |f, open| {
111 let inner = f.available().inset(16.0);
112 f.within(inner, |f| {
113 if let Some(idx) = f.command_palette_inline(id.wrapping_add(1), query, items, placeholder) {
114 chosen = Some(idx);
115 *open = false;
116 }
117 });
118 });
119 chosen
120 }
121}
122
123#[cfg(test)]
124#[path = "command_palette.spec.rs"]
125mod tests;