Skip to main content

bhoo/systems/material/
shortcut_hint.rs

1//! Keyboard shortcut keycap hint widget.
2use crate::*;
3
4impl Frame<'_> {
5    /// Render a compact keycap shortcut hint row (e.g. `keys = &["Ctrl", "K"]`, `description = "Command palette"`).
6    pub fn shortcut_hint(&mut self, keys: &[&str], description: &str) {
7        let t = self.theme();
8        let row = self.reserve(32.0);
9        let key_style = TextStyle {
10            size: 12.0,
11            weight: 600,
12            align: 1,
13            family: FontFamily::Monospace,
14            ..TextStyle::default()
15        };
16        let mut x = row.x;
17        let max_x = row.x + row.width;
18        for (i, &key) in keys.iter().enumerate() {
19            if i > 0 {
20                let plus_w = 14.0;
21                if x + plus_w > max_x {
22                    break;
23                }
24                self.styled_text(
25                    Rect { x, y: row.y, width: plus_w, height: row.height },
26                    "+",
27                    t.on_surface_variant,
28                    TextStyle { size: 12.0, align: 1, ..TextStyle::default() },
29                );
30                x += plus_w;
31            }
32            let kw = (self.text_width(key, key_style) + 14.0).max(24.0);
33            if x + kw > max_x {
34                break;
35            }
36            let cap = Rect { x, y: row.y + 4.0, width: kw, height: 24.0 };
37            self.paint_rect(cap, 6.0, t.surface_highest);
38            self.commands.push(DrawCommand::Outline {
39                bounds: cap,
40                radius: 6.0,
41                width: 1.0,
42                color: t.outline_variant,
43            });
44            self.styled_text(cap, key, t.text, key_style);
45            x += kw;
46        }
47        if !description.is_empty() && x + 12.0 < max_x {
48            self.styled_text(
49                Rect { x: x + 12.0, y: row.y, width: (max_x - x - 12.0).max(0.0), height: row.height },
50                description,
51                t.on_surface_variant,
52                TextStyle { size: 13.0, ..TextStyle::default() },
53            );
54        }
55    }
56}
57
58#[cfg(test)]
59#[path = "shortcut_hint.spec.rs"]
60mod tests;