From fabbbba037265de1abcb4e8893df77b4dcb7f1e9 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 30 Mar 2025 17:06:23 +0200 Subject: [PATCH] Extract function for creating key event with modifiers --- src/input_common.rs | 60 ++++++++++++++++++++++----------------------- src/key.rs | 37 +++++++++------------------- 2 files changed, 41 insertions(+), 56 deletions(-) diff --git a/src/input_common.rs b/src/input_common.rs index 08791211a..d23249235 100644 --- a/src/input_common.rs +++ b/src/input_common.rs @@ -1275,36 +1275,36 @@ fn parse_ss3(&mut self, buffer: &mut Vec) -> Option { let modifiers = parse_mask(raw_mask.saturating_sub(1)); #[rustfmt::skip] let key = match code { - b' ' => Key{modifiers, codepoint: key::Space}, - b'A' => Key{modifiers, codepoint: key::Up}, - b'B' => Key{modifiers, codepoint: key::Down}, - b'C' => Key{modifiers, codepoint: key::Right}, - b'D' => Key{modifiers, codepoint: key::Left}, - b'F' => Key{modifiers, codepoint: key::End}, - b'H' => Key{modifiers, codepoint: key::Home}, - b'I' => Key{modifiers, codepoint: key::Tab}, - b'M' => Key{modifiers, codepoint: key::Enter}, - b'P' => Key{modifiers, codepoint: function_key(1)}, - b'Q' => Key{modifiers, codepoint: function_key(2)}, - b'R' => Key{modifiers, codepoint: function_key(3)}, - b'S' => Key{modifiers, codepoint: function_key(4)}, - b'X' => Key{modifiers, codepoint: '='}, - b'j' => Key{modifiers, codepoint: '*'}, - b'k' => Key{modifiers, codepoint: '+'}, - b'l' => Key{modifiers, codepoint: ','}, - b'm' => Key{modifiers, codepoint: '-'}, - b'n' => Key{modifiers, codepoint: '.'}, - b'o' => Key{modifiers, codepoint: '/'}, - b'p' => Key{modifiers, codepoint: '0'}, - b'q' => Key{modifiers, codepoint: '1'}, - b'r' => Key{modifiers, codepoint: '2'}, - b's' => Key{modifiers, codepoint: '3'}, - b't' => Key{modifiers, codepoint: '4'}, - b'u' => Key{modifiers, codepoint: '5'}, - b'v' => Key{modifiers, codepoint: '6'}, - b'w' => Key{modifiers, codepoint: '7'}, - b'x' => Key{modifiers, codepoint: '8'}, - b'y' => Key{modifiers, codepoint: '9'}, + b' ' => Key::new(modifiers, key::Space), + b'A' => Key::new(modifiers, key::Up), + b'B' => Key::new(modifiers, key::Down), + b'C' => Key::new(modifiers, key::Right), + b'D' => Key::new(modifiers, key::Left), + b'F' => Key::new(modifiers, key::End), + b'H' => Key::new(modifiers, key::Home), + b'I' => Key::new(modifiers, key::Tab), + b'M' => Key::new(modifiers, key::Enter), + b'P' => Key::new(modifiers, function_key(1)), + b'Q' => Key::new(modifiers, function_key(2)), + b'R' => Key::new(modifiers, function_key(3)), + b'S' => Key::new(modifiers, function_key(4)), + b'X' => Key::new(modifiers, '='), + b'j' => Key::new(modifiers, '*'), + b'k' => Key::new(modifiers, '+'), + b'l' => Key::new(modifiers, ','), + b'm' => Key::new(modifiers, '-'), + b'n' => Key::new(modifiers, '.'), + b'o' => Key::new(modifiers, '/'), + b'p' => Key::new(modifiers, '0'), + b'q' => Key::new(modifiers, '1'), + b'r' => Key::new(modifiers, '2'), + b's' => Key::new(modifiers, '3'), + b't' => Key::new(modifiers, '4'), + b'u' => Key::new(modifiers, '5'), + b'v' => Key::new(modifiers, '6'), + b'w' => Key::new(modifiers, '7'), + b'x' => Key::new(modifiers, '8'), + b'y' => Key::new(modifiers, '9'), _ => return None, }; Some(key) diff --git a/src/key.rs b/src/key.rs index 078c6899b..ad599f94c 100644 --- a/src/key.rs +++ b/src/key.rs @@ -100,39 +100,33 @@ pub struct Key { } impl Key { - pub(crate) fn from_raw(codepoint: char) -> Self { + pub(crate) const fn new(modifiers: Modifiers, codepoint: char) -> Self { Self { - modifiers: Modifiers::default(), + modifiers, codepoint, } } + pub(crate) fn from_raw(codepoint: char) -> Self { + Self::new(Modifiers::default(), codepoint) + } } pub(crate) const fn ctrl(codepoint: char) -> Key { let mut modifiers = Modifiers::new(); modifiers.ctrl = true; - Key { - modifiers, - codepoint, - } + Key::new(modifiers, codepoint) } pub(crate) const fn alt(codepoint: char) -> Key { let mut modifiers = Modifiers::new(); modifiers.alt = true; - Key { - modifiers, - codepoint, - } + Key::new(modifiers, codepoint) } pub(crate) const fn shift(codepoint: char) -> Key { let mut modifiers = Modifiers::new(); modifiers.shift = true; - Key { - modifiers, - codepoint, - } + Key::new(modifiers, codepoint) } impl Key { @@ -149,10 +143,7 @@ pub fn from_single_byte(c: u8) -> Self { pub fn canonicalize_control_char(c: u8) -> Option { let codepoint = canonicalize_keyed_control_char(char::from(c)); if u32::from(codepoint) > 255 { - return Some(Key { - modifiers: Modifiers::default(), - codepoint, - }); + return Some(Key::from_raw(codepoint)); } if c < 32 { @@ -303,10 +294,7 @@ pub(crate) fn parse_keys(value: &wstr) -> Result, WString> { .find_map(|(codepoint, name)| (name == key_name).then_some(*codepoint)) .or_else(|| (key_name.len() == 1).then(|| key_name.as_char_slice()[0])); let key = if let Some(codepoint) = codepoint { - canonicalize_key(Key { - modifiers, - codepoint, - })? + canonicalize_key(Key::new(modifiers, codepoint))? } else if codepoint.is_none() && key_name.starts_with('f') && key_name.len() <= 3 { let num = key_name.strip_prefix('f').unwrap(); let codepoint = match fish_wcstoul(num) { @@ -321,10 +309,7 @@ pub(crate) fn parse_keys(value: &wstr) -> Result, WString> { )); } }; - Key { - modifiers, - codepoint, - } + Key::new(modifiers, codepoint) } else { return Err(wgettext_fmt!( "cannot parse key '%s'",