Skip to main content

Binding

Struct Binding 

Source
pub struct Binding {
    pub key: KeyCode,
    pub modifiers: Option<KeyModifiers>,
    pub display: String,
    pub description: String,
    pub visible: bool,
}
Expand description

A single key binding with display text and description.

Fields§

§key: KeyCode

The key code for matching.

§modifiers: Option<KeyModifiers>

Optional modifier (Ctrl, Alt, Shift).

§display: String

Display text shown in help bar (e.g., “q”, “Ctrl+S”, “↑”).

§description: String

Description of what this binding does.

§visible: bool

Whether to show in help bar.

Implementations§

Source§

impl Binding

Source

pub fn matches(&self, key: &KeyEvent) -> bool

Returns true if key is a press of this binding’s registered chord.

This is the dispatch primitive that mirrors bubbletea’s key.Matches: a KeyEvent matches when it is a press (releases and repeats never match), its KeyCode equals Binding::key, and its modifiers satisfy Binding::modifiers:

  • modifiers: None requires that no modifiers are held (so a plain q binding does not fire on Ctrl+q).
  • modifiers: Some(mods) requires that at least every modifier in mods is held, matching the lenient semantics of Context::key_mod.
§Examples
use slt::{Event, KeyCode, KeyMap, KeyModifiers};

let km = KeyMap::new().bind('q', "Quit");
let binding = &km.bindings[0];

let Event::Key(quit) = Event::key_char('q') else { unreachable!() };
assert!(binding.matches(&quit));

// A plain binding ignores modified presses of the same key.
let Event::Key(ctrl_q) = Event::key_ctrl('q') else { unreachable!() };
assert!(!binding.matches(&ctrl_q));

// A different key never matches.
let Event::Key(other) = Event::key_char('x') else { unreachable!() };
assert!(!binding.matches(&other));

// Modifier bindings use `contains` semantics.
let save = KeyMap::new().bind_mod('s', KeyModifiers::CONTROL, "Save");
let Event::Key(ctrl_s) =
    Event::key_mod(KeyCode::Char('s'), KeyModifiers::CONTROL)
else {
    unreachable!()
};
assert!(save.bindings[0].matches(&ctrl_s));

Trait Implementations§

Source§

impl Clone for Binding

Source§

fn clone(&self) -> Binding

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 Debug for Binding

Source§

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

Formats the value using the given formatter. 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> 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.