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 //todo!(remove)
132 let name: SharedString = action.name.into();
133 self.builders_by_name.insert(name.clone(), action.build);
134 self.names_by_type_id.insert(action.type_id, name.clone());
135 self.all_names.push(name);
136 }
137
138 /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
139 pub fn build_action_type(&self, type_id: &TypeId) -> Result<Box<dyn Action>> {
140 let name = self
141 .names_by_type_id
142 .get(type_id)
143 .ok_or_else(|| anyhow!("no action type registered for {:?}", type_id))?
144 .clone();
145
146 self.build_action(&name, None)
147 }
148
149 /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
150 pub fn build_action(
151 &self,
152 name: &str,
153 params: Option<serde_json::Value>,
154 ) -> Result<Box<dyn Action>> {
155 let build_action = self
156 .builders_by_name
157 .get(name)
158 .ok_or_else(|| anyhow!("no action type registered for {}", name))?;
159 (build_action)(params.unwrap_or_else(|| json!({})))
160 .with_context(|| format!("Attempting to build action {}", name))
161 }
162
163 pub fn all_action_names(&self) -> &[SharedString] {
164 self.all_names.as_slice()
165 }
166}
167
168/// Defines unit structs that can be used as actions.
169/// To use more complex data types as actions, use `impl_actions!`
170#[macro_export]
171macro_rules! actions {
172 ($namespace:path, [ $($name:ident),* $(,)? ]) => {
173 $(
174 #[derive(::std::cmp::PartialEq, ::std::clone::Clone, ::std::default::Default, gpui::private::serde_derive::Deserialize)]
175 #[serde(crate = "gpui::private::serde")]
176 pub struct $name;
177
178 gpui::__impl_action!($namespace, $name,
179 fn build(_: gpui::private::serde_json::Value) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
180 Ok(Box::new(Self))
181 }
182 );
183
184 gpui::register_action!($name);
185 )*
186 };
187}
188
189/// Implements the Action trait for any struct that implements Clone, Default, PartialEq, and serde_deserialize::Deserialize
190#[macro_export]
191macro_rules! impl_actions {
192 ($namespace:path, [ $($name:ident),* $(,)? ]) => {
193 $(
194 gpui::__impl_action!($namespace, $name,
195 fn build(value: gpui::private::serde_json::Value) -> gpui::Result<::std::boxed::Box<dyn gpui::Action>> {
196 Ok(std::boxed::Box::new(gpui::private::serde_json::from_value::<Self>(value)?))
197 }
198 );
199
200 gpui::register_action!($name);
201 )*
202 };
203}
204
205#[doc(hidden)]
206#[macro_export]
207macro_rules! __impl_action {
208 ($namespace:path, $name:ident, $build:item) => {
209 impl gpui::Action for $name {
210 fn name(&self) -> &'static str
211 {
212 concat!(
213 stringify!($namespace),
214 "::",
215 stringify!($name),
216 )
217 }
218
219 fn debug_name() -> &'static str
220 where
221 Self: ::std::marker::Sized
222 {
223 concat!(
224 stringify!($namespace),
225 "::",
226 stringify!($name),
227 )
228 }
229
230 $build
231
232 fn partial_eq(&self, action: &dyn gpui::Action) -> bool {
233 action
234 .as_any()
235 .downcast_ref::<Self>()
236 .map_or(false, |a| self == a)
237 }
238
239 fn boxed_clone(&self) -> std::boxed::Box<dyn gpui::Action> {
240 ::std::boxed::Box::new(self.clone())
241 }
242
243 fn as_any(&self) -> &dyn ::std::any::Any {
244 self
245 }
246 }
247 };
248}
249
250mod no_action {
251 use crate as gpui;
252
253 actions!(zed, [NoAction]);
254}