use crate::palette::command::CommandDef;
use crate::app::AppState;
use once_cell::sync::Lazy;
mod registry_commands;
static REGISTRY: Lazy<CommandRegistry> = Lazy::new(|| {
let commands = all_commands();
CommandRegistry { commands }
});
pub struct CommandRegistry {
commands: &'static [CommandDef],
}
impl CommandRegistry {
pub fn instance() -> &'static CommandRegistry {
®ISTRY
}
pub fn enabled_commands(&self, state: &AppState) -> Vec<&'static CommandDef> {
self.commands.iter().filter(|cmd| cmd.enabled(state)).collect()
}
pub fn by_key(&self, key: &str, state: &AppState) -> Option<&'static CommandDef> {
self.commands.iter()
.find(|cmd| cmd.key == key && cmd.enabled(state))
}
}
fn all_commands() -> &'static [CommandDef] {
static COMMANDS: Lazy<Vec<CommandDef>> = Lazy::new(|| {
registry_commands::all_commands()
});
COMMANDS.as_slice()
}