pub trait Action: 'static {
// Required methods
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
where Self: Sized;
fn build(value: Value) -> Result<Box<dyn Action>>
where Self: Sized;
}
Expand description
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.
actions!(MoveUp, MoveDown, MoveLeft, MoveRight, Newline);
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.
#[derive(Clone, PartialEq, serde_derive::Deserialize, Action)]
pub struct SelectNext {
pub replace_newest: bool,
}
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`.
#[gpui::register_action] #[derive(gpui::serde::Deserialize, std::cmp::PartialEq, std::clone::Clone, std::fmt::Debug)] pub struct Paste { pub content: SharedString, }
impl gpui::Action for Paste { ///… }