pub struct Keymap { /* private fields */ }Expand description
The set of key bindings for the application, and the rules for choosing among them.
Usually you don’t build a Keymap yourself. App holds one, and
App::bind_keys adds to it. It is also the place where the
precedence rules are implemented, so this is the reference for how a keystroke resolves.
§Examples
use gpui::{Action, KeyBinding, KeyContext, Keymap, Keystroke, actions};
actions!(keymap_docs, [Save, Format]);
let keymap = Keymap::new(vec![
KeyBinding::new("cmd-s", Format, Some("Workspace")),
KeyBinding::new("cmd-s", Save, Some("Editor")),
]);
// In an editor inside a workspace, the deeper `Editor` binding wins.
let typed = [Keystroke::parse("cmd-s").unwrap()];
let context = [
KeyContext::parse("Workspace").unwrap(),
KeyContext::parse("Editor").unwrap(),
];
let (bindings, pending) = keymap.bindings_for_input(&typed, &context);
assert_eq!(bindings[0].action().name(), "keymap_docs::Save");
assert!(!pending);See keyboard input for the full precedence rules as they apply during dispatch.
Implementations§
Source§impl Keymap
impl Keymap
Sourcepub fn new(bindings: Vec<KeyBinding>) -> Self
pub fn new(bindings: Vec<KeyBinding>) -> Self
Creates a keymap that contains bindings, in the order given.
Sourcepub fn version(&self) -> KeymapVersion
pub fn version(&self) -> KeymapVersion
Returns the current version. See KeymapVersion.
Sourcepub fn add_bindings<T: IntoIterator<Item = KeyBinding>>(&mut self, bindings: T)
pub fn add_bindings<T: IntoIterator<Item = KeyBinding>>(&mut self, bindings: T)
Adds bindings after the existing ones and changes the version.
A binding added later wins over an earlier one at the same depth. Add the user’s bindings after the defaults so that they take precedence.
Sourcepub fn bindings(
&self,
) -> impl DoubleEndedIterator<Item = &KeyBinding> + ExactSizeIterator
pub fn bindings( &self, ) -> impl DoubleEndedIterator<Item = &KeyBinding> + ExactSizeIterator
Sourcepub fn bindings_for_action<'a>(
&'a self,
action: &'a dyn Action,
) -> impl 'a + DoubleEndedIterator<Item = &'a KeyBinding>
pub fn bindings_for_action<'a>( &'a self, action: &'a dyn Action, ) -> impl 'a + DoubleEndedIterator<Item = &'a KeyBinding>
Iterates over the bindings that run action, in the order they were added.
Bindings disabled by a later NoAction or Unbind are
left out. This ignores key contexts, so a binding is included even when it can’t fire in the
current focus. To find the bindings that apply now, use
Window::bindings_for_action. For a shortcut label,
use the last binding, which was added most recently.
Sourcepub fn all_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding>
pub fn all_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding>
Returns every binding that matches all of input, ignoring key contexts.
The bindings are in precedence order: the most recently added comes first. Bindings that
need more keystrokes than input are left out. NoAction and Unbind entries that match
are included, but their effect on other bindings is not applied.
Sourcepub fn bindings_for_input(
&self,
input: &[impl AsKeystroke],
context_stack: &[KeyContext],
) -> (SmallVec<[KeyBinding; 1]>, bool)
pub fn bindings_for_input( &self, input: &[impl AsKeystroke], context_stack: &[KeyContext], ) -> (SmallVec<[KeyBinding; 1]>, bool)
Returns the bindings that match input in context_stack, and whether a longer input
could still match a binding.
The first value holds the matching bindings, highest precedence first. The second value is
true when some binding needs more keystrokes than input, so the caller should wait for
the next keystroke.
Precedence works as follows:
- Depth. A binding’s depth is the deepest point in
context_stackwhere its predicate matches. A binding in a deeper context wins over one in a shallower context. A binding without a context is as deep as the stack, so it is at least as deep as any context-specific binding. At equal depth, the order rule below decides. - Order. At the same depth, the binding added later wins.
NoAction. A winningNoActionremoves the lower-precedence bindings for the same keystrokes, unless they come from a stronger source. SeeKeyBindingMetaIndex.Unbind. A winningUnbindremoves the lower-precedence binding for the same keystrokes that runs the action it names, whatever that binding’s context.
Several bindings can be returned, because a match that isn’t suppressed stays in the list.
§Examples
use gpui::{Action, KeyBinding, KeyContext, Keymap, Keystroke, actions};
actions!(input_docs, [Save, Format]);
let keymap = Keymap::new(vec![
KeyBinding::new("cmd-s", Format, Some("Workspace")),
KeyBinding::new("cmd-s", Save, Some("Editor")),
]);
// The `Editor` binding is at depth 2 and the `Workspace` binding at depth 1.
let (bindings, _) = keymap.bindings_for_input(
&[Keystroke::parse("cmd-s").unwrap()],
&[
KeyContext::parse("Workspace").unwrap(),
KeyContext::parse("Editor").unwrap(),
],
);
assert_eq!(bindings[0].action().name(), "input_docs::Save");Sourcepub fn possible_next_bindings_for_input(
&self,
input: &[Keystroke],
context_stack: &[KeyContext],
) -> Vec<KeyBinding>
pub fn possible_next_bindings_for_input( &self, input: &[Keystroke], context_stack: &[KeyContext], ) -> Vec<KeyBinding>
Returns the bindings that input could still complete with more keystrokes, highest
precedence first, for the given context_stack.
Use it to list the shortcuts available after a prefix, such as after ctrl-k. Bindings
that match input exactly, and NoAction and Unbind entries, are not included.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Keymap
impl !Send for Keymap
impl !Sync for Keymap
impl !UnwindSafe for Keymap
impl Freeze for Keymap
impl Unpin for Keymap
impl UnsafeUnpin for Keymap
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, 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<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().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