Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some clean ups, fixes, config flag #91

Merged
merged 13 commits into from
May 10, 2023
Merged
188 changes: 95 additions & 93 deletions src/keybindings/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,98 +4,100 @@ use winit::event::{ModifiersState, VirtualKeyCode as VirtKey};

const IS_MACOS: bool = cfg!(target_os = "macos");

pub fn defaults() -> Keybindings {
let ctrl_or_command = if IS_MACOS {
ModifiersState::LOGO
} else {
ModifiersState::CTRL
};
impl Default for Keybindings {
fn default() -> Self {
let ctrl_or_command = if IS_MACOS {
ModifiersState::LOGO
} else {
ModifiersState::CTRL
};
coastalwhite marked this conversation as resolved.
Show resolved Hide resolved

vec![
// Copy: Ctrl+C / Command+C
(
Action::Copy,
KeyCombo(vec![ModifiedKey(Key::from(VirtKey::C), ctrl_or_command)]),
),
// Zoom in: Ctrl++ / Command++
(
Action::ZoomIn,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Equals),
ctrl_or_command | ModifiersState::SHIFT,
)]),
),
(
Action::ZoomIn,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Plus),
ctrl_or_command | ModifiersState::SHIFT,
)]),
),
// Zoom out: Ctrl+- / Command+-
(
Action::ZoomOut,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Minus),
ctrl_or_command,
)]),
),
// Zoom reset: Ctrl+= / Command+=
(
Action::ZoomReset,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Equals),
ctrl_or_command,
)]),
),
// Scroll up: Up-arrow
(Action::ScrollUp, KeyCombo::from(VirtKey::Up)),
// Scroll down: Down-arrow
(Action::ScrollDown, KeyCombo::from(VirtKey::Down)),
// Go to top of doc: Home
(Action::ToTop, KeyCombo::from(VirtKey::Home)),
// Go to bottom of doc: End
(Action::ToBottom, KeyCombo::from(VirtKey::End)),
// Quit: Esc
(Action::Quit, KeyCombo::from(VirtKey::Escape)),
// vim-like bindings
// Copy: y
(Action::Copy, KeyCombo::from(VirtKey::Y)),
// Scroll up: k
(Action::ScrollUp, KeyCombo::from(VirtKey::K)),
// Scroll down: j
(Action::ScrollDown, KeyCombo::from(VirtKey::J)),
// Go to top of doc: gg
(
Action::ToTop,
KeyCombo(vec![
ModifiedKey::from(VirtKey::G),
ModifiedKey::from(VirtKey::G),
]),
),
// Go to bottom of doc: G
(
Action::ToBottom,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::G),
ModifiersState::SHIFT,
)]),
),
// Quit: q / ZZ / ZQ
(Action::Quit, KeyCombo::from(VirtKey::Q)),
(
Action::Quit,
KeyCombo(vec![
ModifiedKey(Key::from(VirtKey::Z), ModifiersState::SHIFT),
ModifiedKey(Key::from(VirtKey::Z), ModifiersState::SHIFT),
]),
),
(
Action::Quit,
KeyCombo(vec![
ModifiedKey(Key::from(VirtKey::Z), ModifiersState::SHIFT),
ModifiedKey(Key::from(VirtKey::Q), ModifiersState::SHIFT),
]),
),
]
Self(vec![
// Copy: Ctrl+C / Command+C
(
Action::Copy,
KeyCombo(vec![ModifiedKey(Key::from(VirtKey::C), ctrl_or_command)]),
),
// Zoom in: Ctrl++ / Command++
(
Action::ZoomIn,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Equals),
ctrl_or_command | ModifiersState::SHIFT,
)]),
),
(
Action::ZoomIn,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Plus),
ctrl_or_command | ModifiersState::SHIFT,
)]),
),
// Zoom out: Ctrl+- / Command+-
(
Action::ZoomOut,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Minus),
ctrl_or_command,
)]),
),
// Zoom reset: Ctrl+= / Command+=
(
Action::ZoomReset,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::Equals),
ctrl_or_command,
)]),
),
// Scroll up: Up-arrow
(Action::ScrollUp, KeyCombo::from(VirtKey::Up)),
// Scroll down: Down-arrow
(Action::ScrollDown, KeyCombo::from(VirtKey::Down)),
// Go to top of doc: Home
(Action::ToTop, KeyCombo::from(VirtKey::Home)),
// Go to bottom of doc: End
(Action::ToBottom, KeyCombo::from(VirtKey::End)),
// Quit: Esc
(Action::Quit, KeyCombo::from(VirtKey::Escape)),
// vim-like bindings
// Copy: y
(Action::Copy, KeyCombo::from(VirtKey::Y)),
// Scroll up: k
(Action::ScrollUp, KeyCombo::from(VirtKey::K)),
// Scroll down: j
(Action::ScrollDown, KeyCombo::from(VirtKey::J)),
// Go to top of doc: gg
(
Action::ToTop,
KeyCombo(vec![
ModifiedKey::from(VirtKey::G),
ModifiedKey::from(VirtKey::G),
]),
),
// Go to bottom of doc: G
(
Action::ToBottom,
KeyCombo(vec![ModifiedKey(
Key::from(VirtKey::G),
ModifiersState::SHIFT,
)]),
),
// Quit: q / ZZ / ZQ
(Action::Quit, KeyCombo::from(VirtKey::Q)),
(
Action::Quit,
KeyCombo(vec![
ModifiedKey(Key::from(VirtKey::Z), ModifiersState::SHIFT),
ModifiedKey(Key::from(VirtKey::Z), ModifiersState::SHIFT),
]),
),
(
Action::Quit,
KeyCombo(vec![
ModifiedKey(Key::from(VirtKey::Z), ModifiersState::SHIFT),
ModifiedKey(Key::from(VirtKey::Q), ModifiersState::SHIFT),
]),
),
])
}
}
28 changes: 25 additions & 3 deletions src/keybindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,42 @@ mod serialization;
#[cfg(test)]
mod tests;

