Skip to main content

Shortcuts

Struct Shortcuts 

Source
pub struct Shortcuts<Id, Action> { /* private fields */ }
Expand description

Maps key combinations to app-defined Actions, the same way HitTester maps a pointer position to a widget id.

A lookup table an app consults, not something that owns input handling. Bindings are either global (fire regardless of focus) or scoped to a single FocusRing id (fire only while that id holds focus); resolve checks the scoped binding first, so a widget can shadow a global shortcut for the same key while it’s focused.

This does not replace ad hoc match key.code { .. } handling for widget-specific navigation (arrow keys meaning “move selection” only while a particular id is focused, say) – that kind of binding usually carries extra context (list length, current offset) that doesn’t fit a flat Action enum. Shortcuts is for the simple case: one key, always the same Action, wherever it’s in scope. Bindings are a fixed table set up once (there’s no per-frame begin_frame/registration step like FocusRing’s – a key combination either exists or it doesn’t, regardless of what happened to be drawn this frame).

§Example

use retroglyph_core::{Event, KeyCode, KeyEvent, KeyModifiers};
use retroglyph_widgets::Shortcuts;

#[derive(Clone, Copy, PartialEq, Eq)]
enum Id {
    SearchBox,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Action {
    ToggleTheme,
    ClearSearch,
}

let mut shortcuts = Shortcuts::new();
shortcuts.bind_global(KeyCode::Char('t'), KeyModifiers::NONE, Action::ToggleTheme);
shortcuts.bind_scoped(
    Id::SearchBox,
    KeyCode::Escape,
    KeyModifiers::NONE,
    Action::ClearSearch,
);

let escape = Event::Key(KeyEvent::new(KeyCode::Escape, KeyModifiers::NONE));
assert_eq!(shortcuts.resolve(&escape, Some(Id::SearchBox)), Some(Action::ClearSearch));
assert_eq!(shortcuts.resolve(&escape, None), None); // scoped binding, nothing focused

let t = Event::Key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::NONE));
assert_eq!(shortcuts.resolve(&t, None), Some(Action::ToggleTheme)); // global, focus-independent

Implementations§

Source§

impl<Id, Action> Shortcuts<Id, Action>

Source

pub const fn new() -> Self

An empty registry.

Source§

impl<Id: Copy + PartialEq, Action: Copy> Shortcuts<Id, Action>

Source

pub fn bind_global( &mut self, code: KeyCode, modifiers: KeyModifiers, action: Action, )

Registers a binding that fires regardless of what holds focus.

Source

pub fn bind_scoped( &mut self, id: Id, code: KeyCode, modifiers: KeyModifiers, action: Action, )

Registers a binding that only fires while id holds focus.

Source

pub fn resolve(&self, event: &Event, focused: Option<Id>) -> Option<Action>

Resolves event against focused (typically FocusRing::focused).

None for anything but a key-down event. Otherwise: the first registered binding scoped to focused with a matching code/modifiers, or, failing that, the first matching global binding. A scoped binding never fires for any id other than the one it named, including when nothing is focused.

Trait Implementations§

Source§

impl<Id: Clone, Action: Clone> Clone for Shortcuts<Id, Action>

Source§

fn clone(&self) -> Shortcuts<Id, Action>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Id: Debug, Action: Debug> Debug for Shortcuts<Id, Action>

Source§

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

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

impl<Id, Action> Default for Shortcuts<Id, Action>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Id, Action> Freeze for Shortcuts<Id, Action>

§

impl<Id, Action> RefUnwindSafe for Shortcuts<Id, Action>
where Action: RefUnwindSafe, Id: RefUnwindSafe,

§

impl<Id, Action> Send for Shortcuts<Id, Action>
where Action: Send, Id: Send,

§

impl<Id, Action> Sync for Shortcuts<Id, Action>
where Action: Sync, Id: Sync,

§

impl<Id, Action> Unpin for Shortcuts<Id, Action>
where Action: Unpin, Id: Unpin,

§

impl<Id, Action> UnsafeUnpin for Shortcuts<Id, Action>

§

impl<Id, Action> UnwindSafe for Shortcuts<Id, Action>
where Action: UnwindSafe, Id: 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> 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
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> 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
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.