From 136b4791df2a39d5452851089cd787d389a9c715 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Mon, 7 Jun 2021 21:16:21 +0200 Subject: [PATCH] feat(core): wire up delay options to injectors --- espanso/src/cli/worker/config.rs | 13 ++++++ .../dispatch/executor/event_injector.rs | 41 ++++++++++++------- .../engine/dispatch/executor/key_injector.rs | 23 +++++++++-- .../worker/engine/dispatch/executor/mod.rs | 12 +++++- espanso/src/cli/worker/engine/mod.rs | 4 +- 5 files changed, 72 insertions(+), 21 deletions(-) diff --git a/espanso/src/cli/worker/config.rs b/espanso/src/cli/worker/config.rs index 0528ba9..4b69bee 100644 --- a/espanso/src/cli/worker/config.rs +++ b/espanso/src/cli/worker/config.rs @@ -128,3 +128,16 @@ impl<'a> super::engine::dispatch::executor::clipboard_injector::ClipboardParamsP } } } + +impl<'a> super::engine::dispatch::executor::InjectParamsProvider + for ConfigManager<'a> +{ + fn get(&self) -> super::engine::dispatch::executor::InjectParams { + let active = self.active(); + super::engine::dispatch::executor::InjectParams { + disable_x11_fast_inject: active.disable_x11_fast_inject(), + inject_delay: active.inject_delay(), + key_delay: active.key_delay(), + } + } +} \ No newline at end of file diff --git a/espanso/src/cli/worker/engine/dispatch/executor/event_injector.rs b/espanso/src/cli/worker/engine/dispatch/executor/event_injector.rs index e06bbe6..f1a83f3 100644 --- a/espanso/src/cli/worker/engine/dispatch/executor/event_injector.rs +++ b/espanso/src/cli/worker/engine/dispatch/executor/event_injector.rs @@ -17,45 +17,58 @@ * along with espanso. If not, see . */ -use espanso_inject::Injector; +use std::convert::TryInto; + +use espanso_inject::{InjectionOptions, Injector}; use crate::engine::dispatch::TextInjector; +use super::InjectParamsProvider; + pub struct EventInjectorAdapter<'a> { injector: &'a dyn Injector, + params_provider: &'a dyn InjectParamsProvider, } -impl <'a> EventInjectorAdapter<'a> { - pub fn new(injector: &'a dyn Injector) -> Self { +impl<'a> EventInjectorAdapter<'a> { + pub fn new(injector: &'a dyn Injector, params_provider: &'a dyn InjectParamsProvider) -> Self { Self { - injector + injector, + params_provider, } } } -impl <'a> TextInjector for EventInjectorAdapter<'a> { +impl<'a> TextInjector for EventInjectorAdapter<'a> { fn name(&self) -> &'static str { "event" } - + fn inject_text(&self, text: &str) -> anyhow::Result<()> { + let params = self.params_provider.get(); + // Handle CRLF or LF line endings correctly - let split_sequence = if text.contains("\r\n") { - "\r\n" - } else { - "\n" + let split_sequence = if text.contains("\r\n") { "\r\n" } else { "\n" }; + + let injection_options = InjectionOptions { + delay: params + .inject_delay + .unwrap_or(InjectionOptions::default().delay.try_into().unwrap()) + .try_into() + .unwrap(), + disable_fast_inject: params.disable_x11_fast_inject, }; // We don't use the lines() method because it skips emtpy lines, which is not what we want. for (i, line) in text.split(split_sequence).enumerate() { // We simulate an Return press between lines if i > 0 { - // TODO: handle injection options - self.injector.send_keys(&[espanso_inject::keys::Key::Enter], Default::default())? + self + .injector + .send_keys(&[espanso_inject::keys::Key::Enter], injection_options)? } - // TODO: handle injection options - self.injector.send_string(line, Default::default())?; + self.injector.send_string(line, injection_options)?; } Ok(()) diff --git a/espanso/src/cli/worker/engine/dispatch/executor/key_injector.rs b/espanso/src/cli/worker/engine/dispatch/executor/key_injector.rs index 8fa907e..571e4e3 100644 --- a/espanso/src/cli/worker/engine/dispatch/executor/key_injector.rs +++ b/espanso/src/cli/worker/engine/dispatch/executor/key_injector.rs @@ -17,24 +17,39 @@ * along with espanso. If not, see . */ -use espanso_inject::Injector; +use std::convert::TryInto; +use espanso_inject::{InjectionOptions, Injector}; use crate::engine::dispatch::KeyInjector; +use super::InjectParamsProvider; + pub struct KeyInjectorAdapter<'a> { injector: &'a dyn Injector, + params_provider: &'a dyn InjectParamsProvider, } impl<'a> KeyInjectorAdapter<'a> { - pub fn new(injector: &'a dyn Injector) -> Self { - Self { injector } + pub fn new(injector: &'a dyn Injector, params_provider: &'a dyn InjectParamsProvider) -> Self { + Self { injector, params_provider } } } impl<'a> KeyInjector for KeyInjectorAdapter<'a> { fn inject_sequence(&self, keys: &[crate::engine::event::input::Key]) -> anyhow::Result<()> { + let params = self.params_provider.get(); + + let injection_options = InjectionOptions { + delay: params + .key_delay + .unwrap_or(InjectionOptions::default().delay.try_into().unwrap()) + .try_into() + .unwrap(), + disable_fast_inject: params.disable_x11_fast_inject, + }; + let converted_keys: Vec<_> = keys.iter().map(convert_to_inject_key).collect(); - self.injector.send_keys(&converted_keys, Default::default()) // TODO: handle options + self.injector.send_keys(&converted_keys, injection_options) } } diff --git a/espanso/src/cli/worker/engine/dispatch/executor/mod.rs b/espanso/src/cli/worker/engine/dispatch/executor/mod.rs index 425f595..1a63b8b 100644 --- a/espanso/src/cli/worker/engine/dispatch/executor/mod.rs +++ b/espanso/src/cli/worker/engine/dispatch/executor/mod.rs @@ -21,4 +21,14 @@ pub mod clipboard_injector; pub mod context_menu; pub mod event_injector; pub mod icon; -pub mod key_injector; \ No newline at end of file +pub mod key_injector; + +pub trait InjectParamsProvider { + fn get(&self) -> InjectParams; +} + +pub struct InjectParams { + pub inject_delay: Option, + pub key_delay: Option, + pub disable_x11_fast_inject: bool, +} \ No newline at end of file diff --git a/espanso/src/cli/worker/engine/mod.rs b/espanso/src/cli/worker/engine/mod.rs index cd515fc..9b3c62b 100644 --- a/espanso/src/cli/worker/engine/mod.rs +++ b/espanso/src/cli/worker/engine/mod.rs @@ -156,10 +156,10 @@ pub fn initialize_and_spawn( disable_options, ); - let event_injector = EventInjectorAdapter::new(&*injector); + let event_injector = EventInjectorAdapter::new(&*injector, &config_manager); let clipboard_injector = ClipboardInjectorAdapter::new(&*injector, &*clipboard, &config_manager); - let key_injector = KeyInjectorAdapter::new(&*injector); + let key_injector = KeyInjectorAdapter::new(&*injector, &config_manager); let context_menu_adapter = ContextMenuHandlerAdapter::new(&*ui_remote); let icon_adapter = IconHandlerAdapter::new(&*ui_remote); let dispatcher = crate::engine::dispatch::default(