feat(core): improve clipboard_injector parameter handling

This commit is contained in:
Federico Terzi 2021-05-08 14:57:45 +02:00
parent 139fa7e511
commit 46b9382fef
3 changed files with 71 additions and 23 deletions

View File

@ -106,11 +106,25 @@ impl<'a> ModeProvider for ConfigManager<'a> {
fn active_mode(&self) -> crate::engine::dispatch::Mode { fn active_mode(&self) -> crate::engine::dispatch::Mode {
let config = self.active(); let config = self.active();
match config.backend() { match config.backend() {
espanso_config::config::Backend::Inject => crate::engine::dispatch::Mode::Event, espanso_config::config::Backend::Inject => crate::engine::dispatch::Mode::Event,
espanso_config::config::Backend::Clipboard => crate::engine::dispatch::Mode::Clipboard, espanso_config::config::Backend::Clipboard => crate::engine::dispatch::Mode::Clipboard,
espanso_config::config::Backend::Auto => crate::engine::dispatch::Mode::Auto { espanso_config::config::Backend::Auto => crate::engine::dispatch::Mode::Auto {
clipboard_threshold: config.clipboard_threshold(), 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
}
}
}

View File

@ -17,54 +17,81 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::path::PathBuf; use std::{convert::TryInto, path::PathBuf};
use espanso_inject::{InjectionOptions, Injector, keys::Key};
use espanso_clipboard::Clipboard; 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<String>,
pub disable_x11_fast_inject: bool,
}
pub struct ClipboardInjectorAdapter<'a> { pub struct ClipboardInjectorAdapter<'a> {
injector: &'a dyn Injector, injector: &'a dyn Injector,
clipboard: &'a dyn Clipboard, clipboard: &'a dyn Clipboard,
params_provider: &'a dyn ClipboardParamsProvider,
} }
impl <'a> ClipboardInjectorAdapter<'a> { impl<'a> ClipboardInjectorAdapter<'a> {
pub fn new(injector: &'a dyn Injector, clipboard: &'a dyn Clipboard) -> Self { pub fn new(
injector: &'a dyn Injector,
clipboard: &'a dyn Clipboard,
params_provider: &'a dyn ClipboardParamsProvider,
) -> Self {
Self { Self {
injector, injector,
clipboard, clipboard,
params_provider,
} }
} }
fn send_paste_combination(&self) -> anyhow::Result<()> { fn send_paste_combination(&self) -> anyhow::Result<()> {
// TODO: handle delay duration let params = self.params_provider.get();
std::thread::sleep(std::time::Duration::from_millis(100));
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") { let combination = if cfg!(target_os = "macos") {
&[Key::Meta, Key::V] &[Key::Meta, Key::V]
} else { } else {
&[Key::Control, Key::V] &[Key::Control, Key::V]
}; // TODO: handle case of custom combination };
// TODO: handle user-specified delays // TODO: handle user-specified delays
let paste_combination_delay = if cfg!(target_os = "macos") { // let paste_combination_delay = if cfg!(target_os = "macos") {
5 // 5
} else { // } else {
InjectionOptions::default().delay // InjectionOptions::default().delay
}; // };
// TODO: handle options // TODO: handle options
self.injector.send_key_combination(combination, InjectionOptions { self.injector.send_key_combination(
delay: paste_combination_delay, combination,
..Default::default() InjectionOptions {
})?; delay: params.paste_shortcut_event_delay as i32,
..Default::default()
},
)?;
Ok(()) Ok(())
} }
} }
impl <'a> TextInjector for ClipboardInjectorAdapter<'a> { impl<'a> TextInjector for ClipboardInjectorAdapter<'a> {
fn name(&self) -> &'static str { fn name(&self) -> &'static str {
"clipboard" "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<()> { fn inject_html(&self, html: &str, fallback_text: &str) -> anyhow::Result<()> {
// TODO: handle clipboard restoration // TODO: handle clipboard restoration
self.clipboard.set_html(html, Some(fallback_text))?; 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<()> { fn inject_image(&self, image_path: &str) -> anyhow::Result<()> {
let path = PathBuf::from(image_path); let path = PathBuf::from(image_path);
if !path.is_file() { 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 // TODO: handle clipboard restoration

View File

@ -129,6 +129,7 @@ pub fn initialize_and_spawn(
super::engine::executor::clipboard_injector::ClipboardInjectorAdapter::new( super::engine::executor::clipboard_injector::ClipboardInjectorAdapter::new(
&*injector, &*injector,
&*clipboard, &*clipboard,
&config_manager,
); );
let key_injector = super::engine::executor::key_injector::KeyInjectorAdapter::new(&*injector); let key_injector = super::engine::executor::key_injector::KeyInjectorAdapter::new(&*injector);
let dispatcher = crate::engine::dispatch::default( let dispatcher = crate::engine::dispatch::default(