From d5347fc697f1e52cbd09959c750778d1de37b37b Mon Sep 17 00:00:00 2001 From: Nguyen Duc Toan Date: Sun, 17 Sep 2023 16:33:56 +0700 Subject: [PATCH 1/8] 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); } From 95ffdd78619945a2f1abaa94d137b45272db0aad Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 18 Sep 2023 07:52:33 +0800 Subject: [PATCH 2/8] Format --- config/src/keymap/key.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index 91b4a5d07..96f721c74 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -5,10 +5,10 @@ use serde::Deserialize; #[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 { @@ -43,18 +43,16 @@ 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 { Self { - code: value.code, + code: value.code, shift: value.modifiers.contains(KeyModifiers::SHIFT), - ctrl: value.modifiers.contains(KeyModifiers::CONTROL), - alt: value.modifiers.contains(KeyModifiers::ALT), + ctrl: value.modifiers.contains(KeyModifiers::CONTROL), + alt: value.modifiers.contains(KeyModifiers::ALT), } } } From 91ab4ea676f5a89602120c54b4d54bffa2e08897 Mon Sep 17 00:00:00 2001 From: Nguyen Duc Toan Date: Wed, 20 Sep 2023 21:10:33 +0700 Subject: [PATCH 3/8] Revert "fix shift character keymap" This reverts commit d5347fc697f1e52cbd09959c750778d1de37b37b. --- config/src/keymap/key.rs | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index 91b4a5d07..aabbf4857 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -2,29 +2,13 @@ use anyhow::bail; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use serde::Deserialize; -#[derive(Clone, Debug, Deserialize, Eq)] +#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)] #[serde(try_from = "String")] pub struct Key { - pub code: KeyCode, + pub code: KeyCode, pub shift: 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 - } - } - } + pub ctrl: bool, + pub alt: bool, } impl Key { @@ -43,18 +27,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: value.modifiers.contains(KeyModifiers::SHIFT), - ctrl: value.modifiers.contains(KeyModifiers::CONTROL), - alt: value.modifiers.contains(KeyModifiers::ALT), + code: value.code, + shift: shift || value.modifiers.contains(KeyModifiers::SHIFT), + ctrl: value.modifiers.contains(KeyModifiers::CONTROL), + alt: value.modifiers.contains(KeyModifiers::ALT), } } } @@ -71,6 +55,7 @@ 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); } From 2376a561252080472743284c48d36cb83bc912c1 Mon Sep 17 00:00:00 2001 From: Nguyen Duc Toan Date: Wed, 20 Sep 2023 21:37:14 +0700 Subject: [PATCH 4/8] fix keyevent inconsistency between Windows and Linux/Mac --- config/src/keymap/key.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index aabbf4857..9af5047ee 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -32,11 +32,25 @@ impl Default for Key { impl From for Key { fn from(value: KeyEvent) -> Self { - let shift = matches!(value.code, KeyCode::Char(c) if c.is_ascii_uppercase()); + /* + On Linx and Mac: + shift + alphabet => uppercase alphabet + SHIFT + shift + non alphabet => shifted non alphabet + NULL + On Windows: + shift + alphabet => uppercase alphabet + SHIFT + shift + non alphabet => shifted non alphabet + SHIFT + So we detect (non alphabet + SHIFT) and change it to (non alphabet + NULL) for consistent + behavior between OSs. + */ + let shift = match (value.code, value.modifiers) { + (KeyCode::Char(c), _) if c.is_ascii_uppercase() => true, + (KeyCode::Char(_), m) if m.contains(KeyModifiers::SHIFT) => false, + (_, m) => m.contains(KeyModifiers::SHIFT), + }; Self { code: value.code, - shift: shift || value.modifiers.contains(KeyModifiers::SHIFT), + shift, ctrl: value.modifiers.contains(KeyModifiers::CONTROL), alt: value.modifiers.contains(KeyModifiers::ALT), } From c7353343e2178009ec9e40191efdb523a539e601 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 20 Sep 2023 23:47:46 +0800 Subject: [PATCH 5/8] Update comment --- config/src/keymap/key.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index 9af5047ee..25f45b9a2 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -32,16 +32,17 @@ impl Default for Key { impl From for Key { fn from(value: KeyEvent) -> Self { - /* - On Linx and Mac: - shift + alphabet => uppercase alphabet + SHIFT - shift + non alphabet => shifted non alphabet + NULL - On Windows: - shift + alphabet => uppercase alphabet + SHIFT - shift + non alphabet => shifted non alphabet + SHIFT - So we detect (non alphabet + SHIFT) and change it to (non alphabet + NULL) for consistent - behavior between OSs. - */ + // For alphabet: + // Unix : => Char("A") + Shift + // Windows : => Char("A") + Shift + // + // For non-alphabet: + // Unix : => Char("~") + NULL + // Windows : => Char("`") + Shift + // + // So we detect `Char("`") + Shift`, and change it to `Char("~") + NULL` + // for consistent behavior between OSs. + let shift = match (value.code, value.modifiers) { (KeyCode::Char(c), _) if c.is_ascii_uppercase() => true, (KeyCode::Char(_), m) if m.contains(KeyModifiers::SHIFT) => false, @@ -49,10 +50,10 @@ impl From for Key { }; Self { - code: value.code, + code: value.code, shift, - ctrl: value.modifiers.contains(KeyModifiers::CONTROL), - alt: value.modifiers.contains(KeyModifiers::ALT), + ctrl: value.modifiers.contains(KeyModifiers::CONTROL), + alt: value.modifiers.contains(KeyModifiers::ALT), } } } From 437ecc682f7f7537d49495fc628aa9b402e8d9dd Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 20 Sep 2023 23:50:23 +0800 Subject: [PATCH 6/8] fix --- config/src/keymap/key.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index 25f45b9a2..5bbe48bfb 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -33,12 +33,12 @@ impl Default for Key { impl From for Key { fn from(value: KeyEvent) -> Self { // For alphabet: - // Unix : => Char("A") + Shift - // Windows : => Char("A") + Shift + // Unix : => Char("A") + SHIFT + // Windows : => Char("A") + SHIFT // // For non-alphabet: // Unix : => Char("~") + NULL - // Windows : => Char("`") + Shift + // Windows : => Char("~") + SHIFT // // So we detect `Char("`") + Shift`, and change it to `Char("~") + NULL` // for consistent behavior between OSs. From b9e9acc6d878bd76eb8fb38759b43b06a8b22f81 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 20 Sep 2023 23:51:46 +0800 Subject: [PATCH 7/8] fix again --- config/src/keymap/key.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index 5bbe48bfb..51397bdde 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -40,7 +40,7 @@ impl From for Key { // Unix : => Char("~") + NULL // Windows : => Char("~") + SHIFT // - // So we detect `Char("`") + Shift`, and change it to `Char("~") + NULL` + // So we detect `Char("~") + SHIFT`, and change it to `Char("~") + NULL` // for consistent behavior between OSs. let shift = match (value.code, value.modifiers) { From d1d84a2b9629c6168f3d5efe8ecb0df1e8e7d62d Mon Sep 17 00:00:00 2001 From: sxyazi Date: Thu, 21 Sep 2023 00:09:39 +0800 Subject: [PATCH 8/8] merge same lines --- config/src/keymap/key.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index 51397bdde..aab73b26b 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -44,8 +44,7 @@ impl From for Key { // for consistent behavior between OSs. let shift = match (value.code, value.modifiers) { - (KeyCode::Char(c), _) if c.is_ascii_uppercase() => true, - (KeyCode::Char(_), m) if m.contains(KeyModifiers::SHIFT) => false, + (KeyCode::Char(c), _) => c.is_ascii_uppercase(), (_, m) => m.contains(KeyModifiers::SHIFT), };