Skip to main content

ShortcutRegistry

Struct ShortcutRegistry 

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

Two-layer registry of Shortcuts.

  • defaults holds the records registered by widgets during their build(). Re-registering the same id upserts: code-owned fields are updated, the user override (if any) is preserved.
  • overrides holds user-supplied keystroke rebindings keyed by shortcut id. Overrides persist across widget rebuilds and even when the corresponding default is temporarily unregistered — graveyard semantics, so a widget that disappears and reappears keeps its user-customised bindings.

Every mutation bumps ShortcutRegistry::version so consumers (menu labels, settings UIs) can observe that signal and re-read through ShortcutRegistry::effective.

Implementations§

Source§

impl ShortcutRegistry

Source

pub fn new() -> Self

Source

pub fn version(&self) -> &Signal<u64>

A reactive handle that ticks on every mutation (register, unregister, rebind, put_override). Menus and settings widgets observe it to refresh derived state.

Source

pub fn effective_primary_signal( &mut self, id: &'static str, ) -> Signal<Option<KeyStroke>>

A reactive handle to the effective primary keystroke for a single shortcut id, created lazily and seeded with the current value. It ticks only when that id’s resolved primary actually changes — registering, unregistering or rebinding any other shortcut leaves it untouched.

This is the granular counterpart to Self::version: a widget that displays one shortcut’s accelerator (a menu item, a tooltip) should bind this and update its label as a leaf value, rather than observing the coarse global version and rebuilding.

Source

pub fn register(&mut self, shortcut: Shortcut) -> Option<Shortcut>

Upsert a shortcut default without an owner. If id already exists this replaces the code-owned fields but preserves any existing user override.

Use ShortcutRegistry::register_owned to tie the lifetime of a registration to a widget (so arena destroy can clean up automatically).

Source

pub fn register_owned( &mut self, shortcut: Shortcut, owner: WidgetId, ) -> Option<Shortcut>

Upsert a shortcut default owned by owner. When owner is destroyed, the framework calls ShortcutRegistry::unregister_all_for_owner to remove this registration. Preserves user overrides identically to ShortcutRegistry::register.

Source

pub fn unregister(&mut self, id: &str) -> Option<Shortcut>

Remove a default by id. The user override for that id (if any) stays in the graveyard so it can be re-applied if the shortcut is later re-registered.

Source

pub fn unregister_all_for_owner(&mut self, owner: WidgetId)

Remove every shortcut registered by owner. Called by the widget tree when a widget is destroyed — keeps the registry from leaking entries whose on_activate closures may capture state owned by the destroyed widget.

Source

pub fn owner_of(&self, id: &str) -> Option<WidgetId>

The widget id that currently owns id, if any.

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn iter_defaults(&self) -> impl Iterator<Item = &Shortcut>

Iterate the raw defaults (no overrides merged). Most UI code wants ShortcutRegistry::iter_effective instead.

Source

pub fn get_default(&self, id: &str) -> Option<&Shortcut>

Borrow the raw default record.

Source

pub fn override_for(&self, id: &str) -> Option<KeyStrokeOverride>

Current override for id, if any.

Source

pub fn put_override( &mut self, id: impl Into<String>, override_: KeyStrokeOverride, )

Set the full override for id. Intended for loading persisted user preferences at app startup.

Source

pub fn rebind_primary( &mut self, id: impl Into<String>, keystroke: Option<KeyStroke>, )

Set the primary slot of the override for id. The secondary slot is left untouched — with per-slot SlotOverride semantics the untouched slot continues to delegate to whatever default the shortcut currently declares.

Source

pub fn rebind_secondary( &mut self, id: impl Into<String>, keystroke: Option<KeyStroke>, )

Set the secondary slot of the override for id. The primary slot stays in whatever state it was (Default or user-set).

Source

pub fn clear_override(&mut self, id: &str)

Drop the user override for id, restoring the declared defaults.

Source

pub fn clear_all_overrides(&mut self)

Clear every override. Restores the declared defaults for all registered shortcuts. Graveyard entries are dropped too.

Source

pub fn export_overrides(&self) -> HashMap<String, KeyStrokeOverride>

Snapshot of the full override map, suitable for persisting to disk. Cloned intentionally so callers can serialize without holding a borrow on the registry.

Source

pub fn import_overrides( &mut self, overrides: HashMap<String, KeyStrokeOverride>, )

Replace the entire override map from a persisted snapshot. Typically called once at app startup after loading user preferences from disk. Overrides for ids that are not yet registered are kept in the graveyard so they apply whenever the widget that declares the corresponding default shows up.

Source

pub fn effective(&self, id: &str) -> Option<EffectiveShortcut<'_>>

Effective (defaults + overrides) view of id. None when the id has no registered default — overrides alone don’t manifest as effective records, but they’re kept in the graveyard.

Source

pub fn iter_effective(&self) -> impl Iterator<Item = EffectiveShortcut<'_>>

Iterate all effective shortcuts, including currently-disabled ones. The per-item enabled flag lets settings UIs render disabled rows greyed out.

Order is deterministic: sorted by (category, id) so repeated calls produce identical sequences regardless of the internal HashMap insertion order.

Source

pub fn find_conflict( &self, keystroke: KeyStroke, excluding_id: Option<&str>, ) -> Option<&'static str>

Find the first shortcut id that genuinely conflicts with keystroke, excluding excluding_id if given. Used by settings UIs to auto-unbind conflicts when the user rebinds a chord. Includes disabled shortcuts — a chord is “taken” regardless of whether its current binding is live.

Scope-aware. Two shortcuts only conflict when they could be simultaneously active: either one is ShortcutScope::Global (active everywhere), or both are ShortcutScope::Scoped to the same widget. Two shortcuts scoped to different widgets — e.g. a Delete binding in two separate panels — are not a conflict, because the dispatcher resolves them by focus. (The registry can’t see the tree, so two different Scoped ids are assumed disjoint; the rare genuinely-nested overlap is left unflagged, erring toward allowing the binding.)

When excluding_id is None (or names an unregistered id) the “self” scope is unknown, so every same-chord shortcut is flagged — the safe, conservative fallback.

Source

pub fn matches_by_keystroke( &self, keystroke: KeyStroke, ) -> impl Iterator<Item = EffectiveShortcut<'_>>

All effective shortcuts whose primary or secondary keystroke matches and are currently enabled, in the deterministic (category, id) order of iter_effective.

The dispatcher needs every same-chord candidate, not just the first: the first by id-order may be a Scoped binding whose subtree doesn’t contain the current focus (and so must yield to an applicable Global one), or a Global binding that should itself yield to an in-focus Scoped one (most-specific-scope wins). Resolving that needs the widget tree (descendant checks), which the registry can’t see — so it hands back all candidates and the dispatcher selects with focus in hand.

Source

pub fn find_by_keystroke( &self, keystroke: KeyStroke, ) -> Option<EffectiveShortcut<'_>>

First effective shortcut whose primary or secondary keystroke matches and is currently enabled. Disabled shortcuts are invisible to the dispatcher — the keystroke falls through to the focused widget’s normal on_key handling, matching the “treated as if not registered” semantic advertised by Shortcut::enabled_when.

Note: this ignores scope applicability — for focus-aware resolution the dispatcher uses matches_by_keystroke instead.

Trait Implementations§

Source§

impl Debug for ShortcutRegistry

Source§

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

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

impl Default for ShortcutRegistry

Source§

fn default() -> Self

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

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
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.