Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Components/DesignSystem.swift3// Description: The application's use of Note Visual System 2.0 (SPNotesDesign): palette aliases, date formatting, sync badges, toasts, empty states, panel headers, icon buttons and fields shared by every screen. No literal colour, radius or spacing lives outside the tokens.4import SwiftUI5import SPNotesStore6import SPNotesDesign78/// Semantic colours of the shell. Ultramarine is the only interactive hue, violet marks anything produced by a model,9/// amber survives as the warning hue only.10enum Palette {11 static let accent = NVColor.accent12 static let subtle = NVColor.bgHover13 static func tag(_ name: String?) -> Color {14 switch name { case "red": NVColor.danger; case "orange", "yellow": NVColor.warning; case "green": NVColor.success; case "blue": NVColor.accent; case "purple", "pink": NVColor.ai; default: NVColor.fgMuted }15 }16}1718enum Format {19 private static let time: DateFormatter = { let f = DateFormatter(); f.dateStyle = .none; f.timeStyle = .short; return f }()20 private static let day: DateFormatter = { let f = DateFormatter(); f.dateFormat = "d MMM"; return f }()21 private static let full: DateFormatter = { let f = DateFormatter(); f.dateStyle = .medium; f.timeStyle = .short; return f }()22 private static let year: DateFormatter = { let f = DateFormatter(); f.dateFormat = "d MMM yyyy"; return f }()23 /// Compact date for lists: the time today, « hier », the day this year, the full date before.24 static func short(_ date: Date, now: Date = Date()) -> String {25 let cal = Calendar.current26 if cal.isDate(date, inSameDayAs: now) { return time.string(from: date) }27 if let y = cal.date(byAdding: .day, value: -1, to: now), cal.isDate(date, inSameDayAs: y) { return "Hier" }28 return cal.component(.year, from: date) == cal.component(.year, from: now) ? day.string(from: date) : year.string(from: date)29 }30 static func long(_ date: Date) -> String { full.string(from: date) }31 static func bytes(_ n: Int64) -> String { ByteCountFormatter.string(fromByteCount: n, countStyle: .file) }32 static func words(_ n: Int) -> String { n == 1 ? "1 mot" : "\(n.formatted()) mots" }33 /// « Untitled » is the server's placeholder for an empty title.34 static func title(_ raw: String) -> String { raw.isEmpty || raw == "Untitled" ? "Sans titre" : raw }35}3637/// Small dot that says where a note stands with the server.38struct SyncDot: View {39 let state: SyncState40 var body: some View {41 switch state {42 case .synced: EmptyView()43 case .conflict: Image(systemName: "exclamationmark.triangle.fill").font(NVFont.fixed(.micro)).foregroundStyle(NVColor.warning).help("Conflit à résoudre")44 case .error: Image(systemName: "exclamationmark.circle.fill").font(NVFont.fixed(.micro)).foregroundStyle(NVColor.danger).help("L'envoi a échoué — nouvel essai automatique")45 case .pending: Circle().fill(NVColor.accent).frame(width: NVSpace.sm - NVSpace.xxs, height: NVSpace.sm - NVSpace.xxs).help("Pas encore synchronisée")46 }47 }48}4950struct ToastOverlay: View {51 @Environment(AppEnvironment.self) private var env52 @Environment(\.accessibilityReduceMotion) private var reduceMotion53 var body: some View {54 VStack {55 Spacer()56 if let t = env.toasts.current {57 HStack(spacing: NVSpace.sm) {58 Label(t.text, systemImage: t.symbol).nvFont(.ui).foregroundStyle(t.isError ? NVColor.dangerText : NVColor.fg)59 if let action = t.action { Button(action.title) { action.run(); env.toasts.dismiss() }.buttonStyle(.link).nvFont(.uiStrong).accessibilityIdentifier("toast-action") }60 }61 .padding(.horizontal, NVSpace.lg).padding(.vertical, NVSpace.sm).nvGlass(radius: NVRadius.pill)62 .overlay(Capsule().strokeBorder(t.isError ? NVColor.danger.opacity(0.5) : .clear))63 .padding(.bottom, NVSpace.xl)64 .transition(reduceMotion ? .opacity : .move(edge: .bottom).combined(with: .opacity))65 .accessibilityIdentifier("toast")66 }67 }68 .animation(NVMotion.animation(.spring, reduceMotion: reduceMotion), value: env.toasts.current)69 .allowsHitTesting(env.toasts.current?.action != nil)70 }71}7273struct EmptyState: View {74 let symbol: String, title: String, message: String75 var action: (label: String, run: () -> Void)?76 var body: some View {77 NVEmptyState(systemImage: symbol, title: title, message: message, actionTitle: action?.label, action: action?.run)78 .frame(maxWidth: .infinity, maxHeight: .infinity)79 }80}8182/// Text with search matches highlighted (markers come from the store's FTS snippets).83struct HighlightedText: View {84 let marked: String85 var body: some View {86 SearchHit.segments(of: marked).reduce(Text("")) { acc, seg in87 acc + (seg.highlighted ? Text(seg.text).bold().foregroundStyle(NVColor.accent) : Text(seg.text))88 }89 }90}9192/// Header of a side panel or a feature screen: serif title, optional subtitle, trailing accessory.93struct PanelHeader<Accessory: View>: View {94 let title: String; var symbol: String? = nil; var subtitle: String? = nil; var tone: Color = NVColor.accent95 @ViewBuilder var accessory: Accessory96 var body: some View {97 HStack(alignment: .firstTextBaseline, spacing: NVSpace.sm) {98 if let symbol { Image(systemName: symbol).foregroundStyle(tone).accessibilityHidden(true) }99 VStack(alignment: .leading, spacing: NVSpace.xxs) {100 Text(title).nvFont(.h3).foregroundStyle(NVColor.fg).accessibilityAddTraits(.isHeader)101 if let subtitle { Text(subtitle).nvFont(.ui).foregroundStyle(NVColor.fgMuted) }102 }103 Spacer(minLength: NVSpace.sm)104 accessory105 }106 }107}108extension PanelHeader where Accessory == EmptyView { init(title: String, symbol: String? = nil, subtitle: String? = nil, tone: Color = NVColor.accent) { self.init(title: title, symbol: symbol, subtitle: subtitle, tone: tone) { EmptyView() } } }109110/// Small uppercase label above a group of controls.111struct GroupLabel: View {112 let text: String113 init(_ text: String) { self.text = text }114 var body: some View { Text(text.uppercased()).nvFont(.micro).kerning(0.6).foregroundStyle(NVColor.fgMuted).accessibilityAddTraits(.isHeader) }115}116117/// Toolbar-sized icon button with an active state, never taking the keyboard focus.118struct IconButton: View {119 let symbol: String, help: String120 var active = false121 let action: () -> Void122 @State private var hovering = false123 var body: some View {124 Button(action: action) {125 Image(systemName: symbol).font(.system(size: NVFont.Style.ui.size())).frame(width: NVSpace.xl + NVSpace.xs, height: NVSpace.xl)126 .foregroundStyle(active ? NVColor.accent : NVColor.fgMuted)127 .background(active ? NVColor.accentSoft : hovering ? NVColor.bgHover : .clear, in: NVRadius.shape(NVRadius.sm))128 }129 .buttonStyle(.plain).focusable(false).help(help).accessibilityLabel(help)130 .onHover { hovering = $0 }131 }132}133134/// Rounded search / capture field surface.135struct FieldSurface: ViewModifier {136 var focused = false137 func body(content: Content) -> some View {138 content.padding(.horizontal, NVSpace.md).padding(.vertical, NVSpace.sm)139 .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.md))140 .overlay(NVRadius.shape(NVRadius.md).strokeBorder(focused ? NVColor.accent.opacity(0.6) : NVColor.line))141 }142}143extension View {144 func fieldSurface(focused: Bool = false) -> some View { modifier(FieldSurface(focused: focused)) }145 /// Runs `action` when the value changes, on the main actor, as an async task.146 func onChangeAsync<V: Equatable>(of value: V, _ action: @escaping (V) async -> Void) -> some View { onChange(of: value) { _, new in Task { await action(new) } } }147}148