8447
Co
conveyor / files
conveyor / templates / library / widgets / look.rs
5.7 KB
1//! The app's look, as the theme widget sets it in the shared value "theme": colours, fonts and 2//! their size, and the glass of floating cards (transparency, blur, shadow). Every widget draws 3//! with `of(cx)`: its colours are tok's dark theme fields (it derefs to `tok::Theme`), plus 4//! `accent`, `danger`, `warn`, the text styles and `glass`. The controls drawn with it are in `ui`. 5use super::api::Cx; 6use rune::{Color, El, Family, TextStyle, color::hex, oklch, tok}; 7use serde::{Deserialize, Serialize}; 8use std::{cell::RefCell, collections::HashMap, ops::Deref}; 9 10/// What the user picked, as shared: colours as `#rrggbb`, empty fonts for the app's own. 11#[derive(Clone, PartialEq, Serialize, Deserialize)] 12#[serde(default)] 13pub struct Settings { 14 pub bg: String, 15 pub card: String, 16 pub text: String, 17 pub muted: String, 18 pub accent: String, 19 /// How see-through floating cards are: 0 opaque, up to 0.9. 20 pub transparency: f32, 21 /// The blur behind floating cards, as a sigma in points: 0 to 40. 22 pub blur: f32, 23 /// Floating cards' shadow: 0 none to 1, deep and dark. 24 pub shadow: f32, 25 pub font: String, 26 pub mono: String, 27 /// Body text in points; the other styles scale with it. 28 pub size: f32, 29} 30 31/// The accent the app had before it was themeable. 32pub fn default_accent() -> Color { 33 oklch(0.72, 0.14, 250.0) 34} 35 36/// `#rrggbb`, as settings keep colours. 37pub fn rgb(c: Color) -> String { 38 c.hex()[..7].to_owned() 39} 40 41impl Default for Settings { 42 fn default() -> Self { 43 let t = tok::theme(true); 44 Settings { bg: rgb(t.bg), card: rgb(t.card), text: rgb(t.text), muted: rgb(t.text_secondary), accent: rgb(default_accent()), 45 transparency: 0.05, blur: 16.0, shadow: 0.5, font: String::new(), mono: String::new(), size: 13.0 } 46 } 47} 48 49impl Settings { 50 /// The shared "theme" value, or the defaults. 51 pub fn read(shared: Option<&str>) -> Self { 52 shared.and_then(|s| serde_json::from_str(s).ok()).unwrap_or_default() 53 } 54 55 pub fn json(&self) -> String { 56 serde_json::to_string(self).unwrap_or_default() 57 } 58} 59 60/// The resolved look a widget draws with. 61#[derive(Clone, Copy)] 62pub struct Look { 63 pub theme: tok::Theme, 64 /// Whether the background is dark: hovers, separators and shadows follow it. 65 pub dark: bool, 66 pub accent: Color, 67 /// Text and marks drawn on an accent fill. 68 pub on_accent: Color, 69 /// Something destructive armed (red), and something waiting on the user (amber). 70 pub danger: Color, 71 pub warn: Color, 72 pub body: TextStyle, 73 pub label: TextStyle, 74 pub title: TextStyle, 75 pub mono: TextStyle, 76 /// Floating cards' fill opacity, blur sigma and shadow strength. 77 pub alpha: f32, 78 pub blur: f32, 79 pub depth: f32, 80} 81 82impl Deref for Look { 83 type Target = tok::Theme; 84 fn deref(&self) -> &tok::Theme { 85 &self.theme 86 } 87} 88 89impl Look { 90 fn new(s: &Settings) -> Self { 91 let base = tok::theme(true); 92 let pick = |v: &str, d: Color| hex(v).unwrap_or(d); 93 let bg = pick(&s.bg, base.bg); 94 // Hovers, separators and shadows come from the theme that suits the background. 95 let dark = bg.to_oklab()[0] < 0.6; 96 let mut theme = tok::theme(dark); 97 (theme.bg, theme.card) = (bg, pick(&s.card, base.card)); 98 (theme.text, theme.text_secondary) = (pick(&s.text, base.text), pick(&s.muted, base.text_secondary)); 99 let family = |name: &str, d: Family| if name.trim().is_empty() { d } else { Family::Named(leak(name.trim())) }; 100 let (sans, mono) = (family(&s.font, Family::Sans), family(&s.mono, Family::SystemMono)); 101 let k = s.size.clamp(8.0, 32.0) / tok::type_::BODY.size; 102 let style = |t: TextStyle| TextStyle { size: t.size * k, ..t }.family(sans); 103 let accent = pick(&s.accent, default_accent()); 104 // Ink on the accent: the darker of background and text on a light accent, the lighter on a dark one. 105 let (deep, pale) = if dark { (theme.bg, theme.text) } else { (theme.text, theme.bg) }; 106 let on_accent = if accent.to_oklab()[0] > 0.6 { deep } else { pale }; 107 let l = if dark { 0.0 } else { -0.12 }; 108 Look { theme, dark, accent, on_accent, danger: oklch(0.65 + l, 0.19, 25.0), warn: oklch(0.8 + l, 0.14, 75.0), 109 body: style(tok::type_::BODY), label: style(tok::type_::LABEL), title: style(tok::type_::TITLE), 110 mono: style(tok::type_::BODY).family(mono), 111 alpha: 1.0 - s.transparency.clamp(0.0, 0.9), blur: s.blur.clamp(0.0, 40.0), depth: s.shadow.clamp(0.0, 1.0) } 112 } 113 114 /// A floating card: its fill at the chosen transparency, over a blur of what's behind, with the chosen shadow. 115 pub fn glass(&self, e: El) -> El { 116 let s = self.depth; 117 e.fill(self.card.alpha(self.alpha)).backdrop_blur(self.blur).shadow(12.0 * s, 48.0 * s, Color::BLACK.alpha(s)) 118 } 119} 120 121thread_local! { 122 /// The last "theme" value read and its look, so a frame parses it once. 123 static LAST: RefCell<Option<(String, Look)>> = const { RefCell::new(None) }; 124 /// Font names, kept for the app's life: a text style holds a family by `&'static str`. 125 static NAMES: RefCell<HashMap<String, &'static str>> = RefCell::new(HashMap::new()); 126} 127 128fn leak(name: &str) -> &'static str { 129 NAMES.with(|n| *n.borrow_mut().entry(name.to_owned()).or_insert_with(|| Box::leak(name.to_owned().into_boxed_str()))) 130} 131 132/// The look every widget draws with. 133pub fn of(cx: &Cx) -> Look { 134 let raw = cx.shared("theme").unwrap_or(""); 135 LAST.with(|last| { 136 let mut last = last.borrow_mut(); 137 if let Some((r, l)) = last.as_ref() && r == raw { return *l } 138 let l = Look::new(&Settings::read(Some(raw))); 139 *last = Some((raw.to_owned(), l)); 140 l 141 }) 142}