Add on_function key handler

This adds `on_function` handler to handle F1-F12 keys.

This also fixes issues with previously added handlers and adds a
checklist for future additions.
This commit is contained in:
Arijit Basu 2021-11-05 17:33:33 +05:30 committed by Arijit Basu
parent 6a3b26cc18
commit 48ab6eac21
5 changed files with 106 additions and 15 deletions

View File

@ -16,6 +16,7 @@ Key bindings contains the following information:
- [on_special_character][13]
- [on_character][33]
- [on_navigation][34]
- [on_function][35]
- [default][14]
### on_key
@ -67,6 +68,13 @@ Type: nullable [Action][16]
An action to perform if the keyboard input is a navigation key and is not
mapped via the [on_key][10] field.
### on_function
Type: nullable [Action][16]
An action to perform if the keyboard input is a function key and is not mapped
via the [on_key][10] field.
### default
Type: nullable [Action][16]
@ -221,3 +229,4 @@ Visit [Awesome Plugins][27] for more [integration][28] options.
[32]: #on_alphanumeric
[33]: #on_character
[34]: #on_navigation
[34]: #on_function

View File

@ -32,11 +32,6 @@ xplr.config.modes.custom.test = {
{ LogInfo = "on_key called" },
},
},
f1 = {
messages = {
{ LogInfo = "on_key called" },
},
},
tab = {
messages = {
{ LogInfo = "on_key called" },
@ -52,6 +47,11 @@ xplr.config.modes.custom.test = {
"Terminate",
},
},
f1 = {
messages = {
{ LogInfo = "on_key called" },
},
},
},
on_alphabet = {
messages = {
@ -83,6 +83,11 @@ xplr.config.modes.custom.test = {
{ LogInfo = "on_navigation called" },
},
},
on_function = {
messages = {
{ LogInfo = "on_function called" },
},
},
default = {
messages = {
{ LogInfo = "default called" },

View File

@ -1642,6 +1642,12 @@ impl App {
kb.on_alphabet.as_ref().map(|a| a.messages.clone())
} else if key.is_number() {
kb.on_number.as_ref().map(|a| a.messages.clone())
} else if key.is_special_character() {
kb.on_special_character.as_ref().map(|a| a.messages.clone())
} else if key.is_navigation() {
kb.on_navigation.as_ref().map(|a| a.messages.clone())
} else if key.is_function() {
kb.on_function.as_ref().map(|a| a.messages.clone())
} else {
None
}
@ -1649,8 +1655,6 @@ impl App {
.or_else(|| {
if key.is_alphanumeric() {
kb.on_alphanumeric.as_ref().map(|a| a.messages.clone())
} else if key.is_special_character() {
kb.on_special_character.as_ref().map(|a| a.messages.clone())
} else {
None
}
@ -1658,8 +1662,6 @@ impl App {
.or_else(|| {
if key.is_character() {
kb.on_character.as_ref().map(|a| a.messages.clone())
} else if key.is_navigation() {
kb.on_navigation.as_ref().map(|a| a.messages.clone())
} else {
None
}

View File

@ -291,8 +291,17 @@ pub struct KeyBindings {
#[serde(default)]
pub on_navigation: Option<Action>,
#[serde(default)]
pub on_function: Option<Action>,
#[serde(default)]
pub default: Option<Action>,
// Checklist for adding field:
// - [ ] Update App::handle_key
// - [ ] Update KeyBindings::sanitized
// - [ ] Update Mode::help_menu
// - [ ] Update configure-key-bindings.md
// - [ ] Update debug-key-bindings.md
}
impl KeyBindings {
@ -308,12 +317,19 @@ impl KeyBindings {
self.on_alphabet.and_then(|a| a.sanitized(read_only));
self.on_number =
self.on_number.and_then(|a| a.sanitized(read_only));
self.on_alphanumeric =
self.on_alphanumeric.and_then(|a| a.sanitized(read_only));
self.on_special_character = self
.on_special_character
.and_then(|a| a.sanitized(read_only));
self.on_character =
self.on_character.and_then(|a| a.sanitized(read_only));
self.on_navigation =
self.on_navigation.and_then(|a| a.sanitized(read_only));
self.on_function =
self.on_function.and_then(|a| a.sanitized(read_only));
self.default = self.default.and_then(|a| a.sanitized(read_only));
};
self
}
}
@ -404,11 +420,55 @@ impl Mode {
})
}),
)
.chain(
self.key_bindings
.on_alphanumeric
.iter()
.map(|a| ("[0-Z]", a.help.clone()))
.filter_map(|(k, mh)| {
mh.map(|h| {
HelpMenuLine::KeyMap(k.into(), vec![], h)
})
}),
)
.chain(
self.key_bindings
.on_special_character
.iter()
.map(|a| ("[spcl chars]", a.help.clone()))
.map(|a| ("[^0-Z]", a.help.clone()))
.filter_map(|(k, mh)| {
mh.map(|h| {
HelpMenuLine::KeyMap(k.into(), vec![], h)
})
}),
)
.chain(
self.key_bindings
.on_character
.iter()
.map(|a| ("[*]", a.help.clone()))
.filter_map(|(k, mh)| {
mh.map(|h| {
HelpMenuLine::KeyMap(k.into(), vec![], h)
})
}),
)
.chain(
self.key_bindings
.on_navigation
.iter()
.map(|a| ("[nav]", a.help.clone()))
.filter_map(|(k, mh)| {
mh.map(|h| {
HelpMenuLine::KeyMap(k.into(), vec![], h)
})
}),
)
.chain(
self.key_bindings
.on_function
.iter()
.map(|a| ("[f1-f12]", a.help.clone()))
.filter_map(|(k, mh)| {
mh.map(|h| {
HelpMenuLine::KeyMap(k.into(), vec![], h)

View File

@ -449,9 +449,27 @@ impl Key {
self.is_alphanumeric() || self.is_special_character()
}
pub fn is_function(&self) -> bool {
matches!(
self,
Self::F1
| Self::F2
| Self::F3
| Self::F4
| Self::F5
| Self::F6
| Self::F7
| Self::F8
| Self::F9
| Self::F10
| Self::F11
| Self::F12
)
}
pub fn is_navigation(&self) -> bool {
matches!(
&self,
self,
Self::Backspace
| Self::Left
| Self::Right
@ -464,9 +482,6 @@ impl Key {
| Self::BackTab
| Self::Delete
| Self::Insert
| Self::Enter
| Self::Space
| Self::Tab
| Self::Esc
| Self::CtrlA
| Self::CtrlB