Skip to main content

CommandRegistry

Struct CommandRegistry 

Source
pub struct CommandRegistry<C: Copy + Hash + Eq + Into<CommandId>> { /* private fields */ }
Expand description

Registry that maps command identifiers to their specs and runtime states.

CommandRegistry<C> is the single source of truth for all registered commands in an application. It stores the human-readable CommandSpec and the runtime CommandState for each command, keyed by CommandId.

§Type Parameter

C is your application’s command enum (or any Copy + Hash + Eq type that can be converted into a CommandId via Into<CommandId>).

§Example

use egui_command::{CommandId, CommandRegistry, CommandSpec, CommandState};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum AppCmd {
    Save,
    Quit,
}

impl From<AppCmd> for CommandId {
    fn from(c: AppCmd) -> Self { CommandId::new(c) }
}

let registry = CommandRegistry::new()
    .with(
        AppCmd::Save,
        CommandSpec::new(CommandId::new(AppCmd::Save), "Save"),
    )
    .with(
        AppCmd::Quit,
        CommandSpec::new(CommandId::new(AppCmd::Quit), "Quit"),
    );

assert!(registry.spec(AppCmd::Save).is_some());
assert_eq!(registry.state(AppCmd::Save), Some(CommandState::Enabled));

Implementations§

Source§

impl<C: Copy + Hash + Eq + Into<CommandId>> CommandRegistry<C>

Source

pub fn new() -> Self

Create an empty registry.

Source

pub fn register(&mut self, cmd: C, spec: CommandSpec) -> &mut Self

Register a command with its spec, setting state to CommandState::Enabled if not already present.

Returns &mut Self to allow chained register calls.

§Panics

Panics if spec.id != cmd.into() — i.e. the spec was built for a different command than the one being registered.

Source

pub fn with(self, cmd: C, spec: CommandSpec) -> Self

Builder-style registration. Consumes and returns Self so that registrations can be chained on construction:

let reg = CommandRegistry::new().with(C::A, CommandSpec::new(CommandId::new(C::A), "A"));
Source

pub fn spec(&self, cmd: C) -> Option<&CommandSpec>

Look up the CommandSpec for a command.

Returns None if the command was never registered.

Source

pub fn spec_by_id(&self, id: CommandId) -> Option<&CommandSpec>

Look up the CommandSpec by raw CommandId.

Source

pub fn state(&self, cmd: C) -> Option<CommandState>

Look up the current CommandState for a command.

Returns None if the command was never registered.

Source

pub fn state_by_id(&self, id: CommandId) -> Option<CommandState>

Look up the current CommandState by raw CommandId.

Source

pub fn set_state(&mut self, cmd: C, state: CommandState)

Update the runtime state of a registered command.

Has no effect if the command has not been registered.

Source

pub fn set_state_by_id(&mut self, id: CommandId, state: CommandState)

Update the runtime state by raw CommandId.

Has no effect if the id is not registered.

Source

pub fn iter_specs(&self) -> impl Iterator<Item = (CommandId, &CommandSpec)>

Iterate over all registered (CommandId, &CommandSpec) pairs.

Source

pub fn spec_by_id_mut(&mut self, id: CommandId) -> Option<&mut CommandSpec>

Mutable look up of a CommandSpec by raw CommandId.

Returns None if the id has not been registered.

Trait Implementations§

Source§

impl<C: Debug + Copy + Hash + Eq + Into<CommandId>> Debug for CommandRegistry<C>

Source§

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

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

impl<C: Default + Copy + Hash + Eq + Into<CommandId>> Default for CommandRegistry<C>

Source§

fn default() -> CommandRegistry<C>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C> Freeze for CommandRegistry<C>

§

impl<C> RefUnwindSafe for CommandRegistry<C>
where C: RefUnwindSafe,

§

impl<C> Send for CommandRegistry<C>
where C: Send,

§

impl<C> Sync for CommandRegistry<C>
where C: Sync,

§

impl<C> Unpin for CommandRegistry<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for CommandRegistry<C>

§

impl<C> UnwindSafe for CommandRegistry<C>
where C: UnwindSafe,

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
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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.