Merge 2 gui builders

This commit is contained in:
hodasemi 2024-03-28 08:32:06 +01:00
parent 2ddc27ee30
commit 4bf0607a11

View file

@ -1,5 +1,5 @@
use crate::prelude::*; use crate::prelude::*;
use anyhow::Result; use anyhow::{bail, Result};
use assetpath::AssetPath; use assetpath::AssetPath;
@ -29,7 +29,7 @@ impl GuiBuilder {
path, path,
)?; )?;
Self::_new(gui_handler, validator) Ok(Arc::new(Self::_new(gui_handler, validator)?))
} }
pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Arc<Self>> { pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Arc<Self>> {
@ -39,11 +39,49 @@ impl GuiBuilder {
s, s,
)?; )?;
Self::_new(gui_handler, validator) Ok(Arc::new(Self::_new(gui_handler, validator)?))
}
pub fn merge<'a>(
gui_handler: &Arc<GuiHandler>,
pathes: impl IntoIterator<Item = &'a AssetPath>,
) -> Result<Arc<Self>> {
let mut me = Self {
grids: Default::default(),
default_select: Default::default(),
ids: Default::default(),
decline_callback: Default::default(),
};
for path in pathes.into_iter() {
let validator = Validator::new(
#[cfg(feature = "audio")]
gui_handler,
path,
)?;
let builder = Self::_new(gui_handler, validator)?;
me.grids.extend(builder.grids);
if me.default_select.is_some() && builder.default_select.is_some() {
bail!("multiple default selects are not supported!");
}
if builder.default_select.is_some() {
me.default_select = builder.default_select;
}
me.ids.extend(builder.ids);
}
Ok(Arc::new(me))
} }
#[inline] #[inline]
fn _new(gui_handler: &Arc<GuiHandler>, validator: Validator) -> Result<Arc<Self>> { fn _new(gui_handler: &Arc<GuiHandler>, validator: Validator) -> Result<Self> {
let root = validator.root(); let root = validator.root();
let mut ids = HashMap::new(); let mut ids = HashMap::new();
@ -71,14 +109,14 @@ impl GuiBuilder {
Self::connect_custom_neighbours(&ids, custom_neighbours)?; Self::connect_custom_neighbours(&ids, custom_neighbours)?;
Ok(Arc::new(GuiBuilder { Ok(GuiBuilder {
grids, grids,
default_select: opt_default_select, default_select: opt_default_select,
ids, ids,
decline_callback: RwLock::new(None), decline_callback: RwLock::new(None),
})) })
} }
pub(crate) fn connect_custom_neighbours( pub(crate) fn connect_custom_neighbours(