SPB Git

Repository Public

VQTheme.swift 4.7 KB 97 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/VQuantUI/Support/VQTheme.swift3// Description: What the person chose (up/down pair, number format, language, reduced motion), carried by the4//              environment, and the type-role modifier that makes the tokens follow Dynamic Type.5import SwiftUI6import VQuantKit78/// Per-person presentation settings read by every VQuantUI component. Build it from the synced preference object.9public struct VQTheme: Sendable, Equatable {10    /// Gains/losses colours: classic green/red or the colour-vision-safe blue/orange.11    public var upDown: VQTokens.UpDown12    public var numberLocale: VQNumberLocale13    /// Language of the built-in vocabulary (kind badges, freshness labels, section titles).14    public var language: VQLanguage15    /// `preference.reduceMotion`; the system setting is honoured in addition.16    public var reduceMotion: Bool1718    public init(upDown: VQTokens.UpDown = .classic, numberLocale: VQNumberLocale = .frCA, language: VQLanguage = .preferred, reduceMotion: Bool = false) {19        self.upDown = upDown; self.numberLocale = numberLocale; self.language = language; self.reduceMotion = reduceMotion20    }2122    public init(preference: VQPreference, language: VQLanguage = .preferred) {23        self.init(upDown: preference.upDownColors?.known == .cvd ? .cvd : .classic,24                  numberLocale: VQNumberLocale(preference: preference.numberFormat), language: language, reduceMotion: preference.reduceMotion ?? false)25    }2627    public func color(_ direction: VQDirection) -> Color {28        switch direction {29        case .up: return upDown.up30        case .down: return upDown.down31        case .flat: return VQTokens.Palette.text232        }33    }34    /// The string of `key` (its English text) in the theme's language, from the package's String Catalog.35    func l(_ key: String, _ arguments: CVarArg...) -> String { VQL10n.string(key, language, arguments: arguments) }36}3738private struct VQThemeKey: EnvironmentKey { static let defaultValue = VQTheme() }39private struct VQLanguageOverrideKey: EnvironmentKey { static let defaultValue: VQLanguage? = nil }4041public extension EnvironmentValues {42    /// The theme the components read. Its language is the one forced with `vqLanguage(_:)` when there is one.43    var vqTheme: VQTheme {44        get {45            var theme = self[VQThemeKey.self]46            if let forced = self[VQLanguageOverrideKey.self] { theme.language = forced }47            return theme48        }49        set { self[VQThemeKey.self] = newValue }50    }51    /// The language forced on the VQuant components of this hierarchy (`nil`: the theme's own, by default the app's).52    var vqLanguage: VQLanguage? {53        get { self[VQLanguageOverrideKey.self] }54        set { self[VQLanguageOverrideKey.self] = newValue }55    }56    /// True inside `VQExport`: an off-screen render cannot scroll, so wide content is fitted instead.57    @Entry var vqStaticRender = false58}5960public extension View {61    /// Sets the VQuant presentation settings for this view and its descendants.62    func vqTheme(_ theme: VQTheme) -> some View { environment(\.vqTheme, theme) }6364    /// Forces the language of every VQuant component below, whatever the theme says: `.vqLanguage(.fr)`. By default65    /// the components speak the app's language (`VQLanguage.preferred`); `.vqLanguage(nil)` returns to it.66    func vqLanguage(_ language: VQLanguage?) -> some View { environment(\.vqLanguage, language) }6768    /// Same, from a locale (`fr`, `fr-CA`, `fr_FR` → French; anything else → English): `.vqLanguage(locale: myLocale)`.69    func vqLanguage(locale: Locale) -> some View { environment(\.vqLanguage, VQLanguage(locale: locale)) }7071    /// Applies a type role of the design system (size, weight, face, tracking, case), scaled with Dynamic Type.72    func vqFont(_ role: VQTokens.Typography.Role) -> some View { modifier(VQFontModifier(role: role)) }73}7475struct VQFontModifier: ViewModifier {76    let role: VQTokens.Typography.Role77    @ScaledMetric(relativeTo: .body) private var unit: CGFloat = 17879    func body(content: Content) -> some View {80        let base = Font.system(size: role.size * unit, weight: role.weight, design: role.design)81        content82            .font(role.design == .monospaced ? base.monospacedDigit() : base)83            .tracking(role.tracking * unit)84            .textCase(role.uppercase ? .uppercase : nil)85    }86}8788/// Motion that stops under reduced motion (system setting or the person's preference).89struct VQMotion {90    @Environment(\.accessibilityReduceMotion) private var system91    @Environment(\.vqTheme) private var theme92    var reduce: Bool { system || theme.reduceMotion }93}9495typealias VQP = VQTokens.Palette96typealias VQS = VQTokens.Space97
Files main
Menu

Theme