feat(core): add names to middlewares
This commit is contained in:
parent
da6b68b09e
commit
e532a377b1
|
@ -64,11 +64,11 @@ impl<'a> DefaultProcessor<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
for middleware in self.middleware.iter() {
|
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);
|
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() {
|
while let Some(event) = current_queue.pop_back() {
|
||||||
|
|
|
@ -17,10 +17,8 @@
|
||||||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use log::{debug, error};
|
|
||||||
|
|
||||||
use super::super::Middleware;
|
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 {
|
pub struct ActionMiddleware {
|
||||||
}
|
}
|
||||||
|
@ -32,19 +30,26 @@ impl ActionMiddleware {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Middleware for ActionMiddleware {
|
impl Middleware for ActionMiddleware {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"action"
|
||||||
|
}
|
||||||
|
|
||||||
fn next(&self, event: Event, dispatch: &mut dyn FnMut(Event)) -> Event {
|
fn next(&self, event: Event, dispatch: &mut dyn FnMut(Event)) -> Event {
|
||||||
if let Event::Rendered(m_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 {
|
dispatch(Event::TextInject(TextInjectRequest {
|
||||||
text: m_event.body.clone(),
|
text: m_event.body.clone(),
|
||||||
force_mode: Some(TextInjectMode::Keys), // TODO: determine this one dynamically
|
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
|
// This is executed before the dispatched event
|
||||||
return Event::KeySequenceInject(KeySequenceInjectRequest {
|
return Event::KeySequenceInject(KeySequenceInjectRequest {
|
||||||
keys: delete_sequence
|
keys: (0..m_event.trigger.chars().count()).map(|_| Key::Backspace).collect()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,10 @@ impl<'a> MatchSelectMiddleware<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Middleware for 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 {
|
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
|
||||||
if let Event::MatchesDetected(m_event) = event {
|
if let Event::MatchesDetected(m_event) = event {
|
||||||
let matches_ids: Vec<i32> = m_event.matches.iter().map(|m| m.id).collect();
|
let matches_ids: Vec<i32> = m_event.matches.iter().map(|m| m.id).collect();
|
||||||
|
|
|
@ -48,6 +48,10 @@ impl<'a, State> MatchMiddleware<'a, State> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, State> Middleware for 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 {
|
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
|
||||||
if is_event_of_interest(&event) {
|
if is_event_of_interest(&event) {
|
||||||
let mut matcher_states = self.matcher_states.borrow_mut();
|
let mut matcher_states = self.matcher_states.borrow_mut();
|
||||||
|
|
|
@ -17,10 +17,10 @@
|
||||||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use log::{debug, error};
|
use log::{error};
|
||||||
|
|
||||||
use super::super::Middleware;
|
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> {
|
pub struct MultiplexMiddleware<'a> {
|
||||||
multiplexer: &'a dyn Multiplexer,
|
multiplexer: &'a dyn Multiplexer,
|
||||||
|
@ -33,6 +33,10 @@ impl<'a> MultiplexMiddleware<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Middleware for MultiplexMiddleware<'a> {
|
impl<'a> Middleware for MultiplexMiddleware<'a> {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"multiplex"
|
||||||
|
}
|
||||||
|
|
||||||
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
|
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
|
||||||
if let Event::MatchSelected(m_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) {
|
return match self.multiplexer.convert(m_event.chosen.id, m_event.chosen.trigger, m_event.chosen.args) {
|
||||||
|
|
|
@ -26,6 +26,7 @@ mod default;
|
||||||
mod middleware;
|
mod middleware;
|
||||||
|
|
||||||
pub trait Middleware {
|
pub trait Middleware {
|
||||||
|
fn name(&self) -> &'static str;
|
||||||
fn next(&self, event: Event, dispatch: &mut dyn FnMut(Event)) -> Event;
|
fn next(&self, event: Event, dispatch: &mut dyn FnMut(Event)) -> Event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user