2023-10-19 17:26:52 +00:00
|
|
|
use crate::SharedString;
|
2023-10-21 15:52:47 +00:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2023-11-10 18:56:14 +00:00
|
|
|
use collections::HashMap;
|
2023-11-17 01:32:02 +00:00
|
|
|
pub use no_action::NoAction;
|
|
|
|
use serde_json::json;
|
|
|
|
use std::any::{Any, TypeId};
|
2023-10-19 17:26:52 +00:00
|
|
|
|
2023-11-08 04:26:51 +00:00
|
|
|
/// Actions are used to implement keyboard-driven UI.
|
|
|
|
/// When you declare an action, you can bind keys to the action in the keymap and
|
|
|
|
/// listeners for that action in the element tree.
|
|
|
|
///
|
|
|
|
/// To declare a list of simple actions, you can use the actions! macro, which defines a simple unit struct
|
|
|
|
/// action for each listed action name.
|
|
|
|
/// ```rust
|
|
|
|
/// actions!(MoveUp, MoveDown, MoveLeft, MoveRight, Newline);
|
|
|
|
/// ```
|
2023-11-17 01:32:02 +00:00
|
|
|
/// More complex data types can also be actions. If you annotate your type with the action derive macro
|
|
|
|
/// it will be implemented and registered automatically.
|
2023-11-08 04:26:51 +00:00
|
|
|
/// ```
|
2023-11-17 01:32:02 +00:00
|
|
|
/// #[derive(Clone, PartialEq, serde_derive::Deserialize, Action)]
|
2023-11-08 04:26:51 +00:00
|
|
|
/// pub struct SelectNext {
|
|
|
|
/// pub replace_newest: bool,
|
|
|
|
/// }
|
|
|
|
///
|
2023-11-17 01:32:02 +00:00
|
|
|
/// If you want to control the behavior of the action trait manually, you can use the lower-level `#[register_action]`
|
|
|
|
/// macro, which only generates the code needed to register your action before `main`.
|
2023-11-08 04:26:51 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #[gpui::register_action]
|
|
|
|
/// #[derive(gpui::serde::Deserialize, std::cmp::PartialEq, std::clone::Clone, std::fmt::Debug)]
|
|
|
|
/// pub struct Paste {
|
|
|
|
/// pub content: SharedString,
|
|
|
|
/// }
|
|
|
|
///
|
2023-11-17 01:32:02 +00:00
|
|
|
/// impl gpui::Action for Paste {
|
|
|
|
/// ///...
|
2023-11-08 04:26:51 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2023-11-17 01:32:02 +00:00
|
|
|
pub trait Action: 'static {
|
|
|
|
fn boxed_clone(&self) -> Box<dyn Action>;
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
fn partial_eq(&self, action: &dyn Action) -> bool;
|
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
|
|
|
fn debug_name() -> &'static str
|
2023-11-15 06:21:32 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2023-11-17 01:32:02 +00:00
|
|
|
fn build(value: serde_json::Value) -> Result<Box<dyn Action>>
|
2023-10-21 15:52:47 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2023-10-19 17:26:52 +00:00
|
|
|
}
|
2023-10-19 17:03:10 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
impl std::fmt::Debug for dyn Action {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("dyn Action")
|
|
|
|
.field("type_name", &self.name())
|
|
|
|
.finish()
|
2023-11-08 04:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 16:54:05 +00:00
|
|
|
impl dyn Action {
|
|
|
|
pub fn type_id(&self) -> TypeId {
|
|
|
|
self.as_any().type_id()
|
|
|
|
}
|
|
|
|
}
|
2023-11-09 20:23:30 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
type ActionBuilder = fn(json: serde_json::Value) -> anyhow::Result<Box<dyn Action>>;
|
2023-11-08 03:58:37 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
pub(crate) struct ActionRegistry {
|
2023-11-08 03:58:37 +00:00
|
|
|
builders_by_name: HashMap<SharedString, ActionBuilder>,
|
2023-11-09 20:23:30 +00:00
|
|
|
names_by_type_id: HashMap<TypeId, SharedString>,
|
2023-11-08 03:58:37 +00:00
|
|
|
all_names: Vec<SharedString>, // So we can return a static slice.
|
|
|
|
}
|
2023-11-08 03:23:02 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
impl Default for ActionRegistry {
|
|
|
|
fn default() -> Self {
|
|
|
|
let mut this = ActionRegistry {
|
|
|
|
builders_by_name: Default::default(),
|
|
|
|
names_by_type_id: Default::default(),
|
|
|
|
all_names: Default::default(),
|
|
|
|
};
|
2023-11-08 03:23:02 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
this.load_actions();
|
|
|
|
|
|
|
|
this
|
|
|
|
}
|
2023-11-09 17:51:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
/// This type must be public so that our macros can build it in other crates.
|
|
|
|
/// But this is an implementation detail and should not be used directly.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub type MacroActionBuilder = fn() -> ActionData;
|
2023-11-08 20:49:09 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
/// This type must be public so that our macros can build it in other crates.
|
|
|
|
/// But this is an implementation detail and should not be used directly.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct ActionData {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub type_id: TypeId,
|
|
|
|
pub build: ActionBuilder,
|
2023-11-08 03:23:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
/// This constant must be public to be accessible from other crates.
|
|
|
|
/// But it's existence is an implementation detail and should not be used directly.
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[linkme::distributed_slice]
|
|
|
|
pub static __GPUI_ACTIONS: [MacroActionBuilder];
|
|
|
|
|
|
|
|
impl ActionRegistry {
|
|
|
|
/// Load all registered actions into the registry.
|
|
|
|
pub(crate) fn load_actions(&mut self) {
|
|
|
|
for builder in __GPUI_ACTIONS {
|
|
|
|
let action = builder();
|
|
|
|
let name: SharedString = qualify_action(action.name).into();
|
|
|
|
self.builders_by_name.insert(name.clone(), action.build);
|
|
|
|
self.names_by_type_id.insert(action.type_id, name.clone());
|
|
|
|
self.all_names.push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct an action based on its name and optional JSON parameters sourced from the keymap.
|
|
|
|
pub fn build_action_type(&self, type_id: &TypeId) -> Result<Box<dyn Action>> {
|
|
|
|
let name = self
|
|
|
|
.names_by_type_id
|
|
|
|
.get(type_id)
|
|
|
|
.ok_or_else(|| anyhow!("no action type registered for {:?}", type_id))?
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
self.build_action(&name, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct an action based on its name and optional JSON parameters sourced from the keymap.
|
|
|
|
pub fn build_action(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
params: Option<serde_json::Value>,
|
|
|
|
) -> Result<Box<dyn Action>> {
|
|
|
|
let build_action = self
|
|
|
|
.builders_by_name
|
|
|
|
.get(name)
|
|
|
|
.ok_or_else(|| anyhow!("no action type registered for {}", name))?;
|
|
|
|
(build_action)(params.unwrap_or_else(|| json!({})))
|
|
|
|
.with_context(|| format!("Attempting to build action {}", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn all_action_names(&self) -> &[SharedString] {
|
|
|
|
self.all_names.as_slice()
|
|
|
|
}
|
2023-11-08 03:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 04:48:47 +00:00
|
|
|
/// Defines unit structs that can be used as actions.
|
|
|
|
/// To use more complex data types as actions, annotate your type with the #[action] macro.
|
2023-11-07 16:00:30 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! actions {
|
|
|
|
() => {};
|
|
|
|
|
|
|
|
( $name:ident ) => {
|
2023-11-17 01:32:02 +00:00
|
|
|
#[derive(::std::cmp::PartialEq, ::std::clone::Clone, ::std::default::Default, gpui::serde_derive::Deserialize, gpui::Action)]
|
2023-11-07 17:48:08 +00:00
|
|
|
pub struct $name;
|
2023-11-07 16:00:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
( $name:ident, $($rest:tt)* ) => {
|
|
|
|
actions!($name);
|
|
|
|
actions!($($rest)*);
|
|
|
|
};
|
|
|
|
}
|
2023-11-16 18:32:55 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
/// This used by our macros to pre-process the action name deterministically
|
2023-11-16 18:32:55 +00:00
|
|
|
#[doc(hidden)]
|
2023-11-17 01:32:02 +00:00
|
|
|
pub fn qualify_action(action_name: &'static str) -> String {
|
2023-11-16 18:32:55 +00:00
|
|
|
let mut separator_matches = action_name.rmatch_indices("::");
|
|
|
|
separator_matches.next().unwrap();
|
|
|
|
let name_start_ix = separator_matches.next().map_or(0, |(ix, _)| ix + 2);
|
|
|
|
// todo!() remove the 2 replacement when migration is done
|
2023-11-17 01:32:02 +00:00
|
|
|
action_name[name_start_ix..]
|
|
|
|
.replace("2::", "::")
|
|
|
|
.to_string()
|
2023-11-16 18:32:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
mod no_action {
|
|
|
|
use crate as gpui;
|
2023-11-16 18:32:55 +00:00
|
|
|
|
2023-11-17 01:32:02 +00:00
|
|
|
actions!(NoAction);
|
2023-11-16 18:32:55 +00:00
|
|
|
}
|