diff --git a/espanso-engine/src/process/default.rs b/espanso-engine/src/process/default.rs index 0fc652f..4bb8efd 100644 --- a/espanso-engine/src/process/default.rs +++ b/espanso-engine/src/process/default.rs @@ -34,7 +34,8 @@ use super::{ }, DisableOptions, EnabledStatusProvider, MatchFilter, MatchInfoProvider, MatchProvider, MatchResolver, MatchSelector, Matcher, MatcherMiddlewareConfigProvider, Middleware, - ModifierStateProvider, Multiplexer, PathProvider, Processor, Renderer, UndoEnabledProvider, + ModifierStateProvider, Multiplexer, NotificationManager, PathProvider, Processor, Renderer, + UndoEnabledProvider, }; use crate::{ event::{Event, EventType}, @@ -42,7 +43,8 @@ use crate::{ context_menu::ContextMenuMiddleware, disable::DisableMiddleware, exit::ExitMiddleware, hotkey::HotKeyMiddleware, icon_status::IconStatusMiddleware, image_resolve::ImageResolverMiddleware, match_exec::MatchExecRequestMiddleware, - search::SearchMiddleware, suppress::SuppressMiddleware, undo::UndoMiddleware, + notification::NotificationMiddleware, search::SearchMiddleware, suppress::SuppressMiddleware, + undo::UndoMiddleware, }, }; use std::collections::VecDeque; @@ -71,6 +73,7 @@ impl<'a> DefaultProcessor<'a> { enabled_status_provider: &'a dyn EnabledStatusProvider, modifier_state_provider: &'a dyn ModifierStateProvider, match_resolver: &'a dyn MatchResolver, + notification_manager: &'a dyn NotificationManager, ) -> DefaultProcessor<'a> { Self { event_queue: VecDeque::new(), @@ -105,6 +108,7 @@ impl<'a> DefaultProcessor<'a> { )), Box::new(SearchMiddleware::new(match_provider)), Box::new(MarkdownMiddleware::new()), + Box::new(NotificationMiddleware::new(notification_manager)), Box::new(DelayForModifierReleaseMiddleware::new( modifier_status_provider, )), diff --git a/espanso-engine/src/process/middleware/mod.rs b/espanso-engine/src/process/middleware/mod.rs index 5c72d6e..d42e5c5 100644 --- a/espanso-engine/src/process/middleware/mod.rs +++ b/espanso-engine/src/process/middleware/mod.rs @@ -33,6 +33,7 @@ pub mod match_exec; pub mod match_select; pub mod matcher; pub mod multiplex; +pub mod notification; pub mod render; pub mod search; pub mod suppress; diff --git a/espanso-engine/src/process/middleware/notification.rs b/espanso-engine/src/process/middleware/notification.rs new file mode 100644 index 0000000..31a94aa --- /dev/null +++ b/espanso-engine/src/process/middleware/notification.rs @@ -0,0 +1,55 @@ +/* + * 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 super::super::Middleware; +use crate::event::{Event, EventType}; + +pub trait NotificationManager { + fn notify_status_change(&self, enabled: bool); +} + +pub struct NotificationMiddleware<'a> { + notification_manager: &'a dyn NotificationManager, +} + +impl<'a> NotificationMiddleware<'a> { + pub fn new(notification_manager: &'a dyn NotificationManager) -> Self { + Self { + notification_manager, + } + } +} + +impl<'a> Middleware for NotificationMiddleware<'a> { + fn name(&self) -> &'static str { + "notification" + } + + fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event { + match &event.etype { + EventType::Enabled => self.notification_manager.notify_status_change(true), + EventType::Disabled => self.notification_manager.notify_status_change(false), + _ => {} + } + + event + } +} + +// TODO: test diff --git a/espanso-engine/src/process/mod.rs b/espanso-engine/src/process/mod.rs index eb42768..f55e520 100644 --- a/espanso-engine/src/process/mod.rs +++ b/espanso-engine/src/process/mod.rs @@ -44,6 +44,7 @@ pub use middleware::matcher::{ ModifierStateProvider, }; pub use middleware::multiplex::Multiplexer; +pub use middleware::notification::NotificationManager; pub use middleware::render::{Renderer, RendererError}; pub use middleware::search::MatchProvider; pub use middleware::suppress::EnabledStatusProvider; @@ -67,6 +68,7 @@ pub fn default<'a, MatcherState>( enabled_status_provider: &'a dyn EnabledStatusProvider, modifier_state_provider: &'a dyn ModifierStateProvider, match_resolver: &'a dyn MatchResolver, + notification_manager: &'a dyn NotificationManager, ) -> impl Processor + 'a { default::DefaultProcessor::new( matchers, @@ -85,5 +87,6 @@ pub fn default<'a, MatcherState>( enabled_status_provider, modifier_state_provider, match_resolver, + notification_manager, ) }