feat(engine): implement suppress middleware

This commit is contained in:
Federico Terzi 2021-08-22 20:14:21 +02:00
parent 09109e0d57
commit c624ca676d
4 changed files with 62 additions and 2 deletions

View File

@ -19,7 +19,7 @@
use log::trace; 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}, action::{ActionMiddleware, EventSequenceProvider},
cause::CauseCompensateMiddleware, cause::CauseCompensateMiddleware,
cursor_hint::CursorHintMiddleware, cursor_hint::CursorHintMiddleware,
@ -31,7 +31,7 @@ use super::{DisableOptions, MatchFilter, MatchInfoProvider, MatchProvider, Match
past_discard::PastEventsDiscardMiddleware, past_discard::PastEventsDiscardMiddleware,
render::RenderMiddleware, 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; use std::collections::VecDeque;
pub struct DefaultProcessor<'a> { pub struct DefaultProcessor<'a> {
@ -55,6 +55,7 @@ impl<'a> DefaultProcessor<'a> {
matcher_options_provider: &'a dyn MatcherMiddlewareConfigProvider, matcher_options_provider: &'a dyn MatcherMiddlewareConfigProvider,
match_provider: &'a dyn MatchProvider, match_provider: &'a dyn MatchProvider,
undo_enabled_provider: &'a dyn UndoEnabledProvider, undo_enabled_provider: &'a dyn UndoEnabledProvider,
enabled_status_provider: &'a dyn EnabledStatusProvider,
) -> DefaultProcessor<'a> { ) -> DefaultProcessor<'a> {
Self { Self {
event_queue: VecDeque::new(), event_queue: VecDeque::new(),
@ -63,6 +64,7 @@ impl<'a> DefaultProcessor<'a> {
Box::new(DisableMiddleware::new(disable_options)), Box::new(DisableMiddleware::new(disable_options)),
Box::new(IconStatusMiddleware::new()), Box::new(IconStatusMiddleware::new()),
Box::new(MatcherMiddleware::new(matchers, matcher_options_provider)), Box::new(MatcherMiddleware::new(matchers, matcher_options_provider)),
Box::new(SuppressMiddleware::new(enabled_status_provider)),
Box::new(ContextMenuMiddleware::new()), Box::new(ContextMenuMiddleware::new()),
Box::new(HotKeyMiddleware::new()), Box::new(HotKeyMiddleware::new()),
Box::new(MatchSelectMiddleware::new(match_filter, match_selector)), Box::new(MatchSelectMiddleware::new(match_filter, match_selector)),

View File

@ -34,4 +34,5 @@ pub mod multiplex;
pub mod past_discard; pub mod past_discard;
pub mod render; pub mod render;
pub mod search; pub mod search;
pub mod suppress;
pub mod undo; pub mod undo;

View File

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

View File

@ -44,6 +44,7 @@ pub use middleware::matcher::{
pub use middleware::multiplex::Multiplexer; pub use middleware::multiplex::Multiplexer;
pub use middleware::render::{Renderer, RendererError}; pub use middleware::render::{Renderer, RendererError};
pub use middleware::search::MatchProvider; pub use middleware::search::MatchProvider;
pub use middleware::suppress::EnabledStatusProvider;
pub use middleware::undo::UndoEnabledProvider; pub use middleware::undo::UndoEnabledProvider;
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -61,6 +62,7 @@ pub fn default<'a, MatcherState>(
matcher_options_provider: &'a dyn MatcherMiddlewareConfigProvider, matcher_options_provider: &'a dyn MatcherMiddlewareConfigProvider,
match_provider: &'a dyn MatchProvider, match_provider: &'a dyn MatchProvider,
undo_enabled_provider: &'a dyn UndoEnabledProvider, undo_enabled_provider: &'a dyn UndoEnabledProvider,
enabled_status_provider: &'a dyn EnabledStatusProvider,
) -> impl Processor + 'a { ) -> impl Processor + 'a {
default::DefaultProcessor::new( default::DefaultProcessor::new(
matchers, matchers,
@ -76,5 +78,6 @@ pub fn default<'a, MatcherState>(
matcher_options_provider, matcher_options_provider,
match_provider, match_provider,
undo_enabled_provider, undo_enabled_provider,
enabled_status_provider,
) )
} }