use crate::font::canonical::FontStack;
use crate::Result;
use super::{
AnnotationTheme, AxisTheme, ColorScales, CompositionTheme, LegendTheme, MarkTheme,
ResolvedTheme, SurfaceTheme, TypographyTheme,
};
#[derive(Clone, Debug, PartialEq)]
pub struct Theme {
pub(crate) surfaces: SurfaceTheme,
pub(crate) typography: TypographyTheme,
pub(crate) axes: AxisTheme,
pub(crate) legend: LegendTheme,
pub(crate) composition: CompositionTheme,
pub(crate) marks: MarkTheme,
pub(crate) annotations: AnnotationTheme,
pub(crate) color_scales: ColorScales,
}
#[derive(Clone, Debug)]
pub struct ThemeBuilder {
value: Theme,
}
thread_local! {
static CONSTRUCTION_THEME: std::cell::RefCell<Option<std::sync::Arc<Theme>>> =
const { std::cell::RefCell::new(None) };
}
struct ConstructionThemeGuard(Option<std::sync::Arc<Theme>>);
impl Drop for ConstructionThemeGuard {
fn drop(&mut self) {
let previous = self.0.take();
let retired = CONSTRUCTION_THEME.with(|slot| slot.replace(previous));
drop(retired);
}
}
macro_rules! components {
($($field:ident: $ty:ty => $get:ident),+ $(,)?) => {
impl Theme {
$(
#[doc = concat!("Borrow the complete `", stringify!($field), "` component.")]
pub fn $field(&self) -> &$ty { &self.$field }
)+
}
impl ThemeBuilder {
$(
#[doc = concat!("Borrow the current complete `", stringify!($field), "` component.")]
pub fn $get(&self) -> &$ty { self.value.$field() }
#[doc = concat!("Replace the complete `", stringify!($field), "` construction input.")]
#[must_use]
pub fn $field(mut self, value: $ty) -> Self {
self.value.$field = value;
self
}
)+
}
};
}
components!(surfaces: SurfaceTheme => get_surfaces,
typography: TypographyTheme => get_typography, axes: AxisTheme => get_axes,
legend: LegendTheme => get_legend, composition: CompositionTheme => get_composition,
marks: MarkTheme => get_marks, annotations: AnnotationTheme => get_annotations,
color_scales: ColorScales => get_color_scales);
impl Theme {
pub fn with_context<R>(&self, f: impl FnOnce() -> R) -> R {
let current = std::sync::Arc::new(self.clone());
let previous = CONSTRUCTION_THEME.with(|slot| slot.replace(Some(current)));
let _guard = ConstructionThemeGuard(previous);
f()
}
pub(crate) fn captured_default() -> Option<std::sync::Arc<Self>> {
CONSTRUCTION_THEME.with(|slot| slot.borrow().clone())
}
#[expect(
clippy::too_many_arguments,
reason = "the complete constructor retains eight independently owned components"
)]
pub fn new(
surfaces: SurfaceTheme,
typography: TypographyTheme,
axes: AxisTheme,
legend: LegendTheme,
composition: CompositionTheme,
marks: MarkTheme,
annotations: AnnotationTheme,
color_scales: ColorScales,
) -> Result<Self> {
let value = Self {
surfaces,
typography,
axes,
legend,
composition,
marks,
annotations,
color_scales,
};
value.validate()?;
Ok(value)
}
pub(crate) fn validate(&self) -> Result<()> {
self.typography().validate()?;
self.axes().validate()?;
self.legend().validate_in()?;
self.marks().validate()?;
self.annotations().validate()
}
pub fn publication() -> Self {
super::resolved::publication_theme().expect("validated publication constructor")
}
pub fn nature() -> Self {
super::resolved::nature_theme().expect("validated nature constructor")
}
pub fn science() -> Self {
super::resolved::science_theme().expect("validated science constructor")
}
pub fn chi() -> Self {
super::resolved::chi_theme().expect("validated CHI constructor")
}
pub(crate) fn resolve(&self) -> Result<ResolvedTheme> {
ResolvedTheme::new(self.clone())
}
pub fn customize(&self) -> ThemeBuilder {
ThemeBuilder::new(self.clone())
}
}
impl ThemeBuilder {
pub fn new(value: Theme) -> Self {
Self { value }
}
pub fn font_stack(mut self, stack: FontStack) -> Result<Self> {
self.value.typography = self.value.typography.with_font_stack(stack)?;
Ok(self)
}
pub fn build(self) -> Result<Theme> {
self.value.validate()?;
Ok(self.value)
}
}