use std::sync::Arc;
use crate::PlushieRenderer;
use crate::registry::{PlushieWidget, WidgetRegistry, WidgetSet};
pub type SessionRegistryFactory<R> = Arc<dyn Fn() -> WidgetRegistry<R> + Send + Sync + 'static>;
pub struct PlushieAppBuilder<R: PlushieRenderer = iced::Renderer> {
registry: WidgetRegistry<R>,
session_factory: Option<SessionRegistryFactory<R>>,
}
impl<R: PlushieRenderer> std::fmt::Debug for PlushieAppBuilder<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PlushieAppBuilder")
.field("registry", &self.registry)
.finish()
}
}
impl<R: PlushieRenderer> PlushieAppBuilder<R> {
pub fn new() -> Self {
Self {
registry: WidgetRegistry::new(),
session_factory: None,
}
}
pub fn with_session_factory(
mut self,
factory: impl Fn() -> WidgetRegistry<R> + Send + Sync + 'static,
) -> Self {
self.session_factory = Some(Arc::new(factory));
self
}
pub fn take_session_factory(&mut self) -> Option<SessionRegistryFactory<R>> {
self.session_factory.take()
}
pub fn widget(mut self, widget: impl PlushieWidget<R> + 'static) -> Self {
self.registry.register_strict(Box::new(widget));
self
}
pub fn widget_override(mut self, widget: impl PlushieWidget<R> + 'static) -> Self {
self.registry.register(Box::new(widget));
self
}
pub fn widget_set(mut self, set: &dyn WidgetSet<R>) -> Self {
self.registry.register_set(set);
self
}
pub fn widget_type_names(&self) -> Vec<&str> {
self.registry.type_names()
}
pub fn build(self) -> WidgetRegistry<R> {
self.registry
}
pub fn custom_type_names(&self) -> Vec<&str> {
let builtins = crate::widget::widget_set::IcedWidgetSet::type_names();
self.registry
.type_names()
.into_iter()
.filter(|name| !builtins.iter().any(|b| b == name))
.collect()
}
}
impl<R: PlushieRenderer> Default for PlushieAppBuilder<R> {
fn default() -> Self {
Self::new()
}
}