diff options
| author | a-kenji <aks.kenji@protonmail.com> | 2021-04-04 16:56:19 +0200 |
|---|---|---|
| committer | a-kenji <aks.kenji@protonmail.com> | 2021-04-17 00:15:12 +0200 |
| commit | 1f14683c8fd237cea5a96c81daecff9e32337c3e (patch) | |
| tree | 663bc6ba296de5761fcc24d87119a7b9b93376fc /src | |
| parent | Chore(rustfmt) (diff) | |
| download | sl0ppij-1f14683c8fd237cea5a96c81daecff9e32337c3e.tar.gz sl0ppij-1f14683c8fd237cea5a96c81daecff9e32337c3e.tar.xz sl0ppij-1f14683c8fd237cea5a96c81daecff9e32337c3e.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/input/keybinds.rs | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 6a14a182..653213a7 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -7,14 +7,29 @@ use serde::Deserialize; use strum::IntoEnumIterator; use zellij_tile::data::*; +/// Used in the config struct #[derive(Clone, Debug, PartialEq)] pub struct Keybinds(HashMap<InputMode, ModeKeybinds>); #[derive(Clone, Debug, Default, PartialEq)] pub struct ModeKeybinds(HashMap<Key, Vec<Action>>); /// Intermediate struct used for deserialisation +/// Used in the config file. #[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 #[derive(Clone, Debug, PartialEq, Deserialize)] @@ -23,6 +38,22 @@ pub struct KeyActionFromYaml { 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 { fn default() -> Keybinds { let mut defaults = Keybinds::new(); @@ -41,9 +72,16 @@ impl Keybinds { Keybinds(HashMap::<InputMode, ModeKeybinds>::new()) } - pub fn get_default_keybinds_with_config(keybinds: Option<KeybindsFromYaml>) -> Keybinds { - let default_keybinds = Keybinds::default(); - if let Some(keybinds) = keybinds { + pub fn get_default_keybinds_with_config(from_yaml: Option<KeybindsFromYaml>) -> Keybinds { + let default_keybinds = match from_yaml.clone() { + 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)) } else { default_keybinds @@ -336,7 +374,7 @@ impl From<KeybindsFromYaml> for Keybinds { for mode in InputMode::iter() { 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() { 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. #[cfg(test)] #[path = "./unit/keybinds_test.rs"] |
