command_property

Macro command_property 

Source
macro_rules! command_property {
    ($(
        $(#[$on_cmd_attrs:meta])*
        $vis:vis fn $command:ident {
            cmd: $cmd_init:expr
            $(, enabled: $enabled_var:expr)?
            $(, widget_impl: $Wgt:ty)?
            $(,)?
        }
    )+) => { ... };
}
Expand description

Declare one or more command event properties.

Each declaration expands to two properties on_$command and on_pre_$command. The preview properties call on_pre_command, the main event properties call on_command.

By default a CAN_$COMMAND_VAR and can_$command var and property are also generated.

§Examples

command_property! {
    /// Paste command property docs.
    pub fn paste {
        cmd: PASTE_CMD.scoped(WIDGET.id()),
    }
}

The example above generates event properties on_pre_paste and on_paste, context property can_paste and context var CAN_PASTE_VAR.

The event properties will hold a CommandHandle when the widget is inited, the handle wil signal enabled based on the contextual var value. The context property sets that var in a widget context.

§Command

The cmd: expression evaluates on init to generate the command, this allows for the creation of widget scoped commands. The new command property event handler will receive events for the command and scope that target the widget where the property is set.

If the command is scoped on the root widget and the command property is set on the same root widget a second handle is taken for the window scope too, so callers can target the window using the window ID or the root widget ID.

§Enabled

The enabled: expression evaluates on init to generate a boolean variable that defines if the command handle is enabled. Command event handlers track both their existence and the enabled flag, see Command::subscribe for details.

If not provided a CAN_$CMD_VAR and can_$cmd contextual var and property are generated.

command_property! {
    /// Paste command property docs.
    pub fn paste {
        cmd: PASTE_CMD.scoped(WIDGET.id()),
        enabled: const_var(true), // command handle always enabled
    }
}

In the example above enabled: is set to a custom variable, so the associated CAN_PASTE_VAR and can_paste will not be generated.

§Implement For

You can implement the new properties for a widget or mixin using the widget_impl: directive:

/// Clipboard handler.
#[widget_mixin]
pub struct ClipboardMix<P>(P);

command_property! {
    /// Paste command property docs.
    pub fn paste {
        cmd: PASTE_CMD.scoped(WIDGET.id()),
        widget_impl: ClipboardMix<P>,
    }
}