Skip to content

Commit

Permalink
OPTIMIZE: snippet_code keyboard cpu high
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyuedefeng committed Aug 16, 2022
1 parent b70346f commit 35ad2ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dev-toolbox",
"private": true,
"main": "index.js",
"version": "0.0.5",
"version": "0.0.6",
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
Expand Down
80 changes: 40 additions & 40 deletions src-tauri/src/code_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,51 @@ enum RegexWord {

pub fn watch<E>(executor: E) where E: Fn(String) -> () + Send + Sync + 'static {
let device_state = DeviceState::new();
let get_record_word = |key: String| {
let key = regex::Regex::new(r"^Key").unwrap().replace(&key, "").to_string();
if regex::Regex::new(r"^[A-Z]{1}$").unwrap().is_match(&key) {
return RegexWord::Text(key.to_lowercase())
} else if regex::Regex::new(r"^Space$").unwrap().is_match(&key) {
return RegexWord::Text(" ".to_string())
} else if regex::Regex::new(r"^[0-9\-=]{1}$").unwrap().is_match(&key) {
return RegexWord::ShiftText(key.to_lowercase())
} else if regex::Regex::new(r"^Minus$").unwrap().is_match(&key) {
return RegexWord::ShiftText("-".to_string())
} else if regex::Regex::new(r"^Equal$").unwrap().is_match(&key) {
return RegexWord::ShiftText("=".to_string())
} else if regex::Regex::new(r"^LeftBracket$").unwrap().is_match(&key) {
return RegexWord::ShiftText("[".to_string())
} else if regex::Regex::new(r"^RightBracket$").unwrap().is_match(&key) {
return RegexWord::ShiftText("]".to_string())
} else if regex::Regex::new(r"^BackSlash$").unwrap().is_match(&key) {
return RegexWord::ShiftText("\\".to_string())
} else if regex::Regex::new(r"^Slash$").unwrap().is_match(&key) {
return RegexWord::ShiftText("/".to_string())
} else if regex::Regex::new(r"^Semicolon$").unwrap().is_match(&key) {
return RegexWord::ShiftText(";".to_string())
} else if regex::Regex::new(r"^Apostrophe$").unwrap().is_match(&key) {
return RegexWord::ShiftText("'".to_string())
} else if regex::Regex::new(r"^Comma$").unwrap().is_match(&key) {
return RegexWord::ShiftText(",".to_string())
} else if regex::Regex::new(r"^Dot$").unwrap().is_match(&key) {
return RegexWord::ShiftText(".".to_string())
} else if regex::Regex::new(r"^Grave$").unwrap().is_match(&key) {
return RegexWord::ShiftText("·".to_string())
} else if regex::Regex::new(r"^Tab$").unwrap().is_match(&key) {
return RegexWord::Tab
} else if regex::Regex::new(r"^Backspace$").unwrap().is_match(&key) {
return RegexWord::Delete
}
RegexWord::Ignore
};
let _guard = device_state.on_key_down(move |key: &Keycode| {
let key = key.to_string();
// println!("in-- key: {}", key);
if regex::Regex::new(r"Shift$").unwrap().is_match(&key) {
let mut global_keyboard_events_handler = KEYBOARD_EVENTS_HANDLER.lock().unwrap();
global_keyboard_events_handler.is_shift = true;
} else {
let get_record_word = || {
let key = regex::Regex::new(r"^Key").unwrap().replace(&key, "").to_string();
if regex::Regex::new(r"^[A-Z]{1}$").unwrap().is_match(&key) {
return RegexWord::Text(key.to_lowercase())
} else if regex::Regex::new(r"^Space$").unwrap().is_match(&key) {
return RegexWord::Text(" ".to_string())
} else if regex::Regex::new(r"^[0-9\-=]{1}$").unwrap().is_match(&key) {
return RegexWord::ShiftText(key.to_lowercase())
} else if regex::Regex::new(r"^Minus$").unwrap().is_match(&key) {
return RegexWord::ShiftText("-".to_string())
} else if regex::Regex::new(r"^Equal$").unwrap().is_match(&key) {
return RegexWord::ShiftText("=".to_string())
} else if regex::Regex::new(r"^LeftBracket$").unwrap().is_match(&key) {
return RegexWord::ShiftText("[".to_string())
} else if regex::Regex::new(r"^RightBracket$").unwrap().is_match(&key) {
return RegexWord::ShiftText("]".to_string())
} else if regex::Regex::new(r"^BackSlash$").unwrap().is_match(&key) {
return RegexWord::ShiftText("\\".to_string())
} else if regex::Regex::new(r"^Slash$").unwrap().is_match(&key) {
return RegexWord::ShiftText("/".to_string())
} else if regex::Regex::new(r"^Semicolon$").unwrap().is_match(&key) {
return RegexWord::ShiftText(";".to_string())
} else if regex::Regex::new(r"^Apostrophe$").unwrap().is_match(&key) {
return RegexWord::ShiftText("'".to_string())
} else if regex::Regex::new(r"^Comma$").unwrap().is_match(&key) {
return RegexWord::ShiftText(",".to_string())
} else if regex::Regex::new(r"^Dot$").unwrap().is_match(&key) {
return RegexWord::ShiftText(".".to_string())
} else if regex::Regex::new(r"^Grave$").unwrap().is_match(&key) {
return RegexWord::ShiftText("·".to_string())
} else if regex::Regex::new(r"^Tab$").unwrap().is_match(&key) {
return RegexWord::Tab
} else if regex::Regex::new(r"^Backspace$").unwrap().is_match(&key) {
return RegexWord::Delete
}
RegexWord::Ignore
};

let record_word = get_record_word();
let record_word = get_record_word(key);

match &record_word {
RegexWord::ShiftText(key) => {
Expand Down Expand Up @@ -158,7 +157,8 @@ pub fn watch<E>(executor: E) where E: Fn(String) -> () + Send + Sync + 'static {
global_keyboard_events_handler.is_shift = false;
}
});
loop {}
// loop {}
std::thread::park();
}

pub fn handle_user_input(input_text: String, _replace_content: String) -> Result<(), &'static str> {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "dev-toolbox",
"version": "0.0.5"
"version": "0.0.6"
},
"tauri": {
"allowlist": {
Expand Down

0 comments on commit 35ad2ff

Please sign in to comment.