action.rs

  1use crate::SharedString;
  2use anyhow::{anyhow, Context, Result};
  3use collections::HashMap;
  4pub use no_action::{is_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 + Send {
 43    /// Clone the action into a new box
 44    fn boxed_clone(&self) -> Box<dyn Action>;
 45
 46    /// Cast the action to the any type
 47    fn as_any(&self) -> &dyn Any;
 48
 49    /// Do a partial equality check on this action and the other
 50    fn partial_eq(&self, action: &dyn Action) -> bool;
 51
 52    /// Get the name of this action, for displaying in UI
 53    fn name(&self) -> &str;
 54
 55    /// Get the name of this action for debugging
 56    fn debug_name() -> &'static str
 57    where
 58        Self: Sized;
 59
 60    /// Build this action from a JSON value. This is used to construct actions from the keymap.
 61    /// A value of `{}` will be passed for actions that don't have any parameters.
 62    fn build(value: serde_json::Value) -> Result<Box<dyn Action>>
 63    where
 64        Self: Sized;
 65
 66    /// A list of alternate, deprecated names for this action.
 67    fn deprecated_aliases() -> &'static [&'static str]
 68    where
 69        Self: Sized,
 70    {
 71        &[]
 72    }
 73}
 74
 75impl std::fmt::Debug for dyn Action {
 76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 77        f.debug_struct("dyn Action")
 78            .field("name", &self.name())
 79            .finish()
 80    }
 81}
 82
 83impl dyn Action {
 84    /// Get the type id of this action
 85    pub fn type_id(&self) -> TypeId {
 86        self.as_any().type_id()
 87    }
 88}
 89
 90type ActionBuilder = fn(json: serde_json::Value) -> anyhow::Result<Box<dyn Action>>;
 91
 92pub(crate) struct ActionRegistry {
 93    builders_by_name: HashMap<SharedString, ActionBuilder>,
 94    names_by_type_id: HashMap<TypeId, SharedString>,
 95    all_names: Vec<SharedString>, // So we can return a static slice.
 96    deprecations: Vec<(SharedString, SharedString)>,
 97}
 98
 99impl Default for ActionRegistry {
100    fn default() -> Self {
101        let mut this = ActionRegistry {
102            builders_by_name: Default::default(),
103            names_by_type_id: Default::default(),
104            all_names: Default::default(),
105            deprecations: Default::default(),
106        };
107
108        this.load_actions();
109
110        this
111    }
112}
113
114/// This type must be public so that our macros can build it in other crates.
115/// But this is an implementation detail and should not be used directly.
116#[doc(hidden)]
117pub type MacroActionBuilder = fn() -> ActionData;
118
119/// This type must be public so that our macros can build it in other crates.
120/// But this is an implementation detail and should not be used directly.
121#[doc(hidden)]
122pub struct ActionData {
123    pub name: &'static str,
124    pub aliases: &'static [&'static str],
125    pub type_id: TypeId,
126    pub build: ActionBuilder,
127}
128
129/// This constant must be public to be accessible from other crates.
130/// But its existence is an implementation detail and should not be used directly.
131#[doc(hidden)]
132#[linkme::distributed_slice]
133pub static __GPUI_ACTIONS: [MacroActionBuilder];
134
135impl ActionRegistry {
136    /// Load all registered actions into the registry.
137    pub(crate) fn load_actions(&mut self) {
138        for builder in __GPUI_ACTIONS {
139            let action = builder();
140            self.insert_action(action);
141        }
142    }
143
144    #[cfg(test)]
145    pub(crate) fn load_action<A: Action>(&mut self) {
146        self.insert_action(ActionData {
147            name: A::debug_name(),
148            aliases: A::deprecated_aliases(),
149            type_id: TypeId::of::<A>(),
150            build: A::build,
151        });
152    }
153
154    fn insert_action(&mut self, action: ActionData) {
155        let name: SharedString = action.name.into();
156        self.builders_by_name.insert(name.clone(), action.build);
157        for &alias in action.aliases {
158            self.builders_by_name.insert(alias.into(), action.build);
159            self.deprecations.push((alias.into(), name.clone()));
160        }
161        self.names_by_type_id.insert(action.type_id, name.clone());
162        self.all_names.push(name);
163    }
164
165    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
166    pub fn build_action_type(&self, type_id: &TypeId) -> Result<Box<dyn Action>> {
167        let name = self
168            .names_by_type_id
169            .get(type_id)
170            .ok_or_else(|| anyhow!("no action type registered for {:?}", type_id))?
171            .clone();
172
173        self.build_action(&name, None)
174    }
175
176    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
177    pub fn build_action(
178        &self,
179        name: &str,
180        params: Option<serde_json::Value>,
181    ) -> Result<Box<dyn Action>> {
182        let build_action = self
183            .builders_by_name
184            .get(name)
185            .ok_or_else(|| anyhow!("no action type registered for {}", name))?;
186        (build_action)(params.unwrap_or_else(|| json!({})))
187            .with_context(|| format!("Attempting to build action {}", name))
188    }
189
190    pub fn all_action_names(&self) -> &[SharedString] {
191        self.all_names.as_slice()
192    }
193
194    pub fn action_deprecations(&self) -> &[(SharedString, SharedString)] {
195        self.deprecations.as_slice()
196    }
197}
198
199/// Defines unit structs that can be used as actions.
200/// To use more complex data types as actions, use `impl_actions!`
201#[macro_export]
202macro_rules! actions {
203    ($namespace:path, [ $($name:ident),* $(,)? ]) => {
204        $(
205            #[doc = "The `"]
206            #[doc = stringify!($name)]
207            #[doc = "` action, see [`gpui::actions!`]"]
208            #[derive(::std::clone::Clone,::std::cmp::PartialEq, ::std::default::Default)]
209            pub struct $name;
210
211            gpui::__impl_action!($namespace, $name, $name,
212                fn build(_: gpui::private::serde_json::Value) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
213                    Ok(Box::new(Self))
214                }
215            );
216
217            gpui::register_action!($name);
218        )*
219    };
220}
221
222/// Defines a unit struct that can be used as an actions, with a name
223/// that differs from it's type name.
224///
225/// To use more complex data types as actions, and rename them use
226/// `impl_action_as!`
227#[macro_export]
228macro_rules! action_as {
229    ($namespace:path, $name:ident as $visual_name:ident) => {
230        #[doc = "The `"]
231        #[doc = stringify!($name)]
232        #[doc = "` action, see [`gpui::actions!`]"]
233        #[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default)]
234        pub struct $name;
235
236        gpui::__impl_action!(
237            $namespace,
238            $name,
239            $visual_name,
240            fn build(
241                _: gpui::private::serde_json::Value,
242            ) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
243                Ok(Box::new(Self))
244            }
245        );
246
247        gpui::register_action!($name);
248    };
249}
250
251/// Defines a unit struct that can be used as an action, with some deprecated aliases.
252#[macro_export]
253macro_rules! action_aliases {
254    ($namespace:path, $name:ident, [$($alias:ident),* $(,)?]) => {
255        #[doc = "The `"]
256        #[doc = stringify!($name)]
257        #[doc = "` action, see [`gpui::actions!`]"]
258        #[derive(
259            ::std::cmp::PartialEq,
260            ::std::clone::Clone,
261            ::std::default::Default,
262            ::std::fmt::Debug,
263            gpui::private::serde_derive::Deserialize,
264        )]
265        #[serde(crate = "gpui::private::serde")]
266        pub struct $name;
267
268        gpui::__impl_action!(
269            $namespace,
270            $name,
271            $name,
272            fn build(
273                _: gpui::private::serde_json::Value,
274            ) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
275                Ok(Box::new(Self))
276            },
277            fn deprecated_aliases() -> &'static [&'static str] {
278                &[
279                    $(concat!(stringify!($namespace), "::", stringify!($alias))),*
280                ]
281            }
282        );
283
284        gpui::register_action!($name);
285    };
286}
287
288/// Implements the Action trait for any struct that implements Clone, Default, PartialEq, and serde_deserialize::Deserialize
289#[macro_export]
290macro_rules! impl_actions {
291    ($namespace:path, [ $($name:ident),* $(,)? ]) => {
292        $(
293            gpui::__impl_action!($namespace, $name, $name,
294                fn build(value: gpui::private::serde_json::Value) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
295                    Ok(std::boxed::Box::new(gpui::private::serde_json::from_value::<Self>(value)?))
296                }
297            );
298
299            gpui::register_action!($name);
300        )*
301    };
302}
303
304/// Implements the Action trait for a struct that implements Clone, Default, PartialEq, and serde_deserialize::Deserialize
305/// Allows you to rename the action visually, without changing the struct's name
306#[macro_export]
307macro_rules! impl_action_as {
308    ($namespace:path, $name:ident as $visual_name:tt ) => {
309        gpui::__impl_action!(
310            $namespace,
311            $name,
312            $visual_name,
313            fn build(
314                value: gpui::private::serde_json::Value,
315            ) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
316                Ok(std::boxed::Box::new(
317                    gpui::private::serde_json::from_value::<Self>(value)?,
318                ))
319            }
320        );
321
322        gpui::register_action!($name);
323    };
324}
325
326#[doc(hidden)]
327#[macro_export]
328macro_rules! __impl_action {
329    ($namespace:path, $name:ident, $visual_name:tt, $($items:item),*) => {
330        impl gpui::Action for $name {
331            fn name(&self) -> &'static str
332            {
333                concat!(
334                    stringify!($namespace),
335                    "::",
336                    stringify!($visual_name),
337                )
338            }
339
340            fn debug_name() -> &'static str
341            where
342                Self: ::std::marker::Sized
343            {
344                concat!(
345                    stringify!($namespace),
346                    "::",
347                    stringify!($visual_name),
348                )
349            }
350
351            fn partial_eq(&self, action: &dyn gpui::Action) -> bool {
352                action
353                    .as_any()
354                    .downcast_ref::<Self>()
355                    .map_or(false, |a| self == a)
356            }
357
358            fn boxed_clone(&self) ->  std::boxed::Box<dyn gpui::Action> {
359                ::std::boxed::Box::new(self.clone())
360            }
361
362            fn as_any(&self) -> &dyn ::std::any::Any {
363                self
364            }
365
366            $($items)*
367        }
368    };
369}
370
371mod no_action {
372    use crate as gpui;
373    use std::any::Any as _;
374
375    actions!(zed, [NoAction]);
376
377    /// Returns whether or not this action represents a removed key binding.
378    pub fn is_no_action(action: &dyn gpui::Action) -> bool {
379        action.as_any().type_id() == (NoAction {}).type_id()
380    }
381}