SPB Git

Repository Public

Theme.swift 4.8 KB 110 lines swift

1//2//  SP Chat iOS3//  File: SPChat-iOS/Components/Theme.swift4//  Description: Visual identity « Graphite & Iris » (shared with the web app): colours, provider hues, formatting helpers, small shared views.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import SwiftUI1112extension Color {13    static let appBackground = Color("Background")14    static let surface = Color("Surface")15    static let surface2 = Color("Surface2")16    static let hairline = Color("Border")17    static let textPrimary = Color("TextPrimary")18    static let textSecondary = Color("TextSecondary")19    static let muted = Color("Muted")20    static let danger = Color("Danger")21    static let success = Color("Success")22    static let warning = Color("Warning")23}2425enum Format {26    static func tokens(_ n: Int?) -> String {27        guard let n else { return "—" }28        if n >= 1_000_000 { let m = Double(n) / 1_000_000; return (m.rounded() == m || m >= 10 || abs(m - m.rounded()) < 0.06 ? String(format: "%.0f", m) : String(format: "%.1f", m)) + "M" }29        return n >= 1000 ? String(format: n >= 10_000 ? "%.0fk" : "%.1fk", Double(n) / 1000) : String(n)30    }31    static func usd(_ v: Double?) -> String {32        guard let v else { return "—" }33        if v == 0 { return "0 $" }34        return v < 0.01 ? String(format: "%.4f $", v) : String(format: "%.2f $", v)35    }36    static func duration(ms: Int?) -> String {37        guard let ms else { return "—" }38        return ms >= 1000 ? String(format: "%.1f s", Double(ms) / 1000) : "\(ms) ms"39    }40    static func bytes(_ n: Int?) -> String {41        guard let n else { return "" }42        return ByteCountFormatter.string(fromByteCount: Int64(n), countStyle: .file)43    }44    static func relative(_ d: Date) -> String {45        let s = Date().timeIntervalSince(d)46        if s < 60 { return String(localized: "à l’instant") }47        if s < 3600 { return String(localized: "\(Int(s / 60)) min") }48        if s < 86_400 { return String(localized: "\(Int(s / 3600)) h") }49        if s < 7 * 86_400 { return String(localized: "\(Int(s / 86_400)) j") }50        return d.formatted(.dateTime.day().month(.abbreviated))51    }52}5354/// The SP Chat mark (asset rendered from Design/Mark.svg).55struct BrandMark: View {56    var size: CGFloat = 7257    var body: some View {58        Image("Mark").resizable().interpolation(.high).frame(width: size, height: size)59            .shadow(color: Color.accentColor.opacity(0.35), radius: size * 0.22, y: size * 0.1)60            .accessibilityHidden(true)61    }62}6364struct Wordmark: View {65    var body: some View {66        (Text("spb").foregroundStyle(Color.textPrimary) + Text(".").foregroundStyle(Color.accentColor) + Text("chat").foregroundStyle(Color.textPrimary))67            .font(.system(.title2, design: .rounded, weight: .bold))68            .accessibilityLabel("SP Chat")69    }70}7172/// Coloured dot + provider-tinted model name.73struct Banner: View {74    enum Kind { case info, warning, error }75    let kind: Kind76    let text: String77    var systemImage: String?78    var onDismiss: (() -> Void)?7980    var body: some View {81        HStack(alignment: .firstTextBaseline, spacing: 10) {82            Image(systemName: systemImage ?? (kind == .error ? "exclamationmark.octagon.fill" : kind == .warning ? "exclamationmark.triangle.fill" : "info.circle.fill"))83                .foregroundStyle(tint)84            Text(text).font(.footnote).foregroundStyle(Color.textPrimary).frame(maxWidth: .infinity, alignment: .leading)85            if let onDismiss {86                Button(action: onDismiss) { Image(systemName: "xmark").font(.footnote.weight(.semibold)).foregroundStyle(Color.muted).frame(width: 28, height: 28).contentShape(.rect) }87                    .buttonStyle(.plain).accessibilityLabel(String(localized: "Fermer"))88            }89        }90        .padding(.horizontal, 14).padding(.vertical, 10)91        .background(tint.opacity(0.12), in: .rect(cornerRadius: 12, style: .continuous))92        .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(tint.opacity(0.28)))93        .accessibilityElement(children: .combine)94    }95    private var tint: Color { kind == .error ? .danger : kind == .warning ? .warning : .accentColor }96}9798/// Three dots pulsing while the model has not produced its first token.99extension View {100    /// Liquid Glass on iOS 26+, the system material before.101    @ViewBuilder func glassSurface(cornerRadius: CGFloat) -> some View {102        if #available(iOS 26.0, *) {103            self.glassEffect(.regular, in: .rect(cornerRadius: cornerRadius, style: .continuous))104        } else {105            self.background(.regularMaterial, in: .rect(cornerRadius: cornerRadius, style: .continuous))106                .overlay(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous).strokeBorder(Color.hairline))107        }108    }109}110
Files main
Menu

Theme