Skip to main content

gpui_commands/
command.rs

1use gpui::{Action, App, SharedString, Window};
2
3/// The signature of a command handler: invoked with the window and app
4/// contexts whenever the command is executed.
5type CommandHandler = dyn Fn(&mut Window, &mut App);
6
7/// A single command: its display name, category, underlying GPUI action,
8/// optional keybinding, and the handler invoked when it is executed.
9pub struct Command {
10    pub(crate) name: SharedString,
11    pub(crate) action: Box<dyn Action>,
12    pub(crate) category: SharedString,
13    pub(crate) keybinding: Option<SharedString>,
14    pub(crate) handler: Option<Box<CommandHandler>>,
15}
16
17impl Command {
18    /// Start building a command. `name` is what appears in the palette;
19    /// `action` is the GPUI action this command wraps.
20    pub fn new(name: impl Into<SharedString>, action: impl Action) -> Self {
21        Self {
22            name: name.into(),
23            action: Box::new(action),
24            category: SharedString::new_static("General"),
25            keybinding: None,
26            handler: None,
27        }
28    }
29
30    /// Set the category this command is grouped under in the palette.
31    pub fn category(mut self, category: impl Into<SharedString>) -> Self {
32        self.category = category.into();
33        self
34    }
35
36    /// Record a keybinding for this command, e.g. `"cmd-s"`.
37    pub fn keybinding(mut self, binding: impl Into<SharedString>) -> Self {
38        self.keybinding = Some(binding.into());
39        self
40    }
41
42    /// Set the handler invoked when this command is executed.
43    pub fn handler(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
44        self.handler = Some(Box::new(f));
45        self
46    }
47
48    /// The display name shown in the palette.
49    pub fn name(&self) -> &SharedString {
50        &self.name
51    }
52
53    /// The category this command is grouped under.
54    pub fn category_name(&self) -> &SharedString {
55        &self.category
56    }
57
58    /// The bound keybinding, if any.
59    pub fn binding(&self) -> Option<&SharedString> {
60        self.keybinding.as_ref()
61    }
62
63    /// The underlying GPUI action.
64    pub fn action(&self) -> &dyn Action {
65        self.action.as_ref()
66    }
67
68    /// Execute this command's handler.
69    pub fn run(&self, window: &mut Window, cx: &mut App) {
70        if let Some(handler) = &self.handler {
71            handler(window, cx);
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use gpui::actions;
80
81    actions!([SaveFile, ToggleSidebar]);
82
83    #[test]
84    fn builder_defaults() {
85        let command = Command::new("Save File", SaveFile);
86        assert_eq!(command.name(), "Save File");
87        assert_eq!(command.category_name(), "General");
88        assert!(command.binding().is_none());
89        assert_eq!(command.action().name(), "SaveFile");
90    }
91
92    #[test]
93    fn builder_chaining() {
94        let command = Command::new("Toggle Sidebar", ToggleSidebar)
95            .category("View")
96            .keybinding("cmd-b")
97            .handler(|_, _| {});
98        assert_eq!(command.category_name(), "View");
99        assert_eq!(command.binding().unwrap(), "cmd-b");
100        assert!(command.handler.is_some());
101    }
102}