feat(engine): implement ShowText event

This commit is contained in:
Federico Terzi 2021-11-15 22:24:30 +01:00
parent b2452ecca7
commit 84fd39a952
6 changed files with 74 additions and 1 deletions

View File

@ -17,7 +17,9 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use super::{ContextMenuHandler, Event, IconHandler, ImageInjector, SecureInputManager}; use super::{
ContextMenuHandler, Event, IconHandler, ImageInjector, SecureInputManager, TextUIHandler,
};
use super::{Dispatcher, Executor, HtmlInjector, KeyInjector, ModeProvider, TextInjector}; use super::{Dispatcher, Executor, HtmlInjector, KeyInjector, ModeProvider, TextInjector};
pub struct DefaultDispatcher<'a> { pub struct DefaultDispatcher<'a> {
@ -36,6 +38,7 @@ impl<'a> DefaultDispatcher<'a> {
context_menu_handler: &'a dyn ContextMenuHandler, context_menu_handler: &'a dyn ContextMenuHandler,
icon_handler: &'a dyn IconHandler, icon_handler: &'a dyn IconHandler,
secure_input_manager: &'a dyn SecureInputManager, secure_input_manager: &'a dyn SecureInputManager,
text_ui_handler: &'a dyn TextUIHandler,
) -> Self { ) -> Self {
Self { Self {
executors: vec![ executors: vec![
@ -62,6 +65,9 @@ impl<'a> DefaultDispatcher<'a> {
Box::new(super::executor::secure_input::SecureInputExecutor::new( Box::new(super::executor::secure_input::SecureInputExecutor::new(
secure_input_manager, secure_input_manager,
)), )),
Box::new(super::executor::text_ui::TextUIExecutor::new(
text_ui_handler,
)),
], ],
} }
} }

View File

@ -24,3 +24,4 @@ pub mod image_inject;
pub mod key_inject; pub mod key_inject;
pub mod secure_input; pub mod secure_input;
pub mod text_inject; pub mod text_inject;
pub mod text_ui;

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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

View File

@ -38,6 +38,7 @@ pub use executor::image_inject::ImageInjector;
pub use executor::key_inject::KeyInjector; pub use executor::key_inject::KeyInjector;
pub use executor::secure_input::SecureInputManager; pub use executor::secure_input::SecureInputManager;
pub use executor::text_inject::{Mode, ModeProvider, TextInjector}; pub use executor::text_inject::{Mode, ModeProvider, TextInjector};
pub use executor::text_ui::{TextUIExecutor, TextUIHandler};
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn default<'a>( pub fn default<'a>(
@ -50,6 +51,7 @@ pub fn default<'a>(
context_menu_handler: &'a dyn ContextMenuHandler, context_menu_handler: &'a dyn ContextMenuHandler,
icon_handler: &'a dyn IconHandler, icon_handler: &'a dyn IconHandler,
secure_input_manager: &'a dyn SecureInputManager, secure_input_manager: &'a dyn SecureInputManager,
text_ui_handler: &'a dyn TextUIHandler,
) -> impl Dispatcher + 'a { ) -> impl Dispatcher + 'a {
default::DefaultDispatcher::new( default::DefaultDispatcher::new(
event_injector, event_injector,
@ -61,5 +63,6 @@ pub fn default<'a>(
context_menu_handler, context_menu_handler,
icon_handler, icon_handler,
secure_input_manager, secure_input_manager,
text_ui_handler,
) )
} }

View File

@ -101,6 +101,7 @@ pub enum EventType {
IconStatusChange(ui::IconStatusChangeEvent), IconStatusChange(ui::IconStatusChangeEvent),
DisplaySecureInputTroubleshoot, DisplaySecureInputTroubleshoot,
ShowSearchBar, ShowSearchBar,
ShowText(ui::ShowTextEvent),
// Other // Other
LaunchSecureInputAutoFix, LaunchSecureInputAutoFix,

View File

@ -52,3 +52,9 @@ pub enum IconStatus {
Disabled, Disabled,
SecureInputDisabled, SecureInputDisabled,
} }
#[derive(Debug, Clone, PartialEq)]
pub struct ShowTextEvent {
pub title: String,
pub text: String,
}