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,45 +17,58 @@
* 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 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(())

View File

@ -17,24 +17,39 @@
* 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 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)
}
}

View File

@ -21,4 +21,14 @@ pub mod clipboard_injector;
pub mod context_menu;
pub mod event_injector;
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,
);
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(