use crate::Command;
use gpui::{App, DummyKeyboardMapper, Global, KeyBinding};
use std::any::TypeId;
use std::collections::HashMap;
#[derive(Default)]
pub struct CommandRegistry {
commands: Vec<Command>,
by_action_type: HashMap<TypeId, ()>,
}
impl Global for CommandRegistry {}
impl CommandRegistry {
pub fn new() -> Self {
Self::default()
}
#[track_caller]
pub fn register(&mut self, command: Command) {
assert!(
command.handler.is_some(),
"command `{}` requires a handler",
command.name()
);
let type_id = command.action().as_any().type_id();
assert!(
self.by_action_type.insert(type_id, ()).is_none(),
"command for action `{}` already registered",
command.action().name()
);
self.commands.push(command);
}
pub fn commands(&self) -> &[Command] {
&self.commands
}
pub fn install_keybindings(&self, cx: &mut App) {
let bindings: Vec<KeyBinding> = self
.commands
.iter()
.filter_map(|command| {
let binding = command.binding()?;
let action = command.action().boxed_clone();
Some(
KeyBinding::load(
binding.as_str(),
action,
None,
false,
None,
&DummyKeyboardMapper,
)
.unwrap_or_else(|error| {
panic!(
"invalid keybinding `{}` for command `{}`: {error}",
binding,
command.name()
)
}),
)
})
.collect();
cx.bind_keys(bindings);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Command;
use gpui::actions;
actions!([Ping, Pong]);
#[test]
fn register_and_introspect() {
let mut registry = CommandRegistry::new();
registry.register(Command::new("Ping", Ping).handler(|_, _| {}));
registry.register(Command::new("Pong", Pong).handler(|_, _| {}));
assert_eq!(registry.commands().len(), 2);
assert_eq!(registry.commands()[0].name(), "Ping");
assert_eq!(registry.commands()[1].category_name(), "General");
}
#[test]
#[should_panic(expected = "already registered")]
fn duplicate_action_type_panics() {
let mut registry = CommandRegistry::new();
registry.register(Command::new("Ping", Ping).handler(|_, _| {}));
registry.register(Command::new("Ping Again", Ping).handler(|_, _| {}));
}
#[test]
#[should_panic(expected = "requires a handler")]
fn missing_handler_panics() {
let mut registry = CommandRegistry::new();
registry.register(Command::new("Ping", Ping));
}
}