Skip to main content

zest_theme/theme/
custom.rs

1//! Custom theme builder.
2//!
3//! For applications that want a theme that isn't one of the presets,
4//! `CustomBuilder` provides a starting-from-Dark builder pattern.
5
6use crate::{Component, Container, Palette, Theme, Typography};
7use embedded_graphics::{mono_font::MonoFont, pixelcolor::Rgb888};
8
9/// Builder for a custom theme. Starts from [`crate::theme::dark::THEME`] and
10/// allows overriding individual fields.
11#[derive(Clone)]
12pub struct CustomBuilder<'a> {
13    theme: Theme<'a, Rgb888>,
14}
15
16impl<'a> CustomBuilder<'a> {
17    /// Start from the Dark theme.
18    #[must_use]
19    pub fn from_dark() -> Self {
20        Self {
21            theme: crate::theme::dark::THEME,
22        }
23    }
24
25    /// Start from the Light theme.
26    #[must_use]
27    pub fn from_light() -> Self {
28        Self {
29            theme: crate::theme::light::THEME,
30        }
31    }
32
33    /// Start from any existing theme.
34    #[must_use]
35    pub fn from_theme(theme: Theme<'a, Rgb888>) -> Self {
36        Self { theme }
37    }
38
39    /// Override the accent component.
40    #[must_use]
41    pub fn accent(mut self, accent: Component<Rgb888>) -> Self {
42        self.theme.accent = accent;
43        self
44    }
45
46    /// Override the background region.
47    #[must_use]
48    pub fn background(mut self, bg: Container<Rgb888>) -> Self {
49        self.theme.background = bg;
50        self
51    }
52
53    /// Override the primary region.
54    #[must_use]
55    pub fn primary(mut self, primary: Container<Rgb888>) -> Self {
56        self.theme.primary = primary;
57        self
58    }
59
60    /// Override the palette.
61    #[must_use]
62    pub fn palette(mut self, palette: Palette<Rgb888>) -> Self {
63        self.theme.palette = palette;
64        self
65    }
66
67    /// Override the typography scale.
68    #[must_use]
69    pub fn typography(mut self, typography: Typography<'a>) -> Self {
70        self.theme.typography = typography;
71        self
72    }
73
74    /// Override only the display font for hero text.
75    #[must_use]
76    pub fn display_font(mut self, font: &'a MonoFont<'a>) -> Self {
77        self.theme.typography.display = font;
78        self
79    }
80
81    /// Override only the body font (most common case). Display, heading,
82    /// and caption are left as the inherited theme's choices.
83    #[must_use]
84    pub fn font(mut self, font: &'a MonoFont<'a>) -> Self {
85        self.theme.typography.body = font;
86        self
87    }
88
89    /// Set the is_dark flag.
90    #[must_use]
91    pub fn dark(mut self, is_dark: bool) -> Self {
92        self.theme.is_dark = is_dark;
93        self
94    }
95
96    /// Finish, returning the built theme.
97    #[must_use]
98    pub fn build(self) -> Theme<'a, Rgb888> {
99        self.theme
100    }
101}