Command

Struct Command 

Source
pub struct Command { /* private fields */ }
Expand description

Identifies a command event.

Use the command! to declare commands, it declares command static items with optional metadata initialization.

command! {
    /// Foo-bar command.
    pub static FOO_BAR_CMD { foo: true, bar: false };
}

§Metadata

Commands can have associated metadata, this metadata is extendable and can be used to enable command features such as command shortcuts. The metadata can be accessed using with_meta, metadata extensions traits can use this metadata to store state. See CommandMeta for more details.

§Handles

Unlike other events, commands only notify if it has at least one handler, handlers must call subscribe to indicate that the command is relevant to the current app state and set the subscription handle enabled flag to indicate that the handler can fulfill command requests.

§Scopes

Commands are global by default, meaning an enabled handle anywhere in the app enables it everywhere. You can use scoped to declare sub-commands that are the same command event, but filtered to a scope, metadata of scoped commands inherit from the app scope metadata, but can be overridden just for the scope.

Implementations§

Source§

impl Command

Source

pub fn subscribe(&self, enabled: bool) -> CommandHandle

Create a new handle to this command.

A handle indicates that command handlers are present in the current app, the enabled flag indicates the handler is ready to fulfill command requests.

If the command is scoped on a window or widget if it is added to the command event subscribers.

Source

pub fn subscribe_wgt(&self, enabled: bool, target: WidgetId) -> CommandHandle

Create a new handle for this command for a handler in the target widget.

The handle behaves like subscribe, but include the target on the delivery list for app scoped commands. Note this only works for global commands (app scope), window and widget scoped commands only notify the scope so the target is ignored for scoped commands.

Source

pub fn event(&self) -> Event<CommandArgs>

Underlying event that represents this command in any scope.

Source

pub fn scope(&self) -> CommandScope

Command scope.

Source

pub fn scoped(self, scope: impl Into<CommandScope>) -> Command

Gets the command in a new scope.

Source

