SPB Git

Repository Public

Theme.swift 6.7 KB 165 lines swift

1import SwiftUI23/// Resolved design tokens for the current appearance. Colours come exclusively from the generated `Tokens`4/// (single source: `packages/ui-tokens/tokens.json`); "Increase Contrast" swaps the low-emphasis roles for their5/// stronger neighbours instead of inventing new colours.6public struct MCCTheme: Sendable {7    public let palette: Tokens.Palette8    public let highContrast: Bool9    public let scheme: ColorScheme1011    public init(scheme: ColorScheme, highContrast: Bool = false) {12        self.palette = Tokens.palette(scheme)13        self.highContrast = highContrast14        self.scheme = scheme15    }1617    public var canvas: Color { palette.bgCanvas }18    public var surface: Color { palette.bgSurface }19    public var raised: Color { palette.bgRaised }20    public var inset: Color { palette.bgInset }21    public var hover: Color { palette.bgHover }22    public var textPrimary: Color { palette.textPrimary }23    public var textSecondary: Color { highContrast ? palette.textPrimary : palette.textSecondary }24    public var textTertiary: Color { highContrast ? palette.textSecondary : palette.textTertiary }25    public var textDisabled: Color { highContrast ? palette.textTertiary : palette.textDisabled }26    public var border: Color { highContrast ? palette.borderStrong : palette.borderDefault }27    public var borderSubtle: Color { highContrast ? palette.borderDefault : palette.borderSubtle }28    public var accent: Color { palette.accentDefault }29    public var accentMuted: Color { palette.accentMuted }30    public var accentContrast: Color { palette.accentContrast }31    public var focusRing: Color { palette.focusRing }3233    public var chartColors: [Color] { [palette.chart1, palette.chart2, palette.chart3, palette.chart4, palette.chart5, palette.chart6] }34    public func chartColor(_ index: Int) -> Color { chartColors[((index % 6) + 6) % 6] }35}3637extension EnvironmentValues {38    @Entry public var mccTheme: MCCTheme = MCCTheme(scheme: .dark)39}4041private struct ThemeModifier: ViewModifier {42    @Environment(\.colorScheme) private var scheme43    @Environment(\.colorSchemeContrast) private var contrast44    func body(content: Content) -> some View {45        let theme = MCCTheme(scheme: scheme, highContrast: contrast == .increased)46        content47            .environment(\.mccTheme, theme)48            .tint(theme.accent)49    }50}5152public extension View {53    /// Resolves the tokens for the effective colour scheme and contrast, and publishes them in the environment.54    func mccThemed() -> some View { modifier(ThemeModifier()) }55}5657/// Semantic tone of a state. Each tone has its own symbol shape: state is never conveyed by colour alone.58public enum StatusTone: String, Sendable, CaseIterable, Hashable {59    case ok, warning, critical, info, stale, offline, maintenance, neutral6061    public func color(_ t: MCCTheme) -> Color {62        switch self {63        case .ok: t.palette.stateOk64        case .warning: t.palette.stateWarning65        case .critical: t.palette.stateCritical66        case .info: t.palette.stateInfo67        case .stale: t.palette.stateStale68        case .offline: t.palette.stateOffline69        case .maintenance: t.palette.stateMaintenance70        case .neutral: t.textSecondary71        }72    }7374    public func muted(_ t: MCCTheme) -> Color {75        switch self {76        case .ok: t.palette.stateOkMuted77        case .warning: t.palette.stateWarningMuted78        case .critical: t.palette.stateCriticalMuted79        case .info: t.palette.stateInfoMuted80        case .stale: t.palette.stateStaleMuted81        case .offline: t.palette.stateOfflineMuted82        case .maintenance: t.palette.stateMaintenanceMuted83        case .neutral: t.inset84        }85    }8687    public var symbol: String {88        switch self {89        case .ok: "checkmark.circle.fill"90        case .warning: "exclamationmark.triangle.fill"91        case .critical: "xmark.octagon.fill"92        case .info: "info.circle.fill"93        case .stale: "clock.fill"94        case .offline: "bolt.slash.fill"95        case .maintenance: "wrench.and.screwdriver.fill"96        case .neutral: "minus.circle"97        }98    }99}100101/// Card container used across the screens.102public struct Card<Content: View>: View {103    @Environment(\.mccTheme) private var theme104    private let padding: CGFloat105    private let content: Content106    public init(padding: CGFloat = Tokens.Space.s5, @ViewBuilder content: () -> Content) {107        self.padding = padding108        self.content = content()109    }110    public var body: some View {111        content112            .padding(padding)113            .frame(maxWidth: .infinity, alignment: .topLeading)114            .background(theme.surface, in: RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous))115            .overlay(RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous).strokeBorder(theme.borderSubtle, lineWidth: 1))116    }117}118119/// Small uppercase-free section title with an optional trailing accessory.120public struct SectionTitle<Accessory: View>: View {121    @Environment(\.mccTheme) private var theme122    private let title: String123    private let accessory: Accessory124    public init(_ title: String, @ViewBuilder accessory: () -> Accessory) {125        self.title = title126        self.accessory = accessory()127    }128    public var body: some View {129        HStack(alignment: .firstTextBaseline) {130            Text(title).font(.headline).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)131            Spacer(minLength: Tokens.Space.s3)132            accessory133        }134    }135}136public extension SectionTitle where Accessory == EmptyView {137    init(_ title: String) { self.init(title) { EmptyView() } }138}139140/// "Label: value" row with a selectable value.141public struct KeyValueRow: View {142    @Environment(\.mccTheme) private var theme143    private let key: String144    private let value: String145    private let monospaced: Bool146    public init(_ key: String, _ value: String, monospaced: Bool = false) {147        self.key = key; self.value = value; self.monospaced = monospaced148    }149    public var body: some View {150        HStack(alignment: .firstTextBaseline, spacing: Tokens.Space.s4) {151            Text(key).font(.callout).foregroundStyle(theme.textSecondary)152            Spacer(minLength: Tokens.Space.s3)153            Text(value)154                .font(monospaced ? .callout.monospaced() : .callout)155                .foregroundStyle(theme.textPrimary)156                .multilineTextAlignment(.trailing)157                .textSelection(.enabled)158        }159        // One element reading "key: value". The value is also part of the label because assistive clients and UI160        // automation do not all surface the value of a plain group.161        .accessibilityElement(children: .ignore)162        .accessibilityLabel("\(key) : \(value)")163    }164}165
Files main
Menu

Theme