feat(core): wire up choice extension. #850

This commit is contained in:
Federico Terzi 2021-12-21 22:49:11 +01:00
parent 873c70a248
commit 9801b09ab6
3 changed files with 63 additions and 1 deletions

View File

@ -49,7 +49,9 @@ use crate::{
}, },
multiplex::MultiplexAdapter, multiplex::MultiplexAdapter,
render::{ render::{
extension::{clipboard::ClipboardAdapter, form::FormProviderAdapter}, extension::{
choice::ChoiceSelectorAdapter, clipboard::ClipboardAdapter, form::FormProviderAdapter,
},
RendererAdapter, RendererAdapter,
}, },
}, },
@ -198,6 +200,9 @@ pub fn initialize_and_spawn(
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 = 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 choice_adapter = ChoiceSelectorAdapter::new(&modulo_search_ui);
let choice_extension =
espanso_render::extension::choice::ChoiceExtension::new(&choice_adapter);
let renderer = espanso_render::create(vec![ let renderer = espanso_render::create(vec![
&clipboard_extension, &clipboard_extension,
&date_extension, &date_extension,
@ -207,6 +212,7 @@ pub fn initialize_and_spawn(
&script_extension, &script_extension,
&shell_extension, &shell_extension,
&form_extension, &form_extension,
&choice_extension,
]); ]);
let renderer_adapter = RendererAdapter::new(&match_cache, &config_manager, &renderer); let renderer_adapter = RendererAdapter::new(&match_cache, &config_manager, &renderer);
let path_provider = PathProviderAdapter::new(&paths); let path_provider = PathProviderAdapter::new(&paths);

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 espanso_render::extension::choice::{ChoiceSelector, ChoiceSelectorResult};
use crate::gui::{SearchItem, SearchUI};
pub struct ChoiceSelectorAdapter<'a> {
search_ui: &'a dyn SearchUI,
}
impl<'a> ChoiceSelectorAdapter<'a> {
pub fn new(search_ui: &'a dyn SearchUI) -> Self {
Self { search_ui }
}
}
impl<'a> ChoiceSelector for ChoiceSelectorAdapter<'a> {
fn show(&self, choices: &[espanso_render::extension::choice::Choice]) -> ChoiceSelectorResult {
let items = convert_items(choices);
match self.search_ui.show(&items, None) {
Ok(Some(choice)) => ChoiceSelectorResult::Success(choice),
Ok(None) => ChoiceSelectorResult::Aborted,
Err(err) => ChoiceSelectorResult::Error(err),
}
}
}
fn convert_items(choices: &[espanso_render::extension::choice::Choice]) -> Vec<SearchItem> {
choices
.iter()
.map(|choice| SearchItem {
id: choice.id.to_string(),
label: choice.label.to_string(),
tag: None,
is_builtin: false,
})
.collect()
}

View File

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