From 1a7d33d9e0722e233a3a31c7c19fe58fe1c16c93 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Mon, 6 Jul 2026 10:19:59 -0600 Subject: [PATCH] feat(ui): custom theme editor panel + controller + i18n Wire the custom-theme editor end to end. In AppearanceSettings, when the "Custom" theme is selected, show a "Start from current theme" button, a "Dark theme" polarity toggle, and a grouped token grid (SURFACES / TEXT / ACCENT / STATUS / OTHER) of 11 base tokens. Each token is a swatch that toggles an inline ColorPicker; edits route through AppearanceState.custom-set-token (drag) and custom-set-token-hex (hex commit). The crate::custom_theme controller reads the base straight from the editor swatch properties (the in-memory source of truth, no per-drag disk reads), sets the touched field, re-derives via qbz_theme::theme_from_base, pushes the palette live and persists. seed_from_current snapshots the live Theme global into the base; toggle_dark flips polarity; set_token_hex parses in Rust (Slint cannot). main.rs registers the four callbacks. Also harden the ColorPicker reseed guard: skip decompose when the fed-back value equals our own emitted color, so a drag ending at v=0/s=0 no longer resets the hue. New strings added to all 7 gettext catalogs (es Mexican/ neutral); msgfmt --check clean; full .slint tree validated via the slintcheck stub. cc --- crates/Cargo.lock | 1 + .../translations/de/LC_MESSAGES/qbz-ui.po | 57 +++++++ .../translations/en/LC_MESSAGES/qbz-ui.po | 63 +++++++ .../translations/es/LC_MESSAGES/qbz-ui.po | 57 +++++++ .../translations/fr/LC_MESSAGES/qbz-ui.po | 57 +++++++ .../translations/ja/LC_MESSAGES/qbz-ui.po | 57 +++++++ .../translations/pt/LC_MESSAGES/qbz-ui.po | 57 +++++++ .../translations/ru/LC_MESSAGES/qbz-ui.po | 57 +++++++ crates/qbz-ui/ui/primitives/ColorPicker.slint | 10 +- .../ui/settings/AppearanceSettings.slint | 156 ++++++++++++++++++ crates/qbz-ui/ui/state.slint | 2 + crates/qbz/src/custom_theme.rs | 154 ++++++++++++++++- crates/qbz/src/main.rs | 28 ++++ 13 files changed, 749 insertions(+), 7 deletions(-) diff --git a/crates/Cargo.lock b/crates/Cargo.lock index 8ac887ebb..63ef137e4 100644 --- a/crates/Cargo.lock +++ b/crates/Cargo.lock @@ -5931,6 +5931,7 @@ version = "2.0.0" dependencies = [ "image", "serde", + "serde_json", ] [[package]] diff --git a/crates/qbz-ui/translations/de/LC_MESSAGES/qbz-ui.po b/crates/qbz-ui/translations/de/LC_MESSAGES/qbz-ui.po index 34a09b69b..9b479fc1f 100644 --- a/crates/qbz-ui/translations/de/LC_MESSAGES/qbz-ui.po +++ b/crates/qbz-ui/translations/de/LC_MESSAGES/qbz-ui.po @@ -4394,3 +4394,60 @@ msgstr "Zufällige Playlist abspielen" msgid "Open a random label" msgstr "Zufälliges Label öffnen" + +msgid "Start from current theme" +msgstr "Vom aktuellen Theme ausgehen" + +msgid "Copy the colors of the currently applied theme into the editor as a starting point." +msgstr "Kopiert die Farben des aktuell angewendeten Themes als Ausgangspunkt in den Editor." + +msgid "Use current colors" +msgstr "Aktuelle Farben verwenden" + +msgid "Dark theme" +msgstr "Dunkles Theme" + +msgid "Set the overall light or dark polarity. Affects derived shades, borders and overlays." +msgstr "Legt die generelle helle oder dunkle Polarität fest. Betrifft abgeleitete Schattierungen, Ränder und Overlays." + +msgid "SURFACES" +msgstr "OBERFLÄCHEN" + +msgid "Background" +msgstr "Hintergrund" + +msgid "Surface" +msgstr "Oberfläche" + +msgid "Elevated" +msgstr "Erhöht" + +msgid "TEXT" +msgstr "TEXT" + +msgid "Text" +msgstr "Text" + +msgid "Secondary text" +msgstr "Sekundärer Text" + +msgid "ACCENT" +msgstr "AKZENT" + +msgid "Accent" +msgstr "Akzent" + +msgid "Danger" +msgstr "Gefahr" + +msgid "Warning" +msgstr "Warnung" + +msgid "Success" +msgstr "Erfolg" + +msgid "OTHER" +msgstr "SONSTIGES" + +msgid "Border" +msgstr "Rand" diff --git a/crates/qbz-ui/translations/en/LC_MESSAGES/qbz-ui.po b/crates/qbz-ui/translations/en/LC_MESSAGES/qbz-ui.po index eff848c01..48ba36084 100644 --- a/crates/qbz-ui/translations/en/LC_MESSAGES/qbz-ui.po +++ b/crates/qbz-ui/translations/en/LC_MESSAGES/qbz-ui.po @@ -160,3 +160,66 @@ msgstr "Play a random playlist" msgid "Open a random label" msgstr "Open a random label" + +msgid "Start from current theme" +msgstr "Start from current theme" + +msgid "Copy the colors of the currently applied theme into the editor as a starting point." +msgstr "Copy the colors of the currently applied theme into the editor as a starting point." + +msgid "Use current colors" +msgstr "Use current colors" + +msgid "Dark theme" +msgstr "Dark theme" + +msgid "Set the overall light or dark polarity. Affects derived shades, borders and overlays." +msgstr "Set the overall light or dark polarity. Affects derived shades, borders and overlays." + +msgid "SURFACES" +msgstr "SURFACES" + +msgid "Background" +msgstr "Background" + +msgid "Surface" +msgstr "Surface" + +msgid "Elevated" +msgstr "Elevated" + +msgid "TEXT" +msgstr "TEXT" + +msgid "Text" +msgstr "Text" + +msgid "Secondary text" +msgstr "Secondary text" + +msgid "ACCENT" +msgstr "ACCENT" + +msgid "Accent" +msgstr "Accent" + +msgid "STATUS" +msgstr "STATUS" + +msgid "Danger" +msgstr "Danger" + +msgid "Warning" +msgstr "Warning" + +msgid "Success" +msgstr "Success" + +msgid "OTHER" +msgstr "OTHER" + +msgid "Border" +msgstr "Border" + +msgid "Favorite" +msgstr "Favorite" diff --git a/crates/qbz-ui/translations/es/LC_MESSAGES/qbz-ui.po b/crates/qbz-ui/translations/es/LC_MESSAGES/qbz-ui.po index 7911ffa58..afb52f5fe 100644 --- a/crates/qbz-ui/translations/es/LC_MESSAGES/qbz-ui.po +++ b/crates/qbz-ui/translations/es/LC_MESSAGES/qbz-ui.po @@ -4424,3 +4424,60 @@ msgstr "Reproducir una playlist al azar" msgid "Open a random label" msgstr "Abrir un sello al azar" + +msgid "Start from current theme" +msgstr "Partir del tema actual" + +msgid "Copy the colors of the currently applied theme into the editor as a starting point." +msgstr "Copia los colores del tema aplicado actualmente al editor como punto de partida." + +msgid "Use current colors" +msgstr "Usar colores actuales" + +msgid "Dark theme" +msgstr "Tema oscuro" + +msgid "Set the overall light or dark polarity. Affects derived shades, borders and overlays." +msgstr "Define la polaridad general clara u oscura. Afecta los tonos derivados, los bordes y las superposiciones." + +msgid "SURFACES" +msgstr "SUPERFICIES" + +msgid "Background" +msgstr "Fondo" + +msgid "Surface" +msgstr "Superficie" + +msgid "Elevated" +msgstr "Elevado" + +msgid "TEXT" +msgstr "TEXTO" + +msgid "Text" +msgstr "Texto" + +msgid "Secondary text" +msgstr "Texto secundario" + +msgid "ACCENT" +msgstr "ACENTO" + +msgid "Accent" +msgstr "Acento" + +msgid "Danger" +msgstr "Peligro" + +msgid "Warning" +msgstr "Advertencia" + +msgid "Success" +msgstr "Éxito" + +msgid "OTHER" +msgstr "OTROS" + +msgid "Border" +msgstr "Borde" diff --git a/crates/qbz-ui/translations/fr/LC_MESSAGES/qbz-ui.po b/crates/qbz-ui/translations/fr/LC_MESSAGES/qbz-ui.po index ac2e9b8a0..fcb59d409 100644 --- a/crates/qbz-ui/translations/fr/LC_MESSAGES/qbz-ui.po +++ b/crates/qbz-ui/translations/fr/LC_MESSAGES/qbz-ui.po @@ -4394,3 +4394,60 @@ msgstr "Lire une playlist au hasard" msgid "Open a random label" msgstr "Ouvrir un label au hasard" + +msgid "Start from current theme" +msgstr "Partir du thème actuel" + +msgid "Copy the colors of the currently applied theme into the editor as a starting point." +msgstr "Copie les couleurs du thème actuellement appliqué dans l'éditeur comme point de départ." + +msgid "Use current colors" +msgstr "Utiliser les couleurs actuelles" + +msgid "Dark theme" +msgstr "Thème sombre" + +msgid "Set the overall light or dark polarity. Affects derived shades, borders and overlays." +msgstr "Définit la polarité globale claire ou sombre. Affecte les nuances dérivées, les bordures et les superpositions." + +msgid "SURFACES" +msgstr "SURFACES" + +msgid "Background" +msgstr "Arrière-plan" + +msgid "Surface" +msgstr "Surface" + +msgid "Elevated" +msgstr "Surélevé" + +msgid "TEXT" +msgstr "TEXTE" + +msgid "Text" +msgstr "Texte" + +msgid "Secondary text" +msgstr "Texte secondaire" + +msgid "ACCENT" +msgstr "ACCENT" + +msgid "Accent" +msgstr "Accent" + +msgid "Danger" +msgstr "Danger" + +msgid "Warning" +msgstr "Avertissement" + +msgid "Success" +msgstr "Succès" + +msgid "OTHER" +msgstr "AUTRE" + +msgid "Border" +msgstr "Bordure" diff --git a/crates/qbz-ui/translations/ja/LC_MESSAGES/qbz-ui.po b/crates/qbz-ui/translations/ja/LC_MESSAGES/qbz-ui.po index 18b430ef3..7ce5750cc 100644 --- a/crates/qbz-ui/translations/ja/LC_MESSAGES/qbz-ui.po +++ b/crates/qbz-ui/translations/ja/LC_MESSAGES/qbz-ui.po @@ -4383,3 +4383,60 @@ msgstr "ランダムなプレイリストを再生" msgid "Open a random label" msgstr "ランダムなレーベルを開く" + +msgid "Start from current theme" +msgstr "現在のテーマから開始" + +msgid "Copy the colors of the currently applied theme into the editor as a starting point." +msgstr "現在適用中のテーマの色を出発点としてエディターにコピーします。" + +msgid "Use current colors" +msgstr "現在の色を使用" + +msgid "Dark theme" +msgstr "ダークテーマ" + +msgid "Set the overall light or dark polarity. Affects derived shades, borders and overlays." +msgstr "全体の明暗の極性を設定します。派生する陰影、境界線、オーバーレイに影響します。" + +msgid "SURFACES" +msgstr "サーフェス" + +msgid "Background" +msgstr "背景" + +msgid "Surface" +msgstr "サーフェス" + +msgid "Elevated" +msgstr "浮き出し" + +msgid "TEXT" +msgstr "テキスト" + +msgid "Text" +msgstr "テキスト" + +msgid "Secondary text" +msgstr "セカンダリテキスト" + +msgid "ACCENT" +msgstr "アクセント" + +msgid "Accent" +msgstr "アクセント" + +msgid "Danger" +msgstr "危険" + +msgid "Warning" +msgstr "警告" + +msgid "Success" +msgstr "成功" + +msgid "OTHER" +msgstr "その他" + +msgid "Border" +msgstr "境界線" diff --git a/crates/qbz-ui/translations/pt/LC_MESSAGES/qbz-ui.po b/crates/qbz-ui/translations/pt/LC_MESSAGES/qbz-ui.po index 8b2ebb6fc..03f092cd0 100644 --- a/crates/qbz-ui/translations/pt/LC_MESSAGES/qbz-ui.po +++ b/crates/qbz-ui/translations/pt/LC_MESSAGES/qbz-ui.po @@ -4394,3 +4394,60 @@ msgstr "Reproduzir uma playlist aleatória" msgid "Open a random label" msgstr "Abrir um selo aleatório" + +msgid "Start from current theme" +msgstr "Partir do tema atual" + +msgid "Copy the colors of the currently applied theme into the editor as a starting point." +msgstr "Copia as cores do tema aplicado atualmente para o editor como ponto de partida." + +msgid "Use current colors" +msgstr "Usar cores atuais" + +msgid "Dark theme" +msgstr "Tema escuro" + +msgid "Set the overall light or dark polarity. Affects derived shades, borders and overlays." +msgstr "Define a polaridade geral clara ou escura. Afeta os tons derivados, as bordas e as sobreposições." + +msgid "SURFACES" +msgstr "SUPERFÍCIES" + +msgid "Background" +msgstr "Fundo" + +msgid "Surface" +msgstr "Superfície" + +msgid "Elevated" +msgstr "Elevado" + +msgid "TEXT" +msgstr "TEXTO" + +msgid "Text" +msgstr "Texto" + +msgid "Secondary text" +msgstr "Texto secundário" + +msgid "ACCENT" +msgstr "DESTAQUE" + +msgid "Accent" +msgstr "Destaque" + +msgid "Danger" +msgstr "Perigo" + +msgid "Warning" +msgstr "Aviso" + +msgid "Success" +msgstr "Sucesso" + +msgid "OTHER" +msgstr "OUTROS" + +msgid "Border" +msgstr "Borda" diff --git a/crates/qbz-ui/translations/ru/LC_MESSAGES/qbz-ui.po b/crates/qbz-ui/translations/ru/LC_MESSAGES/qbz-ui.po index f988452dc..05deb32f7 100644 --- a/crates/qbz-ui/translations/ru/LC_MESSAGES/qbz-ui.po +++ b/crates/qbz-ui/translations/ru/LC_MESSAGES/qbz-ui.po @@ -4407,3 +4407,60 @@ msgstr "Воспроизвести случайный плейлист" msgid "Open a random label" msgstr "Открыть случайный лейбл" + +msgid "Start from current theme" +msgstr "Начать с текущей темы" + +msgid "Copy the colors of the currently applied theme into the editor as a starting point." +msgstr "Копирует цвета текущей темы в редактор как отправную точку." + +msgid "Use current colors" +msgstr "Использовать текущие цвета" + +msgid "Dark theme" +msgstr "Тёмная тема" + +msgid "Set the overall light or dark polarity. Affects derived shades, borders and overlays." +msgstr "Задаёт общую светлую или тёмную полярность. Влияет на производные оттенки, границы и наложения." + +msgid "SURFACES" +msgstr "ПОВЕРХНОСТИ" + +msgid "Background" +msgstr "Фон" + +msgid "Surface" +msgstr "Поверхность" + +msgid "Elevated" +msgstr "Приподнятый" + +msgid "TEXT" +msgstr "ТЕКСТ" + +msgid "Text" +msgstr "Текст" + +msgid "Secondary text" +msgstr "Вторичный текст" + +msgid "ACCENT" +msgstr "АКЦЕНТ" + +msgid "Accent" +msgstr "Акцент" + +msgid "Danger" +msgstr "Опасность" + +msgid "Warning" +msgstr "Предупреждение" + +msgid "Success" +msgstr "Успех" + +msgid "OTHER" +msgstr "ДРУГОЕ" + +msgid "Border" +msgstr "Граница" diff --git a/crates/qbz-ui/ui/primitives/ColorPicker.slint b/crates/qbz-ui/ui/primitives/ColorPicker.slint index f0f0b5e5c..c4ca7ab2f 100644 --- a/crates/qbz-ui/ui/primitives/ColorPicker.slint +++ b/crates/qbz-ui/ui/primitives/ColorPicker.slint @@ -71,10 +71,14 @@ export component ColorPicker inherits Rectangle { init => { root.decompose(); } - // Reseed on EXTERNAL changes only (a hex commit fed back, a token re-seed); - // our own drag holds `dragging` so the crosshair does not jump. + // Reseed on EXTERNAL changes only. Two guards: `dragging` skips mid-drag + // feedback, and `value != current` skips our OWN round-tripped color (the + // parent feeds the emitted color back into `value`) — otherwise the final + // post-drag feedback would re-decompose and reset the hue when v or s is 0. + // A genuine external change (a hex commit, a re-seed) differs from `current` + // and does decompose. changed value => { - if (!root.dragging) { + if (!root.dragging && root.value != root.current) { root.decompose(); } } diff --git a/crates/qbz-ui/ui/settings/AppearanceSettings.slint b/crates/qbz-ui/ui/settings/AppearanceSettings.slint index 851c6a2a8..cd3db15ee 100644 --- a/crates/qbz-ui/ui/settings/AppearanceSettings.slint +++ b/crates/qbz-ui/ui/settings/AppearanceSettings.slint @@ -18,6 +18,7 @@ import { AppearanceState, SidebarState, ShellState, ToastState, MyQbzBrandingSta import { QbzToggle } from "../primitives/QbzToggle.slint"; import { QbzSelect } from "../primitives/QbzSelect.slint"; import { QbzIcon } from "../primitives/QbzIcon.slint"; +import { ColorPicker } from "../primitives/ColorPicker.slint"; import { SettingRow } from "SettingRow.slint"; import { LineEdit } from "std-widgets.slint"; @@ -28,6 +29,66 @@ component GroupHeader inherits Text { font-weight: Typography.semibold; } +// A small muted sub-group label inside the custom-theme editor grid. +component TokenGroupLabel inherits Text { + color: Theme.text-muted; + font-size: 10px; + letter-spacing: 1px; + font-weight: Typography.semibold; +} + +// One editable base-token row in the custom-theme editor: a color swatch + label +// that toggles an inline ColorPicker beneath it. The swatch color is bound from +// the parent (an AppearanceState.custom-* property); edits route back to Rust +// through AppearanceState.custom-set-token / custom-set-token-hex. +component CustomTokenRow inherits VerticalLayout { + in property token-key; + in property label; + in property swatch-color; + spacing: 8px; + + property open: AppearanceState.custom-open-token == root.token-key; + + HorizontalLayout { + spacing: 12px; + Rectangle { + width: 44px; + height: 30px; + border-radius: Radius.sm; + border-width: root.open ? 2px : 1px; + border-color: root.open ? Theme.accent : Theme.border-subtle; + background: root.swatch-color; + TouchArea { + mouse-cursor: pointer; + clicked => { + AppearanceState.custom-open-token = + AppearanceState.custom-open-token == root.token-key ? "" : root.token-key; + } + } + } + VerticalLayout { + alignment: center; + horizontal-stretch: 1; + Text { + text: root.label; + color: Theme.text-secondary; + font-size: Typography.body; + vertical-alignment: center; + } + } + } + + if root.open: ColorPicker { + value: root.swatch-color; + changed(c) => { + AppearanceState.custom-set-token(root.token-key, c); + } + hex-committed(s) => { + AppearanceState.custom-set-token-hex(root.token-key, s); + } + } +} + component Divider inherits Rectangle { height: 1px; background: Theme.border-subtle; @@ -239,6 +300,101 @@ export component AppearanceSettings inherits VerticalLayout { } } + // Custom-theme editor — only when the appended "Custom" theme is selected. + // "Start from current theme" re-seeds the base from the live palette; the + // "Dark theme" toggle flips polarity; the grouped grid edits each base token + // (the rest of the palette is derived live in Rust on every change). + if AppearanceState.theme-is-custom: SettingRow { + label: @tr("Start from current theme"); + description: @tr("Copy the colors of the currently applied theme into the editor as a starting point."); + SecondaryButton { + label: @tr("Use current colors"); + clicked => { + AppearanceState.custom-seed-from-current(); + } + } + } + if AppearanceState.theme-is-custom: SettingRow { + label: @tr("Dark theme"); + description: @tr("Set the overall light or dark polarity. Affects derived shades, borders and overlays."); + QbzToggle { + checked: AppearanceState.custom-is-dark; + toggled(v) => { + AppearanceState.custom-toggle-dark(v); + } + } + } + if AppearanceState.theme-is-custom: VerticalLayout { + padding-top: 8px; + padding-bottom: 8px; + spacing: 12px; + + TokenGroupLabel { text: @tr("SURFACES"); } + CustomTokenRow { + token-key: "surface-main"; + label: @tr("Background"); + swatch-color: AppearanceState.custom-surface-main; + } + CustomTokenRow { + token-key: "surface-card"; + label: @tr("Surface"); + swatch-color: AppearanceState.custom-surface-card; + } + CustomTokenRow { + token-key: "surface-elevated"; + label: @tr("Elevated"); + swatch-color: AppearanceState.custom-surface-elevated; + } + + TokenGroupLabel { text: @tr("TEXT"); } + CustomTokenRow { + token-key: "text-primary"; + label: @tr("Text"); + swatch-color: AppearanceState.custom-text-primary; + } + CustomTokenRow { + token-key: "text-secondary"; + label: @tr("Secondary text"); + swatch-color: AppearanceState.custom-text-secondary; + } + + TokenGroupLabel { text: @tr("ACCENT"); } + CustomTokenRow { + token-key: "accent"; + label: @tr("Accent"); + swatch-color: AppearanceState.custom-accent; + } + + TokenGroupLabel { text: @tr("STATUS"); } + CustomTokenRow { + token-key: "danger"; + label: @tr("Danger"); + swatch-color: AppearanceState.custom-danger; + } + CustomTokenRow { + token-key: "warning"; + label: @tr("Warning"); + swatch-color: AppearanceState.custom-warning; + } + CustomTokenRow { + token-key: "success"; + label: @tr("Success"); + swatch-color: AppearanceState.custom-success; + } + + TokenGroupLabel { text: @tr("OTHER"); } + CustomTokenRow { + token-key: "border"; + label: @tr("Border"); + swatch-color: AppearanceState.custom-border; + } + CustomTokenRow { + token-key: "favorite"; + label: @tr("Favorite"); + swatch-color: AppearanceState.custom-favorite; + } + } + Rectangle { height: 12px; } Divider { } Rectangle { height: 12px; } diff --git a/crates/qbz-ui/ui/state.slint b/crates/qbz-ui/ui/state.slint index e2a3ad288..5689b64fa 100644 --- a/crates/qbz-ui/ui/state.slint +++ b/crates/qbz-ui/ui/state.slint @@ -2932,6 +2932,8 @@ export global AppearanceState { in-out property custom-open-token: ""; // A single token's color changed (drag-live). key = token id, color = value. callback custom-set-token(string /* key */, color /* value */); + // A single token committed from the HEX field. key = token id, hex = #rrggbb. + callback custom-set-token-hex(string /* key */, string /* hex */); // Re-seed the base from the currently active theme ("Start from current"). callback custom-seed-from-current(); // Flip the custom theme polarity (dark/light) and re-derive. diff --git a/crates/qbz/src/custom_theme.rs b/crates/qbz/src/custom_theme.rs index 4d3883207..e75eb1239 100644 --- a/crates/qbz/src/custom_theme.rs +++ b/crates/qbz/src/custom_theme.rs @@ -13,6 +13,7 @@ use crate::AppWindow; use crate::AppearanceState; +use crate::Theme as SlintTheme; use qbz_theme::{CustomThemeBase, Rgba}; use slint::{Color, ComponentHandle}; use std::path::PathBuf; @@ -23,6 +24,81 @@ fn to_color(c: Rgba) -> Color { Color::from_argb_u8(c.a, c.r, c.g, c.b) } +/// Convert a Slint `Color` back to a registry `Rgba`. +fn rgba_of(c: Color) -> Rgba { + Rgba::rgba(c.red(), c.green(), c.blue(), c.alpha()) +} + +/// Read the current editable base straight from the editor swatch properties on +/// `AppearanceState` — the in-memory source of truth while the editor is open, so +/// per-drag edits never hit the disk to reconstruct the base. +fn base_from_state(window: &AppWindow) -> CustomThemeBase { + let st = window.global::(); + CustomThemeBase { + is_dark: st.get_custom_is_dark(), + surface_main: rgba_of(st.get_custom_surface_main()).to_hex(), + surface_card: rgba_of(st.get_custom_surface_card()).to_hex(), + surface_elevated: rgba_of(st.get_custom_surface_elevated()).to_hex(), + text_primary: rgba_of(st.get_custom_text_primary()).to_hex(), + text_secondary: rgba_of(st.get_custom_text_secondary()).to_hex(), + accent: rgba_of(st.get_custom_accent()).to_hex(), + danger: rgba_of(st.get_custom_danger()).to_hex(), + warning: rgba_of(st.get_custom_warning()).to_hex(), + success: rgba_of(st.get_custom_success()).to_hex(), + border: rgba_of(st.get_custom_border()).to_hex(), + favorite: rgba_of(st.get_custom_favorite()).to_hex(), + } +} + +/// Assign one base-token field by its stable key (the `token-key` strings the +/// Slint editor rows use). Unknown keys are ignored. +fn set_field(base: &mut CustomThemeBase, key: &str, hex: String) { + match key { + "surface-main" => base.surface_main = hex, + "surface-card" => base.surface_card = hex, + "surface-elevated" => base.surface_elevated = hex, + "text-primary" => base.text_primary = hex, + "text-secondary" => base.text_secondary = hex, + "accent" => base.accent = hex, + "danger" => base.danger = hex, + "warning" => base.warning = hex, + "success" => base.success = hex, + "border" => base.border = hex, + "favorite" => base.favorite = hex, + other => log::debug!("[qbz-slint] custom theme: unknown token key '{other}'"), + } +} + +/// Reflect a single edited token back into its editor swatch (so the swatch +/// preview and the ColorPicker's `value` binding update), WITHOUT touching the +/// open-token state (the inline picker must stay open through the edit). +fn set_one_swatch(window: &AppWindow, key: &str, color: Color) { + let st = window.global::(); + match key { + "surface-main" => st.set_custom_surface_main(color), + "surface-card" => st.set_custom_surface_card(color), + "surface-elevated" => st.set_custom_surface_elevated(color), + "text-primary" => st.set_custom_text_primary(color), + "text-secondary" => st.set_custom_text_secondary(color), + "accent" => st.set_custom_accent(color), + "danger" => st.set_custom_danger(color), + "warning" => st.set_custom_warning(color), + "success" => st.set_custom_success(color), + "border" => st.set_custom_border(color), + "favorite" => st.set_custom_favorite(color), + _ => {} + } +} + +/// Derive `base`, push the palette live, and persist — WITHOUT re-seeding the +/// editor swatches (the caller updates only the touched swatch). Used by the +/// per-token and polarity edits so the inline picker stays open. +fn apply_live(window: &AppWindow, base: &CustomThemeBase) { + let colors = qbz_theme::theme_from_base(base); + crate::theme::push_colors(window, &colors, false, false); + save(base); +} + /// Parse an opaque `#rrggbb` base token into a Slint `Color`, falling back to /// transparent-safe black on malformed input (the derivation applies the real /// fallbacks; this only feeds the editor swatch preview). @@ -106,11 +182,12 @@ fn push_base_to_state(window: &AppWindow, base: &CustomThemeBase) { st.set_custom_open_token("".into()); } -/// Seed the custom-theme editor swatches from the persisted (or freshly seeded) -/// base. Called at startup so the editor reflects the saved base when Settings -/// opens. +/// Seed the custom-theme editor swatches from the persisted base (or the OLED +/// default when none exists). Runs at startup for every user, so it uses the +/// non-persisting [`load`] — the `custom_theme.json` file is only created once +/// the user actually selects/edits the Custom theme (via [`load_or_seed`]). pub fn seed_state(window: &AppWindow) { - let base = load_or_seed(); + let base = load(); push_base_to_state(window, &base); } @@ -123,3 +200,72 @@ pub fn apply_startup(window: &AppWindow) { crate::theme::push_colors(window, &colors, false, false); log::info!("[qbz-slint] applied custom theme"); } + +/// Set one base token to `color` (the live ColorPicker drag path). Re-derives and +/// re-pushes the whole palette in real time, persists, and updates the token's +/// own swatch — the inline picker stays open. +pub fn set_token(window: &AppWindow, key: &str, color: Color) { + let mut base = base_from_state(window); + set_field(&mut base, key, rgba_of(color).to_hex()); + apply_live(window, &base); + set_one_swatch(window, key, color); +} + +/// Set one base token from a committed HEX string (`#rrggbb`). Malformed input is +/// ignored; a valid value reuses [`set_token`] (which also updates the swatch, +/// reseeding the picker's crosshair via its `value` binding). +pub fn set_token_hex(window: &AppWindow, key: &str, hex: &str) { + match Rgba::from_hex(hex) { + Some(c) => set_token(window, key, to_color(Rgba::rgb(c.r, c.g, c.b))), + None => log::debug!("[qbz-slint] custom theme: ignoring malformed hex '{hex}'"), + } +} + +/// Flip the custom theme polarity (dark/light) and re-derive. The base token +/// colors are unchanged; only `is_dark` and the derived shades/overlays flip. +pub fn toggle_dark(window: &AppWindow, is_dark: bool) { + let mut base = base_from_state(window); + base.is_dark = is_dark; + apply_live(window, &base); + window.global::().set_custom_is_dark(is_dark); +} + +/// "Start from current theme": snapshot the LIVE applied palette (whatever is in +/// the `Theme` global — static, auto or custom) into the editable base, then +/// derive/apply/persist and re-seed every editor swatch. `is_dark` is inferred +/// from the surface luminance; `border` prefers the opaque subtle edge, else the +/// strong one (the four legacy P1 themes store a translucent-white hairline in +/// `border_subtle` that would seed as a jarring pure-white edge). +pub fn seed_from_current(window: &AppWindow) { + let c = window.global::().get_c(); + let surface_main = rgba_of(c.surface_main); + let is_dark = qbz_theme::relative_luminance(Rgba::rgb( + surface_main.r, + surface_main.g, + surface_main.b, + )) < 0.5; + let border_subtle = rgba_of(c.border_subtle); + let border = if border_subtle.a == 255 { + border_subtle + } else { + rgba_of(c.border_strong) + }; + let base = CustomThemeBase { + is_dark, + surface_main: surface_main.to_hex(), + surface_card: rgba_of(c.surface_card).to_hex(), + surface_elevated: rgba_of(c.surface_elevated).to_hex(), + text_primary: rgba_of(c.text_primary).to_hex(), + text_secondary: rgba_of(c.text_secondary).to_hex(), + accent: rgba_of(c.accent).to_hex(), + danger: rgba_of(c.danger).to_hex(), + warning: rgba_of(c.warning).to_hex(), + success: rgba_of(c.success).to_hex(), + border: border.to_hex(), + favorite: rgba_of(c.favorite).to_hex(), + }; + let colors = qbz_theme::theme_from_base(&base); + crate::theme::push_colors(window, &colors, false, false); + save(&base); + push_base_to_state(window, &base); +} diff --git a/crates/qbz/src/main.rs b/crates/qbz/src/main.rs index c44b1362c..8dd28e491 100644 --- a/crates/qbz/src/main.rs +++ b/crates/qbz/src/main.rs @@ -8854,6 +8854,34 @@ fn main() -> Result<(), Box> { } other => log::debug!("[qbz-slint] unhandled appearance-action '{other}'"), }); + + // Custom-theme editor callbacks: per-token live edits (drag + hex), + // polarity toggle, and "start from current theme". Each re-derives the + // whole palette in Rust and pushes it live (derivation is cheap). + let ct_weak = window.as_weak(); + appearance.on_custom_set_token(move |key, color| { + if let Some(w) = ct_weak.upgrade() { + crate::custom_theme::set_token(&w, key.as_str(), color); + } + }); + let ct_hex_weak = window.as_weak(); + appearance.on_custom_set_token_hex(move |key, hex| { + if let Some(w) = ct_hex_weak.upgrade() { + crate::custom_theme::set_token_hex(&w, key.as_str(), hex.as_str()); + } + }); + let ct_dark_weak = window.as_weak(); + appearance.on_custom_toggle_dark(move |is_dark| { + if let Some(w) = ct_dark_weak.upgrade() { + crate::custom_theme::toggle_dark(&w, is_dark); + } + }); + let ct_seed_weak = window.as_weak(); + appearance.on_custom_seed_from_current(move || { + if let Some(w) = ct_seed_weak.upgrade() { + crate::custom_theme::seed_from_current(&w); + } + }); } // "My QBZ" nav branding (Settings > Appearance) — persist the label /