Remove catch all keymap and KeyPressed action

This commit is contained in:
Kay Simmons 2023-02-10 16:09:15 -08:00
parent 459060764a
commit 327932ba3b
4 changed files with 14 additions and 45 deletions

View file

@ -65,7 +65,7 @@ impl CommandPalette {
action, action,
keystrokes: bindings keystrokes: bindings
.iter() .iter()
.filter_map(|binding| binding.keystrokes()) .map(|binding| binding.keystrokes())
.last() .last()
.map_or(Vec::new(), |keystrokes| keystrokes.to_vec()), .map_or(Vec::new(), |keystrokes| keystrokes.to_vec()),
}) })

View file

@ -6,24 +6,15 @@ mod keystroke;
use std::{any::TypeId, fmt::Debug}; use std::{any::TypeId, fmt::Debug};
use collections::HashMap; use collections::HashMap;
use serde::Deserialize;
use smallvec::SmallVec; use smallvec::SmallVec;
use crate::{impl_actions, Action}; use crate::Action;
pub use binding::{Binding, BindingMatchResult}; pub use binding::{Binding, BindingMatchResult};
pub use keymap::Keymap; pub use keymap::Keymap;
pub use keymap_context::{KeymapContext, KeymapContextPredicate}; pub use keymap_context::{KeymapContext, KeymapContextPredicate};
pub use keystroke::Keystroke; pub use keystroke::Keystroke;
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
pub struct KeyPressed {
#[serde(default)]
pub keystroke: Keystroke,
}
impl_actions!(gpui, [KeyPressed]);
pub struct KeymapMatcher { pub struct KeymapMatcher {
pub contexts: Vec<KeymapContext>, pub contexts: Vec<KeymapContext>,
pending_views: HashMap<usize, KeymapContext>, pending_views: HashMap<usize, KeymapContext>,
@ -102,13 +93,7 @@ impl KeymapMatcher {
for binding in self.keymap.bindings().iter().rev() { for binding in self.keymap.bindings().iter().rev() {
match binding.match_keys_and_context(&self.pending_keystrokes, &self.contexts[i..]) match binding.match_keys_and_context(&self.pending_keystrokes, &self.contexts[i..])
{ {
BindingMatchResult::Complete(mut action) => { BindingMatchResult::Complete(action) => {
// Swap in keystroke for special KeyPressed action
if action.name() == "KeyPressed" && action.namespace() == "gpui" {
action = Box::new(KeyPressed {
keystroke: keystroke.clone(),
});
}
matched_bindings.push((view_id, action)) matched_bindings.push((view_id, action))
} }
BindingMatchResult::Partial => { BindingMatchResult::Partial => {

View file

@ -7,7 +7,7 @@ use super::{KeymapContext, KeymapContextPredicate, Keystroke};
pub struct Binding { pub struct Binding {
action: Box<dyn Action>, action: Box<dyn Action>,
keystrokes: Option<SmallVec<[Keystroke; 2]>>, keystrokes: SmallVec<[Keystroke; 2]>,
context_predicate: Option<KeymapContextPredicate>, context_predicate: Option<KeymapContextPredicate>,
} }
@ -23,16 +23,10 @@ impl Binding {
None None
}; };
let keystrokes = if keystrokes == "*" { let keystrokes = keystrokes
None // Catch all context .split_whitespace()
} else { .map(Keystroke::parse)
Some( .collect::<Result<_>>()?;
keystrokes
.split_whitespace()
.map(Keystroke::parse)
.collect::<Result<_>>()?,
)
};
Ok(Self { Ok(Self {
keystrokes, keystrokes,
@ -53,20 +47,10 @@ impl Binding {
pending_keystrokes: &Vec<Keystroke>, pending_keystrokes: &Vec<Keystroke>,
contexts: &[KeymapContext], contexts: &[KeymapContext],
) -> BindingMatchResult { ) -> BindingMatchResult {
if self if self.keystrokes.as_ref().starts_with(&pending_keystrokes) && self.match_context(contexts)
.keystrokes
.as_ref()
.map(|keystrokes| keystrokes.starts_with(&pending_keystrokes))
.unwrap_or(true)
&& self.match_context(contexts)
{ {
// If the binding is completed, push it onto the matches list // If the binding is completed, push it onto the matches list
if self if self.keystrokes.as_ref().len() == pending_keystrokes.len() {
.keystrokes
.as_ref()
.map(|keystrokes| keystrokes.len() == pending_keystrokes.len())
.unwrap_or(true)
{
BindingMatchResult::Complete(self.action.boxed_clone()) BindingMatchResult::Complete(self.action.boxed_clone())
} else { } else {
BindingMatchResult::Partial BindingMatchResult::Partial
@ -82,14 +66,14 @@ impl Binding {
contexts: &[KeymapContext], contexts: &[KeymapContext],
) -> Option<SmallVec<[Keystroke; 2]>> { ) -> Option<SmallVec<[Keystroke; 2]>> {
if self.action.eq(action) && self.match_context(contexts) { if self.action.eq(action) && self.match_context(contexts) {
self.keystrokes.clone() Some(self.keystrokes.clone())
} else { } else {
None None
} }
} }
pub fn keystrokes(&self) -> Option<&[Keystroke]> { pub fn keystrokes(&self) -> &[Keystroke] {
self.keystrokes.as_deref() self.keystrokes.as_slice()
} }
pub fn action(&self) -> &dyn Action { pub fn action(&self) -> &dyn Action {

View file

@ -184,7 +184,7 @@ impl MacForegroundPlatform {
.map(|binding| binding.keystrokes()); .map(|binding| binding.keystrokes());
let item; let item;
if let Some(keystrokes) = keystrokes.flatten() { if let Some(keystrokes) = keystrokes {
if keystrokes.len() == 1 { if keystrokes.len() == 1 {
let keystroke = &keystrokes[0]; let keystroke = &keystrokes[0];
let mut mask = NSEventModifierFlags::empty(); let mut mask = NSEventModifierFlags::empty();