Skip to content

Commit

Permalink
feat: extra keybindings override base
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin271 committed Jan 26, 2024
1 parent ff2f969 commit eac6b3b
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/keybindings/keybindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ impl From<KeybindingsSection> for Keybindings {
let mut base = value.base;

if let Some(extra) = value.extra {
for (_, extra_combo) in extra.iter() {
base.0 = base
.clone()
.into_iter()
.filter(|(_, combo)| !combo.starts_with(extra_combo))
.collect();
}

base.extend(extra)
}

Expand All @@ -50,7 +58,7 @@ impl From<KeybindingsSection> for Keybindings {
mod tests {
use winit::event::ModifiersState;

use crate::keybindings::{Key, ModifiedKey};
use crate::keybindings::{action::VertDirection, Key, ModifiedKey};

use super::*;

Expand Down Expand Up @@ -83,4 +91,25 @@ mod tests {
expected
);
}

#[test]
fn from_keybinding_section_extra_override_base() {
let j_combo = KeyCombo(vec![ModifiedKey(
Key::Resolved(winit::event::VirtualKeyCode::J),
ModifiersState::empty(),
)]);

let base = Keybindings(vec![(Action::Scroll(VertDirection::Down), j_combo.clone())]);
let extra = Keybindings(vec![(Action::Page(VertDirection::Down), j_combo.clone())]);

let expected = Keybindings(vec![(Action::Page(VertDirection::Down), j_combo.clone())]);

assert_eq!(
Keybindings::from(KeybindingsSection {
base,
extra: Some(extra)
}),
expected
);
}
}

0 comments on commit eac6b3b

Please sign in to comment.