Per-theme background and accent colours, instant-save appearance page
Browse files at this commit9261c0f58e1fceb0d2d51094b1bb0684a41e41e6Parent: 894eaf4- feat
- per-theme background and accent colours, instant-save appearance page
19 changed files +1225 -318
- crates/forge-core/migrations/0042_colours.sql +12 -0
- crates/forge-core/src/authz.rs +3 -0
- crates/forge-core/src/model.rs +14 -1
- crates/forge-core/src/store/account_tokens.rs +4 -1
- crates/forge-core/src/store/migrate.rs +1 -0
- crates/forge-core/src/store/mod.rs +11 -2
- crates/forge-core/src/store/orgs.rs +2 -2
- crates/forge-core/src/store/ui_prefs.rs +131 -6
- crates/forge-core/src/store/user_delete.rs +3 -0
- crates/forge-web/assets/nav.js +39 -2
- crates/forge-web/build.rs +118 -42
- crates/forge-web/src/accent.rs +118 -37
- crates/forge-web/src/accent_palette.rs +252 -0
- crates/forge-web/src/i18n/accents.rs +29 -2
- crates/forge-web/src/i18n/misc.rs +16 -12
- crates/forge-web/src/pages/settings_appearance.rs +223 -89
- crates/forge-web/src/views/mod.rs +51 -22
- crates/forge-web/tests/ui_prefs_e2e.rs +127 -49
- style/main.scss +71 -51
crates/forge-core/migrations/0042_colours.sqladded+12 -0
| @@ -0,0 +1,12 @@ | ||
| 1 | +-- Colour choices per user: the accent and base palettes for the light and the dark theme. The | |
| 2 | +-- ids are those of forge-web's accent_palette.rs, which decides what exists, so there is no | |
| 3 | +-- CHECK list here. Applied by store/migrate.rs. | |
| 4 | +-- | |
| 5 | +-- `accent` now means the light-theme accent. The dark accent starts as a copy of it, so an | |
| 6 | +-- existing user sees the same colours in both themes until they choose otherwise. | |
| 7 | + | |
| 8 | +ALTER TABLE users ADD COLUMN accent_dark TEXT NOT NULL DEFAULT 'sage'; | |
| 9 | +ALTER TABLE users ADD COLUMN base_light TEXT NOT NULL DEFAULT 'fern'; | |
| 10 | +ALTER TABLE users ADD COLUMN base_dark TEXT NOT NULL DEFAULT 'fern'; | |
| 11 | + | |
| 12 | +UPDATE users SET accent_dark = accent; |
crates/forge-core/src/authz.rsmodified+3 -0
| @@ -129,6 +129,9 @@ mod tests { | ||
| 129 | 129 | theme: "auto".into(), |
| 130 | 130 | language: "auto".into(), |
| 131 | 131 | accent: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 132 | + accent_dark: crate::store::ui_prefs::DEFAULT_ACCENT.into(), | |
| 133 | + base_light: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 134 | + base_dark: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 132 | 135 | kind: AccountKind::User, |
| 133 | 136 | email_notifications: true, |
| 134 | 137 | } |
crates/forge-core/src/model.rsmodified+14 -1
| @@ -47,8 +47,15 @@ pub struct User { | ||
| 47 | 47 | pub theme: String, |
| 48 | 48 | /// Language code, e.g. "en"; "auto" uses the instance default. |
| 49 | 49 | pub language: String, |
| 50 | - /// Accent colour: the id of a palette in forge-web's `accent_palette.rs`. "sage" is the default. | |
| 50 | + /// Accent colour for the light theme: the id of a palette in forge-web's `accent_palette.rs`. | |
| 51 | + /// "sage" is the default. | |
| 51 | 52 | pub accent: String, |
| 53 | + /// Accent colour for the dark theme, chosen separately from `accent`. "sage" is the default. | |
| 54 | + pub accent_dark: String, | |
| 55 | + /// Base (neutral background, surface and text) set for the light theme. "fern" is the default. | |
| 56 | + pub base_light: String, | |
| 57 | + /// Base set for the dark theme. "fern" is the default. | |
| 58 | + pub base_dark: String, | |
| 52 | 59 | /// `User` for a person, `Org` for an organization. |
| 53 | 60 | pub kind: AccountKind, |
| 54 | 61 | /// Whether notifications are also sent by email (needs a verified email and instance mail). |
| @@ -130,6 +137,9 @@ impl std::fmt::Debug for User { | ||
| 130 | 137 | .field("theme", &self.theme) |
| 131 | 138 | .field("language", &self.language) |
| 132 | 139 | .field("accent", &self.accent) |
| 140 | + .field("accent_dark", &self.accent_dark) | |
| 141 | + .field("base_light", &self.base_light) | |
| 142 | + .field("base_dark", &self.base_dark) | |
| 133 | 143 | .field("kind", &self.kind) |
| 134 | 144 | .finish() |
| 135 | 145 | } |
| @@ -219,6 +229,9 @@ mod tests { | ||
| 219 | 229 | theme: "auto".into(), |
| 220 | 230 | language: "auto".into(), |
| 221 | 231 | accent: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 232 | + accent_dark: crate::store::ui_prefs::DEFAULT_ACCENT.into(), | |
| 233 | + base_light: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 234 | + base_dark: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 222 | 235 | kind: AccountKind::User, |
| 223 | 236 | email_notifications: true, |
| 224 | 237 | }; |
crates/forge-core/src/store/account_tokens.rsmodified+4 -1
| @@ -100,7 +100,7 @@ impl Store { | ||
| 100 | 100 | &conn, |
| 101 | 101 | "SELECT id, username, email, password_hash, is_admin, created_at, about, timezone, \ |
| 102 | 102 | (SELECT hash FROM avatars WHERE avatars.user_id = users.id) AS avatar_hash, \ |
| 103 | - theme, language, kind, email_notifications, display_name, accent FROM users WHERE LOWER(email) = LOWER(?1) \ | |
| 103 | + theme, language, kind, email_notifications, display_name, accent, accent_dark, base_light, base_dark FROM users WHERE LOWER(email) = LOWER(?1) \ | |
| 104 | 104 | AND email_verified_at IS NOT NULL AND kind = 'user' LIMIT 1", |
| 105 | 105 | (email.to_owned(),), |
| 106 | 106 | map_user, |
| @@ -304,6 +304,9 @@ impl Store { | ||
| 304 | 304 | theme: "light".into(), |
| 305 | 305 | language: "auto".into(), |
| 306 | 306 | accent: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 307 | + accent_dark: crate::store::ui_prefs::DEFAULT_ACCENT.into(), | |
| 308 | + base_light: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 309 | + base_dark: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 307 | 310 | kind: AccountKind::User, |
| 308 | 311 | email_notifications: true, |
| 309 | 312 | }; |
crates/forge-core/src/store/migrate.rsmodified+1 -0
| @@ -60,6 +60,7 @@ pub const MIGRATIONS: &[(u32, &str)] = &[ | ||
| 60 | 60 | (39, include_str!("../../migrations/0039_totp_device.sql")), |
| 61 | 61 | (40, include_str!("../../migrations/0040_mail_queue.sql")), |
| 62 | 62 | (41, include_str!("../../migrations/0041_accent.sql")), |
| 63 | + (42, include_str!("../../migrations/0042_colours.sql")), | |
| 63 | 64 | ]; |
| 64 | 65 | |
| 65 | 66 | /// The highest migration version this binary knows about. |
crates/forge-core/src/store/mod.rsmodified+11 -2
| @@ -110,7 +110,7 @@ struct Inner { | ||
| 110 | 110 | |
| 111 | 111 | const USER_COLS: &str = "id, username, email, password_hash, is_admin, created_at, about, timezone, \ |
| 112 | 112 | (SELECT hash FROM avatars WHERE avatars.user_id = users.id) AS avatar_hash, \ |
| 113 | - theme, language, kind, email_notifications, display_name, accent"; | |
| 113 | + theme, language, kind, email_notifications, display_name, accent, accent_dark, base_light, base_dark"; | |
| 114 | 114 | const SESSION_COLS: &str = "id, user_id, csrf_token, created_at, expires_at, last_seen, user_agent"; |
| 115 | 115 | const SSH_KEY_COLS: &str = |
| 116 | 116 | "id, user_id, title, algorithm, key_blob, fingerprint, created_at, last_used_at"; |
| @@ -201,6 +201,9 @@ impl Store { | ||
| 201 | 201 | theme: "light".into(), |
| 202 | 202 | language: "auto".into(), |
| 203 | 203 | accent: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 204 | + accent_dark: crate::store::ui_prefs::DEFAULT_ACCENT.into(), | |
| 205 | + base_light: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 206 | + base_dark: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 204 | 207 | kind: AccountKind::User, |
| 205 | 208 | email_notifications: true, |
| 206 | 209 | }; |
| @@ -245,6 +248,9 @@ impl Store { | ||
| 245 | 248 | theme: "light".into(), |
| 246 | 249 | language: "auto".into(), |
| 247 | 250 | accent: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 251 | + accent_dark: crate::store::ui_prefs::DEFAULT_ACCENT.into(), | |
| 252 | + base_light: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 253 | + base_dark: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 248 | 254 | kind: AccountKind::User, |
| 249 | 255 | email_notifications: true, |
| 250 | 256 | }; |
| @@ -1039,6 +1045,9 @@ fn map_user(row: &Row) -> Result<User> { | ||
| 1039 | 1045 | email_notifications: col_bool(row, 12)?, |
| 1040 | 1046 | display_name: col_opt_text(row, 13)?, |
| 1041 | 1047 | accent: col_text(row, 14)?, |
| 1048 | + accent_dark: col_text(row, 15)?, | |
| 1049 | + base_light: col_text(row, 16)?, | |
| 1050 | + base_dark: col_text(row, 17)?, | |
| 1042 | 1051 | }) |
| 1043 | 1052 | } |
| 1044 | 1053 | |
| @@ -2169,7 +2178,7 @@ impl Store { | ||
| 2169 | 2178 | WHERE collaborators.repo_id = ?1 ORDER BY users.username_folded" |
| 2170 | 2179 | ), |
| 2171 | 2180 | (repo_id.to_string(),), |
| 2172 | - |row| Ok((map_user(row)?, parse_role(&col_text(row, 15)?)?)), | |
| 2181 | + |row| Ok((map_user(row)?, parse_role(&col_text(row, 18)?)?)), | |
| 2173 | 2182 | ) |
| 2174 | 2183 | .await |
| 2175 | 2184 | } |
crates/forge-core/src/store/orgs.rsmodified+2 -2
| @@ -277,7 +277,7 @@ impl Store { | ||
| 277 | 277 | ORDER BY (org_role = 'owner') DESC, username_folded" |
| 278 | 278 | ), |
| 279 | 279 | (org_id.to_string(),), |
| 280 | - |row| Ok((map_user(row)?, OrgMemberRole::parse(&col_text(row, 15)?)?)), | |
| 280 | + |row| Ok((map_user(row)?, OrgMemberRole::parse(&col_text(row, 18)?)?)), | |
| 281 | 281 | ) |
| 282 | 282 | .await |
| 283 | 283 | } |
| @@ -397,7 +397,7 @@ impl Store { | ||
| 397 | 397 | (SELECT org_id FROM org_members WHERE user_id = ?1) ORDER BY username_folded" |
| 398 | 398 | ), |
| 399 | 399 | (user_id.to_string(),), |
| 400 | - |row| Ok((map_user(row)?, OrgMemberRole::parse(&col_text(row, 15)?)?)), | |
| 400 | + |row| Ok((map_user(row)?, OrgMemberRole::parse(&col_text(row, 18)?)?)), | |
| 401 | 401 | ) |
| 402 | 402 | .await |
| 403 | 403 | } |
crates/forge-core/src/store/ui_prefs.rsmodified+131 -6
| @@ -1,7 +1,8 @@ | ||
| 1 | -//! Per-user UI preferences: the colour theme, the language and the accent colour. All are stored | |
| 2 | -//! as text in `users` (migrations 0030 and 0041). The web layer decides which values it offers; | |
| 3 | -//! the store only checks that a value is well formed, so a new language or accent needs no store | |
| 4 | -//! change. | |
| 1 | +//! Per-user UI preferences: the colour theme, the language and the colour choices. The colours | |
| 2 | +//! are an accent and a base (neutral background, surface and text) for each of the light and the | |
| 3 | +//! dark theme. All are stored as text in `users` (migrations 0030, 0041 and 0042). The web layer | |
| 4 | +//! decides which values it offers; the store only checks that a value is well formed, so a new | |
| 5 | +//! language, accent or base needs no store change. | |
| 5 | 6 | |
| 6 | 7 | use super::{Store, db_err}; |
| 7 | 8 | use crate::error::{Error, Result}; |
| @@ -16,6 +17,10 @@ pub const AUTO: &str = "auto"; | ||
| 16 | 17 | /// `accent_palette.rs`, which a test there checks. |
| 17 | 18 | pub const DEFAULT_ACCENT: &str = "sage"; |
| 18 | 19 | |
| 20 | +/// The base colour set a new user gets, for both themes. It must match the default base in | |
| 21 | +/// forge-web's `accent_palette.rs`, which a test there checks. | |
| 22 | +pub const DEFAULT_BASE: &str = "fern"; | |
| 23 | + | |
| 19 | 24 | /// Longest accepted language code, in bytes. |
| 20 | 25 | const MAX_LANGUAGE_LEN: usize = 16; |
| 21 | 26 | |
| @@ -70,7 +75,8 @@ impl Store { | ||
| 70 | 75 | Ok(()) |
| 71 | 76 | } |
| 72 | 77 | |
| 73 | - /// Sets the user's accent colour id. A malformed id is rejected and nothing is written. | |
| 78 | + /// Sets the user's accent colour id for both themes. A malformed id is rejected and nothing is | |
| 79 | + /// written. | |
| 74 | 80 | pub async fn set_accent(&self, user_id: &str, accent: &str) -> Result<()> { |
| 75 | 81 | if !valid_accent(accent) { |
| 76 | 82 | return Err(Error::Invalid(format!("accent {accent:?}"))); |
| @@ -78,7 +84,7 @@ impl Store { | ||
| 78 | 84 | let conn = self.inner.conn.lock().await; |
| 79 | 85 | let n = conn |
| 80 | 86 | .execute( |
| 81 | - "UPDATE users SET accent = ?2 WHERE id = ?1", | |
| 87 | + "UPDATE users SET accent = ?2, accent_dark = ?2 WHERE id = ?1", | |
| 82 | 88 | (user_id.to_string(), accent.to_string()), |
| 83 | 89 | ) |
| 84 | 90 | .await |
| @@ -89,6 +95,47 @@ impl Store { | ||
| 89 | 95 | Ok(()) |
| 90 | 96 | } |
| 91 | 97 | |
| 98 | + /// Sets the four colour choices: the accent and the base for the light theme, then the accent | |
| 99 | + /// and the base for the dark theme. If any id is malformed, nothing is written. | |
| 100 | + pub async fn set_colours( | |
| 101 | + &self, | |
| 102 | + user_id: &str, | |
| 103 | + accent_light: &str, | |
| 104 | + accent_dark: &str, | |
| 105 | + base_light: &str, | |
| 106 | + base_dark: &str, | |
| 107 | + ) -> Result<()> { | |
| 108 | + for accent in [accent_light, accent_dark] { | |
| 109 | + if !valid_accent(accent) { | |
| 110 | + return Err(Error::Invalid(format!("accent {accent:?}"))); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + for base in [base_light, base_dark] { | |
| 114 | + if !valid_accent(base) { | |
| 115 | + return Err(Error::Invalid(format!("base {base:?}"))); | |
| 116 | + } | |
| 117 | + } | |
| 118 | + let conn = self.inner.conn.lock().await; | |
| 119 | + let n = conn | |
| 120 | + .execute( | |
| 121 | + "UPDATE users SET accent = ?2, accent_dark = ?3, base_light = ?4, base_dark = ?5 \ | |
| 122 | + WHERE id = ?1", | |
| 123 | + ( | |
| 124 | + user_id.to_string(), | |
| 125 | + accent_light.to_string(), | |
| 126 | + accent_dark.to_string(), | |
| 127 | + base_light.to_string(), | |
| 128 | + base_dark.to_string(), | |
| 129 | + ), | |
| 130 | + ) | |
| 131 | + .await | |
| 132 | + .map_err(db_err)?; | |
| 133 | + if n == 0 { | |
| 134 | + return Err(Error::NotFound); | |
| 135 | + } | |
| 136 | + Ok(()) | |
| 137 | + } | |
| 138 | + | |
| 92 | 139 | /// Turns the email copy of notifications on or off for one user (migration 0035). |
| 93 | 140 | pub async fn set_email_notifications(&self, user_id: &str, on: bool) -> Result<()> { |
| 94 | 141 | let conn = self.inner.conn.lock().await; |
| @@ -159,6 +206,84 @@ mod tests { | ||
| 159 | 206 | ); |
| 160 | 207 | } |
| 161 | 208 | |
| 209 | + #[tokio::test] | |
| 210 | + async fn colours_persist_and_bad_values_change_nothing() { | |
| 211 | + let dir = tempfile::tempdir().unwrap(); | |
| 212 | + let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | |
| 213 | + let user = store | |
| 214 | + .create_user("tahinli", None, "hash", false) | |
| 215 | + .await | |
| 216 | + .unwrap(); | |
| 217 | + assert_eq!(user.accent_dark, DEFAULT_ACCENT); | |
| 218 | + assert_eq!(user.base_light, DEFAULT_BASE); | |
| 219 | + assert_eq!(user.base_dark, DEFAULT_BASE); | |
| 220 | + | |
| 221 | + store | |
| 222 | + .set_colours(&user.id, "ocean", "rose", "stone", "ink") | |
| 223 | + .await | |
| 224 | + .unwrap(); | |
| 225 | + let stored = store.user_by_id(&user.id).await.unwrap().unwrap(); | |
| 226 | + assert_eq!( | |
| 227 | + ( | |
| 228 | + stored.accent.as_str(), | |
| 229 | + stored.accent_dark.as_str(), | |
| 230 | + stored.base_light.as_str(), | |
| 231 | + stored.base_dark.as_str() | |
| 232 | + ), | |
| 233 | + ("ocean", "rose", "stone", "ink") | |
| 234 | + ); | |
| 235 | + | |
| 236 | + assert!( | |
| 237 | + store | |
| 238 | + .set_colours(&user.id, "ocean", "Rose", "stone", "ink") | |
| 239 | + .await | |
| 240 | + .is_err() | |
| 241 | + ); | |
| 242 | + assert!( | |
| 243 | + store | |
| 244 | + .set_colours(&user.id, "ocean", "rose", "../x", "ink") | |
| 245 | + .await | |
| 246 | + .is_err() | |
| 247 | + ); | |
| 248 | + assert!( | |
| 249 | + store | |
| 250 | + .set_colours(&user.id, "ocean", "rose", "stone", "") | |
| 251 | + .await | |
| 252 | + .is_err() | |
| 253 | + ); | |
| 254 | + let still = store.user_by_id(&user.id).await.unwrap().unwrap(); | |
| 255 | + assert_eq!( | |
| 256 | + ( | |
| 257 | + still.accent.as_str(), | |
| 258 | + still.accent_dark.as_str(), | |
| 259 | + still.base_light.as_str(), | |
| 260 | + still.base_dark.as_str() | |
| 261 | + ), | |
| 262 | + ("ocean", "rose", "stone", "ink") | |
| 263 | + ); | |
| 264 | + } | |
| 265 | + | |
| 266 | + #[tokio::test] | |
| 267 | + async fn set_accent_sets_both_accents_and_keeps_bases() { | |
| 268 | + let dir = tempfile::tempdir().unwrap(); | |
| 269 | + let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | |
| 270 | + let user = store | |
| 271 | + .create_user("tahinli", None, "hash", false) | |
| 272 | + .await | |
| 273 | + .unwrap(); | |
| 274 | + store | |
| 275 | + .set_colours(&user.id, "sage", "sage", "mist", "ink") | |
| 276 | + .await | |
| 277 | + .unwrap(); | |
| 278 | + | |
| 279 | + store.set_accent(&user.id, "ocean").await.unwrap(); | |
| 280 | + let stored = store.user_by_id(&user.id).await.unwrap().unwrap(); | |
| 281 | + assert_eq!(stored.accent, "ocean"); | |
| 282 | + assert_eq!(stored.accent_dark, "ocean"); | |
| 283 | + assert_eq!(stored.base_light, "mist"); | |
| 284 | + assert_eq!(stored.base_dark, "ink"); | |
| 285 | + } | |
| 286 | + | |
| 162 | 287 | #[tokio::test] |
| 163 | 288 | async fn prefs_persist_and_bad_values_change_nothing() { |
| 164 | 289 | let dir = tempfile::tempdir().unwrap(); |
crates/forge-core/src/store/user_delete.rsmodified+3 -0
| @@ -167,6 +167,9 @@ impl Store { | ||
| 167 | 167 | theme: "light".into(), |
| 168 | 168 | language: "auto".into(), |
| 169 | 169 | accent: crate::store::ui_prefs::DEFAULT_ACCENT.into(), |
| 170 | + accent_dark: crate::store::ui_prefs::DEFAULT_ACCENT.into(), | |
| 171 | + base_light: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 172 | + base_dark: crate::store::ui_prefs::DEFAULT_BASE.into(), | |
| 170 | 173 | kind: AccountKind::User, |
| 171 | 174 | email_notifications: true, |
| 172 | 175 | }; |
crates/forge-web/assets/nav.jsmodified+39 -2
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | var liveDirty = false; // a live change came while a navigation was on the wire |
| 14 | 14 | var inFlight = 0; // requests on the wire, drives aria-busy |
| 15 | 15 | var pending = new WeakSet(); // forms with a request on the wire: a second submit is ignored |
| 16 | + var queued = new WeakSet(); // forms that changed while pending: submitted again once it completes | |
| 16 | 17 | var shown = location.pathname + location.search; |
| 17 | 18 | var GET_INIT = { headers: { accept: 'text/html' } }; |
| 18 | 19 | |
| @@ -46,9 +47,11 @@ | ||
| 46 | 47 | } |
| 47 | 48 | |
| 48 | 49 | // Field preservation: a dirty or focused field keeps its value, and the caret offsets come along. |
| 50 | + // Radios and checkboxes share a name, so their value is part of the key to tell them apart. | |
| 49 | 51 | function fieldKey(el) { |
| 50 | 52 | var form = el.form ? (el.form.getAttribute('action') || '') : ''; |
| 51 | - return form + '|' + (el.name || '') + '|' + (el.type || '') + '|' + el.tagName; | |
| 53 | + var value = el.type === 'radio' || el.type === 'checkbox' ? el.value : ''; | |
| 54 | + return form + '|' + (el.name || '') + '|' + (el.type || '') + '|' + el.tagName + '|' + value; | |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | function savable(el) { |
| @@ -127,7 +130,7 @@ | ||
| 127 | 130 | |
| 128 | 131 | function adopt(doc, keepFields, dd) { |
| 129 | 132 | document.title = doc.title; |
| 130 | - ['lang', 'data-theme', 'data-accent', 'data-ui'].forEach(function (name) { | |
| 133 | + ['lang', 'data-theme', 'data-accent-light', 'data-accent-dark', 'data-base-light', 'data-base-dark', 'data-ui'].forEach(function (name) { | |
| 131 | 134 | if (doc.documentElement.hasAttribute(name)) { root.setAttribute(name, doc.documentElement.getAttribute(name)); } |
| 132 | 135 | else { root.removeAttribute(name); } |
| 133 | 136 | }); |
| @@ -245,9 +248,24 @@ | ||
| 245 | 248 | userBusy -= 1; |
| 246 | 249 | if (liveDirty) { refreshLive(); } |
| 247 | 250 | } |
| 251 | + if (o.form && queued.has(o.form)) { queued.delete(o.form); resend(o.form); } | |
| 248 | 252 | }); |
| 249 | 253 | } |
| 250 | 254 | |
| 255 | + // The form to send a queued change on: the form itself, or after a swap the one with the same action. | |
| 256 | + function resend(form) { | |
| 257 | + var target = form; | |
| 258 | + if (!form.isConnected) { | |
| 259 | + target = null; | |
| 260 | + var action = form.getAttribute('action') || ''; | |
| 261 | + for (var i = 0; i < document.forms.length; i++) { | |
| 262 | + if ((document.forms[i].getAttribute('action') || '') === action) { target = document.forms[i]; break; } | |
| 263 | + } | |
| 264 | + } | |
| 265 | + if (!target) { return; } | |
| 266 | + if (target.requestSubmit) { target.requestSubmit(); } else { target.submit(); } | |
| 267 | + } | |
| 268 | + | |
| 251 | 269 | function saveScroll() { |
| 252 | 270 | try { history.replaceState({ y: window.scrollY }, '', location.href); } catch (err) { } |
| 253 | 271 | } |
| @@ -399,9 +417,28 @@ | ||
| 399 | 417 | document.addEventListener('change', function (e) { |
| 400 | 418 | var c = e.target && e.target.closest ? e.target.closest('[data-autosubmit]') : null; |
| 401 | 419 | if (!c || !c.form) { return; } |
| 420 | + // A request is already on the wire for this form: the change waits for it, and is sent when it completes. | |
| 421 | + if (pending.has(c.form)) { queued.add(c.form); return; } | |
| 402 | 422 | if (c.form.requestSubmit) { c.form.requestSubmit(); } else { c.form.submit(); } |
| 403 | 423 | }); |
| 404 | 424 | |
| 425 | + // Appearance page: a colour or theme choice shows on <html> at once, before the form's request returns. | |
| 426 | + // The attributes match what the shell renders: theme `auto` has no data-theme, `dark` is dark, else light. | |
| 427 | + var PREVIEW_ATTR = { accent_light: 'data-accent-light', accent_dark: 'data-accent-dark', base_light: 'data-base-light', base_dark: 'data-base-dark' }; | |
| 428 | + | |
| 429 | + document.addEventListener('change', function (e) { | |
| 430 | + var el = e.target; | |
| 431 | + if (!el || !el.form || !el.form.hasAttribute('data-appearance')) { return; } | |
| 432 | + if (el.name === 'theme') { | |
| 433 | + if (el.value === 'auto') { root.removeAttribute('data-theme'); } | |
| 434 | + else { root.setAttribute('data-theme', el.value === 'dark' ? 'dark' : 'light'); } | |
| 435 | + return; | |
| 436 | + } | |
| 437 | + if (el.type === 'radio' && Object.prototype.hasOwnProperty.call(PREVIEW_ATTR, el.name)) { | |
| 438 | + root.setAttribute(PREVIEW_ATTR[el.name], el.value); | |
| 439 | + } | |
| 440 | + }, false); | |
| 441 | + | |
| 405 | 442 | // A filtered view asked for in place: the URL is replaced (no history entry per change), fields |
| 406 | 443 | // and caret come through the swap, and the scroll stays put. |
| 407 | 444 | // Live unread count. The bell is re-rendered by every swap, so it is looked up on each update. One |
crates/forge-web/build.rsmodified+118 -42
| @@ -2,8 +2,8 @@ | ||
| 2 | 2 | //! asset also gets a content hash (FORGE_MAIN_CSS_V, FORGE_NAV_JS_V) that the page links use as |
| 3 | 3 | //! their cache-busting query, so an unchanged file keeps its URL and a changed one gets a new one. |
| 4 | 4 | //! |
| 5 | -//! The accent colours come from `src/accent_palette.rs`, the same data the app reads. This script | |
| 6 | -//! turns it into `_accents.scss` in OUT_DIR, which main.scss imports. | |
| 5 | +//! The accent colours and the neutral bases come from `src/accent_palette.rs`, the same data the | |
| 6 | +//! app reads. This script turns it into `_accents.scss` in OUT_DIR, which main.scss imports. | |
| 7 | 7 | |
| 8 | 8 | use std::{env, fs, path::PathBuf}; |
| 9 | 9 | |
| @@ -14,77 +14,153 @@ use sha2::{Digest, Sha256}; | ||
| 14 | 14 | #[allow(dead_code)] |
| 15 | 15 | mod accent_palette; |
| 16 | 16 | |
| 17 | -use accent_palette::{DEFAULT, PALETTES, Palette, TOKEN_VARS, Tokens}; | |
| 17 | +use accent_palette::{ | |
| 18 | + BASE_DEFAULT, BASE_TOKEN_VARS, BASES, Base, BaseTokens, DEFAULT, PALETTES, Palette, TOKEN_VARS, | |
| 19 | + Tokens, | |
| 20 | +}; | |
| 18 | 21 | |
| 19 | 22 | /// First 12 hex characters of the SHA-256 of `bytes`. |
| 20 | 23 | fn content_version(bytes: &[u8]) -> String { |
| 21 | 24 | encode(Sha256::digest(bytes))[..12].to_string() |
| 22 | 25 | } |
| 23 | 26 | |
| 24 | -/// One `--token: value;` line per token, indented for a rule body. | |
| 25 | -fn declarations(tokens: &Tokens) -> String { | |
| 27 | +/// The `(name, value)` declarations of one accent palette's tokens in one theme. | |
| 28 | +fn accent_decls(tokens: &Tokens) -> Vec<(&'static str, &'static str)> { | |
| 26 | 29 | TOKEN_VARS |
| 27 | 30 | .iter() |
| 28 | - .map(|token| format!(" {}: {};\n", token.name, (token.value)(tokens))) | |
| 31 | + .map(|token| (token.name, (token.value)(tokens))) | |
| 29 | 32 | .collect() |
| 30 | 33 | } |
| 31 | 34 | |
| 32 | -/// Appends `selector { … }` with the palette's tokens for one theme. `indent` is 0 or 2. | |
| 33 | -fn rule(css: &mut String, indent: &str, selector: &str, tokens: &Tokens) { | |
| 35 | +/// The `(name, value)` declarations of one base's neutral tokens in one theme. | |
| 36 | +fn base_decls(tokens: &BaseTokens) -> Vec<(&'static str, &'static str)> { | |
| 37 | + BASE_TOKEN_VARS | |
| 38 | + .iter() | |
| 39 | + .map(|token| (token.name, (token.value)(tokens))) | |
| 40 | + .collect() | |
| 41 | +} | |
| 42 | + | |
| 43 | +/// Appends `selector { … }` with one `--token: value;` line per declaration. `indent` is 0 or 2. | |
| 44 | +fn rule(css: &mut String, indent: &str, selector: &str, decls: &[(&str, &str)]) { | |
| 34 | 45 | css.push_str(&format!("{indent}{selector} {{\n")); |
| 35 | - for line in declarations(tokens).lines() { | |
| 36 | - css.push_str(&format!("{indent}{line}\n")); | |
| 46 | + for (name, value) in decls { | |
| 47 | + css.push_str(&format!("{indent} {name}: {value};\n")); | |
| 37 | 48 | } |
| 38 | 49 | css.push_str(&format!("{indent}}}\n")); |
| 39 | 50 | } |
| 40 | 51 | |
| 41 | -/// The rules for one palette. The default palette sits on bare `:root`, as it always has. Every | |
| 42 | -/// other palette is scoped to `[data-accent]`, which is more specific, so it wins over the | |
| 43 | -/// default in all three theme states: light, dark by `data-theme`, and dark by the OS. | |
| 44 | -fn palette_rules(css: &mut String, palette: &Palette) { | |
| 45 | - let base = if palette.id == DEFAULT { | |
| 46 | - ":root".to_string() | |
| 47 | - } else { | |
| 48 | - format!(":root[data-accent=\"{}\"]", palette.id) | |
| 49 | - }; | |
| 50 | - let os_dark = format!("{base}:not([data-theme=\"light\"])"); | |
| 51 | - let forced_dark = format!("{base}[data-theme=\"dark\"]"); | |
| 52 | - rule(css, "", &base, &palette.light); | |
| 52 | +/// The rules for one choice: its light tokens on `light`, and its dark tokens on `dark` in both | |
| 53 | +/// dark states, by the OS and by `data-theme`. Dark selectors carry one more attribute than the | |
| 54 | +/// light one, so the dark rules win in dark mode whatever the light choice is. | |
| 55 | +fn choice_rules( | |
| 56 | + css: &mut String, | |
| 57 | + light: &str, | |
| 58 | + dark: &str, | |
| 59 | + light_decls: &[(&str, &str)], | |
| 60 | + dark_decls: &[(&str, &str)], | |
| 61 | +) { | |
| 62 | + let os_dark = format!("{dark}:not([data-theme=\"light\"])"); | |
| 63 | + let forced_dark = format!("{dark}[data-theme=\"dark\"]"); | |
| 64 | + rule(css, "", light, light_decls); | |
| 53 | 65 | css.push_str("@media (prefers-color-scheme: dark) {\n"); |
| 54 | - rule(css, " ", &os_dark, &palette.dark); | |
| 66 | + rule(css, " ", &os_dark, dark_decls); | |
| 55 | 67 | css.push_str("}\n"); |
| 56 | - rule(css, "", &forced_dark, &palette.dark); | |
| 68 | + rule(css, "", &forced_dark, dark_decls); | |
| 69 | +} | |
| 70 | + | |
| 71 | +/// The bare fallbacks for pages without the choice attributes: the default accent and base, on | |
| 72 | +/// plain `:root`. | |
| 73 | +fn fallback_rules(css: &mut String) { | |
| 74 | + let accent = PALETTES | |
| 75 | + .iter() | |
| 76 | + .find(|p| p.id == DEFAULT) | |
| 77 | + .expect("the default accent is one of the palettes"); | |
| 78 | + choice_rules( | |
| 79 | + css, | |
| 80 | + ":root", | |
| 81 | + ":root", | |
| 82 | + &accent_decls(&accent.light), | |
| 83 | + &accent_decls(&accent.dark), | |
| 84 | + ); | |
| 85 | + let base = BASES | |
| 86 | + .iter() | |
| 87 | + .find(|b| b.id == BASE_DEFAULT) | |
| 88 | + .expect("the default base is one of the bases"); | |
| 89 | + choice_rules( | |
| 90 | + css, | |
| 91 | + ":root", | |
| 92 | + ":root", | |
| 93 | + &base_decls(&base.light), | |
| 94 | + &base_decls(&base.dark), | |
| 95 | + ); | |
| 57 | 96 | } |
| 58 | 97 | |
| 59 | -/// The swatch chip for one palette in the settings picker: its accent, light or dark. | |
| 60 | -fn swatch_rules(css: &mut String, palette: &Palette) { | |
| 61 | - let swatch = format!(".accent-swatch[data-accent-swatch=\"{}\"]", palette.id); | |
| 62 | - css.push_str(&format!( | |
| 63 | - "{swatch} {{ background: {}; }}\n", | |
| 64 | - palette.light.accent | |
| 65 | - )); | |
| 66 | - css.push_str(&format!( | |
| 67 | - "@media (prefers-color-scheme: dark) {{\n :root:not([data-theme=\"light\"]) {swatch} {{ background: {}; }}\n}}\n", | |
| 68 | - palette.dark.accent | |
| 69 | - )); | |
| 70 | - css.push_str(&format!( | |
| 71 | - ":root[data-theme=\"dark\"] {swatch} {{ background: {}; }}\n", | |
| 72 | - palette.dark.accent | |
| 73 | - )); | |
| 98 | +/// The rules for one accent palette: its light tokens are keyed on `data-accent-light`, its dark | |
| 99 | +/// tokens on `data-accent-dark`. | |
| 100 | +fn palette_rules(css: &mut String, palette: &Palette) { | |
| 101 | + choice_rules( | |
| 102 | + css, | |
| 103 | + &format!(":root[data-accent-light=\"{}\"]", palette.id), | |
| 104 | + &format!(":root[data-accent-dark=\"{}\"]", palette.id), | |
| 105 | + &accent_decls(&palette.light), | |
| 106 | + &accent_decls(&palette.dark), | |
| 107 | + ); | |
| 108 | +} | |
| 109 | + | |
| 110 | +/// The rules for one base: its light tokens are keyed on `data-base-light`, its dark tokens on | |
| 111 | +/// `data-base-dark`. | |
| 112 | +fn base_rules(css: &mut String, base: &Base) { | |
| 113 | + choice_rules( | |
| 114 | + css, | |
| 115 | + &format!(":root[data-base-light=\"{}\"]", base.id), | |
| 116 | + &format!(":root[data-base-dark=\"{}\"]", base.id), | |
| 117 | + &base_decls(&base.light), | |
| 118 | + &base_decls(&base.dark), | |
| 119 | + ); | |
| 74 | 120 | } |
| 75 | 121 | |
| 76 | -/// The whole generated stylesheet: the palettes, then the swatch chips. | |
| 122 | +/// The swatch chips for the settings picker. They show fixed colours, so they do not depend on | |
| 123 | +/// the current theme: an accent chip is the accent itself, a base chip is its background and text | |
| 124 | +/// split diagonally. | |
| 125 | +fn swatch_rules(css: &mut String) { | |
| 126 | + for palette in PALETTES { | |
| 127 | + css.push_str(&format!( | |
| 128 | + ".accent-swatch[data-accent-light-swatch=\"{}\"] {{ background: {}; }}\n", | |
| 129 | + palette.id, palette.light.accent | |
| 130 | + )); | |
| 131 | + css.push_str(&format!( | |
| 132 | + ".accent-swatch[data-accent-dark-swatch=\"{}\"] {{ background: {}; }}\n", | |
| 133 | + palette.id, palette.dark.accent | |
| 134 | + )); | |
| 135 | + } | |
| 136 | + for base in BASES { | |
| 137 | + css.push_str(&format!( | |
| 138 | + ".base-swatch[data-base-light-swatch=\"{}\"] {{ background: linear-gradient(135deg, {} 0 50%, {} 50% 100%); }}\n", | |
| 139 | + base.id, base.light.bg, base.light.text | |
| 140 | + )); | |
| 141 | + css.push_str(&format!( | |
| 142 | + ".base-swatch[data-base-dark-swatch=\"{}\"] {{ background: linear-gradient(135deg, {} 0 50%, {} 50% 100%); }}\n", | |
| 143 | + base.id, base.dark.bg, base.dark.text | |
| 144 | + )); | |
| 145 | + } | |
| 146 | +} | |
| 147 | + | |
| 148 | +/// The whole generated stylesheet: the bare fallbacks, the accent and base rules, then the swatch | |
| 149 | +/// chips. | |
| 77 | 150 | fn accents_scss() -> String { |
| 78 | 151 | let mut css = String::from( |
| 79 | 152 | "/* Generated by crates/forge-web/build.rs from src/accent_palette.rs. Edit that file. */\n", |
| 80 | 153 | ); |
| 81 | 154 | assert_eq!(PALETTES[0].id, DEFAULT, "the default palette comes first"); |
| 155 | + assert_eq!(BASES[0].id, BASE_DEFAULT, "the default base comes first"); | |
| 156 | + fallback_rules(&mut css); | |
| 82 | 157 | for palette in PALETTES { |
| 83 | 158 | palette_rules(&mut css, palette); |
| 84 | 159 | } |
| 85 | - for palette in PALETTES { | |
| 86 | - swatch_rules(&mut css, palette); | |
| 160 | + for base in BASES { | |
| 161 | + base_rules(&mut css, base); | |
| 87 | 162 | } |
| 163 | + swatch_rules(&mut css); | |
| 88 | 164 | css |
| 89 | 165 | } |
| 90 | 166 |
crates/forge-web/src/accent.rsmodified+118 -37
| @@ -1,8 +1,8 @@ | ||
| 1 | -//! Accent colour: which palette a viewer sees. The colours live in `accent_palette.rs`, which | |
| 2 | -//! `build.rs` also turns into the stylesheet. This module only resolves stored ids and checks | |
| 3 | -//! which ids forge offers. | |
| 1 | +//! Accent colour and base: which palette and which neutral set a viewer sees. The values live in | |
| 2 | +//! `accent_palette.rs`, which `build.rs` also turns into the stylesheet. This module only resolves | |
| 3 | +//! stored ids and checks which ids forge offers. | |
| 4 | 4 | |
| 5 | -use crate::accent_palette::{DEFAULT, PALETTES, Palette}; | |
| 5 | +use crate::accent_palette::{BASE_DEFAULT, BASES, Base, DEFAULT, PALETTES, Palette}; | |
| 6 | 6 | |
| 7 | 7 | /// The palette for a stored id. An unknown or removed id falls back to the default. |
| 8 | 8 | pub fn resolve(stored: &str) -> &'static Palette { |
| @@ -22,6 +22,24 @@ pub fn ids() -> impl Iterator<Item = &'static str> { | ||
| 22 | 22 | PALETTES.iter().map(|p| p.id) |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | +/// The base for a stored id. An unknown or removed id falls back to the default. | |
| 26 | +pub fn resolve_base(stored: &str) -> &'static Base { | |
| 27 | + BASES | |
| 28 | + .iter() | |
| 29 | + .find(|b| b.id == stored) | |
| 30 | + .unwrap_or_else(default_base) | |
| 31 | +} | |
| 32 | + | |
| 33 | +/// True when forge offers this base id. | |
| 34 | +pub fn is_offered_base(id: &str) -> bool { | |
| 35 | + BASES.iter().any(|b| b.id == id) | |
| 36 | +} | |
| 37 | + | |
| 38 | +/// Every offered base id, in the order the picker lists them. | |
| 39 | +pub fn base_ids() -> impl Iterator<Item = &'static str> { | |
| 40 | + BASES.iter().map(|b| b.id) | |
| 41 | +} | |
| 42 | + | |
| 25 | 43 | fn default_palette() -> &'static Palette { |
| 26 | 44 | PALETTES |
| 27 | 45 | .iter() |
| @@ -29,18 +47,19 @@ fn default_palette() -> &'static Palette { | ||
| 29 | 47 | .expect("the default accent is one of the palettes") |
| 30 | 48 | } |
| 31 | 49 | |
| 50 | +fn default_base() -> &'static Base { | |
| 51 | + BASES | |
| 52 | + .iter() | |
| 53 | + .find(|b| b.id == BASE_DEFAULT) | |
| 54 | + .expect("the default base is one of the bases") | |
| 55 | +} | |
| 56 | + | |
| 32 | 57 | #[cfg(test)] |
| 33 | 58 | mod tests { |
| 34 | 59 | use std::collections::HashSet; |
| 35 | 60 | |
| 36 | 61 | use super::*; |
| 37 | - use crate::accent_palette::{TOKEN_VARS, Tokens}; | |
| 38 | - | |
| 39 | - /// The neutral backgrounds of style/main.scss, in each theme. | |
| 40 | - const LIGHT_BG: &str = "#F6F8F5"; | |
| 41 | - const LIGHT_SURFACE: &str = "#FFFFFF"; | |
| 42 | - const DARK_BG: &str = "#121613"; | |
| 43 | - const DARK_SURFACE: &str = "#191E1A"; | |
| 62 | + use crate::accent_palette::{BASE_TOKEN_VARS, TOKEN_VARS, Tokens}; | |
| 44 | 63 | |
| 45 | 64 | /// WCAG relative luminance of a `#RRGGBB` colour. |
| 46 | 65 | fn luminance(hex: &str) -> f64 { |
| @@ -82,6 +101,21 @@ mod tests { | ||
| 82 | 101 | } |
| 83 | 102 | } |
| 84 | 103 | |
| 104 | + #[test] | |
| 105 | + fn base_ids_are_slugs_unique_and_the_default_comes_first() { | |
| 106 | + assert_eq!(BASES[0].id, BASE_DEFAULT); | |
| 107 | + assert_eq!(BASE_DEFAULT, forge_core::store::ui_prefs::DEFAULT_BASE); | |
| 108 | + let mut seen = HashSet::new(); | |
| 109 | + for b in BASES { | |
| 110 | + assert!( | |
| 111 | + forge_core::store::ui_prefs::valid_accent(b.id), | |
| 112 | + "{} is not a slug", | |
| 113 | + b.id | |
| 114 | + ); | |
| 115 | + assert!(seen.insert(b.id), "{} is listed twice", b.id); | |
| 116 | + } | |
| 117 | + } | |
| 118 | + | |
| 85 | 119 | #[test] |
| 86 | 120 | fn every_token_is_a_hex_colour() { |
| 87 | 121 | for p in PALETTES { |
| @@ -91,6 +125,13 @@ mod tests { | ||
| 91 | 125 | } |
| 92 | 126 | } |
| 93 | 127 | } |
| 128 | + for b in BASES { | |
| 129 | + for token in BASE_TOKEN_VARS { | |
| 130 | + for t in [&b.light, &b.dark] { | |
| 131 | + assert!(is_hex_colour((token.value)(t)), "{} {}", b.id, token.name); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + } | |
| 94 | 135 | } |
| 95 | 136 | |
| 96 | 137 | #[test] |
| @@ -103,38 +144,78 @@ mod tests { | ||
| 103 | 144 | } |
| 104 | 145 | |
| 105 | 146 | #[test] |
| 106 | - fn every_palette_meets_the_contrast_targets() { | |
| 147 | + fn unknown_and_removed_base_ids_fall_back_to_the_default() { | |
| 148 | + assert_eq!(resolve_base("ink").id, "ink"); | |
| 149 | + assert_eq!(resolve_base("removed-base").id, BASE_DEFAULT); | |
| 150 | + assert_eq!(resolve_base("").id, BASE_DEFAULT); | |
| 151 | + assert!(is_offered_base("mist")); | |
| 152 | + assert!(!is_offered_base("removed-base")); | |
| 153 | + } | |
| 154 | + | |
| 155 | + #[test] | |
| 156 | + fn every_accent_keeps_its_own_text_readable() { | |
| 107 | 157 | for p in PALETTES { |
| 108 | - let themes = [ | |
| 109 | - ("light", &p.light, LIGHT_BG, LIGHT_SURFACE), | |
| 110 | - ("dark", &p.dark, DARK_BG, DARK_SURFACE), | |
| 111 | - ]; | |
| 112 | - for (theme, t, bg, surface) in themes { | |
| 113 | - let check = |what: &str, ratio: f64, min: f64| { | |
| 114 | - assert!(ratio >= min, "{} {theme} {what}: {ratio:.2} < {min}", p.id); | |
| 115 | - }; | |
| 116 | - check( | |
| 117 | - "accent_text on accent", | |
| 118 | - contrast(t.accent_text, t.accent), | |
| 119 | - 4.5, | |
| 120 | - ); | |
| 121 | - check("accent_ink on bg", contrast(t.accent_ink, bg), 4.5); | |
| 122 | - check( | |
| 123 | - "accent_ink on surface", | |
| 124 | - contrast(t.accent_ink, surface), | |
| 125 | - 4.5, | |
| 126 | - ); | |
| 127 | - check( | |
| 128 | - "accent_ink on accent_soft", | |
| 129 | - contrast(t.accent_ink, t.accent_soft), | |
| 130 | - 4.5, | |
| 131 | - ); | |
| 158 | + let check = |what: &str, ratio: f64, min: f64| { | |
| 159 | + assert!(ratio >= min, "{} {what}: {ratio:.2} < {min}", p.id); | |
| 160 | + }; | |
| 161 | + check( | |
| 162 | + "accent_text on accent (light)", | |
| 163 | + contrast(p.light.accent_text, p.light.accent), | |
| 164 | + 4.5, | |
| 165 | + ); | |
| 166 | + check( | |
| 167 | + "accent_text on accent (dark)", | |
| 168 | + contrast(p.dark.accent_text, p.dark.accent), | |
| 169 | + 4.5, | |
| 170 | + ); | |
| 171 | + for t in [&p.light, &p.dark] { | |
| 132 | 172 | check( |
| 133 | 173 | "public_text on public_bg", |
| 134 | 174 | contrast(t.public_text, t.public_bg), |
| 135 | 175 | 4.5, |
| 136 | 176 | ); |
| 137 | - check("focus on bg", contrast(t.focus, bg), 3.0); | |
| 177 | + } | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | + #[test] | |
| 182 | + fn every_base_with_every_accent_meets_the_contrast_targets() { | |
| 183 | + for base in BASES { | |
| 184 | + for p in PALETTES { | |
| 185 | + let themes = [ | |
| 186 | + ("light", &base.light, &p.light), | |
| 187 | + ("dark", &base.dark, &p.dark), | |
| 188 | + ]; | |
| 189 | + for (theme, b, t) in themes { | |
| 190 | + let who = format!("{} with {} ({theme})", base.id, p.id); | |
| 191 | + let check = |what: &str, ratio: f64, min: f64| { | |
| 192 | + assert!(ratio >= min, "{who} {what}: {ratio:.2} < {min}"); | |
| 193 | + }; | |
| 194 | + check("text on bg", contrast(b.text, b.bg), 7.0); | |
| 195 | + check("text on surface", contrast(b.text, b.surface), 7.0); | |
| 196 | + check("text_2 on bg", contrast(b.text_2, b.bg), 4.5); | |
| 197 | + check("text_2 on surface", contrast(b.text_2, b.surface), 4.5); | |
| 198 | + check("muted on bg", contrast(b.muted, b.bg), 4.5); | |
| 199 | + check("muted on surface", contrast(b.muted, b.surface), 4.5); | |
| 200 | + check("ln_text on code_bg", contrast(b.ln_text, b.code_bg), 4.5); | |
| 201 | + check( | |
| 202 | + "private_text on private_bg", | |
| 203 | + contrast(b.private_text, b.private_bg), | |
| 204 | + 4.5, | |
| 205 | + ); | |
| 206 | + check("accent_ink on bg", contrast(t.accent_ink, b.bg), 4.5); | |
| 207 | + check( | |
| 208 | + "accent_ink on surface", | |
| 209 | + contrast(t.accent_ink, b.surface), | |
| 210 | + 4.5, | |
| 211 | + ); | |
| 212 | + check( | |
| 213 | + "accent_ink on accent_soft", | |
| 214 | + contrast(t.accent_ink, t.accent_soft), | |
| 215 | + 4.5, | |
| 216 | + ); | |
| 217 | + check("focus on bg", contrast(t.focus, b.bg), 3.0); | |
| 218 | + } | |
| 138 | 219 | } |
| 139 | 220 | } |
| 140 | 221 | } |
crates/forge-web/src/accent_palette.rsmodified+252 -0
| @@ -336,3 +336,255 @@ pub const PALETTES: &[Palette] = &[ | ||
| 336 | 336 | }, |
| 337 | 337 | }, |
| 338 | 338 | ]; |
| 339 | + | |
| 340 | +/// The neutral colour tokens one base sets in one theme. The values are CSS colours; | |
| 341 | +/// `BASE_TOKEN_VARS` names the custom property each field becomes. | |
| 342 | +pub struct BaseTokens { | |
| 343 | + pub bg: &'static str, | |
| 344 | + pub surface: &'static str, | |
| 345 | + pub surface_2: &'static str, | |
| 346 | + pub text: &'static str, | |
| 347 | + pub text_2: &'static str, | |
| 348 | + pub muted: &'static str, | |
| 349 | + pub border: &'static str, | |
| 350 | + pub border_strong: &'static str, | |
| 351 | + pub code_bg: &'static str, | |
| 352 | + pub private_bg: &'static str, | |
| 353 | + pub private_text: &'static str, | |
| 354 | + pub ln_text: &'static str, | |
| 355 | +} | |
| 356 | + | |
| 357 | +/// One base: the neutral backgrounds, surfaces and text. Its id is the stored value and the | |
| 358 | +/// attribute value, and the tokens are used in light and dark themes. | |
| 359 | +pub struct Base { | |
| 360 | + pub id: &'static str, | |
| 361 | + pub light: BaseTokens, | |
| 362 | + pub dark: BaseTokens, | |
| 363 | +} | |
| 364 | + | |
| 365 | +/// The base used when none is stored, and for anonymous visitors. It must be `BASES[0]`. | |
| 366 | +pub const BASE_DEFAULT: &str = "fern"; | |
| 367 | + | |
| 368 | +/// One base token: the custom property it becomes and the field of `BaseTokens` that holds its | |
| 369 | +/// value. | |
| 370 | +pub struct BaseTokenVar { | |
| 371 | + pub name: &'static str, | |
| 372 | + pub value: fn(&BaseTokens) -> &'static str, | |
| 373 | +} | |
| 374 | + | |
| 375 | +/// Every neutral token a base sets. The stylesheet keeps no copy of these. | |
| 376 | +pub const BASE_TOKEN_VARS: &[BaseTokenVar] = &[ | |
| 377 | + BaseTokenVar { | |
| 378 | + name: "--bg", | |
| 379 | + value: |t| t.bg, | |
| 380 | + }, | |
| 381 | + BaseTokenVar { | |
| 382 | + name: "--surface", | |
| 383 | + value: |t| t.surface, | |
| 384 | + }, | |
| 385 | + BaseTokenVar { | |
| 386 | + name: "--surface-2", | |
| 387 | + value: |t| t.surface_2, | |
| 388 | + }, | |
| 389 | + BaseTokenVar { | |
| 390 | + name: "--text", | |
| 391 | + value: |t| t.text, | |
| 392 | + }, | |
| 393 | + BaseTokenVar { | |
| 394 | + name: "--text-2", | |
| 395 | + value: |t| t.text_2, | |
| 396 | + }, | |
| 397 | + BaseTokenVar { | |
| 398 | + name: "--muted", | |
| 399 | + value: |t| t.muted, | |
| 400 | + }, | |
| 401 | + BaseTokenVar { | |
| 402 | + name: "--border", | |
| 403 | + value: |t| t.border, | |
| 404 | + }, | |
| 405 | + BaseTokenVar { | |
| 406 | + name: "--border-strong", | |
| 407 | + value: |t| t.border_strong, | |
| 408 | + }, | |
| 409 | + BaseTokenVar { | |
| 410 | + name: "--code-bg", | |
| 411 | + value: |t| t.code_bg, | |
| 412 | + }, | |
| 413 | + BaseTokenVar { | |
| 414 | + name: "--private-bg", | |
| 415 | + value: |t| t.private_bg, | |
| 416 | + }, | |
| 417 | + BaseTokenVar { | |
| 418 | + name: "--private-text", | |
| 419 | + value: |t| t.private_text, | |
| 420 | + }, | |
| 421 | + BaseTokenVar { | |
| 422 | + name: "--ln-text", | |
| 423 | + value: |t| t.ln_text, | |
| 424 | + }, | |
| 425 | +]; | |
| 426 | + | |
| 427 | +/// Every base on offer, in the order the picker lists them. `fern` is the original forge | |
| 428 | +/// neutral set. | |
| 429 | +pub const BASES: &[Base] = &[ | |
| 430 | + // Fern: the original forge neutrals, green-tinted. | |
| 431 | + Base { | |
| 432 | + id: "fern", | |
| 433 | + light: BaseTokens { | |
| 434 | + bg: "#F6F8F5", | |
| 435 | + surface: "#FFFFFF", | |
| 436 | + surface_2: "#EEF2EC", | |
| 437 | + text: "#1E2420", | |
| 438 | + text_2: "#3A433D", | |
| 439 | + muted: "#69736B", | |
| 440 | + border: "#DDE3DB", | |
| 441 | + border_strong: "#C5CFC3", | |
| 442 | + code_bg: "#EEF2EC", | |
| 443 | + private_bg: "#EEF2EC", | |
| 444 | + private_text: "#3A433D", | |
| 445 | + ln_text: "#646D65", | |
| 446 | + }, | |
| 447 | + dark: BaseTokens { | |
| 448 | + bg: "#121613", | |
| 449 | + surface: "#191E1A", | |
| 450 | + surface_2: "#1F261F", | |
| 451 | + text: "#E3E8E2", | |
| 452 | + text_2: "#C4CCC3", | |
| 453 | + muted: "#97A198", | |
| 454 | + border: "#2A322C", | |
| 455 | + border_strong: "#3A443C", | |
| 456 | + code_bg: "#1F261F", | |
| 457 | + private_bg: "#1F261F", | |
| 458 | + private_text: "#C4CCC3", | |
| 459 | + ln_text: "#97A198", | |
| 460 | + }, | |
| 461 | + }, | |
| 462 | + // Stone: pure neutral grey. | |
| 463 | + Base { | |
| 464 | + id: "stone", | |
| 465 | + light: BaseTokens { | |
| 466 | + bg: "#F5F5F4", | |
| 467 | + surface: "#FFFFFF", | |
| 468 | + surface_2: "#EDEDEB", | |
| 469 | + text: "#1C1C1B", | |
| 470 | + text_2: "#3D3D3B", | |
| 471 | + muted: "#6B6B68", | |
| 472 | + border: "#DEDEDB", | |
| 473 | + border_strong: "#C6C6C2", | |
| 474 | + code_bg: "#EDEDEB", | |
| 475 | + private_bg: "#EDEDEB", | |
| 476 | + private_text: "#3D3D3B", | |
| 477 | + ln_text: "#6B6B68", | |
| 478 | + }, | |
| 479 | + dark: BaseTokens { | |
| 480 | + bg: "#131313", | |
| 481 | + surface: "#1A1A1A", | |
| 482 | + surface_2: "#212121", | |
| 483 | + text: "#E6E6E4", | |
| 484 | + text_2: "#C7C7C4", | |
| 485 | + muted: "#9A9A96", | |
| 486 | + border: "#2C2C2B", | |
| 487 | + border_strong: "#3C3C3A", | |
| 488 | + code_bg: "#212121", | |
| 489 | + private_bg: "#212121", | |
| 490 | + private_text: "#C7C7C4", | |
| 491 | + ln_text: "#9A9A96", | |
| 492 | + }, | |
| 493 | + }, | |
| 494 | + // Sand: warm paper and warm ink. | |
| 495 | + Base { | |
| 496 | + id: "sand", | |
| 497 | + light: BaseTokens { | |
| 498 | + bg: "#F8F5EF", | |
| 499 | + surface: "#FFFDF9", | |
| 500 | + surface_2: "#F0EBE1", | |
| 501 | + text: "#2A2620", | |
| 502 | + text_2: "#4A443B", | |
| 503 | + muted: "#716A5E", | |
| 504 | + border: "#E2DBCF", | |
| 505 | + border_strong: "#CBC2B3", | |
| 506 | + code_bg: "#F0EBE1", | |
| 507 | + private_bg: "#F0EBE1", | |
| 508 | + private_text: "#4A443B", | |
| 509 | + ln_text: "#6B6457", | |
| 510 | + }, | |
| 511 | + dark: BaseTokens { | |
| 512 | + bg: "#171511", | |
| 513 | + surface: "#1E1B16", | |
| 514 | + surface_2: "#26221C", | |
| 515 | + text: "#E9E4DA", | |
| 516 | + text_2: "#CBC5B9", | |
| 517 | + muted: "#A29B8D", | |
| 518 | + border: "#342F27", | |
| 519 | + border_strong: "#463F35", | |
| 520 | + code_bg: "#26221C", | |
| 521 | + private_bg: "#26221C", | |
| 522 | + private_text: "#CBC5B9", | |
| 523 | + ln_text: "#A29B8D", | |
| 524 | + }, | |
| 525 | + }, | |
| 526 | + // Mist: cool blue-grey. | |
| 527 | + Base { | |
| 528 | + id: "mist", | |
| 529 | + light: BaseTokens { | |
| 530 | + bg: "#F3F6F9", | |
| 531 | + surface: "#FFFFFF", | |
| 532 | + surface_2: "#E9EEF3", | |
| 533 | + text: "#1B2430", | |
| 534 | + text_2: "#384352", | |
| 535 | + muted: "#66717F", | |
| 536 | + border: "#D9E0E8", | |
| 537 | + border_strong: "#C2CCD8", | |
| 538 | + code_bg: "#E9EEF3", | |
| 539 | + private_bg: "#E9EEF3", | |
| 540 | + private_text: "#384352", | |
| 541 | + ln_text: "#5E6977", | |
| 542 | + }, | |
| 543 | + dark: BaseTokens { | |
| 544 | + bg: "#11161C", | |
| 545 | + surface: "#182029", | |
| 546 | + surface_2: "#1E2834", | |
| 547 | + text: "#E2E8EF", | |
| 548 | + text_2: "#C3CCD7", | |
| 549 | + muted: "#949FAD", | |
| 550 | + border: "#28323E", | |
| 551 | + border_strong: "#37424F", | |
| 552 | + code_bg: "#1E2834", | |
| 553 | + private_bg: "#1E2834", | |
| 554 | + private_text: "#C3CCD7", | |
| 555 | + ln_text: "#949FAD", | |
| 556 | + }, | |
| 557 | + }, | |
| 558 | + // Ink: crisp white in light, near-black in dark. | |
| 559 | + Base { | |
| 560 | + id: "ink", | |
| 561 | + light: BaseTokens { | |
| 562 | + bg: "#FCFCFB", | |
| 563 | + surface: "#FFFFFF", | |
| 564 | + surface_2: "#F2F2F0", | |
| 565 | + text: "#111111", | |
| 566 | + text_2: "#333333", | |
| 567 | + muted: "#5F5F5C", | |
| 568 | + border: "#E3E3E0", | |
| 569 | + border_strong: "#C9C9C5", | |
| 570 | + code_bg: "#F2F2F0", | |
| 571 | + private_bg: "#F2F2F0", | |
| 572 | + private_text: "#333333", | |
| 573 | + ln_text: "#5F5F5C", | |
| 574 | + }, | |
| 575 | + dark: BaseTokens { | |
| 576 | + bg: "#000000", | |
| 577 | + surface: "#0B0B0B", | |
| 578 | + surface_2: "#141414", | |
| 579 | + text: "#F2F2F0", | |
| 580 | + text_2: "#CFCFCC", | |
| 581 | + muted: "#9C9C98", | |
| 582 | + border: "#242424", | |
| 583 | + border_strong: "#353535", | |
| 584 | + code_bg: "#141414", | |
| 585 | + private_bg: "#141414", | |
| 586 | + private_text: "#CFCFCC", | |
| 587 | + ln_text: "#9C9C98", | |
| 588 | + }, | |
| 589 | + }, | |
| 590 | +]; |
crates/forge-web/src/i18n/accents.rsmodified+29 -2
| @@ -1,5 +1,5 @@ | ||
| 1 | -//! Names of the accent colours on the appearance page, keyed by palette id. Adding a palette | |
| 2 | -//! without a name here fails `every_palette_has_a_name`. | |
| 1 | +//! Names of the accent colours and the backgrounds on the appearance page, keyed by id. Adding a | |
| 2 | +//! palette or a base without a name here fails `every_palette_has_a_name` or `every_base_has_a_name`. | |
| 3 | 3 | |
| 4 | 4 | use super::Lang; |
| 5 | 5 | |
| @@ -23,6 +23,24 @@ pub fn accent_name(lang: Lang, id: &str) -> &'static str { | ||
| 23 | 23 | } |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | +/// The display name of a base (the neutral background set). An id with no name returns an empty | |
| 27 | +/// string, which the test rules out for every offered base. | |
| 28 | +pub fn base_name(lang: Lang, id: &str) -> &'static str { | |
| 29 | + match (id, lang) { | |
| 30 | + ("fern", Lang::En) => "Fern", | |
| 31 | + ("fern", Lang::Tr) => "Eğrelti", | |
| 32 | + ("stone", Lang::En) => "Stone", | |
| 33 | + ("stone", Lang::Tr) => "Taş", | |
| 34 | + ("sand", Lang::En) => "Sand", | |
| 35 | + ("sand", Lang::Tr) => "Kum", | |
| 36 | + ("mist", Lang::En) => "Mist", | |
| 37 | + ("mist", Lang::Tr) => "Sis", | |
| 38 | + ("ink", Lang::En) => "Ink", | |
| 39 | + ("ink", Lang::Tr) => "Mürekkep", | |
| 40 | + _ => "", | |
| 41 | + } | |
| 42 | +} | |
| 43 | + | |
| 26 | 44 | #[cfg(test)] |
| 27 | 45 | mod tests { |
| 28 | 46 | use super::*; |
| @@ -36,4 +54,13 @@ mod tests { | ||
| 36 | 54 | } |
| 37 | 55 | } |
| 38 | 56 | } |
| 57 | + | |
| 58 | + #[test] | |
| 59 | + fn every_base_has_a_name() { | |
| 60 | + for id in accent::base_ids() { | |
| 61 | + for lang in [Lang::En, Lang::Tr] { | |
| 62 | + assert!(!base_name(lang, id).is_empty(), "{id} has no name"); | |
| 63 | + } | |
| 64 | + } | |
| 65 | + } | |
| 39 | 66 | } |
crates/forge-web/src/i18n/misc.rsmodified+16 -12
| @@ -14,7 +14,6 @@ pub enum K { | ||
| 14 | 14 | Appearance, |
| 15 | 15 | ThemeLabel, |
| 16 | 16 | LanguageLabel, |
| 17 | - ThemeAndLanguage, | |
| 18 | 17 | MatchSystemHelp, |
| 19 | 18 | MatchSystem, |
| 20 | 19 | Light, |
| @@ -24,12 +23,14 @@ pub enum K { | ||
| 24 | 23 | SwitchToLight, |
| 25 | 24 | SwitchToDark, |
| 26 | 25 | SwitchToMatchSystem, |
| 27 | - AppearanceSaved, | |
| 28 | 26 | ChooseTheme, |
| 29 | 27 | ChooseLanguage, |
| 30 | 28 | AccentLabel, |
| 31 | - AccentHelp, | |
| 32 | 29 | ChooseAccent, |
| 30 | + LightColours, | |
| 31 | + DarkColours, | |
| 32 | + BackgroundLabel, | |
| 33 | + ChooseBase, | |
| 33 | 34 | // Notifications: row kinds. |
| 34 | 35 | KindOpenIssue, |
| 35 | 36 | KindClosedIssue, |
| @@ -156,7 +157,6 @@ const ALL: &[K] = &[ | ||
| 156 | 157 | K::Appearance, |
| 157 | 158 | K::ThemeLabel, |
| 158 | 159 | K::LanguageLabel, |
| 159 | - K::ThemeAndLanguage, | |
| 160 | 160 | K::MatchSystemHelp, |
| 161 | 161 | K::MatchSystem, |
| 162 | 162 | K::Light, |
| @@ -165,12 +165,14 @@ const ALL: &[K] = &[ | ||
| 165 | 165 | K::SwitchToLight, |
| 166 | 166 | K::SwitchToDark, |
| 167 | 167 | K::SwitchToMatchSystem, |
| 168 | - K::AppearanceSaved, | |
| 169 | 168 | K::ChooseTheme, |
| 170 | 169 | K::ChooseLanguage, |
| 171 | 170 | K::AccentLabel, |
| 172 | - K::AccentHelp, | |
| 173 | 171 | K::ChooseAccent, |
| 172 | + K::LightColours, | |
| 173 | + K::DarkColours, | |
| 174 | + K::BackgroundLabel, | |
| 175 | + K::ChooseBase, | |
| 174 | 176 | K::KindOpenIssue, |
| 175 | 177 | K::KindClosedIssue, |
| 176 | 178 | K::KindOpenPull, |
| @@ -289,8 +291,6 @@ pub fn t(lang: Lang, key: K) -> &'static str { | ||
| 289 | 291 | (ThemeLabel, Tr) => "Tema", |
| 290 | 292 | (LanguageLabel, En) => "Language", |
| 291 | 293 | (LanguageLabel, Tr) => "Dil", |
| 292 | - (ThemeAndLanguage, En) => "Theme and language", | |
| 293 | - (ThemeAndLanguage, Tr) => "Tema ve dil", | |
| 294 | 294 | (MatchSystemHelp, En) => { |
| 295 | 295 | "Match the system follows your operating system's light or dark setting." |
| 296 | 296 | } |
| @@ -311,18 +311,22 @@ pub fn t(lang: Lang, key: K) -> &'static str { | ||
| 311 | 311 | (SwitchToDark, Tr) => "Koyu temaya geç", |
| 312 | 312 | (SwitchToMatchSystem, En) => "Switch to match the system", |
| 313 | 313 | (SwitchToMatchSystem, Tr) => "Sistem ile eşleştir moduna geç", |
| 314 | - (AppearanceSaved, En) => "Appearance saved.", | |
| 315 | - (AppearanceSaved, Tr) => "Görünüm kaydedildi.", | |
| 316 | 314 | (ChooseTheme, En) => "Choose a theme from the list.", |
| 317 | 315 | (ChooseTheme, Tr) => "Listeden bir tema seçin.", |
| 318 | 316 | (ChooseLanguage, En) => "Choose a language from the list.", |
| 319 | 317 | (ChooseLanguage, Tr) => "Listeden bir dil seçin.", |
| 320 | 318 | (AccentLabel, En) => "Accent colour", |
| 321 | 319 | (AccentLabel, Tr) => "Vurgu rengi", |
| 322 | - (AccentHelp, En) => "Used for buttons, links and the activity heatmap.", | |
| 323 | - (AccentHelp, Tr) => "Düğmeler, bağlantılar ve etkinlik ısı haritası için kullanılır.", | |
| 324 | 320 | (ChooseAccent, En) => "Choose an accent colour from the list.", |
| 325 | 321 | (ChooseAccent, Tr) => "Listeden bir vurgu rengi seçin.", |
| 322 | + (LightColours, En) => "Light theme", | |
| 323 | + (LightColours, Tr) => "Açık tema", | |
| 324 | + (DarkColours, En) => "Dark theme", | |
| 325 | + (DarkColours, Tr) => "Koyu tema", | |
| 326 | + (BackgroundLabel, En) => "Background", | |
| 327 | + (BackgroundLabel, Tr) => "Arka plan", | |
| 328 | + (ChooseBase, En) => "Choose a background from the list.", | |
| 329 | + (ChooseBase, Tr) => "Listeden bir arka plan seçin.", | |
| 326 | 330 | (KindOpenIssue, En) => "Open issue", |
| 327 | 331 | (KindOpenIssue, Tr) => "Açık sorun", |
| 328 | 332 | (KindClosedIssue, En) => "Closed issue", |
crates/forge-web/src/pages/settings_appearance.rsmodified+223 -89
| @@ -1,6 +1,7 @@ | ||
| 1 | -//! Page: appearance (`/settings/appearance`). The colour theme, the accent colour and the language, | |
| 2 | -//! all stored per user. Anonymous visitors set the theme from the footer instead (see `pages/theme_cookie.rs`). | |
| 3 | -//! Every POST checks the CSRF token first. | |
| 1 | +//! Page: appearance (`/settings/appearance`). The colour theme, the language, and for each of the | |
| 2 | +//! light and dark themes a background and an accent colour, all stored per user. Every change | |
| 3 | +//! saves at once, so the form has no Save button. Anonymous visitors set the theme from the footer | |
| 4 | +//! instead (see `pages/theme_cookie.rs`). Every POST checks the CSRF token first. | |
| 4 | 5 | |
| 5 | 6 | use forge_core::AuditAction; |
| 6 | 7 | use forge_core::store::ui_prefs::THEMES; |
| @@ -10,15 +11,15 @@ use topcoat::Result; | ||
| 10 | 11 | use topcoat::context::{Cx, app_context}; |
| 11 | 12 | use topcoat::router::content::Form; |
| 12 | 13 | use topcoat::router::error::{SeeOther, see_other}; |
| 13 | -use topcoat::router::{page, query_params, route}; | |
| 14 | +use topcoat::router::{page, route}; | |
| 14 | 15 | use topcoat::view::{View, component, view}; |
| 15 | 16 | |
| 16 | 17 | use crate::accent; |
| 17 | 18 | use crate::audit::{self, Actor, Target}; |
| 18 | 19 | use crate::auth::{require_csrf, require_user}; |
| 19 | -use crate::i18n::accents::accent_name; | |
| 20 | +use crate::i18n::accents::{accent_name, base_name}; | |
| 20 | 21 | use crate::i18n::misc::{self as g, K}; |
| 21 | -use crate::i18n::{self, Key, Lang}; | |
| 22 | +use crate::i18n::{self, Lang}; | |
| 22 | 23 | use crate::state::AppState; |
| 23 | 24 | use crate::views::shell; |
| 24 | 25 | |
| @@ -29,11 +30,6 @@ pub const LANGUAGE_CODES: &[&str] = &["auto", "en", "tr"]; | ||
| 29 | 30 | /// Theme codes, in display order. |
| 30 | 31 | const THEME_CODES: &[&str] = &["auto", "light", "dark"]; |
| 31 | 32 | |
| 32 | -#[query_params(error = bad_request)] | |
| 33 | -struct AppearanceQuery { | |
| 34 | - saved: Option<String>, | |
| 35 | -} | |
| 36 | - | |
| 37 | 33 | /// Reads a field that is present as a string, even an empty one. The plain `Option<String>` |
| 38 | 34 | /// reading would turn an empty value into `None`, which is the same as a missing field. |
| 39 | 35 | fn present_string<'de, D: Deserializer<'de>>( |
| @@ -48,12 +44,19 @@ struct AppearanceForm { | ||
| 48 | 44 | csrf: String, |
| 49 | 45 | #[serde(default)] |
| 50 | 46 | theme: String, |
| 51 | - /// Optional so a form loaded before the accent picker existed still saves. When absent, | |
| 52 | - /// the stored accent is kept. A present but empty value stays `Some("")` and is rejected. | |
| 53 | - #[serde(default, deserialize_with = "present_string")] | |
| 54 | - accent: Option<String>, | |
| 55 | 47 | #[serde(default)] |
| 56 | 48 | language: String, |
| 49 | + /// The four colour fields are optional so a form loaded before a field existed still saves. | |
| 50 | + /// When a field is absent, its stored value is kept. A present but empty or unknown value | |
| 51 | + /// stays `Some("")` or an unknown id, and is rejected. | |
| 52 | + #[serde(default, deserialize_with = "present_string")] | |
| 53 | + accent_light: Option<String>, | |
| 54 | + #[serde(default, deserialize_with = "present_string")] | |
| 55 | + accent_dark: Option<String>, | |
| 56 | + #[serde(default, deserialize_with = "present_string")] | |
| 57 | + base_light: Option<String>, | |
| 58 | + #[serde(default, deserialize_with = "present_string")] | |
| 59 | + base_dark: Option<String>, | |
| 57 | 60 | } |
| 58 | 61 | |
| 59 | 62 | struct Choice { |
| @@ -62,13 +65,24 @@ struct Choice { | ||
| 62 | 65 | selected: bool, |
| 63 | 66 | } |
| 64 | 67 | |
| 68 | +/// The colour choices of one theme: its background and its accent colour. `dark` picks the | |
| 69 | +/// field names and the swatch attributes that belong to the dark theme. | |
| 70 | +struct ColourGroup { | |
| 71 | + dark: bool, | |
| 72 | + heading: &'static str, | |
| 73 | + background: &'static str, | |
| 74 | + accent: &'static str, | |
| 75 | + bases: Vec<Choice>, | |
| 76 | + accents: Vec<Choice>, | |
| 77 | +} | |
| 78 | + | |
| 65 | 79 | struct AppearancePage { |
| 66 | 80 | lang: Lang, |
| 67 | 81 | csrf: String, |
| 68 | 82 | theme: Vec<Choice>, |
| 69 | - accents: Vec<Choice>, | |
| 70 | 83 | language: Vec<Choice>, |
| 71 | - notice: Option<&'static str>, | |
| 84 | + light: ColourGroup, | |
| 85 | + dark: ColourGroup, | |
| 72 | 86 | } |
| 73 | 87 | |
| 74 | 88 | /// The label of a language code. The two native names stay as they are in every language. |
| @@ -109,105 +123,177 @@ fn choices( | ||
| 109 | 123 | .collect() |
| 110 | 124 | } |
| 111 | 125 | |
| 126 | +/// One theme's colour choices, with the stored (resolved) background and accent selected. | |
| 127 | +fn colour_group(lang: Lang, dark: bool, base: &str, accent_id: &str) -> ColourGroup { | |
| 128 | + let base_codes: Vec<&'static str> = accent::base_ids().collect(); | |
| 129 | + let accent_codes: Vec<&'static str> = accent::ids().collect(); | |
| 130 | + ColourGroup { | |
| 131 | + dark, | |
| 132 | + heading: g::t( | |
| 133 | + lang, | |
| 134 | + if dark { | |
| 135 | + K::DarkColours | |
| 136 | + } else { | |
| 137 | + K::LightColours | |
| 138 | + }, | |
| 139 | + ), | |
| 140 | + background: g::t(lang, K::BackgroundLabel), | |
| 141 | + accent: g::t(lang, K::AccentLabel), | |
| 142 | + bases: choices(&base_codes, base, |id| base_name(lang, id)), | |
| 143 | + accents: choices(&accent_codes, accent_id, |id| accent_name(lang, id)), | |
| 144 | + } | |
| 145 | +} | |
| 146 | + | |
| 147 | +#[component] | |
| 148 | +async fn colour_group_fields(group: ColourGroup) -> Result<impl View> { | |
| 149 | + let ColourGroup { | |
| 150 | + dark, | |
| 151 | + heading, | |
| 152 | + background, | |
| 153 | + accent, | |
| 154 | + bases, | |
| 155 | + accents, | |
| 156 | + } = group; | |
| 157 | + let base_field = if dark { "base_dark" } else { "base_light" }; | |
| 158 | + let accent_field = if dark { "accent_dark" } else { "accent_light" }; | |
| 159 | + Ok(view! { | |
| 160 | + <fieldset class="card colour-group"> | |
| 161 | + <legend>(heading)</legend> | |
| 162 | + <fieldset class="colour-picker"> | |
| 163 | + <legend>(background)</legend> | |
| 164 | + <div class="colour-choices"> | |
| 165 | + for choice in bases.into_iter() { | |
| 166 | + <label class="colour-choice"> | |
| 167 | + <input type="radio" name=(base_field) value=(choice.value) checked=(choice.selected) data-autosubmit="true"> | |
| 168 | + if dark { | |
| 169 | + <span class="base-swatch" data-base-dark-swatch=(choice.value) aria-hidden="true"></span> | |
| 170 | + } else { | |
| 171 | + <span class="base-swatch" data-base-light-swatch=(choice.value) aria-hidden="true"></span> | |
| 172 | + } | |
| 173 | + <span>(choice.label)</span> | |
| 174 | + </label> | |
| 175 | + } | |
| 176 | + </div> | |
| 177 | + </fieldset> | |
| 178 | + <fieldset class="colour-picker"> | |
| 179 | + <legend>(accent)</legend> | |
| 180 | + <div class="colour-choices"> | |
| 181 | + for choice in accents.into_iter() { | |
| 182 | + <label class="colour-choice"> | |
| 183 | + <input type="radio" name=(accent_field) value=(choice.value) checked=(choice.selected) data-autosubmit="true"> | |
| 184 | + if dark { | |
| 185 | + <span class="accent-swatch" data-accent-dark-swatch=(choice.value) aria-hidden="true"></span> | |
| 186 | + } else { | |
| 187 | + <span class="accent-swatch" data-accent-light-swatch=(choice.value) aria-hidden="true"></span> | |
| 188 | + } | |
| 189 | + <span>(choice.label)</span> | |
| 190 | + </label> | |
| 191 | + } | |
| 192 | + </div> | |
| 193 | + </fieldset> | |
| 194 | + </fieldset> | |
| 195 | + }) | |
| 196 | +} | |
| 197 | + | |
| 112 | 198 | #[component] |
| 113 | 199 | async fn appearance_body(page: AppearancePage) -> Result<impl View> { |
| 114 | 200 | let AppearancePage { |
| 115 | 201 | lang, |
| 116 | 202 | csrf, |
| 117 | 203 | theme, |
| 118 | - accents, | |
| 119 | 204 | language, |
| 120 | - notice, | |
| 205 | + light, | |
| 206 | + dark, | |
| 121 | 207 | } = page; |
| 122 | 208 | Ok(view! { |
| 123 | 209 | <div class="settings-layout"> |
| 124 | 210 | <section class="page-head"> |
| 125 | 211 | <h1>(g::t(lang, K::Appearance))</h1> |
| 126 | 212 | </section> |
| 127 | - if let Some(message) = notice { | |
| 128 | - <section class="card notice" role="status"> | |
| 129 | - <p>(message)</p> | |
| 130 | - </section> | |
| 131 | - } | |
| 132 | - <section class="card"> | |
| 133 | - <h2>(g::t(lang, K::ThemeAndLanguage))</h2> | |
| 134 | - <form method="post" action="/settings/appearance" class="stack"> | |
| 135 | - <input type="hidden" name="csrf" value=(csrf.as_str())> | |
| 213 | + <form method="post" action="/settings/appearance" class="appearance" data-appearance="true"> | |
| 214 | + <input type="hidden" name="csrf" value=(csrf.as_str())> | |
| 215 | + <div class="appearance-top"> | |
| 136 | 216 | <label> |
| 137 | 217 | <span>(g::t(lang, K::ThemeLabel))</span> |
| 138 | - <select name="theme"> | |
| 218 | + <select name="theme" data-autosubmit="true"> | |
| 139 | 219 | for choice in theme.into_iter() { |
| 140 | 220 | <option value=(choice.value) selected=(choice.selected)>(choice.label)</option> |
| 141 | 221 | } |
| 142 | 222 | </select> |
| 143 | 223 | <small class="muted">(g::t(lang, K::MatchSystemHelp))</small> |
| 144 | 224 | </label> |
| 145 | - <fieldset class="accent-picker"> | |
| 146 | - <legend>(g::t(lang, K::AccentLabel))</legend> | |
| 147 | - <div class="accent-choices"> | |
| 148 | - for choice in accents.into_iter() { | |
| 149 | - <label class="accent-choice"> | |
| 150 | - <input type="radio" name="accent" value=(choice.value) checked=(choice.selected)> | |
| 151 | - <span class="accent-swatch" data-accent-swatch=(choice.value) aria-hidden="true"></span> | |
| 152 | - <span>(choice.label)</span> | |
| 153 | - </label> | |
| 154 | - } | |
| 155 | - </div> | |
| 156 | - <small class="muted">(g::t(lang, K::AccentHelp))</small> | |
| 157 | - </fieldset> | |
| 158 | 225 | <label> |
| 159 | 226 | <span>(g::t(lang, K::LanguageLabel))</span> |
| 160 | - <select name="language"> | |
| 227 | + <select name="language" data-autosubmit="true"> | |
| 161 | 228 | for choice in language.into_iter() { |
| 162 | 229 | <option value=(choice.value) selected=(choice.selected)>(choice.label)</option> |
| 163 | 230 | } |
| 164 | 231 | </select> |
| 165 | 232 | </label> |
| 166 | - <button type="submit" class="primary">(i18n::t(lang, Key::Save))</button> | |
| 167 | - </form> | |
| 168 | - </section> | |
| 233 | + </div> | |
| 234 | + <div class="appearance-columns"> | |
| 235 | + colour_group_fields(group: light) | |
| 236 | + colour_group_fields(group: dark) | |
| 237 | + </div> | |
| 238 | + </form> | |
| 169 | 239 | </div> |
| 170 | 240 | }) |
| 171 | 241 | } |
| 172 | 242 | |
| 173 | -/// Rejects a theme, accent or language that is not offered. Nothing is saved for any of them. | |
| 174 | -fn check_choice( | |
| 175 | - lang: Lang, | |
| 176 | - theme: &str, | |
| 177 | - accent_id: &str, | |
| 178 | - language: &str, | |
| 179 | -) -> forge_core::Result<()> { | |
| 243 | +/// Rejects a theme or a language that is not offered. Nothing is saved for either of them. | |
| 244 | +fn check_choice(lang: Lang, theme: &str, language: &str) -> forge_core::Result<()> { | |
| 180 | 245 | if !THEMES.contains(&theme) { |
| 181 | 246 | return Err(CoreError::Invalid(g::t(lang, K::ChooseTheme).into())); |
| 182 | 247 | } |
| 183 | - if !accent::is_offered(accent_id) { | |
| 184 | - return Err(CoreError::Invalid(g::t(lang, K::ChooseAccent).into())); | |
| 185 | - } | |
| 186 | 248 | if !LANGUAGE_CODES.contains(&language) { |
| 187 | 249 | return Err(CoreError::Invalid(g::t(lang, K::ChooseLanguage).into())); |
| 188 | 250 | } |
| 189 | 251 | Ok(()) |
| 190 | 252 | } |
| 191 | 253 | |
| 192 | -fn notice_for(lang: Lang, saved: Option<&str>) -> Option<&'static str> { | |
| 193 | - match saved { | |
| 194 | - Some("1") => Some(g::t(lang, K::AppearanceSaved)), | |
| 195 | - _ => None, | |
| 254 | +/// The accent a save stores for one theme. An absent field keeps the stored id. A present one | |
| 255 | +/// must be an offered accent, so an empty or unknown value is rejected. | |
| 256 | +fn accent_choice(lang: Lang, posted: Option<&str>, stored: &str) -> forge_core::Result<String> { | |
| 257 | + match posted { | |
| 258 | + None => Ok(stored.to_owned()), | |
| 259 | + Some(id) if accent::is_offered(id) => Ok(id.to_owned()), | |
| 260 | + Some(_) => Err(CoreError::Invalid(g::t(lang, K::ChooseAccent).into())), | |
| 196 | 261 | } |
| 197 | 262 | } |
| 198 | 263 | |
| 199 | -/// Builds the page for a viewer. The theme, accent and language are the stored values. | |
| 200 | -fn page_for(viewer: &(User, Session), lang: Lang, notice: Option<&'static str>) -> AppearancePage { | |
| 201 | - let accent_codes: Vec<&'static str> = accent::ids().collect(); | |
| 264 | +/// The background a save stores for one theme. An absent field keeps the stored id. A present | |
| 265 | +/// one must be an offered base, so an empty or unknown value is rejected. | |
| 266 | +fn base_choice(lang: Lang, posted: Option<&str>, stored: &str) -> forge_core::Result<String> { | |
| 267 | + match posted { | |
| 268 | + None => Ok(stored.to_owned()), | |
| 269 | + Some(id) if accent::is_offered_base(id) => Ok(id.to_owned()), | |
| 270 | + Some(_) => Err(CoreError::Invalid(g::t(lang, K::ChooseBase).into())), | |
| 271 | + } | |
| 272 | +} | |
| 273 | + | |
| 274 | +/// Builds the page for a viewer. Each choice shows the stored value, or the default when the | |
| 275 | +/// stored id is no longer offered, which is what the page renders on `<html>` too. | |
| 276 | +fn page_for(viewer: &(User, Session), lang: Lang) -> AppearancePage { | |
| 277 | + let user = &viewer.0; | |
| 202 | 278 | AppearancePage { |
| 203 | 279 | lang, |
| 204 | 280 | csrf: viewer.1.csrf_token.clone(), |
| 205 | - theme: choices(THEME_CODES, &viewer.0.theme, |code| theme_label(lang, code)), | |
| 206 | - accents: choices(&accent_codes, &viewer.0.accent, |id| accent_name(lang, id)), | |
| 207 | - language: choices(LANGUAGE_CODES, &viewer.0.language, |code| { | |
| 281 | + theme: choices(THEME_CODES, &user.theme, |code| theme_label(lang, code)), | |
| 282 | + language: choices(LANGUAGE_CODES, &user.language, |code| { | |
| 208 | 283 | language_label(lang, code) |
| 209 | 284 | }), |
| 210 | - notice, | |
| 285 | + light: colour_group( | |
| 286 | + lang, | |
| 287 | + false, | |
| 288 | + accent::resolve_base(&user.base_light).id, | |
| 289 | + accent::resolve(&user.accent).id, | |
| 290 | + ), | |
| 291 | + dark: colour_group( | |
| 292 | + lang, | |
| 293 | + true, | |
| 294 | + accent::resolve_base(&user.base_dark).id, | |
| 295 | + accent::resolve(&user.accent_dark).id, | |
| 296 | + ), | |
| 211 | 297 | } |
| 212 | 298 | } |
| 213 | 299 | |
| @@ -215,8 +301,7 @@ fn page_for(viewer: &(User, Session), lang: Lang, notice: Option<&'static str>) | ||
| 215 | 301 | async fn appearance_page(cx: &Cx) -> Result<impl View> { |
| 216 | 302 | let viewer = require_user(cx).await?; |
| 217 | 303 | let lang = i18n::lang(cx, Some(&viewer.0)); |
| 218 | - let saved = query_params::<AppearanceQuery>(cx)?.saved.clone(); | |
| 219 | - let page = page_for(&viewer, lang, notice_for(lang, saved.as_deref())); | |
| 304 | + let page = page_for(&viewer, lang); | |
| 220 | 305 | Ok(view! { |
| 221 | 306 | shell(title: g::t(lang, K::Appearance), viewer: Some(&viewer), appearance_body(page: page)) |
| 222 | 307 | }) |
| @@ -227,29 +312,46 @@ async fn appearance_save(cx: &Cx, Form(form): Form<AppearanceForm>) -> Result<Se | ||
| 227 | 312 | let viewer = require_csrf(cx, &form.csrf).await?; |
| 228 | 313 | let lang = i18n::lang(cx, Some(&viewer.0)); |
| 229 | 314 | let store: &Store = &app_context::<AppState>(cx).store; |
| 315 | + let user = &viewer.0; | |
| 230 | 316 | |
| 231 | - let accent = form.accent.as_deref().unwrap_or(&viewer.0.accent); | |
| 232 | - check_choice(lang, &form.theme, accent, &form.language)?; | |
| 317 | + check_choice(lang, &form.theme, &form.language)?; | |
| 318 | + let accent_light = accent_choice(lang, form.accent_light.as_deref(), &user.accent)?; | |
| 319 | + let accent_dark = accent_choice(lang, form.accent_dark.as_deref(), &user.accent_dark)?; | |
| 320 | + let base_light = base_choice(lang, form.base_light.as_deref(), &user.base_light)?; | |
| 321 | + let base_dark = base_choice(lang, form.base_dark.as_deref(), &user.base_dark)?; | |
| 233 | 322 | store |
| 234 | - .set_ui_prefs(&viewer.0.id, &form.theme, &form.language) | |
| 323 | + .set_ui_prefs(&user.id, &form.theme, &form.language) | |
| 324 | + .await?; | |
| 325 | + store | |
| 326 | + .set_colours( | |
| 327 | + &user.id, | |
| 328 | + &accent_light, | |
| 329 | + &accent_dark, | |
| 330 | + &base_light, | |
| 331 | + &base_dark, | |
| 332 | + ) | |
| 235 | 333 | .await?; |
| 236 | - if form.accent.is_some() { | |
| 237 | - store.set_accent(&viewer.0.id, accent).await?; | |
| 238 | - } | |
| 239 | 334 | audit::record( |
| 240 | 335 | store, |
| 241 | 336 | Some(cx), |
| 242 | - Actor::User(&viewer.0), | |
| 337 | + Actor::User(user), | |
| 243 | 338 | AuditAction::UiPrefsChanged, |
| 244 | 339 | Target { |
| 245 | 340 | kind: "user", |
| 246 | - id: &viewer.0.id, | |
| 247 | - name: &viewer.0.username, | |
| 341 | + id: &user.id, | |
| 342 | + name: &user.username, | |
| 248 | 343 | }, |
| 249 | - serde_json::json!({ "theme": form.theme, "accent": accent, "language": form.language }), | |
| 344 | + serde_json::json!({ | |
| 345 | + "theme": form.theme, | |
| 346 | + "language": form.language, | |
| 347 | + "accent_light": accent_light, | |
| 348 | + "accent_dark": accent_dark, | |
| 349 | + "base_light": base_light, | |
| 350 | + "base_dark": base_dark, | |
| 351 | + }), | |
| 250 | 352 | ) |
| 251 | 353 | .await; |
| 252 | - Ok(see_other("/settings/appearance?saved=1")) | |
| 354 | + Ok(see_other("/settings/appearance")) | |
| 253 | 355 | } |
| 254 | 356 | |
| 255 | 357 | #[cfg(test)] |
| @@ -258,14 +360,35 @@ mod tests { | ||
| 258 | 360 | |
| 259 | 361 | #[test] |
| 260 | 362 | fn only_offered_choices_pass() { |
| 261 | - assert!(check_choice(Lang::En, "dark", "ocean", "tr").is_ok()); | |
| 262 | - assert!(check_choice(Lang::Tr, "auto", "sage", "auto").is_ok()); | |
| 263 | - assert!(check_choice(Lang::En, "neon", "sage", "en").is_err()); | |
| 264 | - assert!(check_choice(Lang::En, "light", "neon", "en").is_err()); | |
| 265 | - assert!(check_choice(Lang::En, "light", "Ocean", "en").is_err()); | |
| 266 | - assert!(check_choice(Lang::En, "light", "", "en").is_err()); | |
| 267 | - assert!(check_choice(Lang::En, "light", "sage", "de").is_err()); | |
| 268 | - assert!(check_choice(Lang::Tr, "light", "sage", "").is_err()); | |
| 363 | + assert!(check_choice(Lang::En, "dark", "tr").is_ok()); | |
| 364 | + assert!(check_choice(Lang::Tr, "auto", "auto").is_ok()); | |
| 365 | + assert!(check_choice(Lang::En, "neon", "en").is_err()); | |
| 366 | + assert!(check_choice(Lang::En, "light", "de").is_err()); | |
| 367 | + assert!(check_choice(Lang::Tr, "light", "").is_err()); | |
| 368 | + assert!(accent_choice(Lang::En, Some("ocean"), "sage").is_ok()); | |
| 369 | + assert!(accent_choice(Lang::En, Some("neon"), "sage").is_err()); | |
| 370 | + assert!(accent_choice(Lang::En, Some("Ocean"), "sage").is_err()); | |
| 371 | + assert!(accent_choice(Lang::En, Some(""), "sage").is_err()); | |
| 372 | + assert!(base_choice(Lang::En, Some("ink"), "fern").is_ok()); | |
| 373 | + assert!(base_choice(Lang::En, Some("neon"), "fern").is_err()); | |
| 374 | + assert!(base_choice(Lang::En, Some(""), "fern").is_err()); | |
| 375 | + } | |
| 376 | + | |
| 377 | + #[test] | |
| 378 | + fn an_absent_colour_keeps_the_stored_value() { | |
| 379 | + assert_eq!(accent_choice(Lang::En, None, "plum").unwrap(), "plum"); | |
| 380 | + assert_eq!(base_choice(Lang::En, None, "mist").unwrap(), "mist"); | |
| 381 | + } | |
| 382 | + | |
| 383 | + #[test] | |
| 384 | + fn a_rejected_colour_says_what_to_choose() { | |
| 385 | + let accent_err = accent_choice(Lang::En, Some("neon"), "sage").unwrap_err(); | |
| 386 | + assert!( | |
| 387 | + accent_err.to_string().contains("accent colour"), | |
| 388 | + "{accent_err}" | |
| 389 | + ); | |
| 390 | + let base_err = base_choice(Lang::En, Some("neon"), "fern").unwrap_err(); | |
| 391 | + assert!(base_err.to_string().contains("background"), "{base_err}"); | |
| 269 | 392 | } |
| 270 | 393 | |
| 271 | 394 | #[test] |
| @@ -280,7 +403,7 @@ mod tests { | ||
| 280 | 403 | } |
| 281 | 404 | |
| 282 | 405 | #[test] |
| 283 | - fn the_stored_accent_is_selected() { | |
| 406 | + fn the_stored_accent_and_base_are_selected() { | |
| 284 | 407 | let codes: Vec<&'static str> = accent::ids().collect(); |
| 285 | 408 | let options = choices(&codes, "plum", |id| accent_name(Lang::En, id)); |
| 286 | 409 | let selected: Vec<&str> = options |
| @@ -290,12 +413,23 @@ mod tests { | ||
| 290 | 413 | .collect(); |
| 291 | 414 | assert_eq!(selected, ["plum"]); |
| 292 | 415 | assert_eq!(options[0].value, crate::accent_palette::DEFAULT); |
| 416 | + | |
| 417 | + let group = colour_group(Lang::En, true, "ink", "plum"); | |
| 418 | + let bases: Vec<&str> = group | |
| 419 | + .bases | |
| 420 | + .iter() | |
| 421 | + .filter(|c| c.selected) | |
| 422 | + .map(|c| c.value) | |
| 423 | + .collect(); | |
| 424 | + assert_eq!(bases, ["ink"]); | |
| 425 | + assert_eq!(group.bases[0].value, crate::accent_palette::BASE_DEFAULT); | |
| 293 | 426 | } |
| 294 | 427 | |
| 295 | 428 | #[test] |
| 296 | 429 | fn labels_follow_the_page_language() { |
| 297 | 430 | assert_eq!(theme_label(Lang::En, "dark"), "Dark"); |
| 298 | 431 | assert_eq!(theme_label(Lang::Tr, "dark"), "Koyu"); |
| 432 | + assert_eq!(base_name(Lang::Tr, "ink"), "M\u{fc}rekkep"); | |
| 299 | 433 | } |
| 300 | 434 | |
| 301 | 435 | #[test] |
crates/forge-web/src/views/mod.rsmodified+51 -22
| @@ -161,12 +161,28 @@ pub async fn shell( | ||
| 161 | 161 | let back = uri(__cx).path_and_query().map(|p| p.as_str().to_owned()); |
| 162 | 162 | let lang = i18n::lang(__cx, viewer.map(|(user, _)| user)); |
| 163 | 163 | let theme_attr = crate::theme::data_theme(__cx, viewer.map(|(user, _)| user)); |
| 164 | - // Always set, sage included, so a page never keeps the accent of the one before it. | |
| 165 | - let stored_accent = match viewer { | |
| 166 | - Some((user, _)) => user.accent.as_str(), | |
| 167 | - None => crate::accent_palette::DEFAULT, | |
| 168 | - }; | |
| 169 | - let accent_id = crate::accent::resolve(stored_accent).id; | |
| 164 | + // Always set, defaults included, so a page never keeps the colours of the one before it. | |
| 165 | + // Each of the four choices is resolved on its own, so an id that is no longer offered falls | |
| 166 | + // back to its default. | |
| 167 | + let (stored_accent_light, stored_accent_dark, stored_base_light, stored_base_dark) = | |
| 168 | + match viewer { | |
| 169 | + Some((user, _)) => ( | |
| 170 | + user.accent.as_str(), | |
| 171 | + user.accent_dark.as_str(), | |
| 172 | + user.base_light.as_str(), | |
| 173 | + user.base_dark.as_str(), | |
| 174 | + ), | |
| 175 | + None => ( | |
| 176 | + crate::accent_palette::DEFAULT, | |
| 177 | + crate::accent_palette::DEFAULT, | |
| 178 | + crate::accent_palette::BASE_DEFAULT, | |
| 179 | + crate::accent_palette::BASE_DEFAULT, | |
| 180 | + ), | |
| 181 | + }; | |
| 182 | + let accent_light_id = crate::accent::resolve(stored_accent_light).id; | |
| 183 | + let accent_dark_id = crate::accent::resolve(stored_accent_dark).id; | |
| 184 | + let base_light_id = crate::accent::resolve_base(stored_base_light).id; | |
| 185 | + let base_dark_id = crate::accent::resolve_base(stored_base_dark).id; | |
| 170 | 186 | // The stored preference, "auto" included, drives the button: its icon, its label and the |
| 171 | 187 | // value it posts (the next preference in the cycle auto, light, dark). |
| 172 | 188 | let theme_pref = crate::theme::preference(__cx, viewer.map(|(user, _)| user)); |
| @@ -196,7 +212,7 @@ pub async fn shell( | ||
| 196 | 212 | let unread_aside = unread_badge.clone(); |
| 197 | 213 | Ok(view! { |
| 198 | 214 | <!DOCTYPE html> |
| 199 | - <html lang=(lang.code()) data-theme=(theme_attr) data-accent=(accent_id)> | |
| 215 | + <html lang=(lang.code()) data-theme=(theme_attr) data-accent-light=(accent_light_id) data-accent-dark=(accent_dark_id) data-base-light=(base_light_id) data-base-dark=(base_dark_id)> | |
| 200 | 216 | <head> |
| 201 | 217 | <meta charset="utf-8"> |
| 202 | 218 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| @@ -962,8 +978,9 @@ mod shell_tests { | ||
| 962 | 978 | &page[at..at + len + 1] |
| 963 | 979 | } |
| 964 | 980 | |
| 965 | - /// The `<html>` tag of `/settings/sessions` for a signed-in user whose stored accent is `accent`. | |
| 966 | - async fn html_tag_for_accent(accent: &str) -> String { | |
| 981 | + /// The `<html>` tag of `/settings/sessions` for a signed-in user whose stored colours are the | |
| 982 | + /// four ids given, in the order light accent, dark accent, light base, dark base. | |
| 983 | + async fn html_tag_for_colours(colours: [&str; 4]) -> String { | |
| 967 | 984 | use crate::auth::SESSION_COOKIE; |
| 968 | 985 | let dir = tempfile::tempdir().expect("tempdir"); |
| 969 | 986 | let state = crate::test_support::test_state(dir.path()).await; |
| @@ -973,12 +990,16 @@ mod shell_tests { | ||
| 973 | 990 | .create_user("tahinli", None, "x", false) |
| 974 | 991 | .await |
| 975 | 992 | .expect("user"); |
| 976 | - store.set_accent(&user.id, accent).await.expect("accent"); | |
| 977 | - crate::test_support::create_session(&store, &user.id, "accent-token", "accent-csrf").await; | |
| 993 | + let [accent_light, accent_dark, base_light, base_dark] = colours; | |
| 994 | + store | |
| 995 | + .set_colours(&user.id, accent_light, accent_dark, base_light, base_dark) | |
| 996 | + .await | |
| 997 | + .expect("colours"); | |
| 998 | + crate::test_support::create_session(&store, &user.id, "colour-token", "colour-csrf").await; | |
| 978 | 999 | let req = Request::builder() |
| 979 | 1000 | .method("GET") |
| 980 | 1001 | .uri("/settings/sessions") |
| 981 | - .header("cookie", format!("__Host-{SESSION_COOKIE}=accent-token")) | |
| 1002 | + .header("cookie", format!("__Host-{SESSION_COOKIE}=colour-token")) | |
| 982 | 1003 | .body(Body::empty()) |
| 983 | 1004 | .unwrap(); |
| 984 | 1005 | let res = router.handle(req).await; |
| @@ -988,18 +1009,23 @@ mod shell_tests { | ||
| 988 | 1009 | } |
| 989 | 1010 | |
| 990 | 1011 | #[tokio::test] |
| 991 | - async fn the_html_tag_carries_the_viewers_accent() { | |
| 992 | - let ocean = html_tag_for_accent("ocean").await; | |
| 993 | - assert!(ocean.contains("data-accent=\"ocean\""), "{ocean}"); | |
| 994 | - let sage = html_tag_for_accent("sage").await; | |
| 995 | - assert!(sage.contains("data-accent=\"sage\""), "{sage}"); | |
| 996 | - // A stored id that is no longer offered falls back to the default, never to nothing. | |
| 997 | - let removed = html_tag_for_accent("removed-palette").await; | |
| 998 | - assert!(removed.contains("data-accent=\"sage\""), "{removed}"); | |
| 1012 | + async fn the_html_tag_carries_the_viewers_colours() { | |
| 1013 | + let tag = html_tag_for_colours(["ocean", "plum", "ink", "mist"]).await; | |
| 1014 | + assert!(tag.contains("data-accent-light=\"ocean\""), "{tag}"); | |
| 1015 | + assert!(tag.contains("data-accent-dark=\"plum\""), "{tag}"); | |
| 1016 | + assert!(tag.contains("data-base-light=\"ink\""), "{tag}"); | |
| 1017 | + assert!(tag.contains("data-base-dark=\"mist\""), "{tag}"); | |
| 1018 | + assert!(!tag.contains("data-accent="), "{tag}"); | |
| 1019 | + // Stored ids that are no longer offered fall back to the defaults, never to nothing. | |
| 1020 | + let removed = html_tag_for_colours(["removed-palette", "removed-palette", "x", "x"]).await; | |
| 1021 | + assert!(removed.contains("data-accent-light=\"sage\""), "{removed}"); | |
| 1022 | + assert!(removed.contains("data-accent-dark=\"sage\""), "{removed}"); | |
| 1023 | + assert!(removed.contains("data-base-light=\"fern\""), "{removed}"); | |
| 1024 | + assert!(removed.contains("data-base-dark=\"fern\""), "{removed}"); | |
| 999 | 1025 | } |
| 1000 | 1026 | |
| 1001 | 1027 | #[tokio::test] |
| 1002 | - async fn anonymous_pages_render_the_default_accent() { | |
| 1028 | + async fn anonymous_pages_render_the_default_colours() { | |
| 1003 | 1029 | let dir = tempfile::tempdir().expect("tempdir"); |
| 1004 | 1030 | let state = crate::test_support::test_state(dir.path()).await; |
| 1005 | 1031 | let router = crate::test_support::app_router(state); |
| @@ -1012,6 +1038,9 @@ mod shell_tests { | ||
| 1012 | 1038 | let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 1013 | 1039 | let html = String::from_utf8_lossy(&bytes); |
| 1014 | 1040 | let tag = html_tag(&html); |
| 1015 | - assert!(tag.contains("data-accent=\"sage\""), "{tag}"); | |
| 1041 | + assert!(tag.contains("data-accent-light=\"sage\""), "{tag}"); | |
| 1042 | + assert!(tag.contains("data-accent-dark=\"sage\""), "{tag}"); | |
| 1043 | + assert!(tag.contains("data-base-light=\"fern\""), "{tag}"); | |
| 1044 | + assert!(tag.contains("data-base-dark=\"fern\""), "{tag}"); | |
| 1016 | 1045 | } |
| 1017 | 1046 | } |
crates/forge-web/tests/ui_prefs_e2e.rsmodified+127 -49
| @@ -110,10 +110,31 @@ fn html_theme(body: &str) -> Option<String> { | ||
| 110 | 110 | doc.root_element().attr("data-theme").map(str::to_owned) |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | -/// The `data-accent` attribute of `<html>`. | |
| 114 | -fn html_accent(body: &str) -> Option<String> { | |
| 113 | +/// The value of attribute `name` on `<html>`, if the page sets it. | |
| 114 | +fn html_attr(body: &str, name: &str) -> Option<String> { | |
| 115 | 115 | let doc = scraper::Html::parse_document(body); |
| 116 | - doc.root_element().attr("data-accent").map(str::to_owned) | |
| 116 | + doc.root_element().attr(name).map(str::to_owned) | |
| 117 | +} | |
| 118 | + | |
| 119 | +/// The four colour attributes of `<html>`, in the order light accent, dark accent, light base, | |
| 120 | +/// dark base. | |
| 121 | +fn html_colours(body: &str) -> [Option<String>; 4] { | |
| 122 | + [ | |
| 123 | + html_attr(body, "data-accent-light"), | |
| 124 | + html_attr(body, "data-accent-dark"), | |
| 125 | + html_attr(body, "data-base-light"), | |
| 126 | + html_attr(body, "data-base-dark"), | |
| 127 | + ] | |
| 128 | +} | |
| 129 | + | |
| 130 | +/// The checked radio of each colour group, in the same order as `html_colours`. | |
| 131 | +fn checked_colours(body: &str) -> [Option<String>; 4] { | |
| 132 | + [ | |
| 133 | + checked_radio(body, "accent_light"), | |
| 134 | + checked_radio(body, "accent_dark"), | |
| 135 | + checked_radio(body, "base_light"), | |
| 136 | + checked_radio(body, "base_dark"), | |
| 137 | + ] | |
| 117 | 138 | } |
| 118 | 139 | |
| 119 | 140 | /// The value of the checked radio button in the group `name` on a page. |
| @@ -246,25 +267,18 @@ async fn appearance_setting_persists_and_renders_the_html_attribute() { | ||
| 246 | 267 | .http |
| 247 | 268 | .post_form( |
| 248 | 269 | "/settings/appearance", |
| 249 | - &[ | |
| 250 | - ("csrf", &csrf), | |
| 251 | - ("theme", "dark"), | |
| 252 | - ("accent", "sage"), | |
| 253 | - ("language", "tr"), | |
| 254 | - ], | |
| 270 | + &[("csrf", &csrf), ("theme", "dark"), ("language", "tr")], | |
| 255 | 271 | Some(&session), |
| 256 | 272 | ) |
| 257 | 273 | .await; |
| 258 | 274 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 259 | 275 | assert_eq!( |
| 260 | 276 | saved.header(reqwest::header::LOCATION).as_deref(), |
| 261 | - Some("/settings/appearance?saved=1") | |
| 277 | + Some("/settings/appearance"), | |
| 278 | + "a save redirects back with no saved notice" | |
| 262 | 279 | ); |
| 263 | 280 | |
| 264 | - let page = env | |
| 265 | - .http | |
| 266 | - .get("/settings/appearance?saved=1", Some(&session)) | |
| 267 | - .await; | |
| 281 | + let page = env.http.get("/settings/appearance", Some(&session)).await; | |
| 268 | 282 | assert_eq!( |
| 269 | 283 | selected_option(&page.body, "theme").as_deref(), |
| 270 | 284 | Some("dark") |
| @@ -273,29 +287,48 @@ async fn appearance_setting_persists_and_renders_the_html_attribute() { | ||
| 273 | 287 | selected_option(&page.body, "language").as_deref(), |
| 274 | 288 | Some("tr") |
| 275 | 289 | ); |
| 290 | + assert_eq!(html_theme(&page.body).as_deref(), Some("dark")); | |
| 291 | +} | |
| 276 | 292 | |
| 277 | - let home = env.http.get("/settings/appearance", Some(&session)).await; | |
| 278 | - assert_eq!(html_theme(&home.body).as_deref(), Some("dark")); | |
| 279 | - | |
| 293 | +#[tokio::test] | |
| 294 | +async fn the_form_has_no_save_button_and_every_field_autosubmits() { | |
| 295 | + let env = start().await; | |
| 296 | + let session = login(&env.http, "tahinli").await; | |
| 280 | 297 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 281 | - assert_eq!( | |
| 282 | - selected_option(&page.body, "theme").as_deref(), | |
| 283 | - Some("dark") | |
| 284 | - ); | |
| 285 | - assert_eq!( | |
| 286 | - selected_option(&page.body, "language").as_deref(), | |
| 287 | - Some("tr") | |
| 288 | - ); | |
| 298 | + let doc = scraper::Html::parse_document(&page.body); | |
| 299 | + | |
| 300 | + let form = scraper::Selector::parse("form[data-appearance]").expect("selector"); | |
| 301 | + assert_eq!(doc.select(&form).count(), 1, "one appearance form"); | |
| 302 | + let buttons = scraper::Selector::parse("form[data-appearance] button").expect("selector"); | |
| 303 | + assert_eq!(doc.select(&buttons).count(), 0, "no Save button"); | |
| 304 | + let submits = | |
| 305 | + scraper::Selector::parse("form[data-appearance] input[type=\"submit\"]").expect("selector"); | |
| 306 | + assert_eq!(doc.select(&submits).count(), 0, "no submit input"); | |
| 307 | + | |
| 308 | + let fields = | |
| 309 | + scraper::Selector::parse("form[data-appearance] input, form[data-appearance] select") | |
| 310 | + .expect("selector"); | |
| 311 | + for field in doc.select(&fields) { | |
| 312 | + if field.value().attr("type") == Some("hidden") { | |
| 313 | + continue; | |
| 314 | + } | |
| 315 | + assert!( | |
| 316 | + field.value().attr("data-autosubmit").is_some(), | |
| 317 | + "{:?} does not autosubmit", | |
| 318 | + field.value().attr("name") | |
| 319 | + ); | |
| 320 | + } | |
| 289 | 321 | } |
| 290 | 322 | |
| 291 | 323 | #[tokio::test] |
| 292 | -async fn accent_setting_persists_and_renders_the_html_attribute() { | |
| 324 | +async fn light_and_dark_colours_save_independently_and_render_on_html() { | |
| 293 | 325 | let env = start().await; |
| 294 | 326 | let session = login(&env.http, "tahinli").await; |
| 295 | 327 | |
| 296 | 328 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 297 | - assert_eq!(checked_radio(&page.body, "accent").as_deref(), Some("sage")); | |
| 298 | - assert_eq!(html_accent(&page.body).as_deref(), Some("sage")); | |
| 329 | + let defaults = ["sage", "sage", "fern", "fern"].map(str::to_owned); | |
| 330 | + assert_eq!(checked_colours(&page.body), defaults.clone().map(Some)); | |
| 331 | + assert_eq!(html_colours(&page.body), defaults.map(Some)); | |
| 299 | 332 | let csrf = input_value(&page.body, "csrf"); |
| 300 | 333 | |
| 301 | 334 | let saved = env |
| @@ -305,8 +338,11 @@ async fn accent_setting_persists_and_renders_the_html_attribute() { | ||
| 305 | 338 | &[ |
| 306 | 339 | ("csrf", &csrf), |
| 307 | 340 | ("theme", "light"), |
| 308 | - ("accent", "ocean"), | |
| 309 | 341 | ("language", "en"), |
| 342 | + ("accent_light", "ocean"), | |
| 343 | + ("accent_dark", "plum"), | |
| 344 | + ("base_light", "ink"), | |
| 345 | + ("base_dark", "mist"), | |
| 310 | 346 | ], |
| 311 | 347 | Some(&session), |
| 312 | 348 | ) |
| @@ -314,43 +350,85 @@ async fn accent_setting_persists_and_renders_the_html_attribute() { | ||
| 314 | 350 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 315 | 351 | |
| 316 | 352 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 317 | - assert_eq!( | |
| 318 | - checked_radio(&page.body, "accent").as_deref(), | |
| 319 | - Some("ocean") | |
| 320 | - ); | |
| 321 | - assert_eq!(html_accent(&page.body).as_deref(), Some("ocean")); | |
| 353 | + let chosen = ["ocean", "plum", "ink", "mist"].map(str::to_owned); | |
| 354 | + assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); | |
| 355 | + assert_eq!(html_colours(&page.body), chosen.map(Some)); | |
| 356 | + | |
| 357 | + // A single field changes on its own; the other three keep their values. | |
| 358 | + let saved = env | |
| 359 | + .http | |
| 360 | + .post_form( | |
| 361 | + "/settings/appearance", | |
| 362 | + &[ | |
| 363 | + ("csrf", &csrf), | |
| 364 | + ("theme", "light"), | |
| 365 | + ("language", "en"), | |
| 366 | + ("base_dark", "sand"), | |
| 367 | + ], | |
| 368 | + Some(&session), | |
| 369 | + ) | |
| 370 | + .await; | |
| 371 | + assert_eq!(saved.status, 303, "save: {}", saved.body); | |
| 372 | + let page = env.http.get("/settings/appearance", Some(&session)).await; | |
| 373 | + let chosen = ["ocean", "plum", "ink", "sand"].map(str::to_owned); | |
| 374 | + assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); | |
| 375 | + assert_eq!(html_colours(&page.body), chosen.map(Some)); | |
| 322 | 376 | |
| 323 | 377 | // Malformed or unknown ids change nothing. |
| 324 | - for accent in ["Ocean", "../x", "neon", ""] { | |
| 378 | + for (field, bad) in [ | |
| 379 | + ("accent_light", "Ocean"), | |
| 380 | + ("accent_dark", "../x"), | |
| 381 | + ("accent_dark", "neon"), | |
| 382 | + ("accent_light", ""), | |
| 383 | + ("base_light", "neon"), | |
| 384 | + ("base_dark", ""), | |
| 385 | + ] { | |
| 325 | 386 | let form = [ |
| 326 | 387 | ("csrf", csrf.as_str()), |
| 327 | 388 | ("theme", "light"), |
| 328 | 389 | ("language", "en"), |
| 329 | - ("accent", accent), | |
| 390 | + (field, bad), | |
| 330 | 391 | ]; |
| 331 | 392 | let res = env |
| 332 | 393 | .http |
| 333 | 394 | .post_form("/settings/appearance", &form, Some(&session)) |
| 334 | 395 | .await; |
| 335 | - assert_ne!(res.status, 303, "bad accent {accent:?} must not save"); | |
| 396 | + assert_ne!(res.status, 303, "bad {field} {bad:?} must not save"); | |
| 336 | 397 | } |
| 337 | 398 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 338 | - assert_eq!( | |
| 339 | - checked_radio(&page.body, "accent").as_deref(), | |
| 340 | - Some("ocean") | |
| 341 | - ); | |
| 342 | - assert_eq!(html_accent(&page.body).as_deref(), Some("ocean")); | |
| 399 | + let chosen = ["ocean", "plum", "ink", "sand"].map(str::to_owned); | |
| 400 | + assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); | |
| 401 | + assert_eq!(html_colours(&page.body), chosen.map(Some)); | |
| 343 | 402 | } |
| 344 | 403 | |
| 345 | 404 | #[tokio::test] |
| 346 | -async fn appearance_save_without_accent_keeps_the_stored_accent() { | |
| 405 | +async fn a_save_without_colour_fields_keeps_the_stored_colours() { | |
| 347 | 406 | let env = start().await; |
| 348 | 407 | let session = login(&env.http, "tahinli").await; |
| 349 | 408 | |
| 350 | 409 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 351 | 410 | let csrf = input_value(&page.body, "csrf"); |
| 352 | - let before = checked_radio(&page.body, "accent"); | |
| 353 | - assert!(before.is_some(), "an accent is always selected"); | |
| 411 | + let saved = env | |
| 412 | + .http | |
| 413 | + .post_form( | |
| 414 | + "/settings/appearance", | |
| 415 | + &[ | |
| 416 | + ("csrf", &csrf), | |
| 417 | + ("theme", "light"), | |
| 418 | + ("language", "en"), | |
| 419 | + ("accent_light", "rose"), | |
| 420 | + ("accent_dark", "slate"), | |
| 421 | + ("base_light", "sand"), | |
| 422 | + ("base_dark", "stone"), | |
| 423 | + ], | |
| 424 | + Some(&session), | |
| 425 | + ) | |
| 426 | + .await; | |
| 427 | + assert_eq!(saved.status, 303, "save: {}", saved.body); | |
| 428 | + let before = env.http.get("/settings/appearance", Some(&session)).await; | |
| 429 | + let stored = ["rose", "slate", "sand", "stone"].map(str::to_owned); | |
| 430 | + assert_eq!(html_colours(&before.body), stored.clone().map(Some)); | |
| 431 | + | |
| 354 | 432 | let saved = env |
| 355 | 433 | .http |
| 356 | 434 | .post_form( |
| @@ -359,15 +437,15 @@ async fn appearance_save_without_accent_keeps_the_stored_accent() { | ||
| 359 | 437 | Some(&session), |
| 360 | 438 | ) |
| 361 | 439 | .await; |
| 362 | - assert_eq!(saved.status, 303, "save without accent: {}", saved.body); | |
| 440 | + assert_eq!(saved.status, 303, "save without colours: {}", saved.body); | |
| 363 | 441 | |
| 364 | 442 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 365 | 443 | assert_eq!( |
| 366 | 444 | selected_option(&page.body, "theme").as_deref(), |
| 367 | 445 | Some("dark") |
| 368 | 446 | ); |
| 369 | - assert_eq!(checked_radio(&page.body, "accent"), before); | |
| 370 | - assert_eq!(html_accent(&page.body), before); | |
| 447 | + assert_eq!(checked_colours(&page.body), stored.clone().map(Some)); | |
| 448 | + assert_eq!(html_colours(&page.body), stored.map(Some)); | |
| 371 | 449 | } |
| 372 | 450 | |
| 373 | 451 | #[tokio::test] |
| @@ -505,7 +583,7 @@ async fn signed_in_toggle_saves_the_preference_and_returns_to_back() { | ||
| 505 | 583 | &[ |
| 506 | 584 | ("csrf", &csrf), |
| 507 | 585 | ("theme", "auto"), |
| 508 | - ("accent", "sage"), | |
| 586 | + ("accent_light", "sage"), | |
| 509 | 587 | ("language", "tr"), |
| 510 | 588 | ], |
| 511 | 589 | Some(&session), |
style/main.scssmodified+71 -51
| @@ -60,19 +60,13 @@ | ||
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // Accent tokens (light and dark) are generated into _accents.scss from src/accent_palette.rs. |
| 63 | -// They are imported here so they keep their place in the cascade; the neutral tokens follow. | |
| 63 | +// They are imported here so they keep their place in the cascade; the other tokens follow. | |
| 64 | 64 | @import "accents"; |
| 65 | 65 | |
| 66 | -// The neutral dark values are repeated in the two dark blocks below and must stay identical. | |
| 66 | +// The neutral tokens (--bg, --surface, --text and so on) are not here: they come from the | |
| 67 | +// generated _accents.scss, one block per base in src/accent_palette.rs. Only the tokens that are | |
| 68 | +// the same for every base stay below. | |
| 67 | 69 | :root { |
| 68 | - --bg: #F6F8F5; | |
| 69 | - --surface: #FFFFFF; | |
| 70 | - --surface-2: #EEF2EC; | |
| 71 | - --text: #1E2420; | |
| 72 | - --text-2: #3A433D; | |
| 73 | - --muted: #69736B; | |
| 74 | - --border: #DDE3DB; | |
| 75 | - --border-strong: #C5CFC3; | |
| 76 | 70 | --ok: #1D7A62; |
| 77 | 71 | --warn: #9A6200; |
| 78 | 72 | --bad: #B0413A; |
| @@ -81,16 +75,12 @@ | ||
| 81 | 75 | --danger-bg: #F8E1DE; |
| 82 | 76 | --alert-bg: #F8E1DE; |
| 83 | 77 | --alert-text: #7D2B25; |
| 84 | - --code-bg: #EEF2EC; | |
| 85 | - --private-bg: #EEF2EC; | |
| 86 | - --private-text: #3A433D; | |
| 87 | 78 | --add-bg: #DCF2EA; |
| 88 | 79 | --add-text: #174D3F; |
| 89 | 80 | --add-mark: #1D7A62; |
| 90 | 81 | --del-bg: #F8E1DE; |
| 91 | 82 | --del-text: #7D2B25; |
| 92 | 83 | --del-mark: #B0413A; |
| 93 | - --ln-text: #69736B; | |
| 94 | 84 | --row-target: #FFF4C2; |
| 95 | 85 | --tip: #1D7A62; |
| 96 | 86 | --important: #6A4BB0; |
| @@ -102,20 +92,12 @@ | ||
| 102 | 92 | --topbar-h: 52px; |
| 103 | 93 | --sidebar-w: 232px; |
| 104 | 94 | color-scheme: light; |
| 105 | - // Native checkboxes, radios and progress bars pick up the sage accent in every browser. | |
| 95 | + // Native checkboxes, radios and progress bars pick up the chosen accent in every browser. | |
| 106 | 96 | accent-color: var(--accent); |
| 107 | 97 | } |
| 108 | 98 | |
| 109 | 99 | @media (prefers-color-scheme: dark) { |
| 110 | 100 | :root:not([data-theme="light"]) { |
| 111 | - --bg: #121613; | |
| 112 | - --surface: #191E1A; | |
| 113 | - --surface-2: #1F261F; | |
| 114 | - --text: #E3E8E2; | |
| 115 | - --text-2: #C4CCC3; | |
| 116 | - --muted: #97A198; | |
| 117 | - --border: #2A322C; | |
| 118 | - --border-strong: #3A443C; | |
| 119 | 101 | --ok: #7CCBAE; |
| 120 | 102 | --warn: #E6B765; |
| 121 | 103 | --bad: #F09A90; |
| @@ -124,16 +106,12 @@ | ||
| 124 | 106 | --danger-bg: #3A1E1C; |
| 125 | 107 | --alert-bg: #3A1E1C; |
| 126 | 108 | --alert-text: #F2B8B0; |
| 127 | - --code-bg: #1F261F; | |
| 128 | - --private-bg: #1F261F; | |
| 129 | - --private-text: #C4CCC3; | |
| 130 | 109 | --add-bg: #143029; |
| 131 | 110 | --add-text: #A6E3CF; |
| 132 | 111 | --add-mark: #7CCBAE; |
| 133 | 112 | --del-bg: #3A1E1C; |
| 134 | 113 | --del-text: #F2B8B0; |
| 135 | 114 | --del-mark: #F09A90; |
| 136 | - --ln-text: #97A198; | |
| 137 | 115 | --row-target: #3A3510; |
| 138 | 116 | --tip: #7CCBAE; |
| 139 | 117 | --important: #C4A7FF; |
| @@ -148,14 +126,6 @@ | ||
| 148 | 126 | } |
| 149 | 127 | |
| 150 | 128 | :root[data-theme="dark"] { |
| 151 | - --bg: #121613; | |
| 152 | - --surface: #191E1A; | |
| 153 | - --surface-2: #1F261F; | |
| 154 | - --text: #E3E8E2; | |
| 155 | - --text-2: #C4CCC3; | |
| 156 | - --muted: #97A198; | |
| 157 | - --border: #2A322C; | |
| 158 | - --border-strong: #3A443C; | |
| 159 | 129 | --ok: #7CCBAE; |
| 160 | 130 | --warn: #E6B765; |
| 161 | 131 | --bad: #F09A90; |
| @@ -164,16 +134,12 @@ | ||
| 164 | 134 | --danger-bg: #3A1E1C; |
| 165 | 135 | --alert-bg: #3A1E1C; |
| 166 | 136 | --alert-text: #F2B8B0; |
| 167 | - --code-bg: #1F261F; | |
| 168 | - --private-bg: #1F261F; | |
| 169 | - --private-text: #C4CCC3; | |
| 170 | 137 | --add-bg: #143029; |
| 171 | 138 | --add-text: #A6E3CF; |
| 172 | 139 | --add-mark: #7CCBAE; |
| 173 | 140 | --del-bg: #3A1E1C; |
| 174 | 141 | --del-text: #F2B8B0; |
| 175 | 142 | --del-mark: #F09A90; |
| 176 | - --ln-text: #97A198; | |
| 177 | 143 | --row-target: #3A3510; |
| 178 | 144 | --tip: #7CCBAE; |
| 179 | 145 | --important: #C4A7FF; |
| @@ -1857,12 +1823,55 @@ code { | ||
| 1857 | 1823 | max-width: 24rem; |
| 1858 | 1824 | } |
| 1859 | 1825 | |
| 1860 | -// Accent picker (appearance settings). Each choice is a native radio, so the keyboard and screen | |
| 1826 | +// Colour pickers (appearance settings). Each choice is a native radio, so the keyboard and screen | |
| 1861 | 1827 | // readers work as usual; the swatch beside it shows the colour. The swatch colours come from the |
| 1862 | -// generated _accents.scss, so nothing here names a colour. | |
| 1863 | -.accent-picker { | |
| 1828 | +// generated _accents.scss, so nothing here names a colour. The two themes sit side by side, and | |
| 1829 | +// stack on narrow screens, so the whole page fits a laptop window without a long scroll. | |
| 1830 | +.appearance { | |
| 1864 | 1831 | display: grid; |
| 1865 | - gap: 8px; | |
| 1832 | + gap: 16px; | |
| 1833 | + min-width: 0; | |
| 1834 | +} | |
| 1835 | + | |
| 1836 | +.appearance-top { | |
| 1837 | + display: flex; | |
| 1838 | + flex-wrap: wrap; | |
| 1839 | + gap: 16px 24px; | |
| 1840 | + | |
| 1841 | + label { | |
| 1842 | + display: grid; | |
| 1843 | + gap: 4px; | |
| 1844 | + min-width: 0; | |
| 1845 | + } | |
| 1846 | +} | |
| 1847 | + | |
| 1848 | +.appearance-columns { | |
| 1849 | + display: grid; | |
| 1850 | + grid-template-columns: repeat(2, minmax(0, 1fr)); | |
| 1851 | + gap: 16px; | |
| 1852 | + min-width: 0; | |
| 1853 | + | |
| 1854 | + @media (max-width: 720px) { | |
| 1855 | + grid-template-columns: minmax(0, 1fr); | |
| 1856 | + } | |
| 1857 | +} | |
| 1858 | + | |
| 1859 | +.colour-group { | |
| 1860 | + display: grid; | |
| 1861 | + gap: 14px; | |
| 1862 | + min-width: 0; | |
| 1863 | + margin: 0; | |
| 1864 | + | |
| 1865 | + legend { | |
| 1866 | + padding: 0; | |
| 1867 | + margin-bottom: 4px; | |
| 1868 | + font-weight: 600; | |
| 1869 | + } | |
| 1870 | +} | |
| 1871 | + | |
| 1872 | +.colour-picker { | |
| 1873 | + display: grid; | |
| 1874 | + gap: 6px; | |
| 1866 | 1875 | min-width: 0; |
| 1867 | 1876 | margin: 0; |
| 1868 | 1877 | padding: 0; |
| @@ -1870,18 +1879,19 @@ code { | ||
| 1870 | 1879 | |
| 1871 | 1880 | legend { |
| 1872 | 1881 | padding: 0; |
| 1873 | - margin-bottom: 6px; | |
| 1874 | - font-weight: 600; | |
| 1882 | + margin-bottom: 2px; | |
| 1883 | + font-size: 0.875rem; | |
| 1884 | + color: var(--muted); | |
| 1875 | 1885 | } |
| 1876 | 1886 | } |
| 1877 | 1887 | |
| 1878 | -.accent-choices { | |
| 1888 | +.colour-choices { | |
| 1879 | 1889 | display: flex; |
| 1880 | 1890 | flex-wrap: wrap; |
| 1881 | - gap: 10px 18px; | |
| 1891 | + gap: 8px 14px; | |
| 1882 | 1892 | } |
| 1883 | 1893 | |
| 1884 | -.accent-choice { | |
| 1894 | +.colour-choice { | |
| 1885 | 1895 | display: inline-flex; |
| 1886 | 1896 | align-items: center; |
| 1887 | 1897 | gap: 8px; |
| @@ -1896,17 +1906,20 @@ code { | ||
| 1896 | 1906 | opacity: 0; |
| 1897 | 1907 | } |
| 1898 | 1908 | |
| 1899 | - input:focus-visible + .accent-swatch { | |
| 1909 | + input:focus-visible + .accent-swatch, | |
| 1910 | + input:focus-visible + .base-swatch { | |
| 1900 | 1911 | outline: 2px solid var(--focus); |
| 1901 | 1912 | outline-offset: 3px; |
| 1902 | 1913 | } |
| 1903 | 1914 | |
| 1904 | - input:checked + .accent-swatch { | |
| 1915 | + input:checked + .accent-swatch, | |
| 1916 | + input:checked + .base-swatch { | |
| 1905 | 1917 | box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--focus); |
| 1906 | 1918 | } |
| 1907 | 1919 | } |
| 1908 | 1920 | |
| 1909 | -.accent-swatch { | |
| 1921 | +.accent-swatch, | |
| 1922 | +.base-swatch { | |
| 1910 | 1923 | display: inline-block; |
| 1911 | 1924 | flex: none; |
| 1912 | 1925 | width: 28px; |
| @@ -1915,6 +1928,13 @@ code { | ||
| 1915 | 1928 | border: 1px solid var(--border-strong); |
| 1916 | 1929 | } |
| 1917 | 1930 | |
| 1931 | +// A background swatch is split light and dark, so a pale one needs its own outline to show on a | |
| 1932 | +// pale page. | |
| 1933 | +.base-swatch { | |
| 1934 | + outline: 1px solid var(--border-strong); | |
| 1935 | + outline-offset: 1px; | |
| 1936 | +} | |
| 1937 | + | |
| 1918 | 1938 | // branch protection |
| 1919 | 1939 | // Settings page: rules table with a Remove form per row, then the add/update form. |
| 1920 | 1940 | .branch-rules { |