diff --git a/espanso/src/cli/worker/config.rs b/espanso/src/cli/worker/config.rs index 85d34f5..15d654f 100644 --- a/espanso/src/cli/worker/config.rs +++ b/espanso/src/cli/worker/config.rs @@ -106,11 +106,25 @@ impl<'a> ModeProvider for ConfigManager<'a> { fn active_mode(&self) -> crate::engine::dispatch::Mode { let config = self.active(); match config.backend() { - espanso_config::config::Backend::Inject => crate::engine::dispatch::Mode::Event, - espanso_config::config::Backend::Clipboard => crate::engine::dispatch::Mode::Clipboard, + espanso_config::config::Backend::Inject => crate::engine::dispatch::Mode::Event, + espanso_config::config::Backend::Clipboard => crate::engine::dispatch::Mode::Clipboard, espanso_config::config::Backend::Auto => crate::engine::dispatch::Mode::Auto { clipboard_threshold: config.clipboard_threshold(), }, } } } + +impl<'a> super::engine::executor::clipboard_injector::ClipboardParamsProvider + for ConfigManager<'a> +{ + fn get(&self) -> super::engine::executor::clipboard_injector::ClipboardParams { + let active = self.active(); + super::engine::executor::clipboard_injector::ClipboardParams { + pre_paste_delay: active.pre_paste_delay(), + paste_shortcut_event_delay: 5, // TODO: read from config + paste_shortcut: None, // TODO: read from config + disable_x11_fast_inject: false, // TODO: read from config + } + } +} diff --git a/espanso/src/cli/worker/engine/executor/clipboard_injector.rs b/espanso/src/cli/worker/engine/executor/clipboard_injector.rs index 226fdf1..467dcf7 100644 --- a/espanso/src/cli/worker/engine/executor/clipboard_injector.rs +++ b/espanso/src/cli/worker/engine/executor/clipboard_injector.rs @@ -17,54 +17,81 @@ * along with espanso. If not, see . */ -use std::path::PathBuf; +use std::{convert::TryInto, path::PathBuf}; -use espanso_inject::{InjectionOptions, Injector, keys::Key}; use espanso_clipboard::Clipboard; +use espanso_inject::{keys::Key, InjectionOptions, Injector}; -use crate::engine::{dispatch::HtmlInjector, dispatch::{ImageInjector, TextInjector}}; +use crate::engine::{ + dispatch::HtmlInjector, + dispatch::{ImageInjector, TextInjector}, +}; + +pub trait ClipboardParamsProvider { + fn get(&self) -> ClipboardParams; +} + +pub struct ClipboardParams { + pub pre_paste_delay: usize, + pub paste_shortcut_event_delay: usize, + pub paste_shortcut: Option, + pub disable_x11_fast_inject: bool, +} pub struct ClipboardInjectorAdapter<'a> { injector: &'a dyn Injector, clipboard: &'a dyn Clipboard, + params_provider: &'a dyn ClipboardParamsProvider, } -impl <'a> ClipboardInjectorAdapter<'a> { - pub fn new(injector: &'a dyn Injector, clipboard: &'a dyn Clipboard) -> Self { +impl<'a> ClipboardInjectorAdapter<'a> { + pub fn new( + injector: &'a dyn Injector, + clipboard: &'a dyn Clipboard, + params_provider: &'a dyn ClipboardParamsProvider, + ) -> Self { Self { injector, clipboard, + params_provider, } } fn send_paste_combination(&self) -> anyhow::Result<()> { - // TODO: handle delay duration - std::thread::sleep(std::time::Duration::from_millis(100)); + let params = self.params_provider.get(); + std::thread::sleep(std::time::Duration::from_millis( + params.pre_paste_delay.try_into().unwrap(), + )); + + // TODO: handle case of custom combination let combination = if cfg!(target_os = "macos") { &[Key::Meta, Key::V] } else { &[Key::Control, Key::V] - }; // TODO: handle case of custom combination + }; // TODO: handle user-specified delays - let paste_combination_delay = if cfg!(target_os = "macos") { - 5 - } else { - InjectionOptions::default().delay - }; + // let paste_combination_delay = if cfg!(target_os = "macos") { + // 5 + // } else { + // InjectionOptions::default().delay + // }; // TODO: handle options - self.injector.send_key_combination(combination, InjectionOptions { - delay: paste_combination_delay, - ..Default::default() - })?; + self.injector.send_key_combination( + combination, + InjectionOptions { + delay: params.paste_shortcut_event_delay as i32, + ..Default::default() + }, + )?; Ok(()) } } -impl <'a> TextInjector for ClipboardInjectorAdapter<'a> { +impl<'a> TextInjector for ClipboardInjectorAdapter<'a> { fn name(&self) -> &'static str { "clipboard" } @@ -79,7 +106,7 @@ impl <'a> TextInjector for ClipboardInjectorAdapter<'a> { } } -impl <'a> HtmlInjector for ClipboardInjectorAdapter<'a> { +impl<'a> HtmlInjector for ClipboardInjectorAdapter<'a> { fn inject_html(&self, html: &str, fallback_text: &str) -> anyhow::Result<()> { // TODO: handle clipboard restoration self.clipboard.set_html(html, Some(fallback_text))?; @@ -90,11 +117,17 @@ impl <'a> HtmlInjector for ClipboardInjectorAdapter<'a> { } } -impl <'a> ImageInjector for ClipboardInjectorAdapter<'a> { +impl<'a> ImageInjector for ClipboardInjectorAdapter<'a> { fn inject_image(&self, image_path: &str) -> anyhow::Result<()> { let path = PathBuf::from(image_path); if !path.is_file() { - return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "image can't be found in the given path").into()); + return Err( + std::io::Error::new( + std::io::ErrorKind::NotFound, + "image can't be found in the given path", + ) + .into(), + ); } // TODO: handle clipboard restoration diff --git a/espanso/src/cli/worker/engine/mod.rs b/espanso/src/cli/worker/engine/mod.rs index f88bd77..306c339 100644 --- a/espanso/src/cli/worker/engine/mod.rs +++ b/espanso/src/cli/worker/engine/mod.rs @@ -129,6 +129,7 @@ pub fn initialize_and_spawn( super::engine::executor::clipboard_injector::ClipboardInjectorAdapter::new( &*injector, &*clipboard, + &config_manager, ); let key_injector = super::engine::executor::key_injector::KeyInjectorAdapter::new(&*injector); let dispatcher = crate::engine::dispatch::default(