feat(core): add names to middlewares

This commit is contained in:
Federico Terzi 2021-04-16 21:01:58 +02:00
parent da6b68b09e
commit e532a377b1
6 changed files with 29 additions and 11 deletions

View File

@ -64,11 +64,11 @@ impl<'a> DefaultProcessor<'a> {
};
for middleware in self.middleware.iter() {
trace!("middleware received event: {:?}", current_event);
trace!("middleware '{}' received event: {:?}", middleware.name(), current_event);
current_event = middleware.next(current_event, &mut dispatch);
trace!("middleware produced event: {:?}", current_event);
trace!("middleware '{}' produced event: {:?}", middleware.name(), current_event);
}
while let Some(event) = current_queue.pop_back() {

View File

@ -17,10 +17,8 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use log::{debug, error};
use super::super::Middleware;
use crate::engine::{event::{Event, keyboard::{Key, KeySequenceInjectRequest}, matches::MatchSelectedEvent, text::{TextInjectMode, TextInjectRequest}}, process::{MatchFilter, MatchSelector, Multiplexer}};
use crate::engine::{event::{Event, keyboard::{Key, KeySequenceInjectRequest}, text::{TextInjectMode, TextInjectRequest}}, process::{MatchFilter, MatchSelector, Multiplexer}};
pub struct ActionMiddleware {
}
@ -32,19 +30,26 @@ impl ActionMiddleware {
}
impl Middleware for ActionMiddleware {
fn name(&self) -> &'static str {
"action"
}
fn next(&self, event: Event, dispatch: &mut dyn FnMut(Event)) -> Event {
if let Event::Rendered(m_event) = &event {
let delete_count = m_event.trigger.len();
let delete_sequence: Vec<_> = (0..delete_count).map(|_| Key::Backspace).collect();
dispatch(Event::TextInject(TextInjectRequest {
text: m_event.body.clone(),
force_mode: Some(TextInjectMode::Keys), // TODO: determine this one dynamically
}));
if let Some(cursor_hint_back_count) = m_event.cursor_hint_back_count {
dispatch(Event::KeySequenceInject(KeySequenceInjectRequest {
keys: (0..cursor_hint_back_count).map(|_| Key::ArrowLeft).collect(),
}))
}
// This is executed before the dispatched event
return Event::KeySequenceInject(KeySequenceInjectRequest {
keys: delete_sequence
keys: (0..m_event.trigger.chars().count()).map(|_| Key::Backspace).collect()
})
}

View File

@ -43,6 +43,10 @@ impl<'a> MatchSelectMiddleware<'a> {
}
impl<'a> Middleware for MatchSelectMiddleware<'a> {
fn name(&self) -> &'static str {
"match_select"
}
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
if let Event::MatchesDetected(m_event) = event {
let matches_ids: Vec<i32> = m_event.matches.iter().map(|m| m.id).collect();

View File

@ -48,6 +48,10 @@ impl<'a, State> MatchMiddleware<'a, State> {
}
impl<'a, State> Middleware for MatchMiddleware<'a, State> {
fn name(&self) -> &'static str {
"match"
}
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
if is_event_of_interest(&event) {
let mut matcher_states = self.matcher_states.borrow_mut();

View File

@ -17,10 +17,10 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use log::{debug, error};
use log::{error};
use super::super::Middleware;
use crate::engine::{event::{Event, text::{TextInjectRequest, TextInjectMode}, matches::MatchSelectedEvent}, process::{MatchFilter, MatchSelector, Multiplexer}};
use crate::engine::{event::Event, process::Multiplexer};
pub struct MultiplexMiddleware<'a> {
multiplexer: &'a dyn Multiplexer,
@ -33,6 +33,10 @@ impl<'a> MultiplexMiddleware<'a> {
}
impl<'a> Middleware for MultiplexMiddleware<'a> {
fn name(&self) -> &'static str {
"multiplex"
}
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
if let Event::MatchSelected(m_event) = event {
return match self.multiplexer.convert(m_event.chosen.id, m_event.chosen.trigger, m_event.chosen.args) {

View File

@ -26,6 +26,7 @@ mod default;
mod middleware;
pub trait Middleware {
fn name(&self) -> &'static str;
fn next(&self, event: Event, dispatch: &mut dyn FnMut(Event)) -> Event;
}