Skip to main content

functora_egui/widgets/command/
widget.rs

1//! Command builder struct — a command palette with search.
2
3use crate::icons::lucide_icon::LucideIcon;
4
5/// A single palette entry with group and item icons.
6#[derive(Clone, Debug)]
7pub struct CommandItem {
8    pub group: String,
9    pub group_icon: LucideIcon,
10    pub label: String,
11    pub icon: LucideIcon,
12}
13
14impl From<(String, String)> for CommandItem {
15    fn from((group, label): (String, String)) -> Self {
16        Self {
17            group,
18            group_icon: LucideIcon::Component,
19            label,
20            icon: LucideIcon::Component,
21        }
22    }
23}
24
25/// A command palette: centered modal with search input and command list.
26#[must_use]
27pub struct Command {
28    pub(crate) items: Vec<CommandItem>,
29    pub(crate) placeholder: String,
30}
31
32impl Command {
33    /// Items are `(group_name, command_label)` pairs.
34    pub fn new(items: Vec<(String, String)>) -> Self {
35        Self {
36            items: items.into_iter().map(CommandItem::from).collect(),
37            placeholder: "Type a command or search...".to_owned(),
38        }
39    }
40
41    /// Items with icons – the preferred constructor when icons are available.
42    pub fn with_items(items: Vec<CommandItem>) -> Self {
43        Self {
44            items,
45            placeholder: "Type a command or search...".to_owned(),
46        }
47    }
48
49    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
50        self.placeholder = text.into();
51        self
52    }
53}
54
55/// A command palette bound to enum values instead of blind indexes:
56/// each entry pairs a value with its palette item.
57#[must_use]
58pub struct CommandValue<T: Clone> {
59    pub(crate) entries: Vec<(T, CommandItem)>,
60    pub(crate) placeholder: String,
61}
62
63impl<T: Clone> CommandValue<T> {
64    pub fn new(entries: Vec<(T, CommandItem)>) -> Self {
65        Self {
66            entries,
67            placeholder: "Type a command or search...".to_owned(),
68        }
69    }
70
71    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
72        self.placeholder = text.into();
73        self
74    }
75}