diff --git a/espanso-config/src/config/mod.rs b/espanso-config/src/config/mod.rs index d68d9c8..b4e120c 100644 --- a/espanso-config/src/config/mod.rs +++ b/espanso-config/src/config/mod.rs @@ -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; + 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 { store::DefaultConfigStore::load(config_dir) } diff --git a/espanso-config/src/config/parse/mod.rs b/espanso-config/src/config/parse/mod.rs index f3fae0d..d7dc03d 100644 --- a/espanso-config/src/config/parse/mod.rs +++ b/espanso-config/src/config/parse/mod.rs @@ -32,6 +32,8 @@ pub(crate) struct ParsedConfig { pub pre_paste_delay: Option, + pub toggle_key: Option, + // Includes pub includes: Option>, pub excludes: Option>, diff --git a/espanso-config/src/config/parse/yaml.rs b/espanso-config/src/config/parse/yaml.rs index a229ba7..6637b2e 100644 --- a/espanso-config/src/config/parse/yaml.rs +++ b/espanso-config/src/config/parse/yaml.rs @@ -39,6 +39,9 @@ pub(crate) struct YAMLConfig { #[serde(default)] pub pre_paste_delay: Option, + #[serde(default)] + pub toggle_key: Option, + #[serde(default)] pub includes: Option>, @@ -92,6 +95,8 @@ impl TryFrom 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"] @@ -144,6 +150,8 @@ mod tests { clipboard_threshold: Some(200), pre_paste_delay: Some(300), + + toggle_key: Some("CTRL".to_string()), use_standard_includes: Some(true), includes: Some(vec!["test1".to_string()]), diff --git a/espanso-config/src/config/resolve.rs b/espanso-config/src/config/resolve.rs index 72abb92..eb880e8 100644 --- a/espanso-config/src/config/resolve.rs +++ b/espanso-config/src/config/resolve.rs @@ -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 { + // 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, diff --git a/espanso-config/src/legacy/mod.rs b/espanso-config/src/legacy/mod.rs index d050bbc..c7036c0 100644 --- a/espanso-config/src/legacy/mod.rs +++ b/espanso-config/src/legacy/mod.rs @@ -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 { + 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 {