style(core): refactor worker module structure

This commit is contained in:
Federico Terzi 2021-05-23 17:36:06 +02:00
parent 2ad8dfa8a5
commit 023ac615ba
29 changed files with 99 additions and 68 deletions

View File

@ -17,11 +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 std::{ use std::collections::HashSet;
collections::{HashSet},
};
use crate::engine::{dispatch::ModeProvider, process::MatchFilter};
use espanso_config::{ use espanso_config::{
config::{AppProperties, Config, ConfigStore}, config::{AppProperties, Config, ConfigStore},
matches::store::{MatchSet, MatchStore}, matches::store::{MatchSet, MatchStore},
@ -29,8 +26,6 @@ use espanso_config::{
use espanso_info::{AppInfo, AppInfoProvider}; use espanso_info::{AppInfo, AppInfoProvider};
use std::iter::FromIterator; use std::iter::FromIterator;
use super::engine::render::ConfigProvider;
pub struct ConfigManager<'a> { pub struct ConfigManager<'a> {
config_store: &'a dyn ConfigStore, config_store: &'a dyn ConfigStore,
match_store: &'a dyn MatchStore, match_store: &'a dyn MatchStore,
@ -76,7 +71,7 @@ fn to_app_properties(info: &AppInfo) -> AppProperties {
} }
} }
impl<'a> MatchFilter for ConfigManager<'a> { impl<'a> crate::engine::process::MatchFilter for ConfigManager<'a> {
fn filter_active(&self, matches_ids: &[i32]) -> Vec<i32> { fn filter_active(&self, matches_ids: &[i32]) -> Vec<i32> {
let ids_set: HashSet<i32> = HashSet::from_iter(matches_ids.iter().copied()); let ids_set: HashSet<i32> = HashSet::from_iter(matches_ids.iter().copied());
let (_, match_set) = self.active_context(); let (_, match_set) = self.active_context();
@ -90,7 +85,7 @@ impl<'a> MatchFilter for ConfigManager<'a> {
} }
} }
impl<'a> ConfigProvider<'a> for ConfigManager<'a> { impl<'a> super::engine::process::middleware::render::ConfigProvider<'a> for ConfigManager<'a> {
fn configs(&self) -> Vec<(&'a dyn Config, MatchSet)> { fn configs(&self) -> Vec<(&'a dyn Config, MatchSet)> {
self self
.config_store .config_store
@ -105,7 +100,7 @@ impl<'a> ConfigProvider<'a> for ConfigManager<'a> {
} }
} }
impl<'a> ModeProvider for ConfigManager<'a> { impl<'a> crate::engine::dispatch::ModeProvider for ConfigManager<'a> {
fn active_mode(&self) -> crate::engine::dispatch::Mode { fn active_mode(&self) -> crate::engine::dispatch::Mode {
let config = self.active(); let config = self.active();
match config.backend() { match config.backend() {
@ -118,12 +113,12 @@ impl<'a> ModeProvider for ConfigManager<'a> {
} }
} }
impl<'a> super::engine::executor::clipboard_injector::ClipboardParamsProvider impl<'a> super::engine::dispatch::executor::clipboard_injector::ClipboardParamsProvider
for ConfigManager<'a> for ConfigManager<'a>
{ {
fn get(&self) -> super::engine::executor::clipboard_injector::ClipboardParams { fn get(&self) -> super::engine::dispatch::executor::clipboard_injector::ClipboardParams {
let active = self.active(); let active = self.active();
super::engine::executor::clipboard_injector::ClipboardParams { super::engine::dispatch::executor::clipboard_injector::ClipboardParams {
pre_paste_delay: active.pre_paste_delay(), pre_paste_delay: active.pre_paste_delay(),
paste_shortcut_event_delay: 5, // TODO: read from config paste_shortcut_event_delay: 5, // TODO: read from config
paste_shortcut: None, // TODO: read from config paste_shortcut: None, // TODO: read from config

View File

@ -17,5 +17,4 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
pub mod form; pub mod executor;
pub mod selector;

View File

@ -23,21 +23,40 @@ use anyhow::Result;
use crossbeam::channel::Receiver; use crossbeam::channel::Receiver;
use espanso_config::{config::ConfigStore, matches::store::MatchStore}; use espanso_config::{config::ConfigStore, matches::store::MatchStore};
use espanso_path::Paths; use espanso_path::Paths;
use espanso_ui::{UIRemote, event::UIEvent}; use espanso_ui::{event::UIEvent, UIRemote};
use log::info; use log::info;
use ui::selector::MatchSelectorAdapter;
use crate::{cli::worker::engine::{matcher::regex::RegexMatcherAdapterOptions, path::PathProviderAdapter}, engine::event::ExitMode}; use crate::{
cli::worker::{
engine::{
dispatch::executor::{
clipboard_injector::ClipboardInjectorAdapter, context_menu::ContextMenuHandlerAdapter,
event_injector::EventInjectorAdapter, icon::IconHandlerAdapter,
key_injector::KeyInjectorAdapter,
},
process::middleware::{
image_resolve::PathProviderAdapter,
match_select::MatchSelectorAdapter,
matcher::{
convert::MatchConverter,
regex::{RegexMatcherAdapter, RegexMatcherAdapterOptions},
rolling::RollingMatcherAdapter,
},
multiplex::MultiplexAdapter,
render::{
extension::{clipboard::ClipboardAdapter, form::FormProviderAdapter},
RendererAdapter,
},
},
},
match_cache::MatchCache,
},
engine::event::ExitMode,
};
pub mod executor; pub mod dispatch;
pub mod match_cache; pub mod funnel;
pub mod matcher;
pub mod multiplex;
pub mod path;
pub mod process; pub mod process;
pub mod render;
pub mod source;
pub mod ui;
pub fn initialize_and_spawn( pub fn initialize_and_spawn(
paths: Paths, paths: Paths,
@ -56,42 +75,42 @@ pub fn initialize_and_spawn(
espanso_info::get_provider().expect("unable to initialize app info provider"); espanso_info::get_provider().expect("unable to initialize app info provider");
let config_manager = let config_manager =
super::config::ConfigManager::new(&*config_store, &*match_store, &*app_info_provider); super::config::ConfigManager::new(&*config_store, &*match_store, &*app_info_provider);
let match_converter = let match_converter = MatchConverter::new(&*config_store, &*match_store);
super::engine::matcher::convert::MatchConverter::new(&*config_store, &*match_store); let match_cache = MatchCache::load(&*config_store, &*match_store);
let match_cache = super::engine::match_cache::MatchCache::load(&*config_store, &*match_store);
let modulo_manager = crate::gui::modulo::manager::ModuloManager::new(); let modulo_manager = crate::gui::modulo::manager::ModuloManager::new();
let modulo_form_ui = crate::gui::modulo::form::ModuloFormUI::new(&modulo_manager); let modulo_form_ui = crate::gui::modulo::form::ModuloFormUI::new(&modulo_manager);
let modulo_search_ui = crate::gui::modulo::search::ModuloSearchUI::new(&modulo_manager); let modulo_search_ui = crate::gui::modulo::search::ModuloSearchUI::new(&modulo_manager);
let (detect_source, modifier_state_store, sequencer) = let (detect_source, modifier_state_store, sequencer) =
super::engine::source::init_and_spawn().expect("failed to initialize detector module"); super::engine::funnel::init_and_spawn().expect("failed to initialize detector module");
let exit_source = super::engine::source::exit::ExitSource::new(exit_signal, &sequencer); let exit_source = super::engine::funnel::exit::ExitSource::new(exit_signal, &sequencer);
let ui_source = super::engine::source::ui::UISource::new(ui_event_receiver, &sequencer); let ui_source = super::engine::funnel::ui::UISource::new(ui_event_receiver, &sequencer);
let sources: Vec<&dyn crate::engine::funnel::Source> = vec![&detect_source, &exit_source, &ui_source]; let sources: Vec<&dyn crate::engine::funnel::Source> =
vec![&detect_source, &exit_source, &ui_source];
let funnel = crate::engine::funnel::default(&sources); let funnel = crate::engine::funnel::default(&sources);
let rolling_matcher = super::engine::matcher::rolling::RollingMatcherAdapter::new( let rolling_matcher = RollingMatcherAdapter::new(&match_converter.get_rolling_matches());
&match_converter.get_rolling_matches(), let regex_matcher = RegexMatcherAdapter::new(
);
let regex_matcher = super::engine::matcher::regex::RegexMatcherAdapter::new(
&match_converter.get_regex_matches(), &match_converter.get_regex_matches(),
&RegexMatcherAdapterOptions { &RegexMatcherAdapterOptions {
max_buffer_size: 30, // TODO: load from configs max_buffer_size: 30, // TODO: load from configs
} },
); );
let matchers: Vec< let matchers: Vec<
&dyn crate::engine::process::Matcher<super::engine::matcher::MatcherState>, &dyn crate::engine::process::Matcher<
super::engine::process::middleware::matcher::MatcherState,
>,
> = vec![&rolling_matcher, &regex_matcher]; > = vec![&rolling_matcher, &regex_matcher];
let selector = MatchSelectorAdapter::new(&modulo_search_ui, &match_cache); let selector = MatchSelectorAdapter::new(&modulo_search_ui, &match_cache);
let multiplexer = super::engine::multiplex::MultiplexAdapter::new(&match_cache); let multiplexer = MultiplexAdapter::new(&match_cache);
let injector = espanso_inject::get_injector(Default::default()) let injector = espanso_inject::get_injector(Default::default())
.expect("failed to initialize injector module"); // TODO: handle the options .expect("failed to initialize injector module"); // TODO: handle the options
let clipboard = espanso_clipboard::get_clipboard(Default::default()) let clipboard = espanso_clipboard::get_clipboard(Default::default())
.expect("failed to initialize clipboard module"); // TODO: handle options .expect("failed to initialize clipboard module"); // TODO: handle options
let clipboard_adapter = super::engine::render::clipboard::ClipboardAdapter::new(&*clipboard); let clipboard_adapter = ClipboardAdapter::new(&*clipboard);
let clipboard_extension = let clipboard_extension =
espanso_render::extension::clipboard::ClipboardExtension::new(&clipboard_adapter); espanso_render::extension::clipboard::ClipboardExtension::new(&clipboard_adapter);
let date_extension = espanso_render::extension::date::DateExtension::new(); let date_extension = espanso_render::extension::date::DateExtension::new();
@ -104,7 +123,7 @@ pub fn initialize_and_spawn(
&paths.packages, &paths.packages,
); );
let shell_extension = espanso_render::extension::shell::ShellExtension::new(&paths.config); let shell_extension = espanso_render::extension::shell::ShellExtension::new(&paths.config);
let form_adapter = ui::form::FormProviderAdapter::new(&modulo_form_ui); let form_adapter = FormProviderAdapter::new(&modulo_form_ui);
let form_extension = espanso_render::extension::form::FormExtension::new(&form_adapter); let form_extension = espanso_render::extension::form::FormExtension::new(&form_adapter);
let renderer = espanso_render::create(vec![ let renderer = espanso_render::create(vec![
&clipboard_extension, &clipboard_extension,
@ -115,11 +134,11 @@ pub fn initialize_and_spawn(
&shell_extension, &shell_extension,
&form_extension, &form_extension,
]); ]);
let renderer_adapter = let renderer_adapter = RendererAdapter::new(&match_cache, &config_manager, &renderer);
super::engine::render::RendererAdapter::new(&match_cache, &config_manager, &renderer);
let path_provider = PathProviderAdapter::new(&paths); let path_provider = PathProviderAdapter::new(&paths);
let disable_options = process::middleware::disable::extract_disable_options(config_manager.default()); let disable_options =
process::middleware::disable::extract_disable_options(config_manager.default());
let mut processor = crate::engine::process::default( let mut processor = crate::engine::process::default(
&matchers, &matchers,
@ -134,17 +153,12 @@ pub fn initialize_and_spawn(
disable_options, disable_options,
); );
let event_injector = let event_injector = EventInjectorAdapter::new(&*injector);
super::engine::executor::event_injector::EventInjectorAdapter::new(&*injector);
let clipboard_injector = let clipboard_injector =
super::engine::executor::clipboard_injector::ClipboardInjectorAdapter::new( ClipboardInjectorAdapter::new(&*injector, &*clipboard, &config_manager);
&*injector, let key_injector = KeyInjectorAdapter::new(&*injector);
&*clipboard, let context_menu_adapter = ContextMenuHandlerAdapter::new(&*ui_remote);
&config_manager, let icon_adapter = IconHandlerAdapter::new(&*ui_remote);
);
let key_injector = super::engine::executor::key_injector::KeyInjectorAdapter::new(&*injector);
let context_menu_adapter = super::engine::executor::context_menu::ContextMenuHandlerAdapter::new(&*ui_remote);
let icon_adapter = super::engine::executor::icon::IconHandlerAdapter::new(&*ui_remote);
let dispatcher = crate::engine::dispatch::default( let dispatcher = crate::engine::dispatch::default(
&event_injector, &event_injector,
&clipboard_injector, &clipboard_injector,

View File

@ -18,3 +18,8 @@
*/ */
pub mod disable; pub mod disable;
pub mod image_resolve;
pub mod matcher;
pub mod match_select;
pub mod multiplex;
pub mod render;

View File

@ -0,0 +1,21 @@
/*
* 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/>.
*/
pub mod clipboard;
pub mod form;

View File

@ -19,7 +19,7 @@
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap};
pub mod clipboard; pub mod extension;
use espanso_config::{ use espanso_config::{
config::Config, config::Config,

View File

@ -24,10 +24,6 @@ use espanso_config::{
matches::{store::MatchStore, Match, MatchEffect}, matches::{store::MatchStore, Match, MatchEffect},
}; };
use crate::engine::process::MatchInfoProvider;
use super::multiplex::MatchProvider;
pub struct MatchCache<'a> { pub struct MatchCache<'a> {
cache: HashMap<i32, &'a Match>, cache: HashMap<i32, &'a Match>,
} }
@ -47,13 +43,13 @@ impl<'a> MatchCache<'a> {
} }
} }
impl<'a> MatchProvider<'a> for MatchCache<'a> { impl<'a> super::engine::process::middleware::multiplex::MatchProvider<'a> for MatchCache<'a> {
fn get(&self, match_id: i32) -> Option<&'a Match> { fn get(&self, match_id: i32) -> Option<&'a Match> {
self.cache.get(&match_id).map(|m| *m) self.cache.get(&match_id).map(|m| *m)
} }
} }
impl<'a> super::render::MatchProvider<'a> for MatchCache<'a> { impl<'a> super::engine::process::middleware::render::MatchProvider<'a> for MatchCache<'a> {
fn matches(&self) -> Vec<&'a Match> { fn matches(&self) -> Vec<&'a Match> {
self.cache.iter().map(|(_, m)| *m).collect() self.cache.iter().map(|(_, m)| *m).collect()
} }
@ -63,7 +59,7 @@ impl<'a> super::render::MatchProvider<'a> for MatchCache<'a> {
} }
} }
impl<'a> super::ui::selector::MatchProvider<'a> for MatchCache<'a> { impl<'a> super::engine::process::middleware::match_select::MatchProvider<'a> for MatchCache<'a> {
fn get_matches(&self, ids: &[i32]) -> Vec<&'a Match> { fn get_matches(&self, ids: &[i32]) -> Vec<&'a Match> {
ids.iter().flat_map(|id| { ids.iter().flat_map(|id| {
self.cache.get(&id).map(|m| *m) self.cache.get(&id).map(|m| *m)
@ -71,7 +67,7 @@ impl<'a> super::ui::selector::MatchProvider<'a> for MatchCache<'a> {
} }
} }
impl<'a> MatchInfoProvider for MatchCache<'a> { impl<'a> crate::engine::process::MatchInfoProvider for MatchCache<'a> {
fn get_force_mode(&self, match_id: i32) -> Option<crate::engine::event::effect::TextInjectMode> { fn get_force_mode(&self, match_id: i32) -> Option<crate::engine::event::effect::TextInjectMode> {
let m = self.cache.get(&match_id)?; let m = self.cache.get(&match_id)?;
if let MatchEffect::Text(text_effect) = &m.effect { if let MatchEffect::Text(text_effect) = &m.effect {

View File

@ -36,6 +36,7 @@ use super::{CliModule, CliModuleArgs};
mod config; mod config;
mod engine; mod engine;
mod ipc; mod ipc;
mod match_cache;
mod ui; mod ui;
pub fn new() -> CliModule { pub fn new() -> CliModule {