Skip to content

Commit

Permalink
Add Unbind Setting for Keybinds
Browse files Browse the repository at this point in the history
Adds the `unbind: true` and `unbind: false` flag under
keybinds.

Unbinds all default Keybinds.

Some enums are here already for more flexible unbinds.
  • Loading branch information
a-kenji committed Apr 16, 2021
1 parent a4430bf commit 1f14683
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions example/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
keybinds:
unbind: true
normal:
- action: [GoToTab: 1,]
key: [F: 1,]
Expand Down
57 changes: 52 additions & 5 deletions src/common/input/keybinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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()));
}
Expand All @@ -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"]
Expand Down

0 comments on commit 1f14683

Please sign in to comment.