egui_charts/ui_kit/command_palette/
command.rs1use crate::icons::Icon;
4use egui::KeyboardShortcut;
5
6#[derive(Clone)]
8pub struct Command {
9 pub id: String,
11 pub label: String,
13 pub description: Option<String>,
15 pub shortcut: Option<KeyboardShortcut>,
17 pub icon: Option<&'static Icon>,
19 pub category: Option<String>,
21 pub keywords: Vec<String>,
23}
24
25impl Command {
26 pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self {
28 Self {
29 id: id.into(),
30 label: label.into(),
31 description: None,
32 shortcut: None,
33 icon: None,
34 category: None,
35 keywords: Vec::new(),
36 }
37 }
38
39 pub fn description(mut self, desc: impl Into<String>) -> Self {
41 self.description = Some(desc.into());
42 self
43 }
44
45 pub fn shortcut(mut self, shortcut: KeyboardShortcut) -> Self {
47 self.shortcut = Some(shortcut);
48 self
49 }
50
51 pub fn icon(mut self, icon: &'static Icon) -> Self {
53 self.icon = Some(icon);
54 self
55 }
56
57 pub fn category(mut self, category: impl Into<String>) -> Self {
59 self.category = Some(category.into());
60 self
61 }
62
63 pub fn keywords(mut self, keywords: impl IntoIterator<Item = impl Into<String>>) -> Self {
65 self.keywords = keywords.into_iter().map(|k| k.into()).collect();
66 self
67 }
68
69 pub fn searchable_text(&self) -> String {
71 let mut text = self.label.clone();
72 if let Some(desc) = &self.description {
73 text.push(' ');
74 text.push_str(desc);
75 }
76 if let Some(cat) = &self.category {
77 text.push(' ');
78 text.push_str(cat);
79 }
80 for keyword in &self.keywords {
81 text.push(' ');
82 text.push_str(keyword);
83 }
84 text
85 }
86}
87
88impl std::fmt::Debug for Command {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 f.debug_struct("Command")
91 .field("id", &self.id)
92 .field("label", &self.label)
93 .field("category", &self.category)
94 .finish()
95 }
96}