Cotton code
86 lines · 3.0 KB · RustRaw
//! Cotton Chat's themes as kit data (`kits/messages/themes.json`), in the//! shape Notes' are (`kits/notes/writing.json`: each theme a light and a//! dark appearance, the defaults by appearance): the thread's ground (plain//! or a quiet pattern), both bubbles and their inks, the ticks, and the//! tints a typed-initials picture and a group sender's name are drawn in.//! Names are the `chat` line set's (`app.theme.names.<id>`).use std::collections::BTreeMap;use std::sync::OnceLock;use serde::{Deserialize, Serialize};/// Every colour token an appearance carries.pub const TOKENS: &[&str] = &[    "ground", "header", "text", "secondary", "accent", "rule", "bubble_out", "ink_out", "bubble_in", "ink_in", "meta", "tick",    "tick_read", "badge", "badge_ink", "quote_bar", "pattern", "link", "pill", "pill_ink", "reaction", "reaction_ink", "danger",    "meta_out", "meta_in",];#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct Theme {    pub id: String,    /// `plain` or a quiet pattern's id (`weave`), drawn in `pattern`.    pub ground: String,    pub light: BTreeMap<String, String>,    pub dark: BTreeMap<String, String>,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct ByAppearance {    pub light: Vec<String>,    pub dark: Vec<String>,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct Defaults {    pub light_theme: String,    pub dark_theme: String,    pub text: String,    pub ground: String,}/// A text size: the thread's words scaled over the phone's own size.#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct TextSize {    pub id: String,    pub scale: f64,}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct Themes {    pub format_version: u32,    #[serde(default)]    pub about: String,    pub themes: Vec<Theme>,    pub tints: ByAppearance,    pub names: ByAppearance,    pub text: Vec<TextSize>,    /// The thread grounds a person may choose; `theme` is as the theme says.    pub grounds: Vec<String>,    pub defaults: Defaults,}/// The kit's themes.pub fn themes() -> &'static Themes {    static THEMES: OnceLock<Themes> = OnceLock::new();    THEMES.get_or_init(|| serde_json::from_str(include_str!("../../../kits/messages/themes.json")).expect("the kit's chat themes read"))}pub fn known(id: &str) -> bool {    themes().themes.iter().any(|t| t.id == id)}/// One appearance's colours as the phone takes them: every token, and/// `tint.<n>` / `name.<n>` for the core's tint numbers.pub fn colours(theme: &Theme, dark: bool) -> BTreeMap<String, String> {    let all = themes();    let (mut out, tints, names) = if dark { (theme.dark.clone(), &all.tints.dark, &all.names.dark) } else { (theme.light.clone(), &all.tints.light, &all.names.light) };    for (n, colour) in tints.iter().enumerate() {        out.insert(format!("tint.{n}"), colour.clone());    }    for (n, colour) in names.iter().enumerate() {        out.insert(format!("name.{n}"), colour.clone());    }    out}