feat(core): wire up delay options to injectors

This commit is contained in:
Federico Terzi 2021-06-07 21:16:21 +02:00
parent 9efc7cfa0a
commit 136b4791df
5 changed files with 72 additions and 21 deletions

View File

@ -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(),
}
}
}

View File

@ -17,18 +17,24 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use espanso_inject::Injector; use std::convert::TryInto;
use espanso_inject::{InjectionOptions, Injector};
use crate::engine::dispatch::TextInjector; use crate::engine::dispatch::TextInjector;
use super::InjectParamsProvider;
pub struct EventInjectorAdapter<'a> { pub struct EventInjectorAdapter<'a> {
injector: &'a dyn Injector, injector: &'a dyn Injector,
params_provider: &'a dyn InjectParamsProvider,
} }
impl<'a> EventInjectorAdapter<'a> { impl<'a> EventInjectorAdapter<'a> {
pub fn new(injector: &'a dyn Injector) -> Self { pub fn new(injector: &'a dyn Injector, params_provider: &'a dyn InjectParamsProvider) -> Self {
Self { Self {
injector injector,
params_provider,
} }
} }
} }
@ -39,23 +45,30 @@ impl <'a> TextInjector for EventInjectorAdapter<'a> {
} }
fn inject_text(&self, text: &str) -> anyhow::Result<()> { fn inject_text(&self, text: &str) -> anyhow::Result<()> {
let params = self.params_provider.get();
// Handle CRLF or LF line endings correctly // Handle CRLF or LF line endings correctly
let split_sequence = if text.contains("\r\n") { let split_sequence = if text.contains("\r\n") { "\r\n" } else { "\n" };
"\r\n"
} else { let injection_options = InjectionOptions {
"\n" 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. // 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() { for (i, line) in text.split(split_sequence).enumerate() {
// We simulate an Return press between lines // We simulate an Return press between lines
if i > 0 { if i > 0 {
// TODO: handle injection options self
self.injector.send_keys(&[espanso_inject::keys::Key::Enter], Default::default())? .injector
.send_keys(&[espanso_inject::keys::Key::Enter], injection_options)?
} }
// TODO: handle injection options self.injector.send_string(line, injection_options)?;
self.injector.send_string(line, Default::default())?;
} }
Ok(()) Ok(())

View File

@ -17,24 +17,39 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use espanso_inject::Injector; use std::convert::TryInto;
use espanso_inject::{InjectionOptions, Injector};
use crate::engine::dispatch::KeyInjector; use crate::engine::dispatch::KeyInjector;
use super::InjectParamsProvider;
pub struct KeyInjectorAdapter<'a> { pub struct KeyInjectorAdapter<'a> {
injector: &'a dyn Injector, injector: &'a dyn Injector,
params_provider: &'a dyn InjectParamsProvider,
} }
impl<'a> KeyInjectorAdapter<'a> { impl<'a> KeyInjectorAdapter<'a> {
pub fn new(injector: &'a dyn Injector) -> Self { pub fn new(injector: &'a dyn Injector, params_provider: &'a dyn InjectParamsProvider) -> Self {
Self { injector } Self { injector, params_provider }
} }
} }
impl<'a> KeyInjector for KeyInjectorAdapter<'a> { impl<'a> KeyInjector for KeyInjectorAdapter<'a> {
fn inject_sequence(&self, keys: &[crate::engine::event::input::Key]) -> anyhow::Result<()> { 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(); 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)
} }
} }

View File

@ -22,3 +22,13 @@ pub mod context_menu;
pub mod event_injector; pub mod event_injector;
pub mod icon; pub mod icon;
pub mod key_injector; pub mod key_injector;
pub trait InjectParamsProvider {
fn get(&self) -> InjectParams;
}
pub struct InjectParams {
pub inject_delay: Option<usize>,
pub key_delay: Option<usize>,
pub disable_x11_fast_inject: bool,
}

View File

@ -156,10 +156,10 @@ pub fn initialize_and_spawn(
disable_options, disable_options,
); );
let event_injector = EventInjectorAdapter::new(&*injector); let event_injector = EventInjectorAdapter::new(&*injector, &config_manager);
let clipboard_injector = let clipboard_injector =
ClipboardInjectorAdapter::new(&*injector, &*clipboard, &config_manager); 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 context_menu_adapter = ContextMenuHandlerAdapter::new(&*ui_remote);
let icon_adapter = IconHandlerAdapter::new(&*ui_remote); let icon_adapter = IconHandlerAdapter::new(&*ui_remote);
let dispatcher = crate::engine::dispatch::default( let dispatcher = crate::engine::dispatch::default(