From 1f14683c8fd237cea5a96c81daecff9e32337c3e Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 4 Apr 2021 16:56:19 +0200 Subject: [PATCH] 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. --- example/config.yaml | 1 + src/common/input/keybinds.rs | 57 ++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/example/config.yaml b/example/config.yaml index 3bb4fbfada..644d4e4f23 100644 --- a/example/config.yaml +++ b/example/config.yaml @@ -1,5 +1,6 @@ --- keybinds: + unbind: true normal: - action: [GoToTab: 1,] key: [F: 1,] diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 6a14a18232..653213a779 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); #[derive(Clone, Debug, Default, PartialEq)] pub struct ModeKeybinds(HashMap>); /// Intermediate struct used for deserialisation +/// Used in the config file. #[derive(Clone, Debug, PartialEq, Deserialize)] -pub struct KeybindsFromYaml(HashMap>); +pub struct KeybindsFromYaml { + #[serde(flatten)] + keybinds: HashMap>, + 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, } +/// 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), +} + impl Default for Keybinds { fn default() -> Keybinds { let mut defaults = Keybinds::new(); @@ -41,9 +72,16 @@ impl Keybinds { Keybinds(HashMap::::new()) } - pub fn get_default_keybinds_with_config(keybinds: Option) -> Keybinds { - let default_keybinds = Keybinds::default(); - if let Some(keybinds) = keybinds { + pub fn get_default_keybinds_with_config(from_yaml: Option) -> 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 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 for ModeKeybinds { } } +// Currently an enum for future use +impl From 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"]