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>
impl<C: Copy + Hash + Eq + Into<CommandId>> CommandRegistry<C>
Sourcepub fn register(&mut self, cmd: C, spec: CommandSpec) -> &mut Self
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.
Sourcepub fn with(self, cmd: C, spec: CommandSpec) -> Self
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"));Sourcepub fn spec(&self, cmd: C) -> Option<&CommandSpec>
pub fn spec(&self, cmd: C) -> Option<&CommandSpec>
Look up the CommandSpec for a command.
Returns None if the command was never registered.
Sourcepub fn spec_by_id(&self, id: CommandId) -> Option<&CommandSpec>
pub fn spec_by_id(&self, id: CommandId) -> Option<&CommandSpec>
Look up the CommandSpec by raw CommandId.
Sourcepub fn state(&self, cmd: C) -> Option<CommandState>
pub fn state(&self, cmd: C) -> Option<CommandState>
Look up the current CommandState for a command.
Returns None if the command was never registered.
Sourcepub fn state_by_id(&self, id: CommandId) -> Option<CommandState>
pub fn state_by_id(&self, id: CommandId) -> Option<CommandState>
Look up the current CommandState by raw CommandId.
Sourcepub fn set_state(&mut self, cmd: C, state: CommandState)
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.
Sourcepub fn set_state_by_id(&mut self, id: CommandId, state: CommandState)
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.
Sourcepub fn iter_specs(&self) -> impl Iterator<Item = (CommandId, &CommandSpec)>
pub fn iter_specs(&self) -> impl Iterator<Item = (CommandId, &CommandSpec)>
Iterate over all registered (CommandId, &CommandSpec) pairs.
Sourcepub fn spec_by_id_mut(&mut self, id: CommandId) -> Option<&mut CommandSpec>
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.