1use std::collections::HashMap;
2use std::sync::Arc;
3use image::RgbaImage;
4use include_dir::{DirEntry, include_dir, Dir};
5use prism::{canvas, Assets};
6
7use std::fmt;
8use std::fmt::Display;
9
10pub use crate::color::Color;
11pub use crate::colors::*;
12
13#[derive(Default, Clone, Debug)]
14pub struct Theme {
15 pub colors: ColorResources,
16 pub fonts: FontResources,
17 pub icons: IconResources,
18}
19
20impl Theme {
21 pub fn light(assets: &Dir<'static>, color: Color) -> Self {
22 Theme { colors: ColorResources::light(color), icons: IconResources::new(assets), fonts: FontResources::default() }
23 }
24
25 pub fn dark(assets: &Dir<'static>, color: Color) -> Self {
26 Theme { colors: ColorResources::dark(color), icons: IconResources::new(assets), fonts: FontResources::default() }
27 }
28
29 pub fn from(assets: &Dir<'static>, color: Color) -> (Self, bool) {
30 let is_dark = color.is_high_contrast();
31 (if is_dark {Self::dark(assets, color)} else {Self::light(assets, color)}, is_dark)
32 }
33}
34
35#[derive(Debug, Clone)]
36pub struct FontResources {
37 sizes: HashMap<String, f32>,
38 fonts: HashMap<String, canvas::Font>,
39}
40
41impl FontResources {
42 pub fn insert_font<K: Display>(&mut self, key: K, font: canvas::Font) {
43 self.fonts.insert(key.to_string(), font);
44 }
45
46 pub fn insert_size<K: Display>(&mut self, key: K, size: f32) {
47 self.sizes.insert(key.to_string(), size);
48 }
49
50 pub fn get_font<K: Display>(&self, key: K) -> Option<&canvas::Font> {
51 self.fonts.get(&key.to_string())
52 }
53
54 pub fn get_size<K: Display>(&self, key: K) -> f32 {
55 self.sizes.get(&key.to_string()).copied().unwrap_or_default()
56 }
57}
58
59impl Default for FontResources {
60 fn default() -> Self {
61 let bold = canvas::Font::from_bytes(include_bytes!("../resources/fonts/outfit_bold.ttf")).unwrap();
62 let regular = canvas::Font::from_bytes(include_bytes!("../resources/fonts/outfit_regular.ttf")).unwrap();
63 let mut resources = FontResources {fonts: HashMap::new(), sizes: HashMap::new()};
64 resources.insert_font(FontStyle::Heading, bold.clone());
65 resources.insert_font(FontStyle::Text, regular);
66 resources.insert_font(FontStyle::Label, bold);
67 resources.insert_size(TextSize::Title, 72.0);
68 resources.insert_size(TextSize::H1, 48.0);
69 resources.insert_size(TextSize::H2, 32.0);
70 resources.insert_size(TextSize::H3, 24.0);
71 resources.insert_size(TextSize::H4, 20.0);
72 resources.insert_size(TextSize::H5, 16.0);
73 resources.insert_size(TextSize::H6, 14.0);
74 resources.insert_size(TextSize::Xl, 24.0);
75 resources.insert_size(TextSize::Lg, 20.0);
76 resources.insert_size(TextSize::Md, 16.0);
77 resources.insert_size(TextSize::Sm, 14.0);
78 resources.insert_size(TextSize::Xs, 12.0);
79 resources
80 }
81}
82
83pub enum FontStyle {Heading, Text, Label}
84impl fmt::Display for FontStyle { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "FontStyle::{}", match self {
85 FontStyle::Heading => "Heading", FontStyle::Text => "Text", FontStyle::Label => "Label"
86}) } }
87
88#[derive(Debug, Default, Copy, Clone)]
89pub enum TextSize {Title, H1, H2, H3, H4, H5, H6, Xl, #[default] Lg, Md, Sm, Xs}
90impl fmt::Display for TextSize { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "TextSize::{}", match self {
91 TextSize::Title => "Title", TextSize::H1 => "H1", TextSize::H2 => "H2", TextSize::H3 => "H3", TextSize::H4 => "H4",
92 TextSize::H5 => "H5", TextSize::H6 => "H6", TextSize::Xl => "Xl", TextSize::Lg => "Lg", TextSize::Md => "Md",
93 TextSize::Sm => "Sm", TextSize::Xs => "Xs"
94}) } }
95
96
97#[derive(Debug, Clone)]
101pub struct IconResources{
102 stored: HashMap<String, Arc<RgbaImage>>,
103 public: HashMap<String, Arc<RgbaImage>>,
104}
105impl IconResources {
106 pub fn new(assets: &Dir<'static>) -> Self {
107 let mut resources = IconResources::default();
108 resources.include(assets);
109 resources
110 }
111
112 fn include(&mut self, assets: &Dir<'static>) {
113 fn walk(map: &mut HashMap<String, Arc<RgbaImage>>, dir: &Dir<'static>) {
114 for entry in dir.entries() {
115 match entry {
116 DirEntry::File(f)
117 if f.path().extension().and_then(|e| e.to_str()) == Some("svg") =>
118 {
119 let name = f.path().to_str().unwrap();
120 let name = name.strip_prefix("icons/").unwrap_or(name);
121 let name = name.strip_suffix(".svg").unwrap_or(name)
122 .replace(' ', "_");
123
124 map.insert(name, Arc::new(Assets::load_svg(f.contents())));
125 }
126 DirEntry::Dir(d) => walk(map, d),
127 _ => {}
128 }
129 }
130 }
131
132 walk(&mut self.stored, assets);
133 }
134}
135
136impl Default for IconResources {
137 fn default() -> Self {
138 let result = include_dir!("resources/icons").entries().iter().filter_map(|e| match e {
139 DirEntry::File(f) => Some(f),
140 _ => None,
141 }).filter(|p| p.path().to_str().unwrap().ends_with(".svg")).collect::<Vec<_>>();
142
143
144 IconResources {
145 stored: result.iter().map(|p| {
146 let name = p.path().to_str().unwrap().strip_suffix(".svg").unwrap().replace(' ', "_");
147 (name, Arc::new(Assets::load_svg(p.contents())))
148 }).collect(),
149 public: HashMap::default(),
150 }
151 }
152}
153
154impl IconResources {
155 pub fn insert<K: Display>(&mut self, key: K, icon: &str) {
156 self.public.insert(key.to_string(), self.stored.get(icon).unwrap_or_else(|| {
157 self.stored.get("ptsd_error").expect("IconResources corrupted.")
158 }).clone());
159 }
160
161 pub fn get<K: Display>(&self, key: K) -> Arc<RgbaImage> {
162 self.public.get(&key.to_string()).unwrap_or_else(|| {
163 self.stored.get("ptsd_error").expect("IconResources corrupted.")
164 }).clone()
165 }
166}
167
168#[derive(Debug, Clone)]
169pub struct ColorResources(HashMap<String, Color>);
170
171impl ColorResources {
172 pub fn insert<K: Display>(&mut self, key: K, color: Color) {
173 self.0.insert(key.to_string(), color);
174 }
175
176 pub fn get<K: Display>(&self, key: K) -> Color {
177 self.0.get(&key.to_string()).cloned().unwrap_or_default()
178 }
179
180 pub fn light(brand: Color) -> Self {
181 let mut colors = ColorResources(HashMap::new());
182 colors.insert(Background::Primary, Color::WHITE);
183 colors.insert(Background::Secondary, Color::from_hex("#DDDDDD", 255));
184 colors.insert(Text::Primary, Color::BLACK);
185 colors.insert(Text::Secondary, Color::from_hex("#9e9e9e", 255));
186 colors.insert(Text::Heading, Color::BLACK);
187 colors.insert(Outline::Primary, Color::from_hex("#585250", 255));
188 colors.insert(Outline::Secondary, Color::from_hex("#9e9e9e", 255));
189 colors.insert(Status::Success, Color::from_hex("#3ccb5a", 255));
190 colors.insert(Status::Warning, Color::from_hex("#f5bd14", 255));
191 colors.insert(Status::Danger, Color::from_hex("#ff330a", 255));
192 colors.insert(Brand, brand);
193 colors
194 }
195
196 pub fn dark(brand: Color) -> Self {
197 let mut colors = ColorResources(HashMap::new());
198 colors.insert(Background::Primary, Color::BLACK);
199 colors.insert(Background::Secondary, Color::from_hex("#262322", 255));
200 colors.insert(Text::Primary, Color::WHITE);
201 colors.insert(Text::Secondary, Color::from_hex("#a7a29d", 255));
202 colors.insert(Text::Heading, Color::WHITE);
203 colors.insert(Outline::Primary, Color::from_hex("#585250", 255));
204 colors.insert(Outline::Secondary, Color::from_hex("#a7a29d", 255));
205 colors.insert(Status::Success, Color::from_hex("#3ccb5a", 255));
206 colors.insert(Status::Warning, Color::from_hex("#f5bd14", 255));
207 colors.insert(Status::Danger, Color::from_hex("#ff330a", 255));
208 colors.insert(Brand, brand);
209 colors
210 }
211}
212
213impl Default for ColorResources { fn default() -> Self { ColorResources::dark(Color::from_hex("#1ca758", 255)) } }