use super::*;
#[derive(Default)]
pub struct CustomStyles {
themes: HashMap<TypeId, Box<dyn Any>, ahash::RandomState>,
}
#[macro_export]
macro_rules! fill_custom_style {
($theme: ident, $($v: expr),+) => {
$($theme.custom_styles.set_custom_style($($v)+);)+
};
}
pub trait CustomStyle: Sized + Clone + 'static {
fn default_style(ctx: &impl ProviderCtx) -> Self;
#[inline]
fn of(ctx: &impl ProviderCtx) -> Self {
let tid = TypeId::of::<Self>();
ctx
.all_providers::<CustomStyles>()
.find_map(|t| {
t.themes
.get(&tid)
.and_then(|c| c.downcast_ref::<Self>())
.cloned()
})
.unwrap_or_else(|| Self::default_style(ctx))
}
}
impl CustomStyles {
#[inline]
pub fn set_custom_style<T: CustomStyle + 'static>(&mut self, v: T) {
self.themes.insert(v.type_id(), Box::new(v));
}
}