Skip to main content

lichtung_plot/theme/
value.rs

1//! Complete construction values, independent of per-operation resolution state.
2
3use crate::font::canonical::FontStack;
4use crate::Result;
5
6use super::{
7    AnnotationTheme, AxisTheme, ColorScales, CompositionTheme, LegendTheme, MarkTheme,
8    ResolvedTheme, SurfaceTheme, TypographyTheme,
9};
10
11/// Complete immutable appearance, independent of fonts discovered during rendering.
12///
13/// Built-in constructors return ordinary complete values. Customize a cloned
14/// value with [`Theme::customize`] and attach the built result to a Plot or Canvas.
15///
16/// ```
17/// use lichtung_plot::{Color, Theme};
18/// let base = Theme::publication();
19/// let title = base.typography().get_root().get_title().clone()
20///     .color(Color::hex("#194A75")?)?;
21/// let root = base.typography().get_root().clone().title(title)?;
22/// let typography = base.typography().clone().root(root)?;
23/// let custom = base.customize().typography(typography).build()?;
24/// assert_ne!(custom.typography(), base.typography());
25/// # Ok::<(), lichtung_plot::Error>(())
26/// ```
27///
28/// Built-ins are complete values, not closed selector variants:
29/// ```compile_fail
30/// let _ = lichtung_plot::Theme::Publication;
31/// ```
32///
33/// Resolution is private:
34/// ```compile_fail
35/// let _ = lichtung_plot::Theme::publication().resolve();
36/// ```
37#[derive(Clone, Debug, PartialEq)]
38pub struct Theme {
39    pub(crate) surfaces: SurfaceTheme,
40    pub(crate) typography: TypographyTheme,
41    pub(crate) axes: AxisTheme,
42    pub(crate) legend: LegendTheme,
43    pub(crate) composition: CompositionTheme,
44    pub(crate) marks: MarkTheme,
45    pub(crate) annotations: AnnotationTheme,
46    pub(crate) color_scales: ColorScales,
47}
48
49/// Construction-only working copy of a complete [`Theme`].
50#[derive(Clone, Debug)]
51pub struct ThemeBuilder {
52    value: Theme,
53}
54
55macro_rules! components {
56    ($($field:ident: $ty:ty => $get:ident),+ $(,)?) => {
57        impl Theme {
58            $(
59                #[doc = concat!("Borrow the complete `", stringify!($field), "` component.")]
60                pub fn $field(&self) -> &$ty { &self.$field }
61            )+
62        }
63        impl ThemeBuilder {
64            $(
65                #[doc = concat!("Borrow the current complete `", stringify!($field), "` component.")]
66                pub fn $get(&self) -> &$ty { self.value.$field() }
67                #[doc = concat!("Replace the complete `", stringify!($field), "` construction input.")]
68                #[must_use]
69                pub fn $field(mut self, value: $ty) -> Self {
70                    self.value.$field = value;
71                    self
72                }
73            )+
74        }
75    };
76}
77
78components!(surfaces: SurfaceTheme => get_surfaces,
79    typography: TypographyTheme => get_typography, axes: AxisTheme => get_axes,
80    legend: LegendTheme => get_legend, composition: CompositionTheme => get_composition,
81    marks: MarkTheme => get_marks, annotations: AnnotationTheme => get_annotations,
82    color_scales: ColorScales => get_color_scales);
83
84impl Theme {
85    #[expect(
86        clippy::too_many_arguments,
87        reason = "the complete constructor retains eight independently owned components"
88    )]
89    /// Construct all eight complete components without discovering fonts or data domains.
90    pub fn new(
91        surfaces: SurfaceTheme,
92        typography: TypographyTheme,
93        axes: AxisTheme,
94        legend: LegendTheme,
95        composition: CompositionTheme,
96        marks: MarkTheme,
97        annotations: AnnotationTheme,
98        color_scales: ColorScales,
99    ) -> Result<Self> {
100        let value = Self {
101            surfaces,
102            typography,
103            axes,
104            legend,
105            composition,
106            marks,
107            annotations,
108            color_scales,
109        };
110        value.validate()?;
111        Ok(value)
112    }
113
114    pub(crate) fn validate(&self) -> Result<()> {
115        self.typography().validate()?;
116        self.axes().validate()?;
117        self.legend().validate_in()?;
118        self.marks().validate()?;
119        self.annotations().validate()
120    }
121
122    /// Clean high-contrast publication appearance with generous whitespace.
123    pub fn publication() -> Self {
124        super::resolved::publication_theme().expect("validated publication constructor")
125    }
126
127    /// Muted blues and greens with bottom/left coordinate frame sides.
128    pub fn nature() -> Self {
129        super::resolved::nature_theme().expect("validated nature constructor")
130    }
131
132    /// High-saturation scientific palette and emphasized axes.
133    pub fn science() -> Self {
134        super::resolved::science_theme().expect("validated science constructor")
135    }
136
137    /// Accessible pastel appearance for HCI publications.
138    pub fn chi() -> Self {
139        super::resolved::chi_theme().expect("validated CHI constructor")
140    }
141
142    pub(crate) fn resolve(&self) -> Result<ResolvedTheme> {
143        ResolvedTheme::new(self.clone())
144    }
145
146    /// Start a complete construction-only working copy, leaving this value unchanged.
147    pub fn customize(&self) -> ThemeBuilder {
148        ThemeBuilder::new(self.clone())
149    }
150}
151
152impl ThemeBuilder {
153    /// Consume a complete initial theme for construction edits.
154    pub fn new(value: Theme) -> Self {
155        Self { value }
156    }
157
158    /// Replace the stack in all eighteen roles once; later role edits take precedence normally.
159    pub fn font_stack(mut self, stack: FontStack) -> Result<Self> {
160        self.value.typography = self.value.typography.with_font_stack(stack)?;
161        Ok(self)
162    }
163
164    /// Validate and return the complete immutable appearance.
165    pub fn build(self) -> Result<Theme> {
166        self.value.validate()?;
167        Ok(self.value)
168    }
169}