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