feat(engine): implement notification for enabled/disabled on linux. Fix #798

This commit is contained in:
Federico Terzi 2021-10-31 21:07:58 +01:00
parent 1bad4b2bfb
commit a806e180e1
4 changed files with 65 additions and 2 deletions

View File

@ -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,
)),

View File

@ -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;

View File

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

View File

@ -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,
)
}