Action

Trait Action 

Source
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_namespace sets the namespace. In Zed this is required.

  • name = "ActionName" overrides the action’s name. This must not contain ::.

  • no_json causes the build method to always error and action_json_schema to return None, and allows actions not implement serde::Serialize and schemars::JsonSchema.

  • no_register skips registering the action. This is useful for implementing the Action trait 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§

Source

fn boxed_clone(&self) -> Box<dyn Action>

Clone the action into a new box

Source

fn partial_eq(&self, action: &dyn Action) -> bool

Do a partial equality check on this action and the other

Source

fn name(&self) -> &'static str

Get the name of this action, for displaying in UI

Source

fn name_for_type() -> &'static str
where Self: Sized,

Get the name of this action type (static)

Source

fn build(value: Value) -> Result<Box<dyn Action>>
where Self: Sized,

Build this action from a JSON value. This is used to construct actions from the keymap. A value of {} will be passed for actions that don’t have any parameters.

Provided Methods§

Source

fn action_json_schema(_: &mut SchemaGenerator) -> Option<Schema>
where Self: Sized,

Optional JSON schema for the action’s input data.

Source

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.

Source

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.

Source

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.

Implementations§

Source§

impl dyn Action

Source

pub fn as_any(&self) -> &dyn Any

Type-erase Action type.

Trait Implementations§

Source§

impl Debug for dyn Action

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Implementors§