Skip to main content

egui_charts/ui_kit/command_palette/
command.rs

1//! Command struct for the command palette
2
3use crate::icons::Icon;
4use egui::KeyboardShortcut;
5
6/// A command that can be executed from the command palette
7#[derive(Clone)]
8pub struct Command {
9    /// Unique identifier for this command
10    pub id: String,
11    /// Display label shown in the palette
12    pub label: String,
13    /// Optional longer description
14    pub description: Option<String>,
15    /// Optional keyboard shortcut
16    pub shortcut: Option<KeyboardShortcut>,
17    /// Optional icon
18    pub icon: Option<&'static Icon>,
19    /// Optional category for grouping
20    pub category: Option<String>,
21    /// Keywords for better search matching
22    pub keywords: Vec<String>,
23}
24
25impl Command {
26    /// Create a new command with the given ID and label
27    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    /// Set the command description
40    pub fn description(mut self, desc: impl Into<String>) -> Self {
41        self.description = Some(desc.into());
42        self
43    }
44
45    /// Set the keyboard shortcut
46    pub fn shortcut(mut self, shortcut: KeyboardShortcut) -> Self {
47        self.shortcut = Some(shortcut);
48        self
49    }
50
51    /// Set the icon
52    pub fn icon(mut self, icon: &'static Icon) -> Self {
53        self.icon = Some(icon);
54        self
55    }
56
57    /// Set the category
58    pub fn category(mut self, category: impl Into<String>) -> Self {
59        self.category = Some(category.into());
60        self
61    }
62
63    /// Add keywords for better search matching
64    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    /// Get all searchable text for this command
70    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}