use std::cell::RefCell;
thread_local! {
static THEME_CONTEXT: RefCell<Option<ThemeContext>> = const { RefCell::new(None) };
}
#[derive(Debug, Clone)]
pub struct ThemeContext {
pub colors: color::SemanticColors,
pub glassmorphism_enabled: bool,
}
impl ThemeContext {
pub fn dark() -> Self {
Self {
colors: color::SemanticColors::dark(),
glassmorphism_enabled: true,
}
}
pub fn light() -> Self {
Self {
colors: color::SemanticColors::light(),
glassmorphism_enabled: false,
}
}
}
pub fn set_current_theme(colors: color::SemanticColors) {
THEME_CONTEXT.with(|cell| {
let is_light = (colors.background.r + colors.background.g + colors.background.b) / 3.0 > 0.5;
let glassmorphism = !is_light; *cell.borrow_mut() = Some(ThemeContext { colors, glassmorphism_enabled: glassmorphism });
});
}
pub fn set_theme_context(ctx: ThemeContext) {
THEME_CONTEXT.with(|cell| {
*cell.borrow_mut() = Some(ctx);
});
}
pub fn clear_current_theme() {
THEME_CONTEXT.with(|cell| {
*cell.borrow_mut() = None;
});
}
pub fn use_theme() -> color::SemanticColors {
THEME_CONTEXT.with(|cell| {
cell.borrow()
.clone()
.map(|ctx| ctx.colors)
.unwrap_or_else(color::SemanticColors::dark)
})
}
pub fn use_theme_context() -> ThemeContext {
THEME_CONTEXT.with(|cell| {
cell.borrow()
.clone()
.unwrap_or_else(ThemeContext::dark)
})
}
pub fn glassmorphism_enabled() -> bool {
THEME_CONTEXT.with(|cell| {
cell.borrow()
.as_ref()
.map(|ctx| ctx.glassmorphism_enabled)
.unwrap_or(true) })
}