Skip to main content

settings_model/
theme.rs

1//! The names a theme is chosen by: the looks Tilo ships, each designed for light and dark, the
2//! presets those make, and the spacing scale a person picks. The palettes and documents they stand
3//! for are the toolkit's.
4
5use serde::{Deserialize, Serialize};
6
7/// `appearance` key: the person's own themes, each a record a `custom:<id>` names.
8pub const THEMES_KEY: &str = "themes";
9
10/// The spacing scale, as a multiple of the stock 4 px scale.
11#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
12#[serde(deny_unknown_fields)]
13pub struct ThemeDensity {
14    pub spacing: f32,
15}
16
17impl ThemeDensity {
18    /// Band a document may state.
19    pub const ADMITTED: std::ops::RangeInclusive<f32> = 0.5..=2.0;
20    /// Band a theme is built at; a value outside it is clamped.
21    pub const USABLE: std::ops::RangeInclusive<f32> = 0.75..=1.5;
22    /// The compact density Settings offers.
23    pub const COMPACT: f32 = 0.85;
24    pub const REGULAR: f32 = 1.0;
25    /// The large density Settings offers.
26    pub const LARGE: f32 = 1.2;
27}
28
29/// A look Tilo ships: one temperature designed twice, for light and for dark, with the
30/// accent it comes with.
31#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
32pub enum Look {
33    /// Slate; the accent is the person's own colour.
34    Tilo,
35    /// Warm stock and charcoal, with a fountain-pen blue.
36    Paper,
37    /// Sage and forest night, with amber.
38    Moss,
39    /// Lilac and deep indigo, with rose.
40    Dusk,
41    /// True greys, black and white for emphasis, no accent.
42    Mono,
43}
44
45impl Look {
46    pub const ALL: [Self; 5] = [Self::Tilo, Self::Paper, Self::Moss, Self::Dusk, Self::Mono];
47
48    /// The stored id of the look (`tilo`, `paper`).
49    #[must_use]
50    pub const fn id(self) -> &'static str {
51        match self {
52            Self::Tilo => "tilo",
53            Self::Paper => "paper",
54            Self::Moss => "moss",
55            Self::Dusk => "dusk",
56            Self::Mono => "mono",
57        }
58    }
59
60    #[must_use]
61    pub const fn name(self) -> &'static str {
62        match self {
63            Self::Tilo => "Tilo",
64            Self::Paper => "Paper",
65            Self::Moss => "Moss",
66            Self::Dusk => "Dusk",
67            Self::Mono => "Mono",
68        }
69    }
70
71    /// The preset of this look for the polarity `dark` names.
72    #[must_use]
73    pub const fn preset(self, dark: bool) -> Preset {
74        match (self, dark) {
75            (Self::Tilo, true) => Preset::Dark,
76            (Self::Tilo, false) => Preset::Light,
77            (Self::Paper, true) => Preset::PaperDark,
78            (Self::Paper, false) => Preset::PaperLight,
79            (Self::Moss, true) => Preset::MossDark,
80            (Self::Moss, false) => Preset::MossLight,
81            (Self::Dusk, true) => Preset::DuskDark,
82            (Self::Dusk, false) => Preset::DuskLight,
83            (Self::Mono, true) => Preset::MonoDark,
84            (Self::Mono, false) => Preset::MonoLight,
85        }
86    }
87
88    /// A look by id or name, loosely: case and punctuation are ignored.
89    #[must_use]
90    pub fn find(key: &str) -> Option<Self> {
91        let key: String = key
92            .chars()
93            .filter(|c| c.is_alphanumeric())
94            .flat_map(char::to_lowercase)
95            .collect();
96        Self::ALL.into_iter().find(|look| look.id() == key)
97    }
98}
99
100#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
101pub enum Preset {
102    /// Tilo, dark: the default.
103    Dark,
104    /// Tilo, light.
105    Light,
106    PaperDark,
107    PaperLight,
108    MossDark,
109    MossLight,
110    DuskDark,
111    DuskLight,
112    MonoDark,
113    MonoLight,
114    HighContrast,
115    HighContrastLight,
116}
117
118impl Preset {
119    pub const ALL: [Self; 12] = [
120        Self::Dark,
121        Self::Light,
122        Self::PaperDark,
123        Self::PaperLight,
124        Self::MossDark,
125        Self::MossLight,
126        Self::DuskDark,
127        Self::DuskLight,
128        Self::MonoDark,
129        Self::MonoLight,
130        Self::HighContrast,
131        Self::HighContrastLight,
132    ];
133
134    /// The stored id (`appearance.dark_theme`, `appearance.light_theme`).
135    #[must_use]
136    pub const fn id(self) -> &'static str {
137        match self {
138            Self::Dark => "tilo-dark",
139            Self::Light => "tilo-light",
140            Self::PaperDark => "paper-dark",
141            Self::PaperLight => "paper-light",
142            Self::MossDark => "moss-dark",
143            Self::MossLight => "moss-light",
144            Self::DuskDark => "dusk-dark",
145            Self::DuskLight => "dusk-light",
146            Self::MonoDark => "mono-dark",
147            Self::MonoLight => "mono-light",
148            Self::HighContrast => "high-contrast",
149            Self::HighContrastLight => "high-contrast-light",
150        }
151    }
152
153    #[must_use]
154    pub const fn name(self) -> &'static str {
155        match self {
156            Self::Dark => "Tilo dark",
157            Self::Light => "Tilo light",
158            Self::PaperDark => "Paper dark",
159            Self::PaperLight => "Paper light",
160            Self::MossDark => "Moss dark",
161            Self::MossLight => "Moss light",
162            Self::DuskDark => "Dusk dark",
163            Self::DuskLight => "Dusk light",
164            Self::MonoDark => "Mono dark",
165            Self::MonoLight => "Mono light",
166            Self::HighContrast => "High contrast",
167            Self::HighContrastLight => "High contrast light",
168        }
169    }
170
171    /// The look this preset is one side of; high contrast belongs to none.
172    #[must_use]
173    pub const fn look(self) -> Option<Look> {
174        Some(match self {
175            Self::Dark | Self::Light => Look::Tilo,
176            Self::PaperDark | Self::PaperLight => Look::Paper,
177            Self::MossDark | Self::MossLight => Look::Moss,
178            Self::DuskDark | Self::DuskLight => Look::Dusk,
179            Self::MonoDark | Self::MonoLight => Look::Mono,
180            Self::HighContrast | Self::HighContrastLight => return None,
181        })
182    }
183
184    /// A preset by id or name, loosely (`"paper-light"`, `"Paper light"`); a look's name
185    /// alone (`"paper"`) is its dark side, and `dark` and `light` are Tilo's.
186    #[must_use]
187    pub fn find(key: &str) -> Option<Self> {
188        let key: String = key
189            .chars()
190            .filter(|c| c.is_alphanumeric())
191            .flat_map(char::to_lowercase)
192            .collect();
193        if let Some(preset) = Self::ALL.into_iter().find(|preset| {
194            preset
195                .id()
196                .chars()
197                .filter(|c| c.is_alphanumeric())
198                .eq(key.chars())
199        }) {
200            return Some(preset);
201        }
202        Some(match key.as_str() {
203            "dark" => Self::Dark,
204            "light" => Self::Light,
205            "highcontrast" | "contrast" | "highcontrastdark" => Self::HighContrast,
206            other => Look::find(other)?.preset(true),
207        })
208    }
209}