Add Unbind Setting for Keybinds

Adds the `unbind: true` and `unbind: false` flag under
keybinds.

Unbinds all default Keybinds.

Some enums are here already for more flexible unbinds.
This commit is contained in:
a-kenji 2021-04-04 16:56:19 +02:00
parent a4430bf158
commit 1f14683c8f
2 changed files with 53 additions and 5 deletions

View file

@ -1,5 +1,6 @@
--- ---
keybinds: keybinds:
unbind: true
normal: normal:
- action: [GoToTab: 1,] - action: [GoToTab: 1,]
key: [F: 1,] key: [F: 1,]

View file

@ -7,14 +7,29 @@ use serde::Deserialize;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use zellij_tile::data::*; use zellij_tile::data::*;
/// Used in the config struct
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Keybinds(HashMap<InputMode, ModeKeybinds>); pub struct Keybinds(HashMap<InputMode, ModeKeybinds>);
#[derive(Clone, Debug, Default, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct ModeKeybinds(HashMap<Key, Vec<Action>>); pub struct ModeKeybinds(HashMap<Key, Vec<Action>>);
/// Intermediate struct used for deserialisation /// Intermediate struct used for deserialisation
/// Used in the config file.
#[derive(Clone, Debug, PartialEq, Deserialize)] #[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct KeybindsFromYaml(HashMap<InputMode, Vec<KeyActionFromYaml>>); pub struct KeybindsFromYaml {
#[serde(flatten)]
keybinds: HashMap<InputMode, Vec<KeyActionUnbind>>,
unbind: Unbind,
}
/// Intermediate enum used for deserialisation
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(untagged)]
enum KeyActionUnbind {
KeyAction(KeyActionFromYaml),
// TODO: use the enum
//Unbind(UnbindFromYaml),
}
/// Intermediate struct used for deserialisation /// Intermediate struct used for deserialisation
#[derive(Clone, Debug, PartialEq, Deserialize)] #[derive(Clone, Debug, PartialEq, Deserialize)]
@ -23,6 +38,22 @@ pub struct KeyActionFromYaml {
key: Vec<Key>, key: Vec<Key>,
} }
/// Intermediate struct used for deserialisation
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
struct UnbindFromYaml {
unbind: Unbind,
}
/// List of keys, for which to disable their respective default actions
/// `All` is a catch all, and will disable the default actions for all keys.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
#[serde(untagged)]
enum Unbind {
All(bool),
// TODO: use the enum
//Keys(Vec<Key>),
}
impl Default for Keybinds { impl Default for Keybinds {
fn default() -> Keybinds { fn default() -> Keybinds {
let mut defaults = Keybinds::new(); let mut defaults = Keybinds::new();
@ -41,9 +72,16 @@ impl Keybinds {
Keybinds(HashMap::<InputMode, ModeKeybinds>::new()) Keybinds(HashMap::<InputMode, ModeKeybinds>::new())
} }
pub fn get_default_keybinds_with_config(keybinds: Option<KeybindsFromYaml>) -> Keybinds { pub fn get_default_keybinds_with_config(from_yaml: Option<KeybindsFromYaml>) -> Keybinds {
let default_keybinds = Keybinds::default(); let default_keybinds = match from_yaml.clone() {
if let Some(keybinds) = keybinds { Some(keybinds) => match keybinds.unbind {
Unbind::All(true) => Keybinds::new(),
Unbind::All(false) => Keybinds::default(),
},
None => Keybinds::default(),
};
if let Some(keybinds) = from_yaml {
default_keybinds.merge_keybinds(Keybinds::from(keybinds)) default_keybinds.merge_keybinds(Keybinds::from(keybinds))
} else { } else {
default_keybinds default_keybinds
@ -336,7 +374,7 @@ impl From<KeybindsFromYaml> for Keybinds {
for mode in InputMode::iter() { for mode in InputMode::iter() {
let mut mode_keybinds = ModeKeybinds::new(); let mut mode_keybinds = ModeKeybinds::new();
for key_action in keybinds_from_yaml.0.get(&mode).iter() { for key_action in keybinds_from_yaml.keybinds.get(&mode).iter() {
for keybind in key_action.iter() { for keybind in key_action.iter() {
mode_keybinds = mode_keybinds.merge(ModeKeybinds::from(keybind.clone())); mode_keybinds = mode_keybinds.merge(ModeKeybinds::from(keybind.clone()));
} }
@ -362,6 +400,15 @@ impl From<KeyActionFromYaml> for ModeKeybinds {
} }
} }
// Currently an enum for future use
impl From<KeyActionUnbind> for ModeKeybinds {
fn from(key_action_unbind: KeyActionUnbind) -> ModeKeybinds {
match key_action_unbind {
KeyActionUnbind::KeyAction(key_action) => ModeKeybinds::from(key_action),
}
}
}
// The unit test location. // The unit test location.
#[cfg(test)] #[cfg(test)]
#[path = "./unit/keybinds_test.rs"] #[path = "./unit/keybinds_test.rs"]