Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Components/DesignSystem.swift3// Description: Visual System V2 « Graphite & Iris · Lumen »: colour / radius / spacing / motion / type tokens, floating material, chips, shared small views and modifiers.4import SwiftUI5import SPChatCore67/// Visual System V2 « Graphite & Iris · Lumen » — tokens shared with the web and iOS apps (DESIGN-SYSTEM-V2.md).8/// Colours live in the asset catalog (`Assets.xcassets/Tokens`, generated by Scripts/sync-design-tokens.py) so that9/// light / dark / increased contrast are resolved by the system.10enum DS {11 enum C {12 static let bg = Color("Tokens/bg")13 static let bgElev = Color("Tokens/bgElev")14 static let surface = Color("Tokens/surface")15 static let surface2 = Color("Tokens/surface2")16 static let border = Color("Tokens/border")17 static let borderStrong = Color("Tokens/borderStrong")18 static let text = Color("Tokens/text")19 static let text2 = Color("Tokens/text2")20 static let muted = Color("Tokens/muted")21 static let accent = Color("Tokens/accent")22 static let accentSoft = Color("Tokens/accentSoft")23 static let userBubble = Color("Tokens/userBubble")24 static let ok = Color("Tokens/ok")25 static let warn = Color("Tokens/warn")26 static let danger = Color("Tokens/danger")27 static let lumenHi = Color("Tokens/lumenHi")28 static let floatSolid = Color("Tokens/floatSolid")29 }30 /// Radius scale: 6 chips · 10 controls and rows · 14 cards and code · 20 composer, sheets, palette.31 enum R { static let chip: CGFloat = 6; static let control: CGFloat = 10; static let card: CGFloat = 14; static let float: CGFloat = 20; static let bubble: CGFloat = 18 }32 /// Spacing scale 2 4 6 8 12 16 20 24 32 48.33 enum S { static let xxs: CGFloat = 2; static let xs: CGFloat = 4; static let s: CGFloat = 6; static let m: CGFloat = 8; static let l: CGFloat = 12; static let xl: CGFloat = 16; static let xxl: CGFloat = 20; static let xxxl: CGFloat = 24 }34 enum Motion {35 /// Presses, chips, toggles.36 static let snappy = Animation.spring(response: 0.28, dampingFraction: 0.86)37 /// Panels, sheets, morphs.38 static let smooth = Animation.spring(response: 0.42, dampingFraction: 0.88)39 /// Content swaps, and the Reduce Motion fallback of everything.40 static let fade = Animation.easeOut(duration: 0.14)41 static func resolve(_ animation: Animation, reduceMotion: Bool) -> Animation { reduceMotion ? fade : animation }42 }43 enum F {44 static let display = Font.system(size: 28, weight: .semibold)45 static let title = Font.system(size: 20, weight: .semibold)46 static let heading = Font.system(size: 16, weight: .semibold)47 static let ui = Font.system(size: 13, weight: .medium)48 static let uiRegular = Font.system(size: 13)49 static let caption = Font.system(size: 11.5, weight: .medium)50 static let micro = Font.system(size: 10.5, weight: .semibold)51 static func mono(_ size: CGFloat = 12, weight: Font.Weight = .regular) -> Font { .system(size: size, weight: weight, design: .monospaced) }52 }53}5455/// V1 names kept for the existing screens; every value now comes from the V2 tokens. Provider hues are retired:56/// a model is identified by its maker logo (`ModelMark`), never by a coloured dot.57enum Palette {58 static let iris = DS.C.accent59 static let irisDeep = Color(red: 0.353, green: 0.271, blue: 0.878)60 static let success = DS.C.ok61 static let warning = DS.C.warn62 static let danger = DS.C.danger6364 static func hex(_ s: String?) -> Color? {65 guard var h = s?.trimmingCharacters(in: .whitespaces), !h.isEmpty else { return nil }66 if h.hasPrefix("#") { h.removeFirst() }67 guard h.count == 6, let v = UInt32(h, radix: 16) else { return nil }68 return Color(red: Double((v >> 16) & 0xff) / 255, green: Double((v >> 8) & 0xff) / 255, blue: Double(v & 0xff) / 255)69 }70}7172/// Series colours for charts: one iris ramp (the only brand colour), from full to faint — no provider hue.73enum ChartScale {74 static func color(_ index: Int, of count: Int) -> Color {75 let steps: [Double] = [1.0, 0.62, 0.38, 0.24, 0.16, 0.10]76 return index == 0 ? DS.C.accent : (index < steps.count ? DS.C.accent.opacity(steps[index]) : DS.C.muted.opacity(0.5))77 }78}7980enum Metrics {81 /// Reading column ≈ 72 characters.82 static let transcriptMaxWidth: CGFloat = 76083 static let composerMaxWidth: CGFloat = 76084 static let corner: CGFloat = DS.R.control85 static let bubbleCorner: CGFloat = DS.R.bubble86}8788// MARK: - Motion and material helpers8990/// Animates with the given spring, or with a short cross-fade when Reduce Motion is on.91private struct MotionModifier<V: Equatable>: ViewModifier {92 let animation: Animation93 let value: V94 @Environment(\.accessibilityReduceMotion) private var reduceMotion95 func body(content: Content) -> some View { content.animation(DS.Motion.resolve(animation, reduceMotion: reduceMotion), value: value) }96}9798/// Floating material (composer, palette, popovers): translucent + hairline + soft shadow; an opaque surface when the99/// owner asked for less transparency or more contrast.100struct FloatSurface: ViewModifier {101 var radius: CGFloat = DS.R.float102 var stroke: Color = DS.C.border103 var lineWidth: CGFloat = 1104 var shadow = true105 @Environment(\.accessibilityReduceTransparency) private var reduceTransparency106 @Environment(\.colorSchemeContrast) private var contrast107 @Environment(\.colorScheme) private var scheme108 func body(content: Content) -> some View {109 let shape = RoundedRectangle(cornerRadius: radius, style: .continuous)110 let opaque = reduceTransparency || contrast == .increased111 content112 .background {113 if opaque { shape.fill(DS.C.floatSolid) }114 else { shape.fill(.ultraThinMaterial).overlay(shape.fill(DS.C.floatSolid.opacity(scheme == .dark ? 0.62 : 0.58))) }115 }116 .overlay(shape.strokeBorder(stroke, lineWidth: lineWidth))117 .overlay(alignment: .top) { if scheme == .dark, !opaque { shape.strokeBorder(Color.white.opacity(0.04), lineWidth: 1).mask(LinearGradient(colors: [.white, .clear], startPoint: .top, endPoint: .center)) } }118 .shadow(color: shadow ? .black.opacity(scheme == .dark ? 0.45 : 0.12) : .clear, radius: shadow ? 22 : 0, y: shadow ? 12 : 0)119 }120}121122extension View {123 func motion<V: Equatable>(_ animation: Animation = DS.Motion.snappy, value: V) -> some View { modifier(MotionModifier(animation: animation, value: value)) }124 func floatSurface(radius: CGFloat = DS.R.float, stroke: Color = DS.C.border, lineWidth: CGFloat = 1, shadow: Bool = true) -> some View {125 modifier(FloatSurface(radius: radius, stroke: stroke, lineWidth: lineWidth, shadow: shadow))126 }127 /// Opaque surface with a hairline (cards, inputs).128 func surface(radius: CGFloat = DS.R.card, fill: Color = DS.C.surface) -> some View {129 background(fill, in: RoundedRectangle(cornerRadius: radius, style: .continuous)).overlay(RoundedRectangle(cornerRadius: radius, style: .continuous).strokeBorder(DS.C.border, lineWidth: 1))130 }131}132133/// Compact chip (filters, tools, follow-ups): 6 pt radius, `surface-2`, selected = iris soft fill + iris text.134struct ChipStyle: ButtonStyle {135 var selected = false136 var prominent = false137 func makeBody(configuration: Configuration) -> some View {138 configuration.label.font(DS.F.caption).lineLimit(1)139 .padding(.horizontal, 9).padding(.vertical, 5)140 .foregroundStyle(selected ? DS.C.accent : DS.C.text2)141 .background(selected ? DS.C.accentSoft : (prominent ? DS.C.surface : DS.C.surface2), in: Capsule())142 .overlay(Capsule().strokeBorder(selected ? DS.C.accent.opacity(0.45) : DS.C.border, lineWidth: 1))143 .opacity(configuration.isPressed ? 0.7 : 1).scaleEffect(configuration.isPressed ? 0.97 : 1)144 .contentShape(Capsule())145 }146}147148/// Small section caption used in panels and sheets (« MESURÉ SUR CE COMPTE »).149struct SectionCaption: View {150 let text: String151 var body: some View { Text(text.uppercased()).font(DS.F.micro).tracking(0.5).foregroundStyle(DS.C.muted).frame(maxWidth: .infinity, alignment: .leading) }152}153154/// A figure with the label of its source — every number shown in the Explorer, the Arena or the usage screens says155/// where it comes from.156struct SourcedFigure: View {157 let label: String158 let value: String159 var source: String?160 var body: some View {161 VStack(alignment: .leading, spacing: 2) {162 Text(label).font(DS.F.caption).foregroundStyle(DS.C.muted).lineLimit(1)163 Text(value).font(.system(size: 15, weight: .semibold).monospacedDigit()).foregroundStyle(DS.C.text).lineLimit(1).minimumScaleFactor(0.7).contentTransition(.numericText())164 if let source { Text(source).font(.system(size: 10)).foregroundStyle(DS.C.muted).lineLimit(2) }165 }166 .frame(maxWidth: .infinity, alignment: .leading).accessibilityElement(children: .combine)167 }168}169170/// Monospaced metadata chip (tokens, cost, latency).171struct MetaChip: View {172 let systemImage: String173 let text: String174 var help: String?175 var body: some View {176 Label(text, systemImage: systemImage).labelStyle(.titleAndIcon).font(.caption2.monospacedDigit()).foregroundStyle(.secondary)177 .help(help ?? text)178 }179}180181struct StatusPill: View {182 let text: String183 let color: Color184 var systemImage: String?185 var body: some View {186 HStack(spacing: 4) { if let systemImage { Image(systemName: systemImage) }; Text(text) }187 .font(.caption2.weight(.semibold)).foregroundStyle(color)188 .padding(.horizontal, 6).padding(.vertical, 2).background(color.opacity(0.13), in: Capsule())189 }190}191192/// Borderless icon button with a mandatory accessibility label and tooltip.193struct IconButton: View {194 let systemImage: String195 let label: String196 var role: ButtonRole?197 let action: () -> Void198 var body: some View {199 Button(role: role, action: action) { Image(systemName: systemImage).frame(width: 22, height: 22).contentShape(Rectangle()) }200 .buttonStyle(.borderless).help(label).accessibilityLabel(label)201 }202}203204struct EmptyStateView: View {205 let systemImage: String206 let title: String207 var message: String?208 var body: some View {209 ContentUnavailableView { Label(title, systemImage: systemImage) } description: { if let message { Text(message) } }210 }211}212213/// Inline banner (offline, warnings, errors) placed above content.214struct Banner: View {215 enum Kind { case info, warning, error }216 let kind: Kind217 let text: String218 var actionTitle: String?219 var action: (() -> Void)?220 var onDismiss: (() -> Void)?221 private var color: Color { switch kind { case .info: .secondary; case .warning: Palette.warning; case .error: Palette.danger } }222 private var icon: String { switch kind { case .info: "info.circle"; case .warning: "exclamationmark.triangle"; case .error: "xmark.octagon" } }223 var body: some View {224 HStack(spacing: 8) {225 Image(systemName: icon).foregroundStyle(color)226 Text(text).font(.callout).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading)227 if let actionTitle, let action { Button(actionTitle, action: action).controlSize(.small) }228 if let onDismiss { IconButton(systemImage: "xmark", label: "Fermer", action: onDismiss) }229 }230 .padding(.horizontal, 12).padding(.vertical, 8)231 .background(color.opacity(0.10), in: RoundedRectangle(cornerRadius: Metrics.corner))232 .overlay(RoundedRectangle(cornerRadius: Metrics.corner).strokeBorder(color.opacity(0.25)))233 .accessibilityElement(children: .combine)234 }235}236237extension View {238 /// Card surface used in library and settings screens.239 func card(padding: CGFloat = 14) -> some View {240 self.padding(padding).background(DS.C.surface, in: RoundedRectangle(cornerRadius: DS.R.card, style: .continuous))241 .overlay(RoundedRectangle(cornerRadius: DS.R.card, style: .continuous).strokeBorder(DS.C.border, lineWidth: 1))242 }243}244245// MARK: - Toasts246247@Observable final class ToastCenter {248 struct Toast: Identifiable, Equatable { let id = UUID(); let text: String; let isError: Bool }249 private(set) var current: Toast?250 private var dismissal: Task<Void, Never>?251 func show(_ text: String, isError: Bool = false) {252 current = Toast(text: text, isError: isError)253 dismissal?.cancel()254 dismissal = Task { try? await Task.sleep(for: .seconds(isError ? 5 : 2.5)); if !Task.isCancelled { current = nil } }255 if isError { NSAccessibility.post(element: NSApp as Any, notification: .announcementRequested, userInfo: [.announcement: text, .priority: NSAccessibilityPriorityLevel.high.rawValue]) }256 }257 func error(_ error: Error) { show((error as? LocalizedError)?.errorDescription ?? String(describing: error), isError: true) }258}259260struct ToastOverlay: ViewModifier {261 let center: ToastCenter262 func body(content: Content) -> some View {263 content.overlay(alignment: .bottom) {264 if let t = center.current {265 Label(t.text, systemImage: t.isError ? "exclamationmark.triangle.fill" : "checkmark.circle.fill")266 .font(.callout).padding(.horizontal, 14).padding(.vertical, 9)267 .foregroundStyle(t.isError ? Palette.danger : .primary)268 .floatSurface(radius: 18)269 .padding(.bottom, 18).transition(.move(edge: .bottom).combined(with: .opacity))270 .accessibilityIdentifier("toast")271 }272 }273 .animation(.snappy(duration: 0.25), value: center.current)274 }275}276