Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
This prevents a HashMap from unnecessarily being rebuilt a bunch of
times.
  • Loading branch information
nadimkobeissi committed Apr 3, 2024
1 parent c987246 commit 32c22cc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 72 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ regex = "1.10.2"
json = "0.12.4"
open = "5.0.0"
inputbot = "0.6.0"
lazy_static = "1.4.0"

[build-dependencies]
embed-resource = "2.4.0"
144 changes: 72 additions & 72 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,86 @@ use inputbot::KeybdKey::{self, *};
use json::JsonValue;
use regex::Regex;
use std::collections::HashMap;
use lazy_static::lazy_static;

use crate::config;

lazy_static! {
static ref KEY_MAP: HashMap<&'static str, KeybdKey> = [
("CTRL", LControlKey),
("LCTRL", LControlKey),
("RCTRL", RControlKey),
("ALT", LAltKey),
("LALT", LAltKey),
("RALT", RAltKey),
("WIN", LSuper),
("LWIN", LSuper),
("RWIN", RSuper),
("SHIFT", LShiftKey),
("LSHIFT", LShiftKey),
("RSHIFT", RShiftKey),
("F1", F1Key),
("F2", F2Key),
("F3", F3Key),
("F4", F4Key),
("F5", F5Key),
("F6", F6Key),
("F7", F7Key),
("F8", F8Key),
("F9", F9Key),
("F10", F10Key),
("F11", F11Key),
("F12", F12Key),
("A", AKey),
("B", BKey),
("C", CKey),
("D", DKey),
("E", EKey),
("F", FKey),
("G", GKey),
("H", HKey),
("I", IKey),
("J", JKey),
("K", KKey),
("L", LKey),
("M", MKey),
("N", NKey),
("O", OKey),
("P", PKey),
("Q", QKey),
("R", RKey),
("S", SKey),
("T", TKey),
("U", UKey),
("V", VKey),
("W", WKey),
("X", XKey),
("Y", YKey),
("Z", ZKey),
("1", Numrow1Key),
("2", Numrow2Key),
("3", Numrow3Key),
("4", Numrow4Key),
("5", Numrow5Key),
("6", Numrow6Key),
("7", Numrow7Key),
("8", Numrow8Key),
("9", Numrow9Key),
("0", Numrow0Key),
]
.iter()
.cloned()
.collect();
}

pub async fn init() {
bind_shortcuts();
inputbot::handle_input_events();
}

pub fn bind_shortcuts() {
let my_config = config::read();
for (_, value) in key_map().iter() {
for (_, value) in KEY_MAP.iter() {
value.unbind();
}
for i in 0..8 {
Expand All @@ -27,7 +96,7 @@ pub fn bind_shortcuts() {
.take(shortcut.len() - 1)
.all(|key| key.is_pressed())
{
for (_, value) in key_map().iter() {
for (_, value) in KEY_MAP.iter() {
if value.is_pressed() && !shortcut.contains(value) {
return inputbot::BlockInput::DontBlock;
}
Expand Down Expand Up @@ -86,79 +155,10 @@ pub fn check_keyboard_shortcut(input: String) -> bool {
return re.unwrap().is_match(&input);
}

fn key_map() -> HashMap<&'static str, KeybdKey> {
[
("CTRL", LControlKey),
("LCTRL", LControlKey),
("RCTRL", RControlKey),
("ALT", LAltKey),
("LALT", LAltKey),
("RALT", RAltKey),
("WIN", LSuper),
("LWIN", LSuper),
("RWIN", RSuper),
("SHIFT", LShiftKey),
("LSHIFT", LShiftKey),
("RSHIFT", RShiftKey),
("F1", F1Key),
("F2", F2Key),
("F3", F3Key),
("F4", F4Key),
("F5", F5Key),
("F6", F6Key),
("F7", F7Key),
("F8", F8Key),
("F9", F9Key),
("F10", F10Key),
("F11", F11Key),
("F12", F12Key),
("A", AKey),
("B", BKey),
("C", CKey),
("D", DKey),
("E", EKey),
("F", FKey),
("G", GKey),
("H", HKey),
("I", IKey),
("J", JKey),
("K", KKey),
("L", LKey),
("M", MKey),
("N", NKey),
("O", OKey),
("P", PKey),
("Q", QKey),
("R", RKey),
("S", SKey),
("T", TKey),
("U", UKey),
("V", VKey),
("W", WKey),
("X", XKey),
("Y", YKey),
("Z", ZKey),
("1", Numrow1Key),
("2", Numrow2Key),
("3", Numrow3Key),
("4", Numrow4Key),
("5", Numrow5Key),
("6", Numrow6Key),
("7", Numrow7Key),
("8", Numrow8Key),
("9", Numrow9Key),
("0", Numrow0Key),
]
.iter()
.cloned()
.collect()
}

fn build_keyboard_shortcut(input: &str) -> Vec<KeybdKey> {
let key_map = key_map();
input
.split('+')
.filter_map(|part| key_map.get(&part))
.filter_map(|part| KEY_MAP.get(&part))
.cloned()
.collect()
}

0 comments on commit 32c22cc

Please sign in to comment.