From f9ff88113636fa8213fa12661375e37670519d77 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 5 Jun 2021 12:18:52 +0200 Subject: [PATCH] feat(config): add clipboard-related parameters --- espanso-config/src/config/default.rs | 5 +++-- espanso-config/src/config/mod.rs | 17 ++++++++++++++++- espanso-config/src/config/parse/mod.rs | 6 ++++-- espanso-config/src/config/parse/yaml.rs | 22 ++++++++++++++++++++-- espanso-config/src/config/resolve.rs | 25 +++++++++++++++++-------- espanso-config/src/legacy/mod.rs | 19 +++++++++++++++++++ 6 files changed, 79 insertions(+), 15 deletions(-) diff --git a/espanso-config/src/config/default.rs b/espanso-config/src/config/default.rs index a023f5d..074da3e 100644 --- a/espanso-config/src/config/default.rs +++ b/espanso-config/src/config/default.rs @@ -18,5 +18,6 @@ */ pub(crate) const DEFAULT_CLIPBOARD_THRESHOLD: usize = 100; - -pub(crate) const DEFAULT_PRE_PASTE_DELAY: usize = 100; \ No newline at end of file +pub(crate) const DEFAULT_PRE_PASTE_DELAY: usize = 100; +pub(crate) const DEFAULT_SHORTCUT_EVENT_DELAY: usize = 5; +pub(crate) const DEFAULT_RESTORE_CLIPBOARD_DELAY: usize = 300; \ No newline at end of file diff --git a/espanso-config/src/config/mod.rs b/espanso-config/src/config/mod.rs index 1985ca9..205adbd 100644 --- a/espanso-config/src/config/mod.rs +++ b/espanso-config/src/config/mod.rs @@ -49,7 +49,22 @@ pub trait Config: Send { // copied in the clipboard, the operation will fail. fn pre_paste_delay(&self) -> usize; - // TODO: add other delay options (start by the ones needed in clipboard injector) + // Number of milliseconds between keystrokes when simulating the Paste shortcut + // For example: CTRL + (wait 5ms) + V + (wait 5ms) + release V + (wait 5ms) + release CTRL + // This is needed as sometimes (for example on macOS), without a delay some keystrokes + // were not registered correctly + fn paste_shortcut_event_delay(&self) -> usize; + + // Customize the keyboard shortcut used to paste an expansion. + // This should follow this format: CTRL+SHIFT+V + fn paste_shortcut(&self) -> Option; + + // NOTE: This is only relevant on Linux under X11 environments + // Switch to a slower (but sometimes more supported) way of injecting + // key events based on XTestFakeKeyEvent instead of XSendEvent. + // From my experiements, disabling fast inject becomes particularly slow when + // using the Gnome desktop environment. + fn disable_x11_fast_inject(&self) -> bool; // Defines the key that disables/enables espanso when double pressed fn toggle_key(&self) -> Option; diff --git a/espanso-config/src/config/parse/mod.rs b/espanso-config/src/config/parse/mod.rs index 92f4279..47a5304 100644 --- a/espanso-config/src/config/parse/mod.rs +++ b/espanso-config/src/config/parse/mod.rs @@ -31,11 +31,13 @@ pub(crate) struct ParsedConfig { pub clipboard_threshold: Option, pub auto_restart: Option, pub preserve_clipboard: Option, + pub toggle_key: Option, + pub paste_shortcut: Option, + pub disable_x11_fast_inject: Option, pub pre_paste_delay: Option, pub restore_clipboard_delay: Option, - - pub toggle_key: Option, + pub paste_shortcut_event_delay: Option, // Includes diff --git a/espanso-config/src/config/parse/yaml.rs b/espanso-config/src/config/parse/yaml.rs index 5b3cdaf..d78a2dd 100644 --- a/espanso-config/src/config/parse/yaml.rs +++ b/espanso-config/src/config/parse/yaml.rs @@ -51,6 +51,16 @@ pub(crate) struct YAMLConfig { #[serde(default)] pub restore_clipboard_delay: Option, + #[serde(default)] + pub paste_shortcut_event_delay: Option, + + #[serde(default)] + pub paste_shortcut: Option, + + #[serde(default)] + pub disable_x11_fast_inject: Option, + + // Include/Exclude #[serde(default)] pub includes: Option>, @@ -102,12 +112,14 @@ impl TryFrom for ParsedConfig { backend: yaml_config.backend, clipboard_threshold: yaml_config.clipboard_threshold, auto_restart: yaml_config.auto_restart, + toggle_key: yaml_config.toggle_key, preserve_clipboard: yaml_config.preserve_clipboard, + paste_shortcut: yaml_config.paste_shortcut, + disable_x11_fast_inject: yaml_config.disable_x11_fast_inject, pre_paste_delay: yaml_config.pre_paste_delay, restore_clipboard_delay: yaml_config.restore_clipboard_delay, - - toggle_key: yaml_config.toggle_key, + paste_shortcut_event_delay: yaml_config.paste_shortcut_event_delay, use_standard_includes: yaml_config.use_standard_includes, includes: yaml_config.includes, @@ -140,6 +152,9 @@ mod tests { auto_restart: false preserve_clipboard: false restore_clipboard_delay: 400 + paste_shortcut: CTRL+ALT+V + paste_shortcut_event_delay: 10 + disable_x11_fast_inject: true use_standard_includes: true includes: ["test1"] @@ -166,6 +181,9 @@ mod tests { auto_restart: Some(false), preserve_clipboard: Some(false), restore_clipboard_delay: Some(400), + paste_shortcut: Some("CTRL+ALT+V".to_string()), + paste_shortcut_event_delay: Some(10), + disable_x11_fast_inject: Some(true), pre_paste_delay: Some(300), diff --git a/espanso-config/src/config/resolve.rs b/espanso-config/src/config/resolve.rs index 9d47770..f82a036 100644 --- a/espanso-config/src/config/resolve.rs +++ b/espanso-config/src/config/resolve.rs @@ -17,13 +17,7 @@ * along with espanso. If not, see . */ -use super::{ - default::{DEFAULT_CLIPBOARD_THRESHOLD, DEFAULT_PRE_PASTE_DELAY}, - parse::ParsedConfig, - path::calculate_paths, - util::os_matches, - AppProperties, Backend, Config, ToggleKey, -}; +use super::{AppProperties, Backend, Config, ToggleKey, default::{DEFAULT_CLIPBOARD_THRESHOLD, DEFAULT_PRE_PASTE_DELAY, DEFAULT_RESTORE_CLIPBOARD_DELAY, DEFAULT_SHORTCUT_EVENT_DELAY}, parse::ParsedConfig, path::calculate_paths, util::os_matches}; use crate::{counter::next_id, merge}; use anyhow::Result; use log::error; @@ -199,7 +193,19 @@ impl Config for ResolvedConfig { } fn restore_clipboard_delay(&self) -> usize { - self.parsed.restore_clipboard_delay.unwrap_or(300) + self.parsed.restore_clipboard_delay.unwrap_or(DEFAULT_RESTORE_CLIPBOARD_DELAY) + } + + fn paste_shortcut_event_delay(&self) -> usize { + self.parsed.paste_shortcut_event_delay.unwrap_or(DEFAULT_SHORTCUT_EVENT_DELAY) + } + + fn paste_shortcut(&self) -> Option { + self.parsed.paste_shortcut.clone() + } + + fn disable_x11_fast_inject(&self) -> bool { + self.parsed.disable_x11_fast_inject.unwrap_or(false) } } @@ -263,6 +269,9 @@ impl ResolvedConfig { pre_paste_delay, preserve_clipboard, restore_clipboard_delay, + paste_shortcut, + paste_shortcut_event_delay, + disable_x11_fast_inject, toggle_key, includes, excludes, diff --git a/espanso-config/src/legacy/mod.rs b/espanso-config/src/legacy/mod.rs index 1f8c690..6d01bd8 100644 --- a/espanso-config/src/legacy/mod.rs +++ b/espanso-config/src/legacy/mod.rs @@ -292,6 +292,25 @@ impl Config for LegacyInteropConfig { fn restore_clipboard_delay(&self) -> usize { self.config.restore_clipboard_delay.try_into().unwrap() } + + fn paste_shortcut_event_delay(&self) -> usize { + crate::config::default::DEFAULT_SHORTCUT_EVENT_DELAY + } + + fn paste_shortcut(&self) -> Option { + match self.config.paste_shortcut { + model::PasteShortcut::Default => None, + model::PasteShortcut::CtrlV => Some("CTRL+V".to_string()), + model::PasteShortcut::CtrlShiftV => Some("CTRL+SHIFT+V".to_string()), + model::PasteShortcut::ShiftInsert => Some("SHIFT+INSERT".to_string()), + model::PasteShortcut::CtrlAltV => Some("CTRL+ALT+V".to_string()), + model::PasteShortcut::MetaV => Some("META+V".to_string()), + } + } + + fn disable_x11_fast_inject(&self) -> bool { + self.config.fast_inject + } } struct LegacyMatchGroup {