pub fn with_meta<R>(&self, visit: impl FnOnce(&mut CommandMeta<'_>) -> R) -> R

Visit the command custom metadata of the current scope.

Metadata for CommandScope::App is retained for the duration of the app, metadata scoped on window or widgets is dropped after an update cycle with no handler and no strong references to has_handlers and is_enabled.

Source

pub fn has_handlers(&self) -> Var<bool>

Gets a variable that tracks if this command has any handlers.

Source

pub fn is_enabled(&self) -> Var<bool>

Gets a variable that tracks if this command has any enabled handlers.

Source

pub fn visit_scopes<T>( &self, visitor: impl FnMut(Command) -> ControlFlow<T>, ) -> Option<T>

Calls visitor for each scope of this command.

Note that scoped commands are removed if unused, see with_meta for more details.

Source

pub fn notify(&self)

Schedule a command update without param.

Source

pub fn notify_descendants(&self, parent: &WidgetInfo)

Schedule a command update without param for all scopes inside parent.

Source

pub fn notify_param(&self, param: impl Any + Send + Sync)

Schedule a command update with custom param.

Source

pub fn notify_linked( &self, propagation: EventPropagationHandle, param: Option<CommandParam>, )

Schedule a command update linked with an external event propagation.

Source

pub fn each_update( &self, direct_scope_only: bool, ignore_propagation: bool, handler: impl FnMut(&CommandArgs), )

Visit each new update, oldest first, that target the context widget.

This is similar to Event::each_update, but with extra filtering. If direct_scope_only is enabled only handle exact command scope matches, otherwise the app scope matches all events, the window scope matches all events for the window or widgets in the window and the widget scope matches the widget and all descendants.

Source

pub fn latest_update<O>( &self, direct_scope_only: bool, ignore_propagation: bool, handler: impl FnOnce(&CommandArgs) -> O, ) -> Option<O>

Visit the latest update that targets the context widget.

This is similar to Event::latest_update, but with extra filtering.

Source

pub fn has_update( &self, direct_scope_only: bool, ignore_propagation: bool, ) -> bool

Visit the latest update that targets the context widget.

This is similar to Event::has_update, but with extra filtering.

Source

pub fn on_pre_event( &self, init_enabled: bool, direct_scope_only: bool, ignore_propagation: bool, handler: Box<dyn FnMut(&CommandArgs) -> HandlerResult + Send>, ) -> CommandHandle

Creates a preview event handler for the command.

This is similar to Event::on_pre_event, but with extra filtering. The handler is only called if handle is enabled and if the scope matches. if direct_scope_only is enabled only handles exact matches, otherwise the app scope matches all events, the window scope matches all events for the window or widgets in the window and the widget scope matches the widget and all descendants.

The init_enabled value defines the handle initial state.

Source

pub fn on_event( &self, init_enabled: bool, direct_scope_only: bool, ignore_propagation: bool, handler: Box<dyn FnMut(&CommandArgs) -> HandlerResult + Send>, ) -> CommandHandle

Creates an event handler for the command.

This is similar to Event::on_event, but with extra filtering. The handler is only called if the command handle is enabled and if the scope matches. if direct_scope_only is enabled only handles exact matches, otherwise the app scope matches all events, the window scope matches all events for the window or widgets in the window and the widget scope matches the widget and all descendants.

The init_enabled value defines the handle initial state.

Source

pub fn on_pre_event_with_enabled( &self, init_enabled: bool, direct_scope_only: bool, ignore_propagation: bool, handler: Box<dyn FnMut(&(CommandArgs, Var<bool>)) -> HandlerResult + Send>, ) -> CommandHandle

Sets a handler like Command::on_pre_event, but the args include the handle enabled and the handler is called when the handle is disabled as well.

Source

pub fn on_event_with_enabled( &self, init_enabled: bool, direct_scope_only: bool, ignore_propagation: bool, handler: Box<dyn FnMut(&(CommandArgs, Var<bool>)) -> HandlerResult + Send>, ) -> CommandHandle

Sets a handler like Command::on_event, but the args include the handle enabled and the handler is called when the handle is disabled as well.

Source

pub fn static_name(&self) -> &'static str

Name of the static item that defines this command.

Methods from Deref<Target = Event<CommandArgs>>§

Source

pub fn var(&self) -> Var<EventUpdates<A>>

Variable that tracks all the args notified in the last update cycle.

Note that the event variable is only cleared when new notifications are requested.

Source

pub fn var_latest(&self) -> Var<Option<A>>

Variable that tracks the latest update.

Is only None if this event has never notified yet.

Source

pub fn var_map<O>( &self, filter_map: impl FnMut(&A) -> Option<O> + Send + 'static, fallback_init: impl Fn() -> O + Send + 'static, ) -> Var<O>
where O: VarValue,

Filter map the latest args.

The variable tracks the latest args that passes the filter_map. Every event update calls the closure for each pending args, latest first, and stops on the first args that produces a new value.

Source

pub fn var_bind<O>( &self, other: &Var<O>, filter_map: impl FnMut(&A) -> Option<O> + Send + 'static, ) -> VarHandle
where O: VarValue,

Bind filter the latest args to the variable.

The other variable will be updated with the latest args that passes the filter_map. Every event update calls the closure for each pending args, latest first, and stops on the first args that produces a new value.

Source

pub fn notify(&self, args: A)

Modify the event variable to include the args in the next update.

Source

pub fn each_update(&self, ignore_propagation: bool, handler: impl FnMut(&A))

Visit each new update, oldest first, that target the context widget.

If not called inside an widget visits all updates.

If ignore_propagation is false only calls the handler if the propagation is not stopped.

Source

pub fn find_update<O>( &self, ignore_propagation: bool, handler: impl FnMut(&A) -> Option<O>, ) -> Option<O>

Visit each_update, returns on the first args that produces an O.

Source

pub fn any_update( &self, ignore_propagation: bool, handler: impl FnMut(&A) -> bool, ) -> bool

Visit each_update, returns on the first args that produces true.

Source

pub fn latest_update<O>( &self, ignore_propagation: bool, handler: impl FnOnce(&A) -> O, ) -> Option<O>

Visit the latest update that targets the context widget.

If not called inside an widget visits the latest in general.

If ignore_propagation is false only calls the handler if the propagation is not stopped.

Source

pub fn has_update(&self, ignore_propagation: bool) -> bool

If has at least one update for the context widget.

If ignore_propagation is false only returns true if any propagation is not stopped.

Source

pub fn subscribe(&self, op: UpdateOp, widget_id: WidgetId) -> VarHandle

Subscribe the widget to receive updates when events are relevant to it.

Source

pub fn subscribe_when( &self, op: UpdateOp, widget_id: WidgetId, predicate: impl Fn(&A) -> bool + Send + Sync + 'static, ) -> VarHandle

Subscribe the widget to receive updates when events are relevant to it and the latest args passes the predicate.

Source

pub fn on_pre_event( &self, ignore_propagation: bool, handler: Box<dyn FnMut(&A) -> HandlerResult + Send>, ) -> VarHandle

Creates a preview event handler.

The event handler is called for every update that has not stopped propagation. The handler is called before widget handlers and on_event handlers. The handler is called after all previous registered preview handlers.

If ignore_propagation is set also call handlers for args with stopped propagation.

Returns a VarHandle that can be dropped to unsubscribe, you can also unsubscribe from inside the handler by calling unsubscribe.

§Examples
let handle = FOCUS_CHANGED_EVENT.on_pre_event(
    false,
    hn!(|args| {
        println!("focused: {:?}", args.new_focus);
    }),
);

The example listens to all FOCUS_CHANGED_EVENT events, independent of widget context and before all UI handlers.

§Handlers

the event handler can be any Handler<A>, there are multiple flavors of handlers, including async handlers that allow calling .await. The handler closures can be declared using hn!, async_hn!, hn_once! and async_hn_once!.

§Async

Note that for async handlers only the code before the first .await is called in the preview moment, code after runs in subsequent event updates, after the event has already propagated, so stopping propagation only causes the desired effect before the first .await.

Source

pub fn on_event( &self, ignore_propagation: bool, handler: Box<dyn FnMut(&A) -> HandlerResult + Send>, ) -> VarHandle

Creates an event handler.

The event handler is called for every update that has not stopped propagation. The handler is called after all on_pre_event all widget handlers and all on_event handlers registered before this one.

If ignore_propagation is set also call handlers for args with stopped propagation.

Returns an VarHandle that can be dropped to unsubscribe, you can also unsubscribe from inside the handler by calling unsubscribe in the third parameter of hn! or async_hn!.

§Examples
let handle = FOCUS_CHANGED_EVENT.on_event(
    false,
    hn!(|args| {
        println!("focused: {:?}", args.new_focus);
    }),
);

The example listens to all FOCUS_CHANGED_EVENT events, independent of widget context, after the UI was notified.

§Handlers

the event handler can be any Handler<A>, there are multiple flavors of handlers, including async handlers that allow calling .await. The handler closures can be declared using hn!, async_hn!, hn_once! and async_hn_once!.

§Async

Note that for async handlers only the code before the first .await is called in the preview moment, code after runs in subsequent event updates, after the event has already propagated, so stopping propagation only causes the desired effect before the first .await.

Source

pub fn receiver(&self) -> Receiver<A>
where A: Send,

Creates a receiver channel for the event. The event updates are send on hook, before even preview handlers. The receiver is unbounded, it will fill indefinitely if not drained. The receiver can be used in any thread, including non-app threads.

Drop the receiver to stop listening.

Source

pub fn as_any(&self) -> &AnyEvent

Deref as AnyEvent.

Source

pub fn hook( &self, handler: impl FnMut(&A) -> bool + Send + 'static, ) -> VarHandle

Setups a callback for just after the event notifications are listed, the closure runs in the root app context, just like var modify and hook closures.

The closure must return true to be retained and false to be dropped.

Any event notification or var modification done in the handler will apply on the same update that notifies this event.

Source

pub async fn wait_match( &self, predicate: impl Fn(&A) -> bool + Send + Sync + 'static, )

Wait until any args, current or new passes the predicate.

Source

pub fn with<R>(&self, visitor: impl FnOnce(&EventUpdates<A>) -> R) -> R

Visit the args current value of var.

Methods from Deref<Target = AnyEvent>§

Source

pub fn subscribe(&self, op: UpdateOp, widget_id: WidgetId) -> VarHandle

Subscribe the widget to receive updates when events are relevant to it.

Source

pub fn var(&self) -> AnyVar

Variable that tracks all the args notified in the last update cycle.

Note that the event variable is only cleared when new notifications are requested.

Source

pub fn hook( &self, handler: impl FnMut(&(dyn AnyEventArgs + 'static)) -> bool + Send + 'static, ) -> VarHandle

Setups a callback for just after the event notifications are listed, the closure runs in the root app context, just like var modify and hook closures.

The closure must return true to be retained and false to be dropped.

Any event notification or var modification done in the handler will apply on the same update that notifies this event.

Trait Implementations§

Source§

impl Clone for Command

Source§

fn clone(&self) -> Command

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl CommandFocusExt for Command

Source§

fn focus_scoped(self) -> Var<Command>

Gets a command variable with self scoped to the focused (non-alt) widget or app. Read more
Source§

fn focus_scoped_with( self, map: impl FnMut(Option<WidgetInfo>) -> CommandScope + Send + 'static, ) -> Var<Command>

Gets a command variable with self scoped to the output of map. Read more
Source§

impl CommandIconExt for Command

Source§

fn icon(self) -> Var<WidgetFn<()>>

Gets a read-write variable that is the icon for the command.
Source§

fn init_icon(self, icon: WidgetFn<()>) -> Command

Sets the initial icon if it is not set.
Source§

impl CommandInfoExt for Command

Source§

fn info(self) -> Var<Txt>

Gets a read-write variable that is a short informational string about the command.
Source§

fn init_info(self, info: impl Into<Txt>) -> Command

Sets the initial info if it is not set.
Source§

impl CommandNameExt for Command

Source§

fn name(self) -> Var<Txt>

Gets a read-write variable that is the display name for the command.
Source§

fn init_name(self, name: impl Into<Txt>) -> Command

Sets the initial name if it is not set.
Source§

fn name_with_shortcut(self) -> Var<Txt>

Gets a read-only variable that formats the name and first shortcut formatted as "name (first_shortcut)" Read more
Source§

impl CommandShortcutExt for Command

Source§

fn shortcut(self) -> Var<Shortcuts>

Gets a read-write variable that is zero-or-more shortcuts that invoke the command.
Source§

fn shortcut_filter(self) -> Var<ShortcutFilter>

Gets a read-write variable that sets a filter for when the shortcut is valid.
Source§

fn init_shortcut(self, shortcut: impl Into<Shortcuts>) -> Command

Sets the initial shortcuts.
Source§

fn init_shortcut_filter(self, filter: impl Into<ShortcutFilter>) -> Command

Sets the initial shortcut filters.
Source§

fn shortcut_txt(self) -> Var<Txt>
where Self: Sized,

Gets a read-only variable that is the display text for the first shortcut.
Source§

impl CommandShortcutMatchesExt for Command

Source§

fn shortcut_matches(self, shortcut: &Shortcut) -> bool

Returns true if the command has handlers, enabled or disabled, and the shortcut if one of the command shortcuts.
Source§

impl CommandUndoExt for Command

Source§

fn undo_scoped(self) -> Var<Command>

Gets the command scoped in the undo scope widget that is or contains the focused widget, or scoped on the app if there is no focused undo scope.
Source§

fn undo_stack(self) -> UndoStackInfo

Latest undo stack for the given scope, same as calling UNDO::undo_stack inside the scope.
Source§

fn redo_stack(self) -> UndoStackInfo

Latest undo stack for the given scope, same as calling UNDO::redo_stack inside the scope.
Source§

impl Debug for Command

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Deref for Command

Source§

type Target = Event<CommandArgs>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Command as Deref>::Target

Dereferences the value.
Source§

impl Hash for Command

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Command

Source§

fn eq(&self, other: &Command) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Command

Source§

impl Eq for Command

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> AnyVarValue for T
where T: Debug + PartialEq + Clone + Any + Send + Sync,

Source§

fn clone_boxed(&self) -> BoxAnyVarValue

Clone the value.
Source§

fn eq_any(&self, other: &(dyn AnyVarValue + 'static)) -> bool

Gets if self and other are equal.
Source§

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

Value type name. Read more
Source§

fn try_swap(&mut self, other: &mut (dyn AnyVarValue + 'static)) -> bool

Swap value with other if both are of the same type.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

Source§

impl<T> FsChangeNote for T
where T: Debug + Any + Send + Sync,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Access any.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

§

fn into_sample(self) -> T

Source§

impl<T> IntoVar<T> for T
where T: VarValue,

Source§

fn into_var(self) -> Var<T>

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> IntoValue<T> for T
where T: VarValue,

Source§

impl<T> StateValue for T
where T: Any + Send + Sync,

Source§

impl<T> VarValue for T