forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

scheme.rs117 lines, 4.0 KiBHistoryRawDownload
1//! Color schemes: the nine palettes the user can pick, the cookie that remembers the
2//! choice, and the swatch colors the picker shows. The palettes themselves are CSS
3//! custom properties in `styles.css`, keyed by `<html data-scheme="...">`.
4
5use topcoat::{
6 context::Cx,
7 cookie::{Cookie, Cookies, SameSite, cookies, time::Duration},
8};
9
10/// Name of the cookie that stores the color scheme.
11pub const SCHEME_COOKIE: &str = "scheme";
12
13/// How long the color scheme choice is remembered.
14pub const SCHEME_COOKIE_DAYS: i64 = 365;
15
16/// A color scheme. Each one is a palette of light and dark tokens in `styles.css`.
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum Scheme {
19 Graphite,
20 Cobalt,
21 Oxblood,
22 Teal,
23 Plum,
24 Mono,
25 Tangerine,
26 Sage,
27 Spark,
28}
29
30/// Every scheme, in the order the picker lists them.
31pub const ALL: [Scheme; 9] = [
32 Scheme::Graphite,
33 Scheme::Cobalt,
34 Scheme::Oxblood,
35 Scheme::Teal,
36 Scheme::Plum,
37 Scheme::Mono,
38 Scheme::Tangerine,
39 Scheme::Sage,
40 Scheme::Spark,
41];
42
43/// The scheme used when no valid cookie is set.
44pub const DEFAULT: Scheme = Scheme::Sage;
45
46impl Scheme {
47 /// The value stored in the cookie and in `data-scheme` on `<html>`.
48 pub fn id(self) -> &'static str {
49 match self {
50 Self::Graphite => "graphite",
51 Self::Cobalt => "cobalt",
52 Self::Oxblood => "oxblood",
53 Self::Teal => "teal",
54 Self::Plum => "plum",
55 Self::Mono => "mono",
56 Self::Tangerine => "tangerine",
57 Self::Sage => "sage",
58 Self::Spark => "spark",
59 }
60 }
61
62 /// Parses a cookie or form value. Returns `None` for unknown schemes.
63 pub fn from_id(id: &str) -> Option<Self> {
64 ALL.into_iter().find(|s| s.id() == id)
65 }
66
67 /// The proper name of the scheme. Names are not translated.
68 pub fn name(self) -> &'static str {
69 match self {
70 Self::Graphite => "Graphite & Highlighter",
71 Self::Cobalt => "Cobalt Ink",
72 Self::Oxblood => "Oxblood",
73 Self::Teal => "Teal Ledger",
74 Self::Plum => "Plum",
75 Self::Mono => "Monochrome",
76 Self::Tangerine => "Tangerine",
77 Self::Sage => "Sage",
78 Self::Spark => "Spark",
79 }
80 }
81
82 /// Three swatch colors as `(light, dark)` pairs: the sidebar, the accent, and the
83 /// active row. The picker draws them as dots.
84 pub fn dots(self) -> [(&'static str, &'static str); 3] {
85 match self {
86 // @dots:begin
87 Self::Graphite => [("#eceef2", "#1d2026"), ("#f2b600", "#ffc93c"), ("#ffe08a", "#5c4700")],
88 Self::Cobalt => [("#eef3fb", "#121a2c"), ("#1f4fd1", "#5b8bff"), ("#dce7ff", "#1d2f5c")],
89 Self::Oxblood => [("#eeeae8", "#1c1818"), ("#8e1b22", "#d85c63"), ("#f8dcd9", "#4a1d21")],
90 Self::Teal => [("#e8f0f0", "#121d1c"), ("#0f5e5a", "#3fb3a6"), ("#cdeee9", "#15403b")],
91 Self::Plum => [("#efeaf3", "#1a1620"), ("#7b2d6e", "#d27ac4"), ("#f1d9ee", "#4a2446")],
92 Self::Mono => [("#f0f0f0", "#161616"), ("#111111", "#f2f2f2"), ("#e4e4e4", "#2e2e2e")],
93 Self::Tangerine => [("#eceef0", "#16191c"), ("#f59e0b", "#ffb347"), ("#fde9c4", "#4f3412")],
94 Self::Sage => [("#e9efe6", "#111211"), ("#567752", "#4a6847"), ("#d9ebd3", "#233024")],
95 Self::Spark => [("#eeeef0", "#1a1512"), ("#ff8205", "#ff9a2e"), ("#ffe8c7", "#58320a")],
96 // @dots:end
97 }
98 }
99}
100
101/// The scheme of this request. The `scheme` cookie wins; an unknown or missing value
102/// means the default.
103pub fn scheme(cx: &Cx) -> Scheme {
104 cookies(cx)
105 .get(SCHEME_COOKIE)
106 .and_then(|cookie| Scheme::from_id(cookie.value()))
107 .unwrap_or(DEFAULT)
108}
109
110/// The cookie that remembers `scheme` for a year.
111pub fn scheme_cookie(scheme: Scheme) -> Cookie<'static> {
112 Cookie::build((SCHEME_COOKIE, scheme.id()))
113 .path("/")
114 .same_site(SameSite::Lax)
115 .max_age(Duration::days(SCHEME_COOKIE_DAYS))
116 .build()
117}