use crate::icons::Icon;
use egui::KeyboardShortcut;
#[derive(Clone)]
pub struct Command {
pub id: String,
pub label: String,
pub description: Option<String>,
pub shortcut: Option<KeyboardShortcut>,
pub icon: Option<&'static Icon>,
pub category: Option<String>,
pub keywords: Vec<String>,
}
impl Command {
pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self {
Self {
id: id.into(),
label: label.into(),
description: None,
shortcut: None,
icon: None,
category: None,
keywords: Vec::new(),
}
}
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
pub fn shortcut(mut self, shortcut: KeyboardShortcut) -> Self {
self.shortcut = Some(shortcut);
self
}
pub fn icon(mut self, icon: &'static Icon) -> Self {
self.icon = Some(icon);
self
}
pub fn category(mut self, category: impl Into<String>) -> Self {
self.category = Some(category.into());
self
}
pub fn keywords(mut self, keywords: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.keywords = keywords.into_iter().map(|k| k.into()).collect();
self
}
pub fn searchable_text(&self) -> String {
let mut text = self.label.clone();
if let Some(desc) = &self.description {
text.push(' ');
text.push_str(desc);
}
if let Some(cat) = &self.category {
text.push(' ');
text.push_str(cat);
}
for keyword in &self.keywords {
text.push(' ');
text.push_str(keyword);
}
text
}
}
impl std::fmt::Debug for Command {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Command")
.field("id", &self.id)
.field("label", &self.label)
.field("category", &self.category)
.finish()
}
}