diff --git a/espanso-engine/src/process/default.rs b/espanso-engine/src/process/default.rs index 36670b8..9596c78 100644 --- a/espanso-engine/src/process/default.rs +++ b/espanso-engine/src/process/default.rs @@ -19,7 +19,7 @@ use log::trace; -use super::{DisableOptions, MatchFilter, MatchInfoProvider, MatchProvider, MatchSelector, Matcher, MatcherMiddlewareConfigProvider, Middleware, Multiplexer, PathProvider, Processor, Renderer, UndoEnabledProvider, middleware::{ +use super::{DisableOptions, EnabledStatusProvider, MatchFilter, MatchInfoProvider, MatchProvider, MatchSelector, Matcher, MatcherMiddlewareConfigProvider, Middleware, Multiplexer, PathProvider, Processor, Renderer, UndoEnabledProvider, middleware::{ action::{ActionMiddleware, EventSequenceProvider}, cause::CauseCompensateMiddleware, cursor_hint::CursorHintMiddleware, @@ -31,7 +31,7 @@ use super::{DisableOptions, MatchFilter, MatchInfoProvider, MatchProvider, Match past_discard::PastEventsDiscardMiddleware, render::RenderMiddleware, }}; -use crate::{event::{Event, EventType}, process::middleware::{context_menu::ContextMenuMiddleware, disable::DisableMiddleware, exit::ExitMiddleware, hotkey::HotKeyMiddleware, icon_status::IconStatusMiddleware, image_resolve::ImageResolverMiddleware, search::SearchMiddleware, undo::UndoMiddleware}}; +use crate::{event::{Event, EventType}, process::middleware::{context_menu::ContextMenuMiddleware, disable::DisableMiddleware, exit::ExitMiddleware, hotkey::HotKeyMiddleware, icon_status::IconStatusMiddleware, image_resolve::ImageResolverMiddleware, search::SearchMiddleware, suppress::SuppressMiddleware, undo::UndoMiddleware}}; use std::collections::VecDeque; pub struct DefaultProcessor<'a> { @@ -55,6 +55,7 @@ impl<'a> DefaultProcessor<'a> { matcher_options_provider: &'a dyn MatcherMiddlewareConfigProvider, match_provider: &'a dyn MatchProvider, undo_enabled_provider: &'a dyn UndoEnabledProvider, + enabled_status_provider: &'a dyn EnabledStatusProvider, ) -> DefaultProcessor<'a> { Self { event_queue: VecDeque::new(), @@ -63,6 +64,7 @@ impl<'a> DefaultProcessor<'a> { Box::new(DisableMiddleware::new(disable_options)), Box::new(IconStatusMiddleware::new()), Box::new(MatcherMiddleware::new(matchers, matcher_options_provider)), + Box::new(SuppressMiddleware::new(enabled_status_provider)), Box::new(ContextMenuMiddleware::new()), Box::new(HotKeyMiddleware::new()), Box::new(MatchSelectMiddleware::new(match_filter, match_selector)), diff --git a/espanso-engine/src/process/middleware/mod.rs b/espanso-engine/src/process/middleware/mod.rs index 3a7e3eb..ec2bcc7 100644 --- a/espanso-engine/src/process/middleware/mod.rs +++ b/espanso-engine/src/process/middleware/mod.rs @@ -34,4 +34,5 @@ pub mod multiplex; pub mod past_discard; pub mod render; pub mod search; +pub mod suppress; pub mod undo; diff --git a/espanso-engine/src/process/middleware/suppress.rs b/espanso-engine/src/process/middleware/suppress.rs new file mode 100644 index 0000000..3adc0db --- /dev/null +++ b/espanso-engine/src/process/middleware/suppress.rs @@ -0,0 +1,54 @@ +/* + * 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 log::trace; + +use super::super::Middleware; +use crate::event::{Event, EventType}; + +pub trait EnabledStatusProvider { + fn is_config_enabled(&self) -> bool; +} + +pub struct SuppressMiddleware<'a> { + provider: &'a dyn EnabledStatusProvider, +} + +impl<'a> SuppressMiddleware<'a> { + pub fn new(provider: &'a dyn EnabledStatusProvider) -> Self { + Self { provider } + } +} + +impl<'a> Middleware for SuppressMiddleware<'a> { + fn name(&self) -> &'static str { + "suppress" + } + + fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event { + if let EventType::MatchesDetected(_) = event.etype { + if !self.provider.is_config_enabled() { + trace!("suppressing match detected event as active config has enable=false"); + return Event::caused_by(event.source_id, EventType::NOOP); + } + } + + event + } +} \ No newline at end of file diff --git a/espanso-engine/src/process/mod.rs b/espanso-engine/src/process/mod.rs index 2d30cc7..91bce0d 100644 --- a/espanso-engine/src/process/mod.rs +++ b/espanso-engine/src/process/mod.rs @@ -44,6 +44,7 @@ pub use middleware::matcher::{ pub use middleware::multiplex::Multiplexer; pub use middleware::render::{Renderer, RendererError}; pub use middleware::search::MatchProvider; +pub use middleware::suppress::EnabledStatusProvider; pub use middleware::undo::UndoEnabledProvider; #[allow(clippy::too_many_arguments)] @@ -61,6 +62,7 @@ pub fn default<'a, MatcherState>( matcher_options_provider: &'a dyn MatcherMiddlewareConfigProvider, match_provider: &'a dyn MatchProvider, undo_enabled_provider: &'a dyn UndoEnabledProvider, + enabled_status_provider: &'a dyn EnabledStatusProvider, ) -> impl Processor + 'a { default::DefaultProcessor::new( matchers, @@ -76,5 +78,6 @@ pub fn default<'a, MatcherState>( matcher_options_provider, match_provider, undo_enabled_provider, + enabled_status_provider, ) }