diff --git a/espanso-engine/src/dispatch/default.rs b/espanso-engine/src/dispatch/default.rs index bb14a77..146b2c2 100644 --- a/espanso-engine/src/dispatch/default.rs +++ b/espanso-engine/src/dispatch/default.rs @@ -17,7 +17,9 @@ * along with espanso. If not, see . */ -use super::{ContextMenuHandler, Event, IconHandler, ImageInjector, SecureInputManager}; +use super::{ + ContextMenuHandler, Event, IconHandler, ImageInjector, SecureInputManager, TextUIHandler, +}; use super::{Dispatcher, Executor, HtmlInjector, KeyInjector, ModeProvider, TextInjector}; pub struct DefaultDispatcher<'a> { @@ -36,6 +38,7 @@ impl<'a> DefaultDispatcher<'a> { context_menu_handler: &'a dyn ContextMenuHandler, icon_handler: &'a dyn IconHandler, secure_input_manager: &'a dyn SecureInputManager, + text_ui_handler: &'a dyn TextUIHandler, ) -> Self { Self { executors: vec![ @@ -62,6 +65,9 @@ impl<'a> DefaultDispatcher<'a> { Box::new(super::executor::secure_input::SecureInputExecutor::new( secure_input_manager, )), + Box::new(super::executor::text_ui::TextUIExecutor::new( + text_ui_handler, + )), ], } } diff --git a/espanso-engine/src/dispatch/executor/mod.rs b/espanso-engine/src/dispatch/executor/mod.rs index 31bb6d0..e9ac066 100644 --- a/espanso-engine/src/dispatch/executor/mod.rs +++ b/espanso-engine/src/dispatch/executor/mod.rs @@ -24,3 +24,4 @@ pub mod image_inject; pub mod key_inject; pub mod secure_input; pub mod text_inject; +pub mod text_ui; diff --git a/espanso-engine/src/dispatch/executor/text_ui.rs b/espanso-engine/src/dispatch/executor/text_ui.rs new file mode 100644 index 0000000..4e80900 --- /dev/null +++ b/espanso-engine/src/dispatch/executor/text_ui.rs @@ -0,0 +1,56 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2021 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +use crate::event::EventType; +use crate::{dispatch::Executor, event::Event}; +use anyhow::Result; +use log::error; + +pub trait TextUIHandler { + fn show_text(&self, title: &str, text: &str) -> Result<()>; +} + +pub struct TextUIExecutor<'a> { + handler: &'a dyn TextUIHandler, +} + +impl<'a> TextUIExecutor<'a> { + pub fn new(handler: &'a dyn TextUIHandler) -> Self { + Self { handler } + } +} + +impl<'a> Executor for TextUIExecutor<'a> { + fn execute(&self, event: &Event) -> bool { + if let EventType::ShowText(show_text_event) = &event.etype { + if let Err(error) = self + .handler + .show_text(&show_text_event.title, &show_text_event.text) + { + error!("text UI handler reported an error: {:?}", error); + } + + return true; + } + + false + } +} + +// TODO: test diff --git a/espanso-engine/src/dispatch/mod.rs b/espanso-engine/src/dispatch/mod.rs index d2959ff..753fffc 100644 --- a/espanso-engine/src/dispatch/mod.rs +++ b/espanso-engine/src/dispatch/mod.rs @@ -38,6 +38,7 @@ pub use executor::image_inject::ImageInjector; pub use executor::key_inject::KeyInjector; pub use executor::secure_input::SecureInputManager; pub use executor::text_inject::{Mode, ModeProvider, TextInjector}; +pub use executor::text_ui::{TextUIExecutor, TextUIHandler}; #[allow(clippy::too_many_arguments)] pub fn default<'a>( @@ -50,6 +51,7 @@ pub fn default<'a>( context_menu_handler: &'a dyn ContextMenuHandler, icon_handler: &'a dyn IconHandler, secure_input_manager: &'a dyn SecureInputManager, + text_ui_handler: &'a dyn TextUIHandler, ) -> impl Dispatcher + 'a { default::DefaultDispatcher::new( event_injector, @@ -61,5 +63,6 @@ pub fn default<'a>( context_menu_handler, icon_handler, secure_input_manager, + text_ui_handler, ) } diff --git a/espanso-engine/src/event/mod.rs b/espanso-engine/src/event/mod.rs index 2042588..7e6551c 100644 --- a/espanso-engine/src/event/mod.rs +++ b/espanso-engine/src/event/mod.rs @@ -101,6 +101,7 @@ pub enum EventType { IconStatusChange(ui::IconStatusChangeEvent), DisplaySecureInputTroubleshoot, ShowSearchBar, + ShowText(ui::ShowTextEvent), // Other LaunchSecureInputAutoFix, diff --git a/espanso-engine/src/event/ui.rs b/espanso-engine/src/event/ui.rs index b172c45..f23bbb9 100644 --- a/espanso-engine/src/event/ui.rs +++ b/espanso-engine/src/event/ui.rs @@ -52,3 +52,9 @@ pub enum IconStatus { Disabled, SecureInputDisabled, } + +#[derive(Debug, Clone, PartialEq)] +pub struct ShowTextEvent { + pub title: String, + pub text: String, +}