forge
Explore
Log in

Don't you dare go Hollow

Appearance colours are a few presets plus a hue stripe, in light and dark tabs

Browse files at this commit

3f4e1ca0982b0840ac0ac97c633be2e053e9eaf1Ahmet Kaan GümüşParent: cd71b21
  • feat
    • appearance colours are a few presets plus a hue stripe, in light and dark tabs
Show full message
💄 feat: appearance colours are a few presets plus a hue stripe, in light and dark tabs
crates/forge-web/assets/nav.jsmodified+101 -0
@@ -502,6 +502,8 @@
502502 var i = pair.indexOf('=');
503503 if (i < 0) { return; }
504504 var name = pair.slice(0, i), value = pair.slice(i + 1);
505+ // The custom colour sheet's href, or empty when no colour is a custom hue.
506+ if (name === 'custom_css') { setCustomCss(value); return; }
505507 if (name.indexOf('data-') !== 0) { return; }
506508 if (value) { root.setAttribute(name, value); } else { root.removeAttribute(name); }
507509 });
@@ -738,6 +740,52 @@
738740 // The attributes match what the shell renders: theme `auto` has no data-theme, `dark` is dark, else light.
739741 var PREVIEW_ATTR = { accent_light: 'data-accent-light', accent_dark: 'data-accent-dark', base_light: 'data-base-light', base_dark: 'data-base-dark', sidebar_side: 'data-sidebar' };
740742
743+ // The custom stylesheet sets the colours of the hue choices (`hue-N`). Its link follows the four
744+ // colour attributes on <html>: it is added when one of them is a hue, its href is kept in step,
745+ // and it is removed when none is.
746+ // An empty href removes the link. The server sends the href in the live payload (`custom_css`).
747+ function setCustomCss(href) {
748+ var link = document.head.querySelector('link[rel="stylesheet"][href^="/theme/custom.css"]');
749+ if (!href) {
750+ if (link) { link.remove(); }
751+ return;
752+ }
753+ if (!link) {
754+ link = document.createElement('link');
755+ link.rel = 'stylesheet';
756+ document.head.appendChild(link);
757+ }
758+ if (link.getAttribute('href') !== href) { link.setAttribute('href', href); }
759+ }
760+
761+ // Builds the custom sheet's href from the four colour attributes on <html> while a drag or a
762+ // preset pick is shown before the save returns. The server builds the same href on the next load.
763+ function syncCustomCss() {
764+ var slots = ['data-accent-light', 'data-accent-dark', 'data-base-light', 'data-base-dark'];
765+ var vals = slots.map(function (name) { return root.getAttribute(name) || ''; });
766+ var custom = vals.some(function (v) { return /^hue-\d+$/.test(v); });
767+ if (!custom) { setCustomCss(''); return; }
768+ setCustomCss('/theme/custom.css?al=' + encodeURIComponent(vals[0]) + '&ad=' + encodeURIComponent(vals[1]) +
769+ '&bl=' + encodeURIComponent(vals[2]) + '&bd=' + encodeURIComponent(vals[3]));
770+ }
771+
772+ // A drag moves the stylesheet at most once a frame, however many input events it sends.
773+ var previewFrame = 0;
774+ function previewLater() {
775+ if (previewFrame) { return; }
776+ previewFrame = requestAnimationFrame(function () { previewFrame = 0; syncCustomCss(); });
777+ }
778+
779+ // Moves a row's stripe knob to a hue, and makes the row's Custom choice that hue (the value a save posts).
780+ // The hue is also set on the row as `--hue`, which the stripe's knob and the Custom swatch read.
781+ function setKnob(row, hue) {
782+ var range = row.querySelector('input[data-hue-stripe]');
783+ var custom = row.querySelector('input[data-hue-custom]');
784+ if (range) { range.value = String(hue); }
785+ row.style.setProperty('--hue', String(hue));
786+ if (custom) { custom.value = 'hue-' + hue; }
787+ }
788+
741789 document.addEventListener('change', function (e) {
742790 var el = e.target;
743791 if (!el || !el.form || !el.form.hasAttribute('data-appearance')) { return; }
@@ -746,11 +794,63 @@
746794 else { root.setAttribute('data-theme', el.value === 'dark' ? 'dark' : 'light'); }
747795 return;
748796 }
797+ if (el.type === 'radio' && el.hasAttribute('data-colour-tab')) {
798+ var group = el.closest('[data-colour-group]');
799+ if (group) { group.setAttribute('data-tab', el.value); }
800+ return;
801+ }
749802 if (el.type === 'radio' && Object.prototype.hasOwnProperty.call(PREVIEW_ATTR, el.name)) {
750803 root.setAttribute(PREVIEW_ATTR[el.name], el.value);
804+ // A preset moves its row's knob to the preset's hue, without choosing Custom.
805+ var row = el.closest('[data-colour-row]');
806+ var hue = el.getAttribute('data-hue');
807+ if (row && hue) { setKnob(row, hue); }
808+ syncCustomCss();
809+ }
810+ }, false);
811+
812+ // Dragging a stripe selects its Custom choice and shows the hue on <html> while the drag lasts.
813+ // The save waits for the release (the change event), which the autosubmit handler sends.
814+ document.addEventListener('input', function (e) {
815+ var el = e.target;
816+ if (!el || el.type !== 'range' || !el.hasAttribute('data-hue-stripe') || !el.form || !el.form.hasAttribute('data-appearance')) { return; }
817+ var row = el.closest('[data-colour-row]');
818+ var custom = row ? row.querySelector('input[data-hue-custom]') : null;
819+ if (!custom) { return; }
820+ setKnob(row, el.value);
821+ custom.checked = true;
822+ if (Object.prototype.hasOwnProperty.call(PREVIEW_ATTR, custom.name)) {
823+ root.setAttribute(PREVIEW_ATTR[custom.name], custom.value);
751824 }
825+ previewLater();
752826 }, false);
753827
828+ // The theme the viewer sees: `dark` or `light` on <html>, else the system's setting.
829+ function effectiveDark() {
830+ var t = root.getAttribute('data-theme');
831+ if (t === 'dark') { return true; }
832+ if (t === 'light') { return false; }
833+ return !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
834+ }
835+
836+ // The colour panel opens on the theme the viewer sees. A tab the viewer picked is kept across a
837+ // swap, because the swap restores it as a changed field. Each panel is set up once; after that its
838+ // shown panel follows its tab radios. Called on every page change (see `enhance`).
839+ function enhanceColours() {
840+ document.querySelectorAll('[data-colour-group]').forEach(function (group) {
841+ var tabs = group.querySelectorAll('input[data-colour-tab]');
842+ if (!group.__colourTab) {
843+ group.__colourTab = true;
844+ var moved = Array.prototype.some.call(tabs, function (r) { return r.checked !== r.defaultChecked; });
845+ if (!moved) {
846+ Array.prototype.forEach.call(tabs, function (r) { r.checked = r.value === (effectiveDark() ? 'dark' : 'light'); });
847+ }
848+ }
849+ var on = group.querySelector('input[data-colour-tab]:checked');
850+ if (on) { group.setAttribute('data-tab', on.value); }
851+ });
852+ }
853+
754854 // A filtered view asked for in place: the URL is replaced (no history entry per change), fields
755855 // and caret come through the swap, and the scroll stays put.
756856 // Live unread count. The bell is re-rendered by every swap, so it is looked up on each update. One
@@ -1894,6 +1994,7 @@
18941994 // ---- toolbar and upload ----
18951995
18961996 function enhance() {
1997+ enhanceColours();
18971998 document.querySelectorAll('textarea').forEach(function (ta) {
18981999 if (ta.__md || !eligible(ta)) { return; }
18992000 ta.__md = true;
crates/forge-web/src/i18n/misc.rsmodified+16 -12
@@ -25,10 +25,11 @@ pub enum K {
2525 SwitchToMatchSystem,
2626 ChooseTheme,
2727 ChooseLanguage,
28- AccentLabel,
2928 ChooseAccent,
30- LightColours,
31- DarkColours,
29+ ColoursLabel,
30+ AccentRowLabel,
31+ CustomLabel,
32+ HueLabel,
3233 BackgroundLabel,
3334 ChooseBase,
3435 // Appearance: which side the shell sidebar sits on.
@@ -175,10 +176,11 @@ const ALL: &[K] = &[
175176 K::SwitchToMatchSystem,
176177 K::ChooseTheme,
177178 K::ChooseLanguage,
178- K::AccentLabel,
179179 K::ChooseAccent,
180- K::LightColours,
181- K::DarkColours,
180+ K::ColoursLabel,
181+ K::AccentRowLabel,
182+ K::CustomLabel,
183+ K::HueLabel,
182184 K::BackgroundLabel,
183185 K::ChooseBase,
184186 K::SidebarLabel,
@@ -330,14 +332,16 @@ pub fn t(lang: Lang, key: K) -> &'static str {
330332 (ChooseTheme, Tr) => "Listeden bir tema seçin.",
331333 (ChooseLanguage, En) => "Choose a language from the list.",
332334 (ChooseLanguage, Tr) => "Listeden bir dil seçin.",
333- (AccentLabel, En) => "Accent colour",
334- (AccentLabel, Tr) => "Vurgu rengi",
335335 (ChooseAccent, En) => "Choose an accent colour from the list.",
336336 (ChooseAccent, Tr) => "Listeden bir vurgu rengi seçin.",
337- (LightColours, En) => "Light theme",
338- (LightColours, Tr) => "Açık tema",
339- (DarkColours, En) => "Dark theme",
340- (DarkColours, Tr) => "Koyu tema",
337+ (ColoursLabel, En) => "Colours",
338+ (ColoursLabel, Tr) => "Renkler",
339+ (AccentRowLabel, En) => "Accent",
340+ (AccentRowLabel, Tr) => "Vurgu",
341+ (CustomLabel, En) => "Custom",
342+ (CustomLabel, Tr) => "Özel",
343+ (HueLabel, En) => "Hue",
344+ (HueLabel, Tr) => "Ton",
341345 (BackgroundLabel, En) => "Background",
342346 (BackgroundLabel, Tr) => "Arka plan",
343347 (ChooseBase, En) => "Choose a background from the list.",
crates/forge-web/src/pages/settings_appearance.rsmodified+200 -46
@@ -15,7 +15,7 @@ use topcoat::router::error::{SeeOther, see_other};
1515 use topcoat::router::{page, route};
1616 use topcoat::view::{View, component, view};
1717
18-use crate::accent;
18+use crate::accent::{self, hue_of, preset_accent_hue, preset_base_hue};
1919 use crate::audit::{self, Actor, Target};
2020 use crate::auth::{require_csrf, require_user};
2121 use crate::i18n::accents::{accent_name, base_name};
@@ -34,6 +34,9 @@ const THEME_CODES: &[&str] = &["auto", "light", "dark"];
3434 /// Sidebar sides, in display order. Must match `store::ui_prefs::SIDEBAR_SIDES`.
3535 const SIDEBAR_CODES: &[&str] = &["left", "right"];
3636
37+/// The colour tabs, in display order: the light theme's colours, then the dark theme's.
38+const TAB_CODES: &[&str] = &["light", "dark"];
39+
3740 /// Reads a field that is present as a string, even an empty one. The plain `Option<String>`
3841 /// reading would turn an empty value into `None`, which is the same as a missing field.
3942 fn present_string<'de, D: Deserializer<'de>>(
@@ -69,17 +72,29 @@ struct Choice {
6972 value: &'static str,
7073 label: &'static str,
7174 selected: bool,
75+ /// The hue a preset moves the stripe knob to, as text; empty when the preset has none.
76+ hue: String,
77+}
78+
79+/// Where a hue stripe's knob sits. `custom` says the stored choice is that hue, rather than a
80+/// preset whose hue the knob was moved to.
81+struct Stripe {
82+ hue: u16,
83+ custom: bool,
7284 }
7385
74-/// The colour choices of one theme: its background and its accent colour. `dark` picks the
75-/// field names and the swatch attributes that belong to the dark theme.
86+/// The colour choices of one theme: a background and an accent, each a row of presets and a hue
87+/// stripe. `dark` picks the field names and the swatch attributes that belong to the dark theme.
7688 struct ColourGroup {
7789 dark: bool,
78- heading: &'static str,
7990 background: &'static str,
8091 accent: &'static str,
92+ custom: &'static str,
93+ hue: &'static str,
8194 bases: Vec<Choice>,
8295 accents: Vec<Choice>,
96+ base_stripe: Stripe,
97+ accent_stripe: Stripe,
8398 }
8499
85100 struct AppearancePage {
@@ -87,6 +102,10 @@ struct AppearancePage {
87102 csrf: String,
88103 theme: Vec<Choice>,
89104 language: Vec<Choice>,
105+ colours: &'static str,
106+ /// The tab that opens first: the light or the dark colours.
107+ tab: &'static str,
108+ tabs: Vec<Choice>,
90109 light: ColourGroup,
91110 dark: ColourGroup,
92111 sidebar: Vec<Choice>,
@@ -134,52 +153,103 @@ fn choices(
134153 value,
135154 label: label(value),
136155 selected: value == current,
156+ hue: String::new(),
137157 })
138158 .collect()
139159 }
140160
141-/// One theme's colour choices, with the stored (resolved) background and accent selected.
161+/// The label of a colour tab: the light or the dark theme.
162+fn tab_label(lang: Lang, code: &str) -> &'static str {
163+ if code == "dark" {
164+ g::t(lang, K::Dark)
165+ } else {
166+ g::t(lang, K::Light)
167+ }
168+}
169+
170+/// Fills in the hue each preset moves the stripe knob to.
171+fn with_hues(mut list: Vec<Choice>, hue: fn(&str) -> Option<u16>) -> Vec<Choice> {
172+ for choice in &mut list {
173+ choice.hue = hue(choice.value).map(|h| h.to_string()).unwrap_or_default();
174+ }
175+ list
176+}
177+
178+/// The id a row shows as selected. A custom hue stays as stored; a preset is resolved, so an id
179+/// that is no longer offered shows the default, as the page renders it on `<html>`.
180+fn shown<'a>(stored: &'a str, resolved: &'static str) -> &'a str {
181+ if hue_of(stored).is_some() {
182+ stored
183+ } else {
184+ resolved
185+ }
186+}
187+
188+/// The stripe of one row. A stored custom hue puts the knob on it; a preset puts the knob on the
189+/// preset's hue, and nothing is selected as custom.
190+fn stripe(stored: &str, preset: Option<u16>) -> Stripe {
191+ match hue_of(stored) {
192+ Some(hue) => Stripe { hue, custom: true },
193+ None => Stripe {
194+ hue: preset.unwrap_or(0),
195+ custom: false,
196+ },
197+ }
198+}
199+
200+/// One theme's colour choices, with the stored background and accent selected.
142201 fn colour_group(lang: Lang, dark: bool, base: &str, accent_id: &str) -> ColourGroup {
143202 let base_codes: Vec<&'static str> = accent::base_ids().collect();
144203 let accent_codes: Vec<&'static str> = accent::ids().collect();
204+ let base_shown = shown(base, accent::resolve_base(base).id);
205+ let accent_shown = shown(accent_id, accent::resolve(accent_id).id);
145206 ColourGroup {
146207 dark,
147- heading: g::t(
148- lang,
149- if dark {
150- K::DarkColours
151- } else {
152- K::LightColours
153- },
154- ),
155208 background: g::t(lang, K::BackgroundLabel),
156- accent: g::t(lang, K::AccentLabel),
157- bases: choices(&base_codes, base, |id| base_name(lang, id)),
158- accents: choices(&accent_codes, accent_id, |id| accent_name(lang, id)),
209+ accent: g::t(lang, K::AccentRowLabel),
210+ custom: g::t(lang, K::CustomLabel),
211+ hue: g::t(lang, K::HueLabel),
212+ bases: with_hues(
213+ choices(&base_codes, base_shown, |id| base_name(lang, id)),
214+ preset_base_hue,
215+ ),
216+ accents: with_hues(
217+ choices(&accent_codes, accent_shown, |id| accent_name(lang, id)),
218+ preset_accent_hue,
219+ ),
220+ base_stripe: stripe(base, preset_base_hue(base_shown)),
221+ accent_stripe: stripe(accent_id, preset_accent_hue(accent_shown)),
159222 }
160223 }
161224
225+/// One theme's panel: a Background row and an Accent row. Each row is its presets as round
226+/// radios, then a hue stripe whose Custom radio carries the `hue-N` value.
162227 #[component]
163-async fn colour_group_fields(group: ColourGroup) -> Result<impl View> {
228+async fn colour_panel(group: ColourGroup) -> Result<impl View> {
164229 let ColourGroup {
165230 dark,
166- heading,
167231 background,
168232 accent,
233+ custom,
234+ hue,
169235 bases,
170236 accents,
237+ base_stripe,
238+ accent_stripe,
171239 } = group;
172240 let base_field = if dark { "base_dark" } else { "base_light" };
173241 let accent_field = if dark { "accent_dark" } else { "accent_light" };
242+ let panel = if dark { "dark" } else { "light" };
243+ let base_custom = format!("hue-{}", base_stripe.hue);
244+ let accent_custom = format!("hue-{}", accent_stripe.hue);
174245 Ok(view! {
175- <fieldset class="card colour-group">
176- <legend>(heading)</legend>
177- <fieldset class="colour-picker">
246+ <div class="colour-panel" data-colour-panel=(panel)>
247+ <fieldset class="colour-picker" data-colour-row="true">
178248 <legend>(background)</legend>
179249 <div class="colour-choices">
180250 for choice in bases.into_iter() {
181251 <label class="colour-choice">
182- <input type="radio" name=(base_field) value=(choice.value) checked=(choice.selected) data-autosubmit="true">
252+ <input type="radio" name=(base_field) value=(choice.value) checked=(choice.selected) data-hue=(choice.hue.as_str()) data-autosubmit="true">
183253 if dark {
184254 <span class="base-swatch" data-base-dark-swatch=(choice.value) aria-hidden="true"></span>
185255 } else {
@@ -189,13 +259,21 @@ async fn colour_group_fields(group: ColourGroup) -> Result<impl View> {
189259 </label>
190260 }
191261 </div>
262+ <div class="hue-row">
263+ <label class="colour-choice">
264+ <input type="radio" name=(base_field) value=(base_custom.as_str()) checked=(base_stripe.custom) data-hue-custom="true" data-autosubmit="true">
265+ <span class="base-swatch hue-swatch" aria-hidden="true"></span>
266+ <span>(custom)</span>
267+ </label>
268+ <input type="range" class="hue-stripe hue-stripe-base" min="0" max="359" value=(base_stripe.hue) aria-label=(hue) data-hue-stripe="true" data-autosubmit="true">
269+ </div>
192270 </fieldset>
193- <fieldset class="colour-picker">
271+ <fieldset class="colour-picker" data-colour-row="true">
194272 <legend>(accent)</legend>
195273 <div class="colour-choices">
196274 for choice in accents.into_iter() {
197275 <label class="colour-choice">
198- <input type="radio" name=(accent_field) value=(choice.value) checked=(choice.selected) data-autosubmit="true">
276+ <input type="radio" name=(accent_field) value=(choice.value) checked=(choice.selected) data-hue=(choice.hue.as_str()) data-autosubmit="true">
199277 if dark {
200278 <span class="accent-swatch" data-accent-dark-swatch=(choice.value) aria-hidden="true"></span>
201279 } else {
@@ -205,8 +283,16 @@ async fn colour_group_fields(group: ColourGroup) -> Result<impl View> {
205283 </label>
206284 }
207285 </div>
286+ <div class="hue-row">
287+ <label class="colour-choice">
288+ <input type="radio" name=(accent_field) value=(accent_custom.as_str()) checked=(accent_stripe.custom) data-hue-custom="true" data-autosubmit="true">
289+ <span class="accent-swatch hue-swatch" aria-hidden="true"></span>
290+ <span>(custom)</span>
291+ </label>
292+ <input type="range" class="hue-stripe" min="0" max="359" value=(accent_stripe.hue) aria-label=(hue) data-hue-stripe="true" data-autosubmit="true">
293+ </div>
208294 </fieldset>
209- </fieldset>
295+ </div>
210296 })
211297 }
212298
@@ -217,6 +303,9 @@ async fn appearance_body(page: AppearancePage) -> Result<impl View> {
217303 csrf,
218304 theme,
219305 language,
306+ colours,
307+ tab,
308+ tabs,
220309 light,
221310 dark,
222311 sidebar,
@@ -258,10 +347,19 @@ async fn appearance_body(page: AppearancePage) -> Result<impl View> {
258347 </div>
259348 </fieldset>
260349 </div>
261- <div class="appearance-columns">
262- colour_group_fields(group: light)
263- colour_group_fields(group: dark)
264- </div>
350+ <fieldset class="card colour-group" data-colour-group="true" data-tab=(tab)>
351+ <legend>(colours)</legend>
352+ <div class="segmented snippet-segmented colour-tabs" role="group">
353+ for choice in tabs.into_iter() {
354+ <label class="segment">
355+ <input type="radio" name="colour_tab" value=(choice.value) checked=(choice.selected) data-colour-tab=(choice.value)>
356+ <span>(choice.label)</span>
357+ </label>
358+ }
359+ </div>
360+ colour_panel(group: light)
361+ colour_panel(group: dark)
362+ </fieldset>
265363 </form>
266364 </div>
267365 })
@@ -318,6 +416,13 @@ pub fn sidebar_stored(stored: &str) -> &'static str {
318416 /// stored id is no longer offered, which is what the page renders on `<html>` too.
319417 fn page_for(viewer: &(User, Session), lang: Lang) -> AppearancePage {
320418 let user = &viewer.0;
419+ // The tab that opens first is the dark colours for a dark theme. The page script moves it to
420+ // the one the viewer sees when the theme follows the system.
421+ let tab = if user.theme == "dark" {
422+ "dark"
423+ } else {
424+ "light"
425+ };
321426 AppearancePage {
322427 lang,
323428 csrf: viewer.1.csrf_token.clone(),
@@ -325,18 +430,11 @@ fn page_for(viewer: &(User, Session), lang: Lang) -> AppearancePage {
325430 language: choices(LANGUAGE_CODES, &user.language, |code| {
326431 language_label(lang, code)
327432 }),
328- light: colour_group(
329- lang,
330- false,
331- accent::resolve_base(&user.base_light).id,
332- accent::resolve(&user.accent).id,
333- ),
334- dark: colour_group(
335- lang,
336- true,
337- accent::resolve_base(&user.base_dark).id,
338- accent::resolve(&user.accent_dark).id,
339- ),
433+ colours: g::t(lang, K::ColoursLabel),
434+ tab,
435+ tabs: choices(TAB_CODES, tab, |code| tab_label(lang, code)),
436+ light: colour_group(lang, false, &user.base_light, &user.accent),
437+ dark: colour_group(lang, true, &user.base_dark, &user.accent_dark),
340438 sidebar: choices(SIDEBAR_CODES, sidebar_stored(&user.sidebar_side), |code| {
341439 sidebar_label(lang, code)
342440 }),
@@ -426,18 +524,40 @@ mod tests {
426524 #[test]
427525 fn the_newer_colours_pass_and_their_wrong_cases_do_not() {
428526 for id in [
429- "teal", "moss", "indigo", "amber", "clay", "berry", "ember", "crimson", "coral",
430- "gold", "lime", "mint", "cyan", "sky", "violet", "fuchsia",
527+ "sage", "spark", "ocean", "plum", "rose", "gold", "hue-0", "hue-359",
431528 ] {
432529 assert_eq!(accent_choice(Lang::En, Some(id), "sage").unwrap(), id);
433530 assert!(accent_choice(Lang::Tr, Some(&id.to_uppercase()), "sage").is_err());
434531 }
435- for id in ["dune", "lilac", "dusk", "blush", "graphite", "pine"] {
532+ for id in ["fern", "spark", "graphite", "dusk", "ink", "hue-120"] {
436533 assert_eq!(base_choice(Lang::Tr, Some(id), "fern").unwrap(), id);
437534 assert!(base_choice(Lang::Tr, Some(&id.to_uppercase()), "fern").is_err());
438535 }
439- assert!(accent_choice(Lang::En, Some("terracotta"), "sage").is_err());
440- assert!(base_choice(Lang::En, Some("slate-blue"), "fern").is_err());
536+ // Removed presets, and hue values that are out of range or not canonical, are refused.
537+ for id in [
538+ "teal",
539+ "moss",
540+ "ember",
541+ "terracotta",
542+ "hue-360",
543+ "hue-07",
544+ "hue-",
545+ "hue-+5",
546+ "teal",
547+ ] {
548+ assert!(accent_choice(Lang::En, Some(id), "sage").is_err(), "{id}");
549+ }
550+ for id in [
551+ "dune",
552+ "lilac",
553+ "blush",
554+ "pine",
555+ "slate-blue",
556+ "hue-360",
557+ "hue-07",
558+ ] {
559+ assert!(base_choice(Lang::En, Some(id), "fern").is_err(), "{id}");
560+ }
441561 }
442562
443563 #[test]
@@ -491,6 +611,40 @@ mod tests {
491611 assert_eq!(group.bases[0].value, crate::accent_palette::BASE_DEFAULT);
492612 }
493613
614+ #[test]
615+ fn a_stored_hue_checks_custom_and_sets_the_knob() {
616+ let group = colour_group(Lang::En, false, "hue-200", "hue-40");
617+ assert!(group.base_stripe.custom);
618+ assert_eq!(group.base_stripe.hue, 200);
619+ assert!(group.accent_stripe.custom);
620+ assert_eq!(group.accent_stripe.hue, 40);
621+ assert!(group.bases.iter().all(|c| !c.selected));
622+ assert!(group.accents.iter().all(|c| !c.selected));
623+ }
624+
625+ #[test]
626+ fn a_stored_preset_checks_the_preset_and_moves_the_knob_to_its_hue() {
627+ let group = colour_group(Lang::En, true, "ink", "plum");
628+ let plum_hue = preset_accent_hue("plum").unwrap();
629+ let ink_hue = preset_base_hue("ink").unwrap();
630+ assert!(!group.accent_stripe.custom);
631+ assert_eq!(group.accent_stripe.hue, plum_hue);
632+ assert!(!group.base_stripe.custom);
633+ assert_eq!(group.base_stripe.hue, ink_hue);
634+ let plum = group.accents.iter().find(|c| c.value == "plum").unwrap();
635+ assert!(plum.selected);
636+ assert_eq!(plum.hue, plum_hue.to_string());
637+ }
638+
639+ #[test]
640+ fn a_stored_hue_is_only_read_in_range() {
641+ assert_eq!(hue_of("hue-359"), Some(359));
642+ assert_eq!(hue_of("hue-360"), None);
643+ assert_eq!(hue_of("hue-"), None);
644+ assert_eq!(hue_of("hue-+5"), None);
645+ assert_eq!(hue_of("sage"), None);
646+ }
647+
494648 #[test]
495649 fn labels_follow_the_page_language() {
496650 assert_eq!(theme_label(Lang::En, "dark"), "Dark");
crates/forge-web/tests/ui_prefs_e2e.rsmodified+21 -18
@@ -309,7 +309,10 @@ async fn the_form_has_no_save_button_and_every_field_autosubmits() {
309309 scraper::Selector::parse("form[data-appearance] input, form[data-appearance] select")
310310 .expect("selector");
311311 for field in doc.select(&fields) {
312- if field.value().attr("type") == Some("hidden") {
312+ // The hidden fields never save, and the colour tab switch only shows a panel.
313+ if field.value().attr("type") == Some("hidden")
314+ || field.value().attr("data-colour-tab").is_some()
315+ {
313316 continue;
314317 }
315318 assert!(
@@ -342,7 +345,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
342345 ("accent_light", "ocean"),
343346 ("accent_dark", "plum"),
344347 ("base_light", "ink"),
345- ("base_dark", "mist"),
348+ ("base_dark", "dusk"),
346349 ],
347350 Some(&session),
348351 )
@@ -350,7 +353,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
350353 assert_eq!(saved.status, 303, "save: {}", saved.body);
351354
352355 let page = env.http.get("/settings/appearance", Some(&session)).await;
353- let chosen = ["ocean", "plum", "ink", "mist"].map(str::to_owned);
356+ let chosen = ["ocean", "plum", "ink", "dusk"].map(str::to_owned);
354357 assert_eq!(checked_colours(&page.body), chosen.clone().map(Some));
355358 assert_eq!(html_colours(&page.body), chosen.map(Some));
356359
@@ -363,14 +366,14 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
363366 ("csrf", &csrf),
364367 ("theme", "light"),
365368 ("language", "en"),
366- ("base_dark", "sand"),
369+ ("base_dark", "spark"),
367370 ],
368371 Some(&session),
369372 )
370373 .await;
371374 assert_eq!(saved.status, 303, "save: {}", saved.body);
372375 let page = env.http.get("/settings/appearance", Some(&session)).await;
373- let chosen = ["ocean", "plum", "ink", "sand"].map(str::to_owned);
376+ let chosen = ["ocean", "plum", "ink", "spark"].map(str::to_owned);
374377 assert_eq!(checked_colours(&page.body), chosen.clone().map(Some));
375378 assert_eq!(html_colours(&page.body), chosen.map(Some));
376379
@@ -396,7 +399,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
396399 assert_ne!(res.status, 303, "bad {field} {bad:?} must not save");
397400 }
398401 let page = env.http.get("/settings/appearance", Some(&session)).await;
399- let chosen = ["ocean", "plum", "ink", "sand"].map(str::to_owned);
402+ let chosen = ["ocean", "plum", "ink", "spark"].map(str::to_owned);
400403 assert_eq!(checked_colours(&page.body), chosen.clone().map(Some));
401404 assert_eq!(html_colours(&page.body), chosen.map(Some));
402405
@@ -409,9 +412,9 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
409412 ("csrf", &csrf),
410413 ("theme", "light"),
411414 ("language", "tr"),
412- ("accent_light", "teal"),
413- ("accent_dark", "berry"),
414- ("base_light", "dune"),
415+ ("accent_light", "hue-200"),
416+ ("accent_dark", "gold"),
417+ ("base_light", "spark"),
415418 ("base_dark", "dusk"),
416419 ],
417420 Some(&session),
@@ -419,7 +422,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
419422 .await;
420423 assert_eq!(saved.status, 303, "save: {}", saved.body);
421424 let page = env.http.get("/settings/appearance", Some(&session)).await;
422- let chosen = ["teal", "berry", "dune", "dusk"].map(str::to_owned);
425+ let chosen = ["hue-200", "gold", "spark", "dusk"].map(str::to_owned);
423426 assert_eq!(checked_colours(&page.body), chosen.clone().map(Some));
424427 assert_eq!(html_colours(&page.body), chosen.map(Some));
425428
@@ -432,9 +435,9 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
432435 ("csrf", &csrf),
433436 ("theme", "light"),
434437 ("language", "en"),
435- ("accent_light", "crimson"),
436- ("accent_dark", "fuchsia"),
437- ("base_light", "pine"),
438+ ("accent_light", "sage"),
439+ ("accent_dark", "ocean"),
440+ ("base_light", "ink"),
438441 ("base_dark", "graphite"),
439442 ],
440443 Some(&session),
@@ -442,7 +445,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() {
442445 .await;
443446 assert_eq!(saved.status, 303, "save: {}", saved.body);
444447 let page = env.http.get("/settings/appearance", Some(&session)).await;
445- let chosen = ["crimson", "fuchsia", "pine", "graphite"].map(str::to_owned);
448+ let chosen = ["sage", "ocean", "ink", "graphite"].map(str::to_owned);
446449 assert_eq!(checked_colours(&page.body), chosen.clone().map(Some));
447450 assert_eq!(html_colours(&page.body), chosen.map(Some));
448451 }
@@ -463,16 +466,16 @@ async fn a_save_without_colour_fields_keeps_the_stored_colours() {
463466 ("theme", "light"),
464467 ("language", "en"),
465468 ("accent_light", "rose"),
466- ("accent_dark", "slate"),
467- ("base_light", "sand"),
468- ("base_dark", "stone"),
469+ ("accent_dark", "gold"),
470+ ("base_light", "fern"),
471+ ("base_dark", "spark"),
469472 ],
470473 Some(&session),
471474 )
472475 .await;
473476 assert_eq!(saved.status, 303, "save: {}", saved.body);
474477 let before = env.http.get("/settings/appearance", Some(&session)).await;
475- let stored = ["rose", "slate", "sand", "stone"].map(str::to_owned);
478+ let stored = ["rose", "gold", "fern", "spark"].map(str::to_owned);
476479 assert_eq!(html_colours(&before.body), stored.clone().map(Some));
477480
478481 let saved = env
style/main.scssmodified+115 -30
@@ -2094,8 +2094,8 @@ code {
20942094
20952095 // Colour pickers (appearance settings). Each choice is a native radio, so the keyboard and screen
20962096 // readers work as usual; the swatch beside it shows the colour. The swatch colours come from the
2097-// generated _accents.scss, so nothing here names a colour. The two themes sit side by side, and
2098-// stack on narrow screens, so the whole page fits a laptop window without a long scroll.
2097+// generated _accents.scss, so nothing here names a colour. One theme's panel shows at a time
2098+// (Light or Dark tabs), so the page fits a laptop window without a long scroll.
20992099 .appearance {
21002100 display: grid;
21012101 gap: 16px;
@@ -2178,13 +2178,114 @@ code {
21782178 }
21792179 }
21802180
2181-// One theme card per row, so each card is wide enough for 11 labelled colour columns. The light
2182-// card and the dark card are the same shape, so they still line up.
2183-.appearance-columns {
2181+// One card for both themes, with Light and Dark tabs. nav.js sets data-tab on the card; the panel
2182+// that is not shown is hidden, but it stays in the form, so a save still posts all four colours.
2183+.colour-group[data-tab="light"] > .colour-panel[data-colour-panel="dark"],
2184+.colour-group[data-tab="dark"] > .colour-panel[data-colour-panel="light"] {
2185+ display: none;
2186+}
2187+
2188+.colour-panel {
21842189 display: grid;
2185- grid-template-columns: minmax(0, 1fr);
2186- gap: 16px;
2190+ gap: 18px;
2191+ min-width: 0;
2192+}
2193+
2194+// The hue stripe: a range input whose track is a rainbow, with a round knob. The background stripe
2195+// is a pale, low-saturation band so it reads as a tint. nav.js sets --hue on the row: the knob and the
2196+// Custom swatch take their colour from it.
2197+[data-colour-row] {
2198+ --hue: 0;
2199+}
2200+
2201+.hue-row {
2202+ display: flex;
2203+ align-items: center;
2204+ gap: 12px;
21872205 min-width: 0;
2206+
2207+ // The Custom choice takes the width of one preset, so the stripe lines up with the presets' row.
2208+ > .colour-choice {
2209+ flex: none;
2210+ width: 4.5rem;
2211+ }
2212+}
2213+
2214+.hue-stripe {
2215+ --track: linear-gradient(to right, hsl(0 85% 55%), hsl(60 90% 52%), hsl(120 65% 45%), hsl(180 70% 42%), hsl(240 80% 58%), hsl(300 75% 55%), hsl(360 85% 55%));
2216+ -webkit-appearance: none;
2217+ appearance: none;
2218+ flex: 1 1 auto;
2219+ width: 100%;
2220+ min-width: 0;
2221+ height: 22px;
2222+ margin: 0;
2223+ padding: 0;
2224+ border: 0;
2225+ background: transparent;
2226+ cursor: pointer;
2227+
2228+ &.hue-stripe-base {
2229+ --track: linear-gradient(to right, hsl(0 45% 84%), hsl(60 45% 84%), hsl(120 35% 81%), hsl(180 35% 81%), hsl(240 40% 85%), hsl(300 35% 83%), hsl(360 45% 84%));
2230+ }
2231+
2232+ &::-webkit-slider-runnable-track {
2233+ height: 14px;
2234+ border-radius: var(--radius-full);
2235+ background: var(--track);
2236+ }
2237+
2238+ &::-moz-range-track {
2239+ height: 14px;
2240+ border-radius: var(--radius-full);
2241+ background: var(--track);
2242+ }
2243+
2244+ &::-webkit-slider-thumb {
2245+ -webkit-appearance: none;
2246+ appearance: none;
2247+ box-sizing: border-box;
2248+ width: 22px;
2249+ height: 22px;
2250+ margin-top: -4px;
2251+ border-radius: 50%;
2252+ border: 3px solid var(--surface);
2253+ background: hsl(var(--hue) 80% 50%);
2254+ box-shadow: 0 0 0 1px var(--border-strong);
2255+ }
2256+
2257+ &::-moz-range-thumb {
2258+ box-sizing: border-box;
2259+ width: 22px;
2260+ height: 22px;
2261+ border-radius: 50%;
2262+ border: 3px solid var(--surface);
2263+ background: hsl(var(--hue) 80% 50%);
2264+ box-shadow: 0 0 0 1px var(--border-strong);
2265+ }
2266+
2267+ &.hue-stripe-base::-webkit-slider-thumb {
2268+ background: hsl(var(--hue) 40% 62%);
2269+ }
2270+
2271+ &.hue-stripe-base::-moz-range-thumb {
2272+ background: hsl(var(--hue) 40% 62%);
2273+ }
2274+
2275+ &:focus-visible {
2276+ outline: 2px solid var(--focus);
2277+ outline-offset: 3px;
2278+ }
2279+}
2280+
2281+// The Custom swatch shows the hue the stripe is on. The accent one is saturated, the background
2282+// one pale, like the presets beside it.
2283+.hue-swatch {
2284+ background: hsl(var(--hue) 75% 50%);
2285+}
2286+
2287+.base-swatch.hue-swatch {
2288+ background: hsl(var(--hue) 40% 82%);
21882289 }
21892290
21902291 .colour-group {
@@ -2222,30 +2323,13 @@ code {
22222323 }
22232324 }
22242325
2225-// The picker is a grid with equal columns, so rows are exact and the Background and Accent rows
2226-// share column lines. The card is the container: wide cards get 11 columns (the 11 backgrounds
2227-// are one row, the 22 accents two), medium cards 6, and phone cards 4.
2228-.colour-group {
2229- container-type: inline-size;
2230-}
2231-
2326+// The presets are a wrapping row of fixed-width columns, so a phone wraps them and a wide card
2327+// keeps them in one line.
22322328 .colour-choices {
2233- display: grid;
2234- grid-template-columns: repeat(4, minmax(0, 1fr));
2235- gap: 12px 4px;
2236-}
2237-
2238-@container (min-width: 30rem) {
2239- .colour-choices {
2240- grid-template-columns: repeat(6, minmax(0, 1fr));
2241- }
2242-}
2243-
2244-@container (min-width: 34rem) {
2245- .colour-choices {
2246- grid-template-columns: repeat(11, minmax(0, 1fr));
2247- gap: 12px 2px;
2248- }
2329+ display: flex;
2330+ flex-wrap: wrap;
2331+ gap: 12px 8px;
2332+ min-width: 0;
22492333 }
22502334
22512335 .colour-choice {
@@ -2253,6 +2337,7 @@ code {
22532337 flex-direction: column;
22542338 align-items: center;
22552339 gap: 4px;
2340+ width: 4.5rem;
22562341 min-width: 0;
22572342 position: relative;
22582343 cursor: pointer;