action.rs

  1use crate::SharedString;
  2use anyhow::{anyhow, Context, Result};
  3use collections::HashMap;
  4pub use no_action::NoAction;
  5use serde_json::json;
  6use std::any::{Any, TypeId};
  7
  8/// Actions are used to implement keyboard-driven UI.
  9/// When you declare an action, you can bind keys to the action in the keymap and
 10/// listeners for that action in the element tree.
 11///
 12/// To declare a list of simple actions, you can use the actions! macro, which defines a simple unit struct
 13/// action for each listed action name in the given namespace.
 14/// ```rust
 15/// actions!(editor, [MoveUp, MoveDown, MoveLeft, MoveRight, Newline]);
 16/// ```
 17/// More complex data types can also be actions, providing they implement Clone, PartialEq,
 18/// and serde_derive::Deserialize.
 19/// Use `impl_actions!` to automatically implement the action in the given namespace.
 20/// ```
 21/// #[derive(Clone, PartialEq, serde_derive::Deserialize)]
 22/// pub struct SelectNext {
 23///     pub replace_newest: bool,
 24/// }
 25/// impl_actions!(editor, [SelectNext]);
 26/// ```
 27///
 28/// If you want to control the behavior of the action trait manually, you can use the lower-level `#[register_action]`
 29/// macro, which only generates the code needed to register your action before `main`.
 30///
 31/// ```
 32/// #[derive(gpui::private::serde::Deserialize, std::cmp::PartialEq, std::clone::Clone)]
 33/// pub struct Paste {
 34///     pub content: SharedString,
 35/// }
 36///
 37/// impl gpui::Action for Paste {
 38///      ///...
 39/// }
 40/// register_action!(Paste);
 41/// ```
 42pub trait Action: 'static {
 43    fn boxed_clone(&self) -> Box<dyn Action>;
 44    fn as_any(&self) -> &dyn Any;
 45    fn partial_eq(&self, action: &dyn Action) -> bool;
 46    fn name(&self) -> &str;
 47
 48    fn debug_name() -> &'static str
 49    where
 50        Self: Sized;
 51    fn build(value: serde_json::Value) -> Result<Box<dyn Action>>
 52    where
 53        Self: Sized;
 54}
 55
 56impl std::fmt::Debug for dyn Action {
 57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 58        f.debug_struct("dyn Action")
 59            .field("name", &self.name())
 60            .finish()
 61    }
 62}
 63
 64impl dyn Action {
 65    pub fn type_id(&self) -> TypeId {
 66        self.as_any().type_id()
 67    }
 68}
 69
 70type ActionBuilder = fn(json: serde_json::Value) -> anyhow::Result<Box<dyn Action>>;
 71
 72pub(crate) struct ActionRegistry {
 73    builders_by_name: HashMap<SharedString, ActionBuilder>,
 74    names_by_type_id: HashMap<TypeId, SharedString>,
 75    all_names: Vec<SharedString>, // So we can return a static slice.
 76}
 77
 78impl Default for ActionRegistry {
 79    fn default() -> Self {
 80        let mut this = ActionRegistry {
 81            builders_by_name: Default::default(),
 82            names_by_type_id: Default::default(),
 83            all_names: Default::default(),
 84        };
 85
 86        this.load_actions();
 87
 88        this
 89    }
 90}
 91
 92/// This type must be public so that our macros can build it in other crates.
 93/// But this is an implementation detail and should not be used directly.
 94#[doc(hidden)]
 95pub type MacroActionBuilder = fn() -> ActionData;
 96
 97/// This type must be public so that our macros can build it in other crates.
 98/// But this is an implementation detail and should not be used directly.
 99#[doc(hidden)]
