1use crate::SharedString;
2use anyhow::{anyhow, Context, Result};
3use collections::HashMap;
4pub use no_action::NoAction;
5use serde_json::json;
6use std::{
7 any::{Any, TypeId},
8 ops::Deref,
9};
10
11/// Actions are used to implement keyboard-driven UI.
12/// When you declare an action, you can bind keys to the action in the keymap and
13/// listeners for that action in the element tree.
14///
15/// To declare a list of simple actions, you can use the actions! macro, which defines a simple unit struct
16/// action for each listed action name.
17/// ```rust
18/// actions!(MoveUp, MoveDown, MoveLeft, MoveRight, Newline);
19/// ```
20/// More complex data types can also be actions. If you annotate your type with the action derive macro
21/// it will be implemented and registered automatically.
22/// ```
23/// #[derive(Clone, PartialEq, serde_derive::Deserialize, Action)]
24/// pub struct SelectNext {
25/// pub replace_newest: bool,
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/// #[gpui::register_action]
33/// #[derive(gpui::serde::Deserialize, std::cmp::PartialEq, std::clone::Clone, std::fmt::Debug)]
34/// pub struct Paste {
35/// pub content: SharedString,
36/// }
37///
38/// impl gpui::Action for Paste {
39/// ///...
40/// }
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("type_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 it's 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 //todo(remove)
118 let name: SharedString = remove_the_2(action.name).into();
119 self.builders_by_name.insert(name.clone(), action.build);
120 self.names_by_type_id.insert(action.type_id, name.clone());
121 self.all_names.push(name);
122 }
123 }
124
125 /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
126 pub fn build_action_type(&self, type_id: &TypeId) -> Result<Box<dyn Action>> {
127 let name = self
128 .names_by_type_id
129 .get(type_id)
130 .ok_or_else(|| anyhow!("no action type registered for {:?}", type_id))?
131 .clone();
132
133 self.build_action(&name, None)
134 }
135
136 /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
137 pub fn build_action(
138 &self,
139 name: &str,
140 params: Option<serde_json::Value>,
141 ) -> Result<Box<dyn Action>> {
142 //todo(remove)
143 let name = remove_the_2(name);
144 let build_action = self
145 .builders_by_name
146 .get(name.deref())
147 .ok_or_else(|| anyhow!("no action type registered for {}", name))?;
148 (build_action)(params.unwrap_or_else(|| json!({})))
149 .with_context(|| format!("Attempting to build action {}", name))
150 }
151
152 pub fn all_action_names(&self) -> &[SharedString] {
153 self.all_names.as_slice()
154 }
155}
156
157/// Defines unit structs that can be used as actions.
158/// To use more complex data types as actions, annotate your type with the #[action] macro.
159#[macro_export]
160macro_rules! actions {
161 () => {};
162
163 ( $name:ident ) => {
164 #[derive(::std::cmp::PartialEq, ::std::clone::Clone, ::std::default::Default, gpui::serde_derive::Deserialize, gpui::Action)]
165 pub struct $name;
166 };
167
168 ( $name:ident, $($rest:tt)* ) => {
169 actions!($name);
170 actions!($($rest)*);
171 };
172}
173
174//todo!(remove)
175pub fn remove_the_2(action_name: &str) -> String {
176 let mut separator_matches = action_name.rmatch_indices("::");
177 separator_matches.next().unwrap();
178 let name_start_ix = separator_matches.next().map_or(0, |(ix, _)| ix + 2);
179 // todo!() remove the 2 replacement when migration is done
180 action_name[name_start_ix..]
181 .replace("2::", "::")
182 .to_string()
183}
184
185mod no_action {
186 use crate as gpui;
187
188 actions!(NoAction);
189}