diff --git a/crates/Cargo.lock b/crates/Cargo.lock index 1c3cd8c62..6664fc9bb 100644 --- a/crates/Cargo.lock +++ b/crates/Cargo.lock @@ -5766,6 +5766,7 @@ dependencies = [ "qbz-playlist-import", "qbz-plex", "qbz-qobuz", + "qbz-theme", "qconnect-app", "qconnect-transport-ws", "rand 0.10.1", diff --git a/crates/qbz-slint/Cargo.toml b/crates/qbz-slint/Cargo.toml index 99df884cd..834c5403f 100644 --- a/crates/qbz-slint/Cargo.toml +++ b/crates/qbz-slint/Cargo.toml @@ -59,6 +59,7 @@ serde_json = { workspace = true } dirs = "5" chrono = { workspace = true, features = ["unstable-locales"] } qbz-app = { path = "../qbz-app" } +qbz-theme = { path = "../qbz-theme" } qbz-media-controls = { path = "../qbz-media-controls" } qbz-audio = { path = "../qbz-audio" } qbz-core = { path = "../qbz-core" } diff --git a/crates/qbz-slint/src/main.rs b/crates/qbz-slint/src/main.rs index 5ad2377a9..220f615ed 100644 --- a/crates/qbz-slint/src/main.rs +++ b/crates/qbz-slint/src/main.rs @@ -102,6 +102,7 @@ mod settings; mod share; mod sidebar; mod suggestions; +mod theme; mod toast; mod tray; mod tray_settings; @@ -4321,6 +4322,29 @@ fn main() -> Result<(), Box> { .global::() .set_album_header_gradient(crate::ui_prefs::load().album_header_gradient); + // Theme: seed the dropdown list from the Rust registry, then restore the + // persisted theme (slug is the source of truth; the dropdown index is + // derived). Fresh profiles default to OLED Dark (owner decision). This must + // run before the shell renders so the first paint is the right palette. + { + let appearance = window.global::(); + let labels: Vec = crate::theme::dropdown_labels() + .into_iter() + .map(slint::SharedString::from) + .collect(); + appearance.set_themes(slint::ModelRc::new(slint::VecModel::from(labels))); + + let id = crate::theme::id_for_slug(&crate::ui_prefs::load().theme); + appearance.set_theme_index(crate::theme::index_for_id(id)); + appearance.set_theme_is_system(id == qbz_theme::ThemeId::System); + // Keep the legacy ThemeState.mode in sync with the dropdown index for + // any residual reads; the palette itself is driven by apply_theme. + window + .global::() + .set_mode(crate::theme::index_for_id(id)); + crate::theme::apply_theme(&window, id); + } + // Tell the tray settings UI which platform it's on so it can show the // macOS-only controls ("Menu Bar" header, hide-Dock toggle) and hide the // Linux/Windows-only minimize-to-tray row. @@ -5237,7 +5261,8 @@ fn main() -> Result<(), Box> { "tray-mac-hide-dock" => tray_settings::set_mac_hide_dock(value), other => log::debug!("[qbz-slint] unhandled appearance-bool '{other}'"), }); - appearance.on_appearance_select(|key, index| match key.as_str() { + let theme_weak = window.as_weak(); + appearance.on_appearance_select(move |key, index| match key.as_str() { "tray-icon-theme" => { tray_settings::set_icon_theme_index(index); // Re-theme the running tray icon live (no restart). @@ -5245,6 +5270,17 @@ fn main() -> Result<(), Box> { t.set_icon_theme(tray_settings::theme_for_index(index)); } } + "theme" => { + // Slug is the source of truth: map the dropdown index -> stable + // id, persist the slug, then hot-switch the live palette. + let id = crate::theme::id_for_index(index); + let mut prefs = crate::ui_prefs::load(); + prefs.theme = id.slug().to_string(); + crate::ui_prefs::save(&prefs); + if let Some(w) = theme_weak.upgrade() { + crate::theme::apply_theme(&w, id); + } + } other => log::debug!("[qbz-slint] unhandled appearance-select '{other}'"), }); } diff --git a/crates/qbz-slint/src/theme.rs b/crates/qbz-slint/src/theme.rs new file mode 100644 index 000000000..1ff306d98 --- /dev/null +++ b/crates/qbz-slint/src/theme.rs @@ -0,0 +1,170 @@ +//! Theme application: bridge the frontend-agnostic `qbz-theme` registry to the +//! Slint `Theme` global. +//! +//! `qbz-theme` owns all palette data (ADR-006). This module is the only place +//! that knows about both `qbz_theme::Rgba` and the generated Slint `ThemeColors` +//! struct: it converts one to the other and pushes it into `Theme.c`, plus sets +//! the `Theme.is-system` flag so the System theme keeps following the OS. +//! +//! The dropdown shows a filtered/ordered list of themes; the stable slug is the +//! source of truth (persisted in `ui_prefs.theme`), and the dropdown index is +//! DERIVED from it — never the reverse. + +use crate::{AppWindow, Theme as SlintTheme, ThemeColors as SlintThemeColors}; +use qbz_theme::{Rgba, ThemeId}; +use slint::{Color, ComponentHandle}; + +/// Convert a registry `Rgba` to a Slint `Color` (straight alpha). +fn to_color(c: Rgba) -> Color { + Color::from_argb_u8(c.a, c.r, c.g, c.b) +} + +/// Build the generated Slint `ThemeColors` from a registry `ThemeColors`. +fn to_slint(c: &qbz_theme::ThemeColors) -> SlintThemeColors { + SlintThemeColors { + surface_main: to_color(c.surface_main), + surface_card: to_color(c.surface_card), + surface_elevated: to_color(c.surface_elevated), + surface_hover: to_color(c.surface_hover), + bg_hover: to_color(c.bg_hover), + + text_primary: to_color(c.text_primary), + text_secondary: to_color(c.text_secondary), + text_muted: to_color(c.text_muted), + text_disabled: to_color(c.text_disabled), + + accent: to_color(c.accent), + accent_hover: to_color(c.accent_hover), + accent_pressed: to_color(c.accent_pressed), + accent_text: to_color(c.accent_text), + + danger: to_color(c.danger), + danger_bg: to_color(c.danger_bg), + danger_border: to_color(c.danger_border), + danger_hover: to_color(c.danger_hover), + + warning: to_color(c.warning), + warning_bg: to_color(c.warning_bg), + warning_border: to_color(c.warning_border), + warning_hover: to_color(c.warning_hover), + + success: to_color(c.success), + success_bg: to_color(c.success_bg), + success_border: to_color(c.success_border), + success_hover: to_color(c.success_hover), + + border_subtle: to_color(c.border_subtle), + border_muted: to_color(c.border_muted), + border_strong: to_color(c.border_strong), + + focus_ring: to_color(c.focus_ring), + + favorite: to_color(c.favorite), + card_shadow: to_color(c.card_shadow), + + alpha_4: to_color(c.alpha_pct(4)), + alpha_5: to_color(c.alpha_pct(5)), + alpha_6: to_color(c.alpha_pct(6)), + alpha_8: to_color(c.alpha_pct(8)), + alpha_10: to_color(c.alpha_pct(10)), + alpha_12: to_color(c.alpha_pct(12)), + alpha_15: to_color(c.alpha_pct(15)), + alpha_18: to_color(c.alpha_pct(18)), + alpha_20: to_color(c.alpha_pct(20)), + alpha_25: to_color(c.alpha_pct(25)), + alpha_30: to_color(c.alpha_pct(30)), + alpha_35: to_color(c.alpha_pct(35)), + alpha_40: to_color(c.alpha_pct(40)), + alpha_45: to_color(c.alpha_pct(45)), + alpha_50: to_color(c.alpha_pct(50)), + alpha_55: to_color(c.alpha_pct(55)), + alpha_60: to_color(c.alpha_pct(60)), + alpha_65: to_color(c.alpha_pct(65)), + alpha_70: to_color(c.alpha_pct(70)), + alpha_75: to_color(c.alpha_pct(75)), + alpha_80: to_color(c.alpha_pct(80)), + alpha_85: to_color(c.alpha_pct(85)), + alpha_90: to_color(c.alpha_pct(90)), + alpha_95: to_color(c.alpha_pct(95)), + } +} + +/// Push the palette for `id` into the running window's `Theme` global. Sets +/// `is-system` so the System theme follows the OS (the struct is still pushed as +/// a sane fallback for any non-System-overridden tokens). +pub fn apply_theme(window: &AppWindow, id: ThemeId) { + let colors = qbz_theme::palette(id); + let theme = window.global::(); + theme.set_c(to_slint(&colors)); + theme.set_is_system(id == ThemeId::System); + log::info!("[qbz-slint] applied theme '{}'", id.slug()); +} + +/// The themes shown in the Settings dropdown, in display order. P1 exposes only +/// the implemented rows. The dropdown index is just a position in THIS list. +pub fn dropdown_themes() -> Vec { + qbz_theme::implemented_theme_list() + .into_iter() + .map(|e| e.id) + .collect() +} + +/// Display names for the dropdown, matching [`dropdown_themes`] order. +pub fn dropdown_labels() -> Vec { + dropdown_themes() + .into_iter() + .map(|id| id.display_name().to_string()) + .collect() +} + +/// Map a persisted slug to a `ThemeId`, falling back to the default (OLED) when +/// the slug is unknown or absent. +pub fn id_for_slug(slug: &str) -> ThemeId { + ThemeId::from_slug(slug).unwrap_or_else(qbz_theme::default_theme_id) +} + +/// Map a dropdown index to a `ThemeId`. Out-of-range indices fall back to the +/// default theme. +pub fn id_for_index(index: i32) -> ThemeId { + let list = dropdown_themes(); + list.get(index as usize) + .copied() + .unwrap_or_else(qbz_theme::default_theme_id) +} + +/// Derive the dropdown index for a `ThemeId` (position in [`dropdown_themes`]). +/// Returns `0` if the id is not in the dropdown list (e.g. a P2/P3 theme not yet +/// exposed) — the caller should treat that as "no explicit selection". +pub fn index_for_id(id: ThemeId) -> i32 { + dropdown_themes() + .iter() + .position(|&t| t == id) + .map(|p| p as i32) + .unwrap_or(0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn slug_index_roundtrip_for_p1() { + for (i, id) in dropdown_themes().into_iter().enumerate() { + assert_eq!(index_for_id(id), i as i32); + assert_eq!(id_for_index(i as i32), id); + assert_eq!(id_for_slug(id.slug()), id); + } + } + + #[test] + fn unknown_slug_falls_back_to_oled() { + assert_eq!(id_for_slug("nope"), ThemeId::Oled); + assert_eq!(id_for_slug(""), ThemeId::Oled); + } + + #[test] + fn out_of_range_index_falls_back_to_default() { + assert_eq!(id_for_index(9999), qbz_theme::default_theme_id()); + assert_eq!(id_for_index(-1), qbz_theme::default_theme_id()); + } +} diff --git a/crates/qbz-slint/src/ui_prefs.rs b/crates/qbz-slint/src/ui_prefs.rs index 1e544adb8..fa0f7469d 100644 --- a/crates/qbz-slint/src/ui_prefs.rs +++ b/crates/qbz-slint/src/ui_prefs.rs @@ -54,6 +54,11 @@ pub struct UiPrefs { /// Whether album/artist detail headers use artwork-derived backdrops. #[serde(default = "default_album_header_gradient")] pub album_header_gradient: bool, + /// Active theme — a stable `qbz_theme::ThemeId` slug ("oled", "dark", + /// "tokyo-night", "system", ...). Stored as a slug (not an index) so it is + /// order-independent and stable across releases. Owner default: OLED Dark. + #[serde(default = "default_theme")] + pub theme: String, // ---- Miniplayer ---------------------------------------------------- /// Last miniplayer surface: 0 micro · 1 compact · 2 artwork · 3 queue · 4 lyrics. @@ -97,12 +102,20 @@ fn default_album_header_gradient() -> bool { DEFAULT_ALBUM_HEADER_GRADIENT } +/// Default theme slug. Owner decision 2026-06-20: OLED Dark is the default for +/// fresh installs and any profile without a persisted theme. Sourced from the +/// `qbz-theme` registry so the default stays single-sourced. +fn default_theme() -> String { + qbz_theme::default_slug().to_string() +} + impl Default for UiPrefs { fn default() -> Self { Self { streaming_quality: default_streaming_quality(), npb_mode: default_npb_mode(), album_header_gradient: default_album_header_gradient(), + theme: default_theme(), mini_surface: default_mini_surface(), mini_width: default_mini_width(), mini_height: default_mini_height(), @@ -202,5 +215,13 @@ mod tests { let prefs: UiPrefs = serde_json::from_str("{}").expect("empty object deserializes"); assert_eq!(prefs.streaming_quality, "hires_plus"); assert!(prefs.album_header_gradient); + // A profile that predates the theme field falls back to OLED. + assert_eq!(prefs.theme, "oled"); + } + + #[test] + fn default_theme_is_oled() { + assert_eq!(UiPrefs::default().theme, "oled"); + assert_eq!(default_theme(), "oled"); } } diff --git a/crates/qbz-slint/ui/app.slint b/crates/qbz-slint/ui/app.slint index 3c88133c4..5998ceba8 100644 --- a/crates/qbz-slint/ui/app.slint +++ b/crates/qbz-slint/ui/app.slint @@ -11,7 +11,8 @@ import "assets/fonts/Inter_18pt-Medium.ttf"; import "assets/fonts/Inter_18pt-SemiBold.ttf"; import "assets/fonts/Inter_18pt-Bold.ttf"; -import { Theme } from "foundation/semantic-colors.slint"; +import { Theme, ThemeColors } from "foundation/semantic-colors.slint"; +import { ThemeState } from "foundation/theme-state.slint"; import { SplashScreen } from "splash/SplashScreen.slint"; import { LoginScreen } from "login/LoginScreen.slint"; import { AppShell } from "shell/AppShell.slint"; @@ -19,6 +20,11 @@ import { Toast } from "primitives/Toast.slint"; import { ShellState, MiniPlayerState } from "state.slint"; import { MiniShell } from "miniplayer/MiniShell.slint"; +// Re-export the theming globals so the Rust layer can push palettes (Theme.c / +// Theme.is-system) and keep the dropdown index (ThemeState.mode) in sync. +export { Theme, ThemeColors } from "foundation/semantic-colors.slint"; +export { ThemeState } from "foundation/theme-state.slint"; + // Re-export the state globals so the Rust layer can populate them. export { HomeState, HomeActions, DiscoverState, DiscoverActions, SectionDescriptor, ConfigRow, DiscoverBrowseState, DiscoverBrowseActions, ForYouState, MixState, GenreFilterState, GenreFilterActions, AlbumState, ArtistState, NavState, ShellState, SessionState, SettingsState, AlbumActions, ArtistActions, ArtworkActions, NowPlayingState, QueueState, LyricsState, LyricsLineItem, SearchState, SearchActions, NetworkSidebarState, NetworkSidebarActions, MusicianState, MusicianActions, LabelState, LabelActions, AwardState, AwardActions, AwardEntry, LocationViewState, LocationViewActions, FavoritesState, FavoritesActions, PlaylistPickerState, PlaylistPickerActions, DuplicateConfirmState, DuplicateConfirmActions, PlaylistState, PlaylistActions, SidebarState, SidebarActions, SidebarFolderPopupState, CreatePlaylistState, CreatePlaylistActions, EditPlaylistState, EditPlaylistActions, CreateFolderState, CreateFolderActions, MyQbzCreateState, MyQbzCreateActions, DragState, DragActions, PlaylistManagerState, PlaylistManagerActions, OfflineManagerState, OfflineManagerActions, BlacklistState, BlacklistActions, BlacklistedArtistItem, MyQbzState, MyQbzActions, MixtapeCardItem, MyQbzAddState, MyQbzAddActions, MyQbzAddRow, MyQbzDetailState, MyQbzDetailActions, MixtapeDetailItem, MyQbzEditState, MyQbzEditActions, MyQbzMixState, MyQbzMixActions, DiscoBuilderState, DiscoBuilderActions, DiscoGroup, DiscoCandidate, LocalLibraryState, LocalLibraryActions, LibraryFoldersState, LibFolderEditState, LibraryManageActions, LibraryScanState, LibAlbumFilterState, LocalAlbumState, LocalAlbumActions, TagEditorState, TagEditorActions, FolderEditState, FolderEditActions, ToastState, TextUtil, QconnectDevState, QconnectDevice, CastState, CastDevice, CastActions, AppearanceState, MyQbzBrandingState, EphemeralPlayChoiceState, EphemeralPlayChoiceActions, PlexSettingsState, PlexAuthActions, PlexSectionItem, ScrobbleState, ScrobbleActions, OfflineState, OfflineModeActions, OfflineFavoritesState, OfflineFavoritesActions, ImportLogEntry, PlaylistImportState, PlaylistImportActions, DacWizardState, DacWizardActions, DacCandidateRow, RemediationRow, DacConfigRow, InfoCreditRow, InfoCreditPair, AlbumCreditPerformer, AlbumCreditTrack, TrackInfoState, TrackInfoActions, AlbumInfoState, AlbumInfoActions, SuggestionsState, SuggestionsActions, SuggestionCard, VisualizerState, ImmersiveState, MiniPlayerState, WindowControlActions } from "state.slint"; diff --git a/crates/qbz-slint/ui/settings/AppearanceSettings.slint b/crates/qbz-slint/ui/settings/AppearanceSettings.slint index 7a91aff07..9037f8e85 100644 --- a/crates/qbz-slint/ui/settings/AppearanceSettings.slint +++ b/crates/qbz-slint/ui/settings/AppearanceSettings.slint @@ -164,9 +164,10 @@ export component AppearanceSettings inherits VerticalLayout { current-index: AppearanceState.theme-index; selected(i) => { AppearanceState.theme-index = i; - // The themes list is trimmed to the ported set, so the - // dropdown index maps 1:1 to the mode (0 Dark, 1 OLED, - // 2 Tokyo Night, 3 System). + // The Rust handler ("theme" arm) maps this dropdown index to + // a stable theme id, persists the slug, and hot-switches the + // palette via Theme.set_c. ThemeState.mode is kept in sync + // only as a legacy index reference (no longer drives colors). ThemeState.mode = i; AppearanceState.appearance-select("theme", i); } diff --git a/crates/qbz-slint/ui/state.slint b/crates/qbz-slint/ui/state.slint index a132fcd50..4f7e885e3 100644 --- a/crates/qbz-slint/ui/state.slint +++ b/crates/qbz-slint/ui/state.slint @@ -2537,8 +2537,10 @@ export global SettingsState { // so the UI renders standalone even while unwired. export global AppearanceState { // --- THEME ------------------------------------------------------------- - // Only the themes that are actually ported/wired. Order is load-bearing: - // the index maps 1:1 to ThemeState.mode (0 Dark, 1 OLED, 2 Tokyo, 3 System). + // Seeded at startup by Rust from the qbz-theme registry + // (theme::dropdown_labels), in registry display order. The list below is a + // standalone-preview fallback only; the index is DERIVED from the persisted + // theme slug (theme::index_for_id), never assumed. in-out property <[string]> themes: [ "Dark", "OLED Black", "Tokyo Night", "System" ];