100pub struct ActionData {
101    pub name: &'static str,
102    pub type_id: TypeId,
103    pub build: ActionBuilder,
104}
105
106/// This constant must be public to be accessible from other crates.
107/// But its existence is an implementation detail and should not be used directly.
108#[doc(hidden)]
109#[linkme::distributed_slice]
110pub static __GPUI_ACTIONS: [MacroActionBuilder];
111
112impl ActionRegistry {
113    /// Load all registered actions into the registry.
114    pub(crate) fn load_actions(&mut self) {
115        for builder in __GPUI_ACTIONS {
116            let action = builder();
117            self.insert_action(action);
118        }
119    }
120
121    #[cfg(test)]
122    pub(crate) fn load_action<A: Action>(&mut self) {
123        self.insert_action(ActionData {
124            name: A::debug_name(),
125            type_id: TypeId::of::<A>(),
126            build: A::build,
127        });
128    }
129
130    fn insert_action(&mut self, action: ActionData) {
131        let name: SharedString = action.name.into();
132        self.builders_by_name.insert(name.clone(), action.build);
133        self.names_by_type_id.insert(action.type_id, name.clone());
134        self.all_names.push(name);
135    }
136
137    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
138    pub fn build_action_type(&self, type_id: &TypeId) -> Result<Box<dyn Action>> {
139        let name = self
140            .names_by_type_id
141            .get(type_id)
142            .ok_or_else(|| anyhow!("no action type registered for {:?}", type_id))?
143            .clone();
144
145        self.build_action(&name, None)
146    }
147
148    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
149    pub fn build_action(
150        &self,
151        name: &str,
152        params: Option<serde_json::Value>,
153    ) -> Result<Box<dyn Action>> {
154        let build_action = self
155            .builders_by_name
156            .get(name)
157            .ok_or_else(|| anyhow!("no action type registered for {}", name))?;
158        (build_action)(params.unwrap_or_else(|| json!({})))
159            .with_context(|| format!("Attempting to build action {}", name))
160    }
161
162    pub fn all_action_names(&self) -> &[SharedString] {
163        self.all_names.as_slice()
164    }
165}
166
167/// Defines unit structs that can be used as actions.
168/// To use more complex data types as actions, use `impl_actions!`
169#[macro_export]
170macro_rules! actions {
171    ($namespace:path, [ $($name:ident),* $(,)? ]) => {
172        $(
173            #[derive(::std::cmp::PartialEq, ::std::clone::Clone, ::std::default::Default, ::std::fmt::Debug, gpui::private::serde_derive::Deserialize)]
174            #[serde(crate = "gpui::private::serde")]
175            pub struct $name;
176
177            gpui::__impl_action!($namespace, $name,
178                fn build(_: gpui::private::serde_json::Value) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
179                    Ok(Box::new(Self))
180                }
181            );
182
183            gpui::register_action!($name);
184        )*
185    };
186}
187
188/// Implements the Action trait for any struct that implements Clone, Default, PartialEq, and serde_deserialize::Deserialize
189#[macro_export]
190macro_rules! impl_actions {
191    ($namespace:path, [ $($name:ident),* $(,)? ]) => {
192        $(
193            gpui::__impl_action!($namespace, $name,
194                fn build(value: gpui::private::serde_json::Value) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
195                    Ok(std::boxed::Box::new(gpui::private::serde_json::from_value::<Self>(value)?))
196                }
197            );
198
199            gpui::register_action!($name);
200        )*
201    };
202}
203
204#[doc(hidden)]
205#[macro_export]
206macro_rules! __impl_action {
207    ($namespace:path, $name:ident, $build:item) => {
208        impl gpui::Action for $name {
209            fn name(&self) -> &'static str
210            {
211                concat!(
212                    stringify!($namespace),
213                    "::",
214                    stringify!($name),
215                )
216            }
217
218            fn debug_name() -> &'static str
219            where
220                Self: ::std::marker::Sized
221            {
222                concat!(
223                    stringify!($namespace),
224                    "::",
225                    stringify!($name),
226                )
227            }
228
229            $build
230
231            fn partial_eq(&self, action: &dyn gpui::Action) -> bool {
232                action
233                    .as_any()
234                    .downcast_ref::<Self>()
235                    .map_or(false, |a| self == a)
236            }
237
238            fn boxed_clone(&self) ->  std::boxed::Box<dyn gpui::Action> {
239                ::std::boxed::Box::new(self.clone())
240            }
241
242            fn as_any(&self) -> &dyn ::std::any::Any {
243                self
244            }
245        }
246    };
247}
248
249mod no_action {
250    use crate as gpui;
251
252    actions!(zed, [NoAction]);
253}