pub struct Binding { /* private fields */ }Expand description
Describes a set of keybindings and their associated help text. A Binding
represents a single semantic action that can be triggered by one or more
physical key presses.
Implementations§
Source§impl Binding
impl Binding
Sourcepub fn new<K: Into<KeyPress>>(keys: Vec<K>) -> Self
pub fn new<K: Into<KeyPress>>(keys: Vec<K>) -> Self
Creates a new keybinding with a set of associated key presses.
The input can be anything convertible into a Vec<KeyPress>, making it
ergonomic to define keys with or without modifiers. The binding is
created in an enabled state with empty help text.
§Arguments
keys- A vector of items that can be converted toKeyPress
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::{KeyCode, KeyModifiers};
// Binding with multiple keys, some with modifiers
let save_binding = Binding::new(vec![
(KeyCode::Char('s'), KeyModifiers::CONTROL), // Ctrl+S
(KeyCode::F(2), KeyModifiers::NONE), // F2 key
]);
// Binding from string representations
let quit_binding = Binding::new(vec!["q", "ctrl+c"]);Sourcepub fn new_binding(opts: Vec<BindingOpt>) -> Self
pub fn new_binding(opts: Vec<BindingOpt>) -> Self
Creates a new binding using builder options.
This provides a flexible way to create bindings with various configuration options applied. Similar to Go’s NewBinding function.
§Arguments
opts- A vector of builder options to configure the binding
§Examples
use bubbletea_widgets::key::{Binding, with_keys_str, with_help};
let save_binding = Binding::new_binding(vec![
with_keys_str(&["ctrl+s", "f2"]),
with_help("ctrl+s", "Save the current file"),
]);Sourcepub fn with_help(self, key: impl Into<String>, desc: impl Into<String>) -> Self
pub fn with_help(self, key: impl Into<String>, desc: impl Into<String>) -> Self
Sets the help text for the keybinding using a builder pattern.
§Arguments
key- The human-readable key representation for help displaydesc- A brief description of what the binding does
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let binding = Binding::new(vec![KeyCode::Enter])
.with_help("enter", "Confirm selection");Sourcepub fn with_enabled(self, enabled: bool) -> Self
pub fn with_enabled(self, enabled: bool) -> Self
Sets the initial enabled state of the keybinding.
Disabled bindings will not match key events and will not appear in help views.
§Arguments
enabled- Whether the binding should be enabled
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let disabled_binding = Binding::new(vec![KeyCode::F(1)])
.with_enabled(false);Sourcepub fn with_disabled(self) -> Self
pub fn with_disabled(self) -> Self
Sets the keybinding to disabled state (convenience method).
This is equivalent to calling with_enabled(false) but more readable
when you specifically want to disable a binding.
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let disabled_binding = Binding::new(vec![KeyCode::F(1)])
.with_disabled();Sourcepub fn with_keys(self, keys: &[&str]) -> Self
pub fn with_keys(self, keys: &[&str]) -> Self
Sets the keys for this binding from string representations.
This provides a convenient way to set multiple keys using human-readable string representations.
§Arguments
keys- Array of string key representations
§Examples
use bubbletea_widgets::key::Binding;
let binding = Binding::new::<&str>(vec![])
.with_keys(&["ctrl+s", "f2", "alt+s"]);Sourcepub fn set_keys<K: Into<KeyPress>>(&mut self, keys: Vec<K>)
pub fn set_keys<K: Into<KeyPress>>(&mut self, keys: Vec<K>)
Sets the keys for the keybinding (mutable version).
§Arguments
keys- A vector of items that can be converted toKeyPress
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let mut binding = Binding::new::<&str>(vec![]);
binding.set_keys(vec![KeyCode::Enter, KeyCode::Char(' ')]);Sourcepub fn keys(&self) -> &[KeyPress]
pub fn keys(&self) -> &[KeyPress]
Returns the key presses associated with this binding.
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let binding = Binding::new(vec![KeyCode::Enter]);
let keys = binding.keys();
assert_eq!(keys.len(), 1);Sourcepub fn set_help(&mut self, key: impl Into<String>, desc: impl Into<String>)
pub fn set_help(&mut self, key: impl Into<String>, desc: impl Into<String>)
Sets the help text for the keybinding (mutable version).
§Arguments
key- The human-readable key representation for help displaydesc- A brief description of what the binding does
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let mut binding = Binding::new(vec![KeyCode::Enter]);
binding.set_help("enter", "Confirm selection");Sourcepub fn help(&self) -> &Help
pub fn help(&self) -> &Help
Returns the help information for this binding.
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let binding = Binding::new(vec![KeyCode::Enter])
.with_help("enter", "Confirm selection");
let help = binding.help();
assert_eq!(help.key, "enter");
assert_eq!(help.desc, "Confirm selection");Sourcepub fn enabled(&self) -> bool
pub fn enabled(&self) -> bool
Returns true if the keybinding is enabled and has keys configured.
Disabled bindings or bindings with no keys will not match key events and will not appear in help views.
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let binding = Binding::new(vec![KeyCode::Enter]);
assert!(binding.enabled());
let disabled = Binding::new(vec![KeyCode::F(1)]).with_disabled();
assert!(!disabled.enabled());
let empty = Binding::new::<KeyCode>(vec![]);
assert!(!empty.enabled());Sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Sets the enabled state of the keybinding (mutable version).
§Arguments
enabled- Whether the binding should be enabled
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let mut binding = Binding::new(vec![KeyCode::F(1)]);
binding.set_enabled(false);
assert!(!binding.enabled());Sourcepub fn unbind(&mut self)
pub fn unbind(&mut self)
Removes all keys and help text, effectively nullifying the binding.
After calling this method, the binding will be disabled and will not match any key events or appear in help views.
§Examples
use bubbletea_widgets::key::Binding;
use crossterm::event::KeyCode;
let mut binding = Binding::new(vec![KeyCode::Enter])
.with_help("enter", "Confirm");
assert!(binding.enabled());
binding.unbind();
assert!(!binding.enabled());
assert!(binding.keys().is_empty());Sourcepub fn matches(&self, key_msg: &KeyMsg) -> bool
pub fn matches(&self, key_msg: &KeyMsg) -> bool
Checks if a KeyMsg from bubbletea-rs matches this binding.
The match is successful if the binding is enabled and the event’s
code and modifiers match one of the KeyPress entries exactly.
§Arguments
key_msg- The key message to test against this binding
§Returns
true if the key message matches this binding, false otherwise.
§Examples
use bubbletea_widgets::key::Binding;
use bubbletea_rs::KeyMsg;
use crossterm::event::{KeyCode, KeyModifiers};
let binding = Binding::new(vec![(KeyCode::Char('s'), KeyModifiers::CONTROL)]);
let ctrl_s = KeyMsg {
key: KeyCode::Char('s'),
modifiers: KeyModifiers::CONTROL,
};
assert!(binding.matches(&ctrl_s));Sourcepub fn matches_any(key_msg: &KeyMsg, bindings: &[&Self]) -> bool
pub fn matches_any(key_msg: &KeyMsg, bindings: &[&Self]) -> bool
A convenience function that checks if a KeyMsg matches any of the provided bindings.
§Arguments
key_msg- The key message to testbindings- A slice of binding references to check against
§Returns
true if the key message matches any of the bindings, false otherwise.
§Examples
use bubbletea_widgets::key::Binding;
use bubbletea_rs::KeyMsg;
use crossterm::event::{KeyCode, KeyModifiers};
let save = Binding::new(vec![(KeyCode::Char('s'), KeyModifiers::CONTROL)]);
let quit = Binding::new(vec![(KeyCode::Char('q'), KeyModifiers::CONTROL)]);
let bindings = vec![&save, &quit];
let ctrl_s = KeyMsg {
key: KeyCode::Char('s'),
modifiers: KeyModifiers::CONTROL,
};
assert!(Binding::matches_any(&ctrl_s, &bindings));Trait Implementations§
Auto Trait Implementations§
impl Freeze for Binding
impl RefUnwindSafe for Binding
impl Send for Binding
impl Sync for Binding
impl Unpin for Binding
impl UnwindSafe for Binding
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more