style(core): refactor worker module structure
This commit is contained in:
		
							parent
							
								
									2ad8dfa8a5
								
							
						
					
					
						commit
						023ac615ba
					
				| 
						 | 
				
			
			@ -17,11 +17,8 @@
 | 
			
		|||
 * along with espanso.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
use std::{
 | 
			
		||||
  collections::{HashSet},
 | 
			
		||||
};
 | 
			
		||||
use std::collections::HashSet;
 | 
			
		||||
 | 
			
		||||
use crate::engine::{dispatch::ModeProvider, process::MatchFilter};
 | 
			
		||||
use espanso_config::{
 | 
			
		||||
  config::{AppProperties, Config, ConfigStore},
 | 
			
		||||
  matches::store::{MatchSet, MatchStore},
 | 
			
		||||
| 
						 | 
				
			
			@ -29,8 +26,6 @@ use espanso_config::{
 | 
			
		|||
use espanso_info::{AppInfo, AppInfoProvider};
 | 
			
		||||
use std::iter::FromIterator;
 | 
			
		||||
 | 
			
		||||
use super::engine::render::ConfigProvider;
 | 
			
		||||
 | 
			
		||||
pub struct ConfigManager<'a> {
 | 
			
		||||
  config_store: &'a dyn ConfigStore,
 | 
			
		||||
  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> {
 | 
			
		||||
    let ids_set: HashSet<i32> = HashSet::from_iter(matches_ids.iter().copied());
 | 
			
		||||
    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)> {
 | 
			
		||||
    self
 | 
			
		||||
      .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 {
 | 
			
		||||
    let config = self.active();
 | 
			
		||||
    match config.backend() {
 | 
			
		||||
| 
						 | 
				
			
			@ -118,15 +113,15 @@ 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>
 | 
			
		||||
{
 | 
			
		||||
  fn get(&self) -> super::engine::executor::clipboard_injector::ClipboardParams {
 | 
			
		||||
  fn get(&self) -> super::engine::dispatch::executor::clipboard_injector::ClipboardParams {
 | 
			
		||||
    let active = self.active();
 | 
			
		||||
    super::engine::executor::clipboard_injector::ClipboardParams {
 | 
			
		||||
    super::engine::dispatch::executor::clipboard_injector::ClipboardParams {
 | 
			
		||||
      pre_paste_delay: active.pre_paste_delay(),
 | 
			
		||||
      paste_shortcut_event_delay: 5, // TODO: read from config
 | 
			
		||||
      paste_shortcut: None, // TODO: read from config
 | 
			
		||||
      paste_shortcut_event_delay: 5,  // TODO: read from config
 | 
			
		||||
      paste_shortcut: None,           // TODO: read from config
 | 
			
		||||
      disable_x11_fast_inject: false, // TODO: read from config
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,5 +17,4 @@
 | 
			
		|||
 * along with espanso.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
pub mod form;
 | 
			
		||||
pub mod selector;
 | 
			
		||||
pub mod executor;
 | 
			
		||||
| 
						 | 
				
			
			@ -23,21 +23,40 @@ use anyhow::Result;
 | 
			
		|||
use crossbeam::channel::Receiver;
 | 
			
		||||
use espanso_config::{config::ConfigStore, matches::store::MatchStore};
 | 
			
		||||
use espanso_path::Paths;
 | 
			
		||||
use espanso_ui::{UIRemote, event::UIEvent};
 | 
			
		||||
use espanso_ui::{event::UIEvent, UIRemote};
 | 
			
		||||
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 match_cache;
 | 
			
		||||
pub mod matcher;
 | 
			
		||||
pub mod multiplex;
 | 
			
		||||
pub mod path;
 | 
			
		||||
pub mod dispatch;
 | 
			
		||||
pub mod funnel;
 | 
			
		||||
pub mod process;
 | 
			
		||||
pub mod render;
 | 
			
		||||
pub mod source;
 | 
			
		||||
pub mod ui;
 | 
			
		||||
 | 
			
		||||
pub fn initialize_and_spawn(
 | 
			
		||||
  paths: Paths,
 | 
			
		||||
| 
						 | 
				
			
			@ -56,42 +75,42 @@ pub fn initialize_and_spawn(
 | 
			
		|||
        espanso_info::get_provider().expect("unable to initialize app info provider");
 | 
			
		||||
      let config_manager =
 | 
			
		||||
        super::config::ConfigManager::new(&*config_store, &*match_store, &*app_info_provider);
 | 
			
		||||
      let match_converter =
 | 
			
		||||
        super::engine::matcher::convert::MatchConverter::new(&*config_store, &*match_store);
 | 
			
		||||
      let match_cache = super::engine::match_cache::MatchCache::load(&*config_store, &*match_store);
 | 
			
		||||
      let match_converter = MatchConverter::new(&*config_store, &*match_store);
 | 
			
		||||
      let match_cache = MatchCache::load(&*config_store, &*match_store);
 | 
			
		||||
 | 
			
		||||
      let modulo_manager = crate::gui::modulo::manager::ModuloManager::new();
 | 
			
		||||
      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 (detect_source, modifier_state_store, sequencer) =
 | 
			
		||||
        super::engine::source::init_and_spawn().expect("failed to initialize detector module");
 | 
			
		||||
      let exit_source = super::engine::source::exit::ExitSource::new(exit_signal, &sequencer);
 | 
			
		||||
      let ui_source = super::engine::source::ui::UISource::new(ui_event_receiver, &sequencer);
 | 
			
		||||
      let sources: Vec<&dyn crate::engine::funnel::Source> = vec![&detect_source, &exit_source, &ui_source];
 | 
			
		||||
        super::engine::funnel::init_and_spawn().expect("failed to initialize detector module");
 | 
			
		||||
      let exit_source = super::engine::funnel::exit::ExitSource::new(exit_signal, &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 funnel = crate::engine::funnel::default(&sources);
 | 
			
		||||
 | 
			
		||||
      let rolling_matcher = super::engine::matcher::rolling::RollingMatcherAdapter::new(
 | 
			
		||||
        &match_converter.get_rolling_matches(),
 | 
			
		||||
      );
 | 
			
		||||
      let regex_matcher = super::engine::matcher::regex::RegexMatcherAdapter::new(
 | 
			
		||||
      let rolling_matcher = RollingMatcherAdapter::new(&match_converter.get_rolling_matches());
 | 
			
		||||
      let regex_matcher = RegexMatcherAdapter::new(
 | 
			
		||||
        &match_converter.get_regex_matches(),
 | 
			
		||||
        &RegexMatcherAdapterOptions {
 | 
			
		||||
          max_buffer_size: 30,  // TODO: load from configs
 | 
			
		||||
        }
 | 
			
		||||
          max_buffer_size: 30, // TODO: load from configs
 | 
			
		||||
        },
 | 
			
		||||
      );
 | 
			
		||||
      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, ®ex_matcher];
 | 
			
		||||
      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())
 | 
			
		||||
        .expect("failed to initialize injector module"); // TODO: handle the options
 | 
			
		||||
      let clipboard = espanso_clipboard::get_clipboard(Default::default())
 | 
			
		||||
        .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 =
 | 
			
		||||
        espanso_render::extension::clipboard::ClipboardExtension::new(&clipboard_adapter);
 | 
			
		||||
      let date_extension = espanso_render::extension::date::DateExtension::new();
 | 
			
		||||
| 
						 | 
				
			
			@ -104,7 +123,7 @@ pub fn initialize_and_spawn(
 | 
			
		|||
        &paths.packages,
 | 
			
		||||
      );
 | 
			
		||||
      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 renderer = espanso_render::create(vec![
 | 
			
		||||
        &clipboard_extension,
 | 
			
		||||
| 
						 | 
				
			
			@ -115,11 +134,11 @@ pub fn initialize_and_spawn(
 | 
			
		|||
        &shell_extension,
 | 
			
		||||
        &form_extension,
 | 
			
		||||
      ]);
 | 
			
		||||
      let renderer_adapter =
 | 
			
		||||
        super::engine::render::RendererAdapter::new(&match_cache, &config_manager, &renderer);
 | 
			
		||||
      let renderer_adapter = RendererAdapter::new(&match_cache, &config_manager, &renderer);
 | 
			
		||||
      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(
 | 
			
		||||
        &matchers,
 | 
			
		||||
| 
						 | 
				
			
			@ -134,17 +153,12 @@ pub fn initialize_and_spawn(
 | 
			
		|||
        disable_options,
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      let event_injector =
 | 
			
		||||
        super::engine::executor::event_injector::EventInjectorAdapter::new(&*injector);
 | 
			
		||||
      let event_injector = EventInjectorAdapter::new(&*injector);
 | 
			
		||||
      let clipboard_injector =
 | 
			
		||||
        super::engine::executor::clipboard_injector::ClipboardInjectorAdapter::new(
 | 
			
		||||
          &*injector,
 | 
			
		||||
          &*clipboard,
 | 
			
		||||
          &config_manager,
 | 
			
		||||
        );
 | 
			
		||||
      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);
 | 
			
		||||
        ClipboardInjectorAdapter::new(&*injector, &*clipboard, &config_manager);
 | 
			
		||||
      let key_injector = KeyInjectorAdapter::new(&*injector);
 | 
			
		||||
      let context_menu_adapter = ContextMenuHandlerAdapter::new(&*ui_remote);
 | 
			
		||||
      let icon_adapter = IconHandlerAdapter::new(&*ui_remote);
 | 
			
		||||
      let dispatcher = crate::engine::dispatch::default(
 | 
			
		||||
        &event_injector,
 | 
			
		||||
        &clipboard_injector,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -82,4 +82,4 @@ impl<'a> MatchSelector for MatchSelectorAdapter<'a> {
 | 
			
		|||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO: test
 | 
			
		||||
// TODO: test
 | 
			
		||||
| 
						 | 
				
			
			@ -17,4 +17,9 @@
 | 
			
		|||
 * along with espanso.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
pub mod disable;
 | 
			
		||||
pub mod disable;
 | 
			
		||||
pub mod image_resolve;
 | 
			
		||||
pub mod matcher;
 | 
			
		||||
pub mod match_select;
 | 
			
		||||
pub mod multiplex;
 | 
			
		||||
pub mod render;
 | 
			
		||||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +19,7 @@
 | 
			
		|||
 | 
			
		||||
use std::{cell::RefCell, collections::HashMap};
 | 
			
		||||
 | 
			
		||||
pub mod clipboard;
 | 
			
		||||
pub mod extension;
 | 
			
		||||
 | 
			
		||||
use espanso_config::{
 | 
			
		||||
  config::Config,
 | 
			
		||||
| 
						 | 
				
			
			@ -24,10 +24,6 @@ use espanso_config::{
 | 
			
		|||
  matches::{store::MatchStore, Match, MatchEffect},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use crate::engine::process::MatchInfoProvider;
 | 
			
		||||
 | 
			
		||||
use super::multiplex::MatchProvider;
 | 
			
		||||
 | 
			
		||||
pub struct MatchCache<'a> {
 | 
			
		||||
  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> {
 | 
			
		||||
    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> {
 | 
			
		||||
    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> {
 | 
			
		||||
    ids.iter().flat_map(|id| {
 | 
			
		||||
      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> {
 | 
			
		||||
    let m = self.cache.get(&match_id)?;
 | 
			
		||||
    if let MatchEffect::Text(text_effect) = &m.effect {
 | 
			
		||||
| 
						 | 
				
			
			@ -36,6 +36,7 @@ use super::{CliModule, CliModuleArgs};
 | 
			
		|||
mod config;
 | 
			
		||||
mod engine;
 | 
			
		||||
mod ipc;
 | 
			
		||||
mod match_cache;
 | 
			
		||||
mod ui;
 | 
			
		||||
 | 
			
		||||
pub fn new() -> CliModule {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user