feat(config): add clipboard-related parameters

This commit is contained in:
Federico Terzi 2021-06-05 12:18:52 +02:00
parent 0d226619a9
commit f9ff881136
6 changed files with 79 additions and 15 deletions

View File

@ -18,5 +18,6 @@
*/ */
pub(crate) const DEFAULT_CLIPBOARD_THRESHOLD: usize = 100; pub(crate) const DEFAULT_CLIPBOARD_THRESHOLD: usize = 100;
pub(crate) const DEFAULT_PRE_PASTE_DELAY: usize = 100;
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;

View File

@ -49,7 +49,22 @@ pub trait Config: Send {
// copied in the clipboard, the operation will fail. // copied in the clipboard, the operation will fail.
fn pre_paste_delay(&self) -> usize; 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<String>;
// 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 // Defines the key that disables/enables espanso when double pressed
fn toggle_key(&self) -> Option<ToggleKey>; fn toggle_key(&self) -> Option<ToggleKey>;

View File

@ -31,11 +31,13 @@ pub(crate) struct ParsedConfig {
pub clipboard_threshold: Option<usize>, pub clipboard_threshold: Option<usize>,
pub auto_restart: Option<bool>, pub auto_restart: Option<bool>,
pub preserve_clipboard: Option<bool>, pub preserve_clipboard: Option<bool>,
pub toggle_key: Option<String>,
pub paste_shortcut: Option<String>,
pub disable_x11_fast_inject: Option<bool>,
pub pre_paste_delay: Option<usize>, pub pre_paste_delay: Option<usize>,
pub restore_clipboard_delay: Option<usize>, pub restore_clipboard_delay: Option<usize>,
pub paste_shortcut_event_delay: Option<usize>,
pub toggle_key: Option<String>,
// Includes // Includes

View File

@ -51,6 +51,16 @@ pub(crate) struct YAMLConfig {
#[serde(default)] #[serde(default)]
pub restore_clipboard_delay: Option<usize>, pub restore_clipboard_delay: Option<usize>,
#[serde(default)]
pub paste_shortcut_event_delay: Option<usize>,
#[serde(default)]
pub paste_shortcut: Option<String>,
#[serde(default)]
pub disable_x11_fast_inject: Option<bool>,
// Include/Exclude
#[serde(default)] #[serde(default)]
pub includes: Option<Vec<String>>, pub includes: Option<Vec<String>>,
@ -102,12 +112,14 @@ impl TryFrom<YAMLConfig> for ParsedConfig {
backend: yaml_config.backend, backend: yaml_config.backend,
clipboard_threshold: yaml_config.clipboard_threshold, clipboard_threshold: yaml_config.clipboard_threshold,
auto_restart: yaml_config.auto_restart, auto_restart: yaml_config.auto_restart,
toggle_key: yaml_config.toggle_key,
preserve_clipboard: yaml_config.preserve_clipboard, 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, pre_paste_delay: yaml_config.pre_paste_delay,
restore_clipboard_delay: yaml_config.restore_clipboard_delay, restore_clipboard_delay: yaml_config.restore_clipboard_delay,
paste_shortcut_event_delay: yaml_config.paste_shortcut_event_delay,
toggle_key: yaml_config.toggle_key,
use_standard_includes: yaml_config.use_standard_includes, use_standard_includes: yaml_config.use_standard_includes,
includes: yaml_config.includes, includes: yaml_config.includes,
@ -140,6 +152,9 @@ mod tests {
auto_restart: false auto_restart: false
preserve_clipboard: false preserve_clipboard: false
restore_clipboard_delay: 400 restore_clipboard_delay: 400
paste_shortcut: CTRL+ALT+V
paste_shortcut_event_delay: 10
disable_x11_fast_inject: true
use_standard_includes: true use_standard_includes: true
includes: ["test1"] includes: ["test1"]
@ -166,6 +181,9 @@ mod tests {
auto_restart: Some(false), auto_restart: Some(false),
preserve_clipboard: Some(false), preserve_clipboard: Some(false),
restore_clipboard_delay: Some(400), 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), pre_paste_delay: Some(300),

View File

@ -17,13 +17,7 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use super::{ 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};
default::{DEFAULT_CLIPBOARD_THRESHOLD, DEFAULT_PRE_PASTE_DELAY},
parse::ParsedConfig,
path::calculate_paths,
util::os_matches,
AppProperties, Backend, Config, ToggleKey,
};
use crate::{counter::next_id, merge}; use crate::{counter::next_id, merge};
use anyhow::Result; use anyhow::Result;
use log::error; use log::error;
@ -199,7 +193,19 @@ impl Config for ResolvedConfig {
} }
fn restore_clipboard_delay(&self) -> usize { 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<String> {
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, pre_paste_delay,
preserve_clipboard, preserve_clipboard,
restore_clipboard_delay, restore_clipboard_delay,
paste_shortcut,
paste_shortcut_event_delay,
disable_x11_fast_inject,
toggle_key, toggle_key,
includes, includes,
excludes, excludes,

View File

@ -292,6 +292,25 @@ impl Config for LegacyInteropConfig {
fn restore_clipboard_delay(&self) -> usize { fn restore_clipboard_delay(&self) -> usize {
self.config.restore_clipboard_delay.try_into().unwrap() 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<String> {
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 { struct LegacyMatchGroup {