Add check to avoid espanso reinterpreting its own actions

This commit is contained in:
Federico Terzi 2020-01-19 23:41:11 +01:00
parent 9332899969
commit e2c98284b6
2 changed files with 42 additions and 1 deletions

View File

@ -59,6 +59,7 @@ fn default_passive_match_regex() -> String{ "(?P<name>:\\p{L}+)(/(?P<args>.*)/)?
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<Match> { 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
}

View File

@ -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<bool>,
last_action_time: RefCell<SystemTime>, // 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