2019-08-31 14:07:45 +00:00
|
|
|
use crate::matcher::{Match, MatchReceiver};
|
|
|
|
use crate::keyboard::KeyboardSender;
|
2019-09-07 14:13:13 +00:00
|
|
|
use crate::config::ConfigManager;
|
2019-09-07 08:43:23 +00:00
|
|
|
use crate::config::BackendType;
|
2019-09-06 22:38:13 +00:00
|
|
|
use crate::clipboard::ClipboardManager;
|
2019-09-08 11:37:58 +00:00
|
|
|
use log::{info};
|
|
|
|
use crate::ui::UIManager;
|
2019-08-31 14:07:45 +00:00
|
|
|
|
2019-09-08 11:37:58 +00:00
|
|
|
pub struct Engine<'a, S: KeyboardSender, C: ClipboardManager, M: ConfigManager,
|
|
|
|
U: UIManager> {
|
2019-09-05 18:54:19 +00:00
|
|
|
sender: S,
|
2019-09-07 14:13:13 +00:00
|
|
|
clipboard_manager: &'a C,
|
|
|
|
config_manager: &'a M,
|
2019-09-08 11:37:58 +00:00
|
|
|
ui_manager: &'a U,
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 11:37:58 +00:00
|
|
|
impl <'a, S: KeyboardSender, C: ClipboardManager, M: ConfigManager, U: UIManager>
|
|
|
|
Engine<'a, S, C, M, U> {
|
|
|
|
pub fn new<'b>(sender: S, clipboard_manager: &'b C, config_manager: &'b M, ui_manager: &'b U) -> Engine<'b, S, C, M, U> {
|
|
|
|
Engine{sender, clipboard_manager, config_manager, ui_manager }
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 11:37:58 +00:00
|
|
|
impl <'a, S: KeyboardSender, C: ClipboardManager, M: ConfigManager, U: UIManager>
|
|
|
|
MatchReceiver for Engine<'a, S, C, M, U>{
|
|
|
|
|
2019-08-31 15:00:23 +00:00
|
|
|
fn on_match(&self, m: &Match) {
|
2019-09-09 15:13:58 +00:00
|
|
|
let config = self.config_manager.active_config();
|
|
|
|
|
|
|
|
if config.disabled {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-07 15:59:34 +00:00
|
|
|
|
2019-08-31 14:07:45 +00:00
|
|
|
self.sender.delete_string(m.trigger.len() as i32);
|
2019-09-06 08:21:33 +00:00
|
|
|
|
2019-09-07 15:59:34 +00:00
|
|
|
match config.backend {
|
2019-09-07 08:43:23 +00:00
|
|
|
BackendType::Inject => {
|
|
|
|
// Send the expected string. On linux, newlines are managed automatically
|
|
|
|
// while on windows and macos, we need to emulate a Enter key press.
|
2019-09-06 08:21:33 +00:00
|
|
|
|
2019-09-07 08:43:23 +00:00
|
|
|
if cfg!(target_os = "linux") {
|
|
|
|
self.sender.send_string(m.replace.as_str());
|
|
|
|
}else{
|
|
|
|
// To handle newlines, substitute each "\n" char with an Enter key press.
|
|
|
|
let splits = m.replace.lines();
|
2019-09-06 20:19:28 +00:00
|
|
|
|
2019-09-07 08:43:23 +00:00
|
|
|
for (i, split) in splits.enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
self.sender.send_enter();
|
|
|
|
}
|
2019-09-06 08:21:33 +00:00
|
|
|
|
2019-09-07 08:43:23 +00:00
|
|
|
self.sender.send_string(split);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BackendType::Clipboard => {
|
|
|
|
self.clipboard_manager.set_clipboard(m.replace.as_str());
|
|
|
|
self.sender.trigger_paste();
|
|
|
|
},
|
2019-09-06 08:21:33 +00:00
|
|
|
}
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
2019-09-08 11:37:58 +00:00
|
|
|
|
|
|
|
fn on_toggle(&self, status: bool) {
|
|
|
|
let message = if status {
|
|
|
|
"espanso enabled"
|
|
|
|
}else{
|
|
|
|
"espanso disabled"
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("Toggled: {}", message);
|
|
|
|
|
|
|
|
self.ui_manager.notify(message);
|
|
|
|
}
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|