From d5347fc697f1e52cbd09959c750778d1de37b37b Mon Sep 17 00:00:00 2001 From: Nguyen Duc Toan Date: Sun, 17 Sep 2023 16:33:56 +0700 Subject: [PATCH] fix shift character keymap --- config/src/keymap/key.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index aabbf4857..91b4a5d07 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -2,13 +2,29 @@ use anyhow::bail; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use serde::Deserialize; -#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Deserialize, Eq)] #[serde(try_from = "String")] pub struct Key { - pub code: KeyCode, + pub code: KeyCode, pub shift: bool, - pub ctrl: bool, - pub alt: bool, + pub ctrl: bool, + pub alt: bool, +} + +impl PartialEq for Key { + fn eq(&self, other: &Self) -> bool { + match (self.code, other.code) { + (KeyCode::Char(_), KeyCode::Char(_)) => { + self.code == other.code && self.ctrl == other.ctrl && self.alt == other.alt + } + _ => { + self.code == other.code + && self.shift == other.shift + && self.ctrl == other.ctrl + && self.alt == other.alt + } + } + } } impl Key { @@ -27,18 +43,18 @@ impl Key { } impl Default for Key { - fn default() -> Self { Self { code: KeyCode::Null, shift: false, ctrl: false, alt: false } } + fn default() -> Self { + Self { code: KeyCode::Null, shift: false, ctrl: false, alt: false } + } } impl From for Key { fn from(value: KeyEvent) -> Self { - let shift = matches!(value.code, KeyCode::Char(c) if c.is_ascii_uppercase()); - Self { - code: value.code, - shift: shift || value.modifiers.contains(KeyModifiers::SHIFT), - ctrl: value.modifiers.contains(KeyModifiers::CONTROL), - alt: value.modifiers.contains(KeyModifiers::ALT), + code: value.code, + shift: value.modifiers.contains(KeyModifiers::SHIFT), + ctrl: value.modifiers.contains(KeyModifiers::CONTROL), + alt: value.modifiers.contains(KeyModifiers::ALT), } } } @@ -55,7 +71,6 @@ impl TryFrom for Key { if !s.starts_with('<') || !s.ends_with('>') { let c = s.chars().next().unwrap(); key.code = KeyCode::Char(c); - key.shift = c.is_ascii_uppercase(); return Ok(key); }