From e2c98284b6faf6d19c0d922ed68bdadc6cf5e468 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 19 Jan 2020 23:41:11 +0100 Subject: [PATCH] Add check to avoid espanso reinterpreting its own actions --- src/config/mod.rs | 5 +++++ src/engine.rs | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 166b6bc..96e6a8f 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -59,6 +59,7 @@ fn default_passive_match_regex() -> String{ "(?P:\\p{L}+)(/(?P.*)/)? fn default_passive_arg_delimiter() -> char { '/' } fn default_passive_arg_escape() -> char { '\\' } fn default_passive_key() -> KeyModifier { KeyModifier::OFF } +fn default_action_noop_interval() -> u128 { 500 } fn default_backspace_limit() -> i32 { 3 } fn default_exclude_default_matches() -> bool {false} fn default_matches() -> Vec { Vec::new() } @@ -119,6 +120,9 @@ pub struct Configs { #[serde(default = "default_passive_key")] pub passive_key: KeyModifier, + #[serde(default = "default_action_noop_interval")] + pub action_noop_interval: u128, + #[serde(default)] pub paste_shortcut: PasteShortcut, @@ -171,6 +175,7 @@ impl Configs { validate_field!(result, self.passive_arg_delimiter, default_passive_arg_delimiter()); validate_field!(result, self.passive_arg_escape, default_passive_arg_escape()); validate_field!(result, self.passive_key, default_passive_key()); + validate_field!(result, self.action_noop_interval, default_action_noop_interval()); result } diff --git a/src/engine.rs b/src/engine.rs index 7ef9ee8..4145d48 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -32,6 +32,7 @@ use std::process::exit; use std::collections::HashMap; use std::path::PathBuf; use regex::{Regex, Captures}; +use std::time::SystemTime; pub struct Engine<'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIManager, R: Renderer> { @@ -42,6 +43,8 @@ pub struct Engine<'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager< renderer: &'a R, enabled: RefCell, + last_action_time: RefCell, // Used to block espanso from re-interpreting it's own inputs + action_noop_interval: u128, } impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIManager, R: Renderer> @@ -50,13 +53,17 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa config_manager: &'a M, ui_manager: &'a U, renderer: &'a R) -> Engine<'a, S, C, M, U, R> { let enabled = RefCell::new(true); + let last_action_time = RefCell::new(SystemTime::now()); + let action_noop_interval = config_manager.default_config().action_noop_interval; Engine{keyboard_manager, clipboard_manager, config_manager, ui_manager, renderer, - enabled + enabled, + last_action_time, + action_noop_interval, } } @@ -102,6 +109,20 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa None } } + + /// Used to check if the last action has been executed within a specified interval. + /// If so, return true (blocking the action), otherwise false. + fn check_last_action_and_set(&self, interval: u128) -> bool { + let mut last_action_time = self.last_action_time.borrow_mut(); + if let Ok(elapsed) = last_action_time.elapsed() { + if elapsed.as_millis() < interval { + return true; + } + } + + (*last_action_time) = SystemTime::now(); + return false; + } } lazy_static! { @@ -118,6 +139,11 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa return; } + // avoid espanso reinterpreting its own actions + if self.check_last_action_and_set(self.action_noop_interval) { + return; + } + let char_count = if trailing_separator.is_none() { m.trigger.chars().count() as i32 }else{ @@ -218,6 +244,11 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa } fn on_enable_update(&self, status: bool) { + // avoid espanso reinterpreting its own actions + if self.check_last_action_and_set(self.action_noop_interval) { + return; + } + let message = if status { "espanso enabled" }else{ @@ -233,6 +264,11 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa } fn on_passive(&self) { + // avoid espanso reinterpreting its own actions + if self.check_last_action_and_set(self.action_noop_interval) { + return; + } + info!("Passive mode activated"); // Trigger a copy shortcut to transfer the content of the selection to the clipboard