pub struct KeyBinding { /* private fields */ }Expand description
A keybinding: a keystroke sequence, the action it runs, and the key context it applies in.
You usually create one with KeyBinding::new and register it with
App::bind_keys. Its accessors let you inspect a binding, for
example to show its shortcut in a menu:
use gpui::{Action, KeyBinding, actions};
actions!(binding_docs, [Save]);
let binding = KeyBinding::new("cmd-k cmd-s", Save, Some("Editor"));
assert_eq!(binding.keystrokes().len(), 2);
assert_eq!(binding.action().name(), "binding_docs::Save");
assert!(binding.predicate().is_some());See keyboard input for how bindings are matched against the focus path.
Implementations§
Source§impl KeyBinding
impl KeyBinding
Sourcepub fn new<A: Action>(
keystrokes: &str,
action: A,
context: Option<&str>,
) -> Self
pub fn new<A: Action>( keystrokes: &str, action: A, context: Option<&str>, ) -> Self
Creates a binding that runs action when keystrokes is typed in context.
keystrokes is a whitespace-separated sequence of
keystrokes, such as "cmd-s" or "ctrl-k ctrl-s".
context is a key context predicate,
such as "Editor" or "Editor && mode == insert". Pass None to make the binding apply
everywhere, which counts as the deepest context for precedence.
Bindings made here use the keys as written. They never go through the platform’s keyboard mapping, so use this for bindings in code.
§Panics
Panics if any keystroke or the context fails to parse. Use KeyBinding::load to get an
error instead.
§Examples
use gpui::{KeyBinding, actions};
actions!(binding_new_docs, [Save]);
let binding = KeyBinding::new("secondary-s", Save, Some("Editor"));
assert_eq!(binding.keystrokes().len(), 1);Sourcepub fn load(
keystrokes: &str,
action: Box<dyn Action>,
context_predicate: Option<Rc<KeyBindingContextPredicate>>,
use_key_equivalents: bool,
action_input: Option<SharedString>,
keyboard_mapper: &dyn PlatformKeyboardMapper,
) -> Result<Self, InvalidKeystrokeError>
pub fn load( keystrokes: &str, action: Box<dyn Action>, context_predicate: Option<Rc<KeyBindingContextPredicate>>, use_key_equivalents: bool, action_input: Option<SharedString>, keyboard_mapper: &dyn PlatformKeyboardMapper, ) -> Result<Self, InvalidKeystrokeError>
Creates a binding from the parts that a keymap file provides.
keystrokes is split on spaces and each part is parsed with Keystroke::parse. Each
keystroke is then passed to keyboard_mapper, and use_key_equivalents asks it for the
layout-dependent mapping. On macOS, that replaces a key with the character the current
layout types for it. For bindings from a keymap file, action_input is the JSON the action
was built from. Pass None when there is none.
§Errors
Returns InvalidKeystrokeError if any keystroke fails to parse. The context is already
parsed, so this function doesn’t check it.
Sourcepub fn with_meta(self, meta: KeyBindingMetaIndex) -> Self
pub fn with_meta(self, meta: KeyBindingMetaIndex) -> Self
Sets the source metadata for this binding and returns it.
See KeyBindingMetaIndex for how the keymap uses this. A binding without metadata
counts as the strongest source.
Sourcepub fn set_meta(&mut self, meta: KeyBindingMetaIndex)
pub fn set_meta(&mut self, meta: KeyBindingMetaIndex)
Sets the source metadata for this binding. See KeyBinding::with_meta.
Sourcepub fn match_keystrokes(&self, typed: &[impl AsKeystroke]) -> Option<bool>
pub fn match_keystrokes(&self, typed: &[impl AsKeystroke]) -> Option<bool>
Returns how the typed keystrokes relate to this binding’s keystroke sequence.
Returns None if they don’t match. Returns Some(true) if they match the start of the
sequence and more keystrokes are needed, and Some(false) if they match all of it. The
keymap uses this to decide whether to wait for the next key.
§Examples
use gpui::{Keystroke, KeyBinding, actions};
actions!(match_docs, [Save]);
let binding = KeyBinding::new("ctrl-k ctrl-s", Save, None);
let ctrl_k = [Keystroke::parse("ctrl-k").unwrap()];
let ctrl_k_ctrl_s = [
Keystroke::parse("ctrl-k").unwrap(),
Keystroke::parse("ctrl-s").unwrap(),
];
assert_eq!(binding.match_keystrokes(&ctrl_k), Some(true));
assert_eq!(binding.match_keystrokes(&ctrl_k_ctrl_s), Some(false));
assert_eq!(binding.match_keystrokes(&[Keystroke::parse("ctrl-x").unwrap()]), None);Sourcepub fn keystrokes(&self) -> &[KeybindingKeystroke]
pub fn keystrokes(&self) -> &[KeybindingKeystroke]
Returns the keystrokes that trigger this binding, in order. A binding has one keystroke
for a single key, and more for a sequence such as ctrl-k ctrl-s.
Sourcepub fn predicate(&self) -> Option<Rc<KeyBindingContextPredicate>>
pub fn predicate(&self) -> Option<Rc<KeyBindingContextPredicate>>
Returns the key context predicate this binding applies in, or None if it applies in
every context.
Sourcepub fn meta(&self) -> Option<KeyBindingMetaIndex>
pub fn meta(&self) -> Option<KeyBindingMetaIndex>
Returns the source metadata set with KeyBinding::with_meta, if any.
Sourcepub fn action_input(&self) -> Option<SharedString>
pub fn action_input(&self) -> Option<SharedString>
Returns the JSON input the action was built from, if the binding came from a keymap file.
Trait Implementations§
Source§impl Clone for KeyBinding
impl Clone for KeyBinding
Auto Trait Implementations§
impl !RefUnwindSafe for KeyBinding
impl !Send for KeyBinding
impl !Sync for KeyBinding
impl !UnwindSafe for KeyBinding
impl Freeze for KeyBinding
impl Unpin for KeyBinding
impl UnsafeUnpin for KeyBinding
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>,
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
Source§type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
Source§fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
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.impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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, C> ComponentsInto<C> for T
impl<T, C> ComponentsInto<C> for T
Source§fn components_into(self) -> C
fn components_into(self) -> C
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§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> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
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