pub struct Command { /* private fields */ }Expand description
An application operation shared by buttons, menus, toolbars, and direct callers
(docs/commands.md): one id, one title, one availability, one handler, presented through
button, menu_item and
toolbar_item.
Built with Command::new in the scope that owns it. Clones share the handler and reactive
sources. Builders configure a copy, not existing presentations. Invocation after the owning
scope’s disposal is a no-op. This is a UI-thread value, not a global registry or a native
edit role.
use day_pieces::prelude::*;
use day_reactive::Signal;
let count = Signal::new(0);
let add = Command::new(day_core::id!(add), "Add one", move || count.update(|n| *n += 1))
.enabled(move || count.get() < 3);
assert!(add.invoke());
assert_eq!(count.get(), 1);Implementations§
Source§impl Command
impl Command
Sourcepub fn new<M>(
id: ElementId,
label: impl IntoText<M>,
action: impl Fn() + 'static,
) -> Command
pub fn new<M>( id: ElementId, label: impl IntoText<M>, action: impl Fn() + 'static, ) -> Command
A command with its default presentation id, its title (the same sources as a button: a literal, a localized text, a signal, a closure) and its synchronous handler. Availability is checked before the handler is called. Captures the current reactive scope as the command’s owner.
Sourcepub fn enabled<M>(self, enabled: impl IntoReactive<bool, M>) -> Self
pub fn enabled<M>(self, enabled: impl IntoReactive<bool, M>) -> Self
Availability shared by every presentation and checked again on every invocation.
Sourcepub fn checked<M>(self, checked: impl IntoReactive<bool, M>) -> Self
pub fn checked<M>(self, checked: impl IntoReactive<bool, M>) -> Self
Optional check state. The handler owns mutations; invoking never flips this itself.
pub fn icon(self, symbol: Symbol) -> Self
pub fn image(self, name: impl Into<ImageName>) -> Self
Sourcepub fn shortcut(self, shortcut: Shortcut) -> Self
pub fn shortcut(self, shortcut: Shortcut) -> Self
Menu accelerator metadata. Only installing a menu entry installs the shortcut.
pub fn id(&self) -> &str
Sourcepub fn label(&self) -> String
pub fn label(&self) -> String
Current title, with tracked reads for reactive presentation builders.
pub fn is_enabled(&self) -> bool
pub fn is_checked(&self) -> Option<bool>
Sourcepub fn invoke(&self) -> bool
pub fn invoke(&self) -> bool
Run if still available. Returns whether the handler ran, not whether its work succeeded. Reads are untracked; invoking inside an effect does not subscribe it to the handler.
A live button with this command’s default id, title, icon, availability, and handler. A push button does not draw a check mark. Add presentation styling as usual.
A live menu entry: its title, availability and check mark follow the command, and the menu it sits in re-lowers when they change. Invocation always rechecks availability.
Sourcepub fn toolbar_item(&self) -> ToolbarEntry
pub fn toolbar_item(&self) -> ToolbarEntry
Snapshot for a derived toolbar: .toolbar(move || vec![command.toolbar_item()]).
Checked commands draw a toggle; the handler remains the sole owner of its state.
Reactive builders refresh titles/checks; availability also gets a targeted live binding.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Command
impl !Send for Command
impl !Sync for Command
impl !UnwindSafe for Command
impl Freeze for Command
impl Unpin for Command
impl UnsafeUnpin for Command
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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