lichtung_plot/theme/
value.rs1use crate::font::canonical::FontStack;
4use crate::Result;
5
6use super::{
7 AnnotationTheme, AxisTheme, ColorScales, CompositionTheme, LegendTheme, MarkTheme,
8 ResolvedTheme, SurfaceTheme, TypographyTheme,
9};
10
11#[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#[derive(Clone, Debug)]
51pub struct ThemeBuilder {
52 value: Theme,
53}
54
55thread_local! {
56 static CONSTRUCTION_THEME: std::cell::RefCell<Option<std::sync::Arc<Theme>>> =
57 const { std::cell::RefCell::new(None) };
58}
59
60struct ConstructionThemeGuard(Option<std::sync::Arc<Theme>>);
61
62impl Drop for ConstructionThemeGuard {
63 fn drop(&mut self) {
64 let previous = self.0.take();
65 let retired = CONSTRUCTION_THEME.with(|slot| slot.replace(previous));
66 drop(retired);
67 }
68}
69
70macro_rules! components {
71 ($($field:ident: $ty:ty => $get:ident),+ $(,)?) => {
72 impl Theme {
73 $(
74 #[doc = concat!("Borrow the complete `", stringify!($field), "` component.")]
75 pub fn $field(&self) -> &$ty { &self.$field }
76 )+
77 }
78 impl ThemeBuilder {
79 $(
80 #[doc = concat!("Borrow the current complete `", stringify!($field), "` component.")]
81 pub fn $get(&self) -> &$ty { self.value.$field() }
82 #[doc = concat!("Replace the complete `", stringify!($field), "` construction input.")]
83 #[must_use]
84 pub fn $field(mut self, value: $ty) -> Self {
85 self.value.$field = value;
86 self
87 }
88 )+
89 }
90 };
91}
92
93components!(surfaces: SurfaceTheme => get_surfaces,
94 typography: TypographyTheme => get_typography, axes: AxisTheme => get_axes,
95 legend: LegendTheme => get_legend, composition: CompositionTheme => get_composition,
96 marks: MarkTheme => get_marks, annotations: AnnotationTheme => get_annotations,
97 color_scales: ColorScales => get_color_scales);
98
99impl Theme {
100 pub fn with_context<R>(&self, f: impl FnOnce() -> R) -> R {
107 let current = std::sync::Arc::new(self.clone());
108 let previous = CONSTRUCTION_THEME.with(|slot| slot.replace(Some(current)));
109 let _guard = ConstructionThemeGuard(previous);
110 f()
111 }
112
113 pub(crate) fn captured_default() -> Option<std::sync::Arc<Self>> {
114 CONSTRUCTION_THEME.with(|slot| slot.borrow().clone())
115 }
116
117 #[expect(
118 clippy::too_many_arguments,
119 reason = "the complete constructor retains eight independently owned components"
120 )]
121 pub fn new(
123 surfaces: SurfaceTheme,
124 typography: TypographyTheme,
125 axes: AxisTheme,
126 legend: LegendTheme,
127 composition: CompositionTheme,
128 marks: MarkTheme,
129 annotations: AnnotationTheme,
130 color_scales: ColorScales,
131 ) -> Result<Self> {
132 let value = Self {
133 surfaces,
134 typography,
135 axes,
136 legend,
137 composition,
138 marks,
139 annotations,
140 color_scales,
141 };
142 value.validate()?;
143 Ok(value)
144 }
145
146 pub(crate) fn validate(&self) -> Result<()> {
147 self.typography().validate()?;
148 self.axes().validate()?;
149 self.legend().validate_in()?;
150 self.marks().validate()?;
151 self.annotations().validate()
152 }
153
154 pub fn publication() -> Self {
156 super::resolved::publication_theme().expect("validated publication constructor")
157 }
158
159 pub fn nature() -> Self {
161 super::resolved::nature_theme().expect("validated nature constructor")
162 }
163
164 pub fn science() -> Self {
166 super::resolved::science_theme().expect("validated science constructor")
167 }
168
169 pub fn chi() -> Self {
171 super::resolved::chi_theme().expect("validated CHI constructor")
172 }
173
174 pub(crate) fn resolve(&self) -> Result<ResolvedTheme> {
175 ResolvedTheme::new(self.clone())
176 }
177
178 pub fn customize(&self) -> ThemeBuilder {
180 ThemeBuilder::new(self.clone())
181 }
182}
183
184impl ThemeBuilder {
185 pub fn new(value: Theme) -> Self {
187 Self { value }
188 }
189
190 pub fn font_stack(mut self, stack: FontStack) -> Result<Self> {
192 self.value.typography = self.value.typography.with_font_stack(stack)?;
193 Ok(self)
194 }
195
196 pub fn build(self) -> Result<Theme> {
198 self.value.validate()?;
199 Ok(self.value)
200 }
201}