pub trait Action: Any + Send {
// Required methods
fn boxed_clone(&self) -> Box<dyn Action>;
fn partial_eq(&self, action: &dyn Action) -> bool;
fn name(&self) -> &'static str;
fn name_for_type() -> &'static str
where Self: Sized;
fn build(value: Value) -> Result<Box<dyn Action>>
where Self: Sized;
// Provided methods
fn action_json_schema(_: &mut SchemaGenerator) -> Option<Schema>
where Self: Sized { ... }
fn deprecated_aliases() -> &'static [&'static str]
where Self: Sized { ... }
fn deprecation_message() -> Option<&'static str>
where Self: Sized { ... }
fn documentation() -> Option<&'static str>
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 in the given namespace.
use gpui::actions;
actions!(editor, [MoveUp, MoveDown, MoveLeft, MoveRight, Newline]);Registering the actions with the same name will result in a panic during App creation.
§Derive Macro
More complex data types can also be actions, by using the derive macro for Action:
use gpui::Action;
#[derive(Clone, PartialEq, serde::Deserialize, schemars::JsonSchema, Action)]
#[action(namespace = editor)]
pub struct SelectNext {
pub replace_newest: bool,
}The derive macro for Action requires that the type implement Clone and PartialEq. It also
requires serde::Deserialize and schemars::JsonSchema unless #[action(no_json)] is
specified. In Zed these trait impls are used to load keymaps from JSON.
Multiple arguments separated by commas may be specified in #[action(...)]:
-
namespace = some_namespacesets the namespace. In Zed this is required. -
name = "ActionName"overrides the action’s name. This must not contain::. -
no_jsoncauses thebuildmethod to always error andaction_json_schemato returnNone, and allows actions not implementserde::Serializeandschemars::JsonSchema. -
no_registerskips registering the action. This is useful for implementing theActiontrait while not supporting invocation by name or JSON deserialization. -
deprecated_aliases = ["editor::SomeAction"]specifies deprecated old names for the action. These action names should not correspond to any actions that are registered. These old names can then still be used to refer to invoke this action. In Zed, the keymap JSON schema will accept these old names and provide warnings. -
deprecated = "Message about why this action is deprecation"specifies a deprecation message. In Zed, the keymap JSON schema will cause this to be displayed as a warning.
§Manual Implementation
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.
use gpui::{SharedString, register_action};
#[derive(Clone, PartialEq, Eq, serde::Deserialize, schemars::JsonSchema)]
pub struct Paste {
pub content: SharedString,
}
impl gpui::Action for Paste {
}
register_action!(Paste);Required Methods§
Sourcefn boxed_clone(&self) -> Box<dyn Action>
fn boxed_clone(&self) -> Box<dyn Action>
Clone the action into a new box
Sourcefn partial_eq(&self, action: &dyn Action) -> bool
fn partial_eq(&self, action: &dyn Action) -> bool
Do a partial equality check on this action and the other
Sourcefn name_for_type() -> &'static strwhere
Self: Sized,
fn name_for_type() -> &'static strwhere
Self: Sized,
Get the name of this action type (static)
Provided Methods§
Sourcefn action_json_schema(_: &mut SchemaGenerator) -> Option<Schema>where
Self: Sized,
fn action_json_schema(_: &mut SchemaGenerator) -> Option<Schema>where
Self: Sized,
Optional JSON schema for the action’s input data.
Sourcefn deprecated_aliases() -> &'static [&'static str]where
Self: Sized,
fn deprecated_aliases() -> &'static [&'static str]where
Self: Sized,
A list of alternate, deprecated names for this action. These names can still be used to invoke the action. In Zed, the keymap JSON schema will accept these old names and provide warnings.
Sourcefn deprecation_message() -> Option<&'static str>where
Self: Sized,
fn deprecation_message() -> Option<&'static str>where
Self: Sized,
Returns the deprecation message for this action, if any. In Zed, the keymap JSON schema will cause this to be displayed as a warning.
Sourcefn documentation() -> Option<&'static str>where
Self: Sized,
fn documentation() -> Option<&'static str>where
Self: Sized,
The documentation for this action, if any. When using the derive macro for actions this will be automatically generated from the doc comments on the action struct.