remove unneeded allocations in bridge::keybindings

macos-click-through
Luis Holanda 5 years ago
parent 20a7320474
commit 4df81f9cd9

@ -1,153 +1,157 @@
use skulpin::winit::event::{KeyboardInput, ElementState, ModifiersState, VirtualKeyCode};
fn parse_keycode(keycode: VirtualKeyCode) -> Option<(String, bool)> {
fn parse_keycode(keycode: VirtualKeyCode) -> Option<(&'static str, bool)> {
match keycode {
VirtualKeyCode::Key1 => Some(("1".to_string(), false)),
VirtualKeyCode::Key2 => Some(("2".to_string(), false)),
VirtualKeyCode::Key3 => Some(("3".to_string(), false)),
VirtualKeyCode::Key4 => Some(("4".to_string(), false)),
VirtualKeyCode::Key5 => Some(("5".to_string(), false)),
VirtualKeyCode::Key6 => Some(("6".to_string(), false)),
VirtualKeyCode::Key7 => Some(("7".to_string(), false)),
VirtualKeyCode::Key8 => Some(("8".to_string(), false)),
VirtualKeyCode::Key9 => Some(("9".to_string(), false)),
VirtualKeyCode::Key0 => Some(("0".to_string(), false)),
VirtualKeyCode::A => Some(("a".to_string(), false)),
VirtualKeyCode::B => Some(("b".to_string(), false)),
VirtualKeyCode::C => Some(("c".to_string(), false)),
VirtualKeyCode::D => Some(("d".to_string(), false)),
VirtualKeyCode::E => Some(("e".to_string(), false)),
VirtualKeyCode::F => Some(("f".to_string(), false)),
VirtualKeyCode::G => Some(("g".to_string(), false)),
VirtualKeyCode::H => Some(("h".to_string(), false)),
VirtualKeyCode::I => Some(("i".to_string(), false)),
VirtualKeyCode::J => Some(("j".to_string(), false)),
VirtualKeyCode::K => Some(("k".to_string(), false)),
VirtualKeyCode::L => Some(("l".to_string(), false)),
VirtualKeyCode::M => Some(("m".to_string(), false)),
VirtualKeyCode::N => Some(("n".to_string(), false)),
VirtualKeyCode::O => Some(("o".to_string(), false)),
VirtualKeyCode::P => Some(("p".to_string(), false)),
VirtualKeyCode::Q => Some(("q".to_string(), false)),
VirtualKeyCode::R => Some(("r".to_string(), false)),
VirtualKeyCode::S => Some(("s".to_string(), false)),
VirtualKeyCode::T => Some(("t".to_string(), false)),
VirtualKeyCode::U => Some(("u".to_string(), false)),
VirtualKeyCode::V => Some(("v".to_string(), false)),
VirtualKeyCode::W => Some(("w".to_string(), false)),
VirtualKeyCode::X => Some(("x".to_string(), false)),
VirtualKeyCode::Y => Some(("y".to_string(), false)),
VirtualKeyCode::Z => Some(("z".to_string(), false)),
VirtualKeyCode::Escape => Some(("ESC".to_string(), true)),
VirtualKeyCode::F1 => Some(("F1".to_string(), true)),
VirtualKeyCode::F2 => Some(("F2".to_string(), true)),
VirtualKeyCode::F3 => Some(("F3".to_string(), true)),
VirtualKeyCode::F4 => Some(("F4".to_string(), true)),
VirtualKeyCode::F5 => Some(("F5".to_string(), true)),
VirtualKeyCode::F6 => Some(("F6".to_string(), true)),
VirtualKeyCode::F7 => Some(("F7".to_string(), true)),
VirtualKeyCode::F8 => Some(("F8".to_string(), true)),
VirtualKeyCode::F9 => Some(("F9".to_string(), true)),
VirtualKeyCode::F10 => Some(("F10".to_string(), true)),
VirtualKeyCode::F11 => Some(("F11".to_string(), true)),
VirtualKeyCode::F12 => Some(("F12".to_string(), true)),
VirtualKeyCode::F13 => Some(("F13".to_string(), true)),
VirtualKeyCode::F14 => Some(("F14".to_string(), true)),
VirtualKeyCode::F15 => Some(("F15".to_string(), true)),
VirtualKeyCode::F16 => Some(("F16".to_string(), true)),
VirtualKeyCode::F17 => Some(("F17".to_string(), true)),
VirtualKeyCode::F18 => Some(("F18".to_string(), true)),
VirtualKeyCode::F19 => Some(("F19".to_string(), true)),
VirtualKeyCode::F20 => Some(("F20".to_string(), true)),
VirtualKeyCode::F21 => Some(("F21".to_string(), true)),
VirtualKeyCode::F22 => Some(("F22".to_string(), true)),
VirtualKeyCode::F23 => Some(("F23".to_string(), true)),
VirtualKeyCode::F24 => Some(("F24".to_string(), true)),
VirtualKeyCode::Insert => Some(("Insert".to_string(), true)),
VirtualKeyCode::Home => Some(("Home".to_string(), true)),
VirtualKeyCode::Delete => Some(("Delete".to_string(), true)),
VirtualKeyCode::End => Some(("End".to_string(), true)),
VirtualKeyCode::PageDown => Some(("PageDown".to_string(), true)),
VirtualKeyCode::PageUp => Some(("PageUp".to_string(), true)),
VirtualKeyCode::Left => Some(("Left".to_string(), true)),
VirtualKeyCode::Up => Some(("Up".to_string(), true)),
VirtualKeyCode::Right => Some(("Right".to_string(), true)),
VirtualKeyCode::Down => Some(("Down".to_string(), true)),
VirtualKeyCode::Back => Some(("BS".to_string(), true)),
VirtualKeyCode::Return => Some(("Enter".to_string(), true)),
VirtualKeyCode::Space => Some(("Space".to_string(), true)),
VirtualKeyCode::Caret => Some(("^".to_string(), false)),
VirtualKeyCode::Apostrophe => Some(("'".to_string(), false)),
VirtualKeyCode::Backslash => Some(("Bslash".to_string(), true)),
VirtualKeyCode::Colon => Some((":".to_string(), false)),
VirtualKeyCode::Comma => Some((",".to_string(), false)),
VirtualKeyCode::Equals => Some(("=".to_string(), false)),
VirtualKeyCode::Grave => Some(("`".to_string(), false)),
VirtualKeyCode::LBracket => Some(("[".to_string(), false)),
VirtualKeyCode::Minus => Some(("-".to_string(), false)),
VirtualKeyCode::Period => Some((".".to_string(), false)),
VirtualKeyCode::RBracket => Some(("]".to_string(), false)),
VirtualKeyCode::Semicolon => Some((";".to_string(), false)),
VirtualKeyCode::Slash => Some(("/".to_string(), false)),
VirtualKeyCode::Tab => Some(("Tab".to_string(), true)),
VirtualKeyCode::Key1 => Some(("1", false)),
VirtualKeyCode::Key2 => Some(("2", false)),
VirtualKeyCode::Key3 => Some(("3", false)),
VirtualKeyCode::Key4 => Some(("4", false)),
VirtualKeyCode::Key5 => Some(("5", false)),
VirtualKeyCode::Key6 => Some(("6", false)),
VirtualKeyCode::Key7 => Some(("7", false)),
VirtualKeyCode::Key8 => Some(("8", false)),
VirtualKeyCode::Key9 => Some(("9", false)),
VirtualKeyCode::Key0 => Some(("0", false)),
VirtualKeyCode::A => Some(("a", false)),
VirtualKeyCode::B => Some(("b", false)),
VirtualKeyCode::C => Some(("c", false)),
VirtualKeyCode::D => Some(("d", false)),
VirtualKeyCode::E => Some(("e", false)),
VirtualKeyCode::F => Some(("f", false)),
VirtualKeyCode::G => Some(("g", false)),
VirtualKeyCode::H => Some(("h", false)),
VirtualKeyCode::I => Some(("i", false)),
VirtualKeyCode::J => Some(("j", false)),
VirtualKeyCode::K => Some(("k", false)),
VirtualKeyCode::L => Some(("l", false)),
VirtualKeyCode::M => Some(("m", false)),
VirtualKeyCode::N => Some(("n", false)),
VirtualKeyCode::O => Some(("o", false)),
VirtualKeyCode::P => Some(("p", false)),
VirtualKeyCode::Q => Some(("q", false)),
VirtualKeyCode::R => Some(("r", false)),
VirtualKeyCode::S => Some(("s", false)),
VirtualKeyCode::T => Some(("t", false)),
VirtualKeyCode::U => Some(("u", false)),
VirtualKeyCode::V => Some(("v", false)),
VirtualKeyCode::W => Some(("w", false)),
VirtualKeyCode::X => Some(("x", false)),
VirtualKeyCode::Y => Some(("y", false)),
VirtualKeyCode::Z => Some(("z", false)),
VirtualKeyCode::Escape => Some(("ESC", true)),
VirtualKeyCode::F1 => Some(("F1", true)),
VirtualKeyCode::F2 => Some(("F2", true)),
VirtualKeyCode::F3 => Some(("F3", true)),
VirtualKeyCode::F4 => Some(("F4", true)),
VirtualKeyCode::F5 => Some(("F5", true)),
VirtualKeyCode::F6 => Some(("F6", true)),
VirtualKeyCode::F7 => Some(("F7", true)),
VirtualKeyCode::F8 => Some(("F8", true)),
VirtualKeyCode::F9 => Some(("F9", true)),
VirtualKeyCode::F10 => Some(("F10", true)),
VirtualKeyCode::F11 => Some(("F11", true)),
VirtualKeyCode::F12 => Some(("F12", true)),
VirtualKeyCode::F13 => Some(("F13", true)),
VirtualKeyCode::F14 => Some(("F14", true)),
VirtualKeyCode::F15 => Some(("F15", true)),
VirtualKeyCode::F16 => Some(("F16", true)),
VirtualKeyCode::F17 => Some(("F17", true)),
VirtualKeyCode::F18 => Some(("F18", true)),
VirtualKeyCode::F19 => Some(("F19", true)),
VirtualKeyCode::F20 => Some(("F20", true)),
VirtualKeyCode::F21 => Some(("F21", true)),
VirtualKeyCode::F22 => Some(("F22", true)),
VirtualKeyCode::F23 => Some(("F23", true)),
VirtualKeyCode::F24 => Some(("F24", true)),
VirtualKeyCode::Insert => Some(("Insert", true)),
VirtualKeyCode::Home => Some(("Home", true)),
VirtualKeyCode::Delete => Some(("Delete", true)),
VirtualKeyCode::End => Some(("End", true)),
VirtualKeyCode::PageDown => Some(("PageDown", true)),
VirtualKeyCode::PageUp => Some(("PageUp", true)),
VirtualKeyCode::Left => Some(("Left", true)),
VirtualKeyCode::Up => Some(("Up", true)),
VirtualKeyCode::Right => Some(("Right", true)),
VirtualKeyCode::Down => Some(("Down", true)),
VirtualKeyCode::Back => Some(("BS", true)),
VirtualKeyCode::Return => Some(("Enter", true)),
VirtualKeyCode::Space => Some(("Space", true)),
VirtualKeyCode::Caret => Some(("^", false)),
VirtualKeyCode::Apostrophe => Some(("'", false)),
VirtualKeyCode::Backslash => Some(("Bslash", true)),
VirtualKeyCode::Colon => Some((":", false)),
VirtualKeyCode::Comma => Some((",", false)),
VirtualKeyCode::Equals => Some(("=", false)),
VirtualKeyCode::Grave => Some(("`", false)),
VirtualKeyCode::LBracket => Some(("[", false)),
VirtualKeyCode::Minus => Some(("-", false)),
VirtualKeyCode::Period => Some((".", false)),
VirtualKeyCode::RBracket => Some(("]", false)),
VirtualKeyCode::Semicolon => Some((";", false)),
VirtualKeyCode::Slash => Some(("/", false)),
VirtualKeyCode::Tab => Some(("Tab", true)),
_ => None
}
}
fn append_modifiers(modifiers: ModifiersState, keycode_text: String, special: bool) -> String {
let mut result = keycode_text;
fn append_modifiers(modifiers: ModifiersState, keycode_text: &str, special: bool) -> String {
let mut result = String::with_capacity(10 + keycode_text.len()); // Add 10 due to modifiers terms.
let mut special = special;
if modifiers.logo() {
special = true;
result.push_str("D-");
}
if modifiers.alt() {
special = true;
result.push_str("M-");
}
if modifiers.ctrl() {
special = true;
result.push_str("C-");
}
if modifiers.shift() {
result = match result.as_ref() {
"1" => "!".to_string(),
"2" => "@".to_string(),
"3" => "#".to_string(),
"4" => "$".to_string(),
"5" => "%".to_string(),
"6" => "^".to_string(),
"7" => "&".to_string(),
"8" => "*".to_string(),
"9" => "(".to_string(),
"0" => ")".to_string(),
"'" => "\"".to_string(),
match keycode_text {
"1" => result.push('!'),
"2" => result.push('@'),
"3" => result.push('#'),
"4" => result.push('$'),
"5" => result.push('%'),
"6" => result.push('^'),
"7" => result.push('&'),
"8" => result.push('*'),
"9" => result.push('('),
"0" => result.push(')'),
"'" => result.push('"'),
"Bslash" => {
special = false;
"|".to_string()
result.push('|');
},
"," => {
special = true;
"lt".to_string()
result.push_str("lt");
},
"=" => "+".to_string(),
"`" => "~".to_string(),
"[" => "{".to_string(),
"-" => "_".to_string(),
"." => ">".to_string(),
"]" => "}".to_string(),
";" => ":".to_string(),
"/" => "?".to_string(),
"=" => result.push('+'),
"`" => result.push('~'),
"[" => result.push('{'),
"-" => result.push('_'),
"." => result.push('>'),
"]" => result.push('}'),
";" => result.push(':'),
"/" => result.push('?'),
other => {
special = true;
format!("S-{}", other)
result.push_str("S-");
result.push_str(other);
}
};
}
if modifiers.ctrl() {
special = true;
result = format!("C-{}", result);
}
if modifiers.alt() {
special = true;
result = format!("M-{}", result);
}
if modifiers.logo() {
special = true;
result = format!("D-{}", result);
}
if special {
result = format!("<{}>", result);
result.insert(0, '<');
result.push('>');
}
result

Loading…
Cancel
Save