Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement configurable hotkey feature (#18)
* Implement configurable hotkey feature * Write on the go * Don't dismiss error when writing buffer * Implement config manager * Better write logic for Hotkey Display * Final batch of implementation
- Loading branch information
Showing
9 changed files
with
457 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::{ | ||
collections::HashMap, | ||
fs::File, | ||
io::{Read, Result, Write}, | ||
path::PathBuf, | ||
sync::Mutex, | ||
}; | ||
|
||
use once_cell::sync::Lazy; | ||
|
||
use crate::platform::get_home_dir; | ||
|
||
pub static CONFIG_MANAGER: Lazy<Mutex<ConfigStore>> = Lazy::new(|| Mutex::new(ConfigStore::new())); | ||
|
||
pub struct ConfigStore { | ||
data: HashMap<String, String>, | ||
} | ||
|
||
impl ConfigStore { | ||
fn get_config_path() -> PathBuf { | ||
get_home_dir() | ||
.expect("Cannot read home directory!") | ||
.join(".goxkey") | ||
} | ||
|
||
fn load_config_data() -> Result<HashMap<String, String>> { | ||
let mut data = HashMap::new(); | ||
let config_path = ConfigStore::get_config_path(); | ||
let mut file = File::open(config_path.as_path()); | ||
let mut buf = String::new(); | ||
if let Ok(mut file) = file { | ||
file.read_to_string(&mut buf); | ||
} else { | ||
buf = format!( | ||
"{} = {}\n{} = {}", | ||
HOTKEY_CONFIG_KEY, "super+ctrl+space", TYPING_METHOD_CONFIG_KEY, "telex" | ||
); | ||
} | ||
buf.lines().for_each(|line| { | ||
if let Some((key, value)) = line.split_once('=') { | ||
data.insert(key.trim().to_owned(), value.trim().to_owned()); | ||
} | ||
}); | ||
Ok(data) | ||
} | ||
|
||
fn write_config_data(data: &HashMap<String, String>) -> Result<()> { | ||
let config_path = ConfigStore::get_config_path(); | ||
let mut file = File::create(config_path.as_path())?; | ||
let mut content = String::new(); | ||
for (key, value) in data { | ||
content.push_str(&format!("{} = {}\n", key, value)); | ||
} | ||
file.write_all(content.as_bytes()) | ||
} | ||
|
||
pub fn new() -> Self { | ||
Self { | ||
data: ConfigStore::load_config_data().expect("Cannot read config file!"), | ||
} | ||
} | ||
|
||
pub fn read(&self, key: &str) -> String { | ||
return self.data.get(key).unwrap_or(&String::new()).to_string(); | ||
} | ||
|
||
pub fn write(&mut self, key: &str, value: &str) { | ||
self.data.insert(key.to_string(), value.to_string()); | ||
ConfigStore::write_config_data(&self.data).expect("Cannot write to config file!"); | ||
} | ||
} | ||
|
||
pub const HOTKEY_CONFIG_KEY: &str = "hotkey"; | ||
pub const TYPING_METHOD_CONFIG_KEY: &str = "method"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use std::{ascii::AsciiExt, fmt::Display}; | ||
|
||
use crate::platform::{ | ||
KeyModifier, KEY_DELETE, KEY_ENTER, KEY_ESCAPE, KEY_SPACE, KEY_TAB, SYMBOL_ALT, SYMBOL_CTRL, | ||
SYMBOL_SHIFT, SYMBOL_SUPER, | ||
}; | ||
|
||
pub struct Hotkey { | ||
modifiers: KeyModifier, | ||
keycode: char, | ||
} | ||
|
||
impl Hotkey { | ||
pub fn from_str(input: &str) -> Self { | ||
let mut modifiers = KeyModifier::new(); | ||
let mut keycode: char = '\0'; | ||
input | ||
.split('+') | ||
.for_each(|token| match token.trim().to_uppercase().as_str() { | ||
"SHIFT" => modifiers.add_shift(), | ||
"ALT" => modifiers.add_alt(), | ||
"SUPER" => modifiers.add_super(), | ||
"CTRL" => modifiers.add_control(), | ||
"ENTER" => keycode = KEY_ENTER, | ||
"SPACE" => keycode = KEY_SPACE, | ||
"TAB" => keycode = KEY_TAB, | ||
"DELETE" => keycode = KEY_DELETE, | ||
"ESC" => keycode = KEY_ESCAPE, | ||
c => { | ||
keycode = c.chars().last().unwrap(); | ||
} | ||
}); | ||
Self { modifiers, keycode } | ||
} | ||
|
||
pub fn from(modifiers: KeyModifier, keycode: char) -> Self { | ||
Self { modifiers, keycode } | ||
} | ||
|
||
pub fn is_match(&self, modifiers: KeyModifier, keycode: &char) -> bool { | ||
return self.modifiers == modifiers && self.keycode.eq_ignore_ascii_case(keycode); | ||
} | ||
|
||
pub fn inner(&self) -> (KeyModifier, char) { | ||
(self.modifiers, self.keycode) | ||
} | ||
} | ||
|
||
impl Display for Hotkey { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
if self.modifiers.is_control() { | ||
write!(f, "{} ", SYMBOL_CTRL)?; | ||
} | ||
if self.modifiers.is_shift() { | ||
write!(f, "{} ", SYMBOL_SHIFT)?; | ||
} | ||
if self.modifiers.is_alt() { | ||
write!(f, "{} ", SYMBOL_ALT)?; | ||
} | ||
if self.modifiers.is_super() { | ||
write!(f, "{} ", SYMBOL_SUPER)?; | ||
} | ||
match self.keycode { | ||
KEY_ENTER => write!(f, "Enter"), | ||
KEY_SPACE => write!(f, "Space"), | ||
KEY_TAB => write!(f, "Tab"), | ||
KEY_DELETE => write!(f, "Del"), | ||
KEY_ESCAPE => write!(f, "Esc"), | ||
c => write!(f, "{}", c.to_ascii_uppercase()), | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_parse() { | ||
let hotkey = Hotkey::from_str("super+shift+z"); | ||
let mut actual_modifier = KeyModifier::new(); | ||
actual_modifier.add_shift(); | ||
actual_modifier.add_super(); | ||
assert_eq!(hotkey.modifiers, actual_modifier); | ||
assert_eq!(hotkey.keycode, 'Z'); | ||
assert_eq!(hotkey.is_match(actual_modifier, &'z'), true); | ||
} | ||
|
||
#[test] | ||
fn test_parse_long_input() { | ||
let hotkey = Hotkey::from_str("super+shift+ctrl+alt+w"); | ||
let mut actual_modifier = KeyModifier::new(); | ||
actual_modifier.add_shift(); | ||
actual_modifier.add_super(); | ||
actual_modifier.add_control(); | ||
actual_modifier.add_alt(); | ||
assert_eq!(hotkey.modifiers, actual_modifier); | ||
assert_eq!(hotkey.keycode, 'W'); | ||
assert_eq!(hotkey.is_match(actual_modifier, &'W'), true); | ||
} | ||
|
||
#[test] | ||
fn test_parse_with_named_keycode() { | ||
let hotkey = Hotkey::from_str("super+ctrl+space"); | ||
let mut actual_modifier = KeyModifier::new(); | ||
actual_modifier.add_super(); | ||
actual_modifier.add_control(); | ||
assert_eq!(hotkey.modifiers, actual_modifier); | ||
assert_eq!(hotkey.keycode, KEY_SPACE); | ||
assert_eq!(hotkey.is_match(actual_modifier, &KEY_SPACE), true); | ||
} | ||
|
||
#[test] | ||
fn test_display() { | ||
assert_eq!( | ||
format!("{}", Hotkey::from_str("super+ctrl+space")), | ||
format!("{} {} Space", SYMBOL_CTRL, SYMBOL_SUPER) | ||
); | ||
|
||
assert_eq!( | ||
format!("{}", Hotkey::from_str("super+alt+z")), | ||
format!("{} {} Z", SYMBOL_ALT, SYMBOL_SUPER) | ||
); | ||
|
||
assert_eq!( | ||
format!("{}", Hotkey::from_str("ctrl+shift+o")), | ||
format!("{} {} O", SYMBOL_CTRL, SYMBOL_SHIFT) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.