feat(config): add toggle_key option

This commit is contained in:
Federico Terzi 2021-05-23 15:45:58 +02:00
parent 76b4a4a302
commit d193cb749b
5 changed files with 84 additions and 1 deletions

View File

@ -51,6 +51,9 @@ pub trait Config: Send {
// TODO: add other delay options (start by the ones needed in clipboard injector)
// Defines the key that disables/enables espanso when double pressed
fn toggle_key(&self) -> Option<ToggleKey>;
fn is_match<'a>(&self, app: &AppProperties<'a>) -> bool;
}
@ -75,6 +78,23 @@ pub enum Backend {
Auto,
}
#[derive(Debug, Copy, Clone)]
pub enum ToggleKey {
Ctrl,
Meta,
Alt,
Shift,
RightCtrl,
RightAlt,
RightShift,
RightMeta,
LeftCtrl,
LeftAlt,
LeftShift,
LeftMeta,
}
pub fn load_store(config_dir: &Path) -> Result<impl ConfigStore> {
store::DefaultConfigStore::load(config_dir)
}

View File

@ -32,6 +32,8 @@ pub(crate) struct ParsedConfig {
pub pre_paste_delay: Option<usize>,
pub toggle_key: Option<String>,
// Includes
pub includes: Option<Vec<String>>,
pub excludes: Option<Vec<String>>,

View File

@ -39,6 +39,9 @@ pub(crate) struct YAMLConfig {
#[serde(default)]
pub pre_paste_delay: Option<usize>,
#[serde(default)]
pub toggle_key: Option<String>,
#[serde(default)]
pub includes: Option<Vec<String>>,
@ -92,6 +95,8 @@ impl TryFrom<YAMLConfig> for ParsedConfig {
pre_paste_delay: yaml_config.pre_paste_delay,
toggle_key: yaml_config.toggle_key,
use_standard_includes: yaml_config.use_standard_includes,
includes: yaml_config.includes,
extra_includes: yaml_config.extra_includes,
@ -119,6 +124,7 @@ mod tests {
backend: clipboard
clipboard_threshold: 200
pre_paste_delay: 300
toggle_key: CTRL
use_standard_includes: true
includes: ["test1"]
@ -145,6 +151,8 @@ mod tests {
pre_paste_delay: Some(300),
toggle_key: Some("CTRL".to_string()),
use_standard_includes: Some(true),
includes: Some(vec!["test1".to_string()]),
extra_includes: Some(vec!["test2".to_string()]),

View File

@ -22,7 +22,7 @@ use super::{
parse::ParsedConfig,
path::calculate_paths,
util::os_matches,
AppProperties, Backend, Config,
AppProperties, Backend, Config, ToggleKey,
};
use crate::{counter::next_id, merge};
use anyhow::Result;
@ -124,6 +124,7 @@ impl Config for ResolvedConfig {
}
fn backend(&self) -> Backend {
// TODO: test
match self
.parsed
.backend
@ -134,6 +135,7 @@ impl Config for ResolvedConfig {
Some("clipboard") => Backend::Clipboard,
Some("inject") => Backend::Inject,
Some("auto") => Backend::Auto,
None => Backend::Auto,
err => {
error!("invalid backend specified {:?}, falling back to Auto", err);
Backend::Auto
@ -154,6 +156,36 @@ impl Config for ResolvedConfig {
.pre_paste_delay
.unwrap_or(DEFAULT_PRE_PASTE_DELAY)
}
fn toggle_key(&self) -> Option<ToggleKey> {
// TODO: test
match self
.parsed
.toggle_key
.as_deref()
.map(|key| key.to_lowercase())
.as_deref()
{
Some("ctrl") => Some(ToggleKey::Ctrl),
Some("alt") => Some(ToggleKey::Alt),
Some("shift") => Some(ToggleKey::Shift),
Some("meta") | Some("cmd") => Some(ToggleKey::Meta),
Some("right_ctrl") => Some(ToggleKey::RightCtrl),
Some("right_alt") => Some(ToggleKey::RightAlt),
Some("right_shift") => Some(ToggleKey::RightShift),
Some("right_meta") | Some("right_cmd")=> Some(ToggleKey::RightMeta),
Some("left_ctrl") => Some(ToggleKey::LeftCtrl),
Some("left_alt") => Some(ToggleKey::LeftAlt),
Some("left_shift") => Some(ToggleKey::LeftShift),
Some("left_meta") | Some("left_cmd") => Some(ToggleKey::LeftMeta),
Some("off") => None,
None => Some(ToggleKey::Alt),
err => {
error!("invalid toggle_key specified {:?}, falling back to ALT", err);
Some(ToggleKey::Alt)
}
}
}
}
impl ResolvedConfig {
@ -213,6 +245,7 @@ impl ResolvedConfig {
backend,
clipboard_threshold,
pre_paste_delay,
toggle_key,
includes,
excludes,
extra_includes,

View File

@ -260,6 +260,26 @@ impl Config for LegacyInteropConfig {
fn pre_paste_delay(&self) -> usize {
crate::config::default::DEFAULT_PRE_PASTE_DELAY
}
fn toggle_key(&self) -> Option<crate::config::ToggleKey> {
match self.config.toggle_key {
model::KeyModifier::CTRL => Some(crate::config::ToggleKey::Ctrl),
model::KeyModifier::SHIFT => Some(crate::config::ToggleKey::Shift),
model::KeyModifier::ALT => Some(crate::config::ToggleKey::Alt),
model::KeyModifier::META => Some(crate::config::ToggleKey::Meta),
model::KeyModifier::BACKSPACE => None,
model::KeyModifier::OFF => None,
model::KeyModifier::LEFT_CTRL => Some(crate::config::ToggleKey::LeftCtrl),
model::KeyModifier::RIGHT_CTRL => Some(crate::config::ToggleKey::RightCtrl),
model::KeyModifier::LEFT_ALT => Some(crate::config::ToggleKey::LeftAlt),
model::KeyModifier::RIGHT_ALT => Some(crate::config::ToggleKey::RightAlt),
model::KeyModifier::LEFT_META => Some(crate::config::ToggleKey::LeftMeta),
model::KeyModifier::RIGHT_META => Some(crate::config::ToggleKey::RightMeta),
model::KeyModifier::LEFT_SHIFT => Some(crate::config::ToggleKey::LeftShift),
model::KeyModifier::RIGHT_SHIFT => Some(crate::config::ToggleKey::RightShift),
model::KeyModifier::CAPS_LOCK => None
}
}
}
struct LegacyMatchGroup {