pub use defaults::defaults;

use std::{collections::BTreeMap, fmt, slice::Iter, str::FromStr, vec::IntoIter};

use serde::Deserialize;
use winit::event::{ModifiersState, ScanCode, VirtualKeyCode};

pub type Keybindings = Vec<(Action, KeyCombo)>;
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Keybindings(Vec<(Action, KeyCombo)>);

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Key {
Resolved(VirtualKeyCode),
ScanCode(ScanCode),
}

impl Keybindings {
#[inline]
coastalwhite marked this conversation as resolved.
Show resolved Hide resolved
pub fn new(bindings: Vec<(Action, KeyCombo)>) -> Self {
Self(bindings)
}
}

impl Extend<(Action, KeyCombo)> for Keybindings {
fn extend<I: IntoIterator<Item = (Action, KeyCombo)>>(&mut self, iter: I) {
self.0.extend(iter)
}
}

impl IntoIterator for Keybindings {
type Item = (Action, KeyCombo);
type IntoIter = <Vec<(Action, KeyCombo)> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl Key {
pub fn new(resolved: Option<VirtualKeyCode>, scan_code: ScanCode) -> Self {
match resolved {
Expand Down Expand Up @@ -170,6 +191,7 @@ pub struct KeyCombos {

impl KeyCombos {
pub fn new(keybinds: Keybindings) -> anyhow::Result<Self> {
let keybinds = keybinds.0;
let position = ROOT_INDEX;

// A keycombo that starts with another keycombo will never be reachable since the prefixing
Expand Down
3 changes: 2 additions & 1 deletion src/keybindings/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Action, Key, KeyCombo, KeyCombos, ModifiedKey};
use super::{Action, Key, KeyCombo, KeyCombos, ModifiedKey, Keybindings};

use serde::Deserialize;
use winit::event::{ModifiersState, VirtualKeyCode};
Expand All @@ -25,6 +25,7 @@ inner = [
let cap_g = ModifiedKey(Key::from(VirtualKeyCode::G), ModifiersState::SHIFT);
let j = ModifiedKey::from(VirtualKeyCode::J);

let bindings = Keybindings::new(bindings);
let mut key_combos = KeyCombos::new(bindings).unwrap();

// Invalid combo 'gG' where the key that broke us out is a singlekey combo
Expand Down
Loading