SPB Git

Repository Public

NVComponents.swift 12.7 KB 378 lines swift

1// SP Notes Core2// File: Sources/SPNotesDesign/NVComponents.swift3// Description: Reusable SwiftUI components of the Note Visual System 2.0 shared by the macOS and iOS apps: card, chip,4//              section header, empty state, AI badge, citation chip, glass toolbar, progress dots.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI910/// The semantic tone of a chip or badge.11public enum NVTone: String, Sendable, Hashable, CaseIterable {12    case neutral, accent, ai, success, warning, danger1314    /// Text / icon colour.15    public var foreground: Color {16        switch self {17        case .neutral: return NVColor.fgMuted18        case .accent: return NVColor.accent19        case .ai: return NVColor.ai20        case .success: return NVColor.success21        case .warning: return NVColor.warning22        case .danger: return NVColor.danger23        }24    }2526    /// Soft fill behind the text.27    public var background: Color {28        switch self {29        case .neutral: return NVColor.bgSunken30        case .accent: return NVColor.accentSoft31        case .ai: return NVColor.aiSoft32        case .success: return NVColor.success.opacity(0.14)33        case .warning: return NVColor.warning.opacity(0.16)34        case .danger: return NVColor.danger.opacity(0.14)35        }36    }37}3839// MARK: - NVCard4041/// A padded surface with the card elevation.42public struct NVCard<Content: View>: View {43    let padding: CGFloat44    let radius: CGFloat45    let content: Content4647    public init(padding: CGFloat = NVSpace.lg, radius: CGFloat = NVRadius.lg, @ViewBuilder content: () -> Content) {48        self.padding = padding49        self.radius = radius50        self.content = content()51    }5253    public var body: some View {54        content55            .padding(padding)56            .frame(maxWidth: .infinity, alignment: .leading)57            .nvCard(radius: radius)58    }59}6061// MARK: - NVChip6263/// A small pill: tag, filter, status. With an `action` it is a button; `isSelected` fills it with the tone.64public struct NVChip: View {65    let title: String66    let systemImage: String?67    let tone: NVTone68    let isSelected: Bool69    let action: (() -> Void)?7071    public init(_ title: String, systemImage: String? = nil, tone: NVTone = .neutral, isSelected: Bool = false, action: (() -> Void)? = nil) {72        self.title = title73        self.systemImage = systemImage74        self.tone = tone75        self.isSelected = isSelected76        self.action = action77    }7879    public var body: some View {80        if let action {81            Button(action: action) { label }82                .buttonStyle(.plain)83                .accessibilityAddTraits(isSelected ? .isSelected : [])84        } else {85            label86        }87    }8889    private var label: some View {90        HStack(spacing: NVSpace.xs) {91            if let systemImage { Image(systemName: systemImage).imageScale(.small).accessibilityHidden(true) }92            Text(verbatim: title).lineLimit(1)93        }94        .nvFont(.micro)95        .foregroundStyle(isSelected && tone == .accent ? NVColor.accentFg : (tone == .neutral && isSelected ? NVColor.fg : tone.foreground))96        .padding(.horizontal, NVSpace.sm)97        .padding(.vertical, NVSpace.xs)98        .background(isSelected && tone == .accent ? NVColor.accent : tone.background, in: Capsule())99        .overlay(Capsule().strokeBorder(isSelected && tone != .accent ? tone.foreground.opacity(0.5) : .clear, lineWidth: 1))100        .contentShape(Capsule())101    }102}103104// MARK: - NVSectionHeader105106/// A section title with an optional subtitle and a trailing accessory.107public struct NVSectionHeader<Accessory: View>: View {108    let title: String109    let subtitle: String?110    let accessory: Accessory111112    public init(_ title: String, subtitle: String? = nil, @ViewBuilder accessory: () -> Accessory) {113        self.title = title114        self.subtitle = subtitle115        self.accessory = accessory()116    }117118    public var body: some View {119        HStack(alignment: .firstTextBaseline, spacing: NVSpace.sm) {120            VStack(alignment: .leading, spacing: NVSpace.xxs) {121                Text(verbatim: title)122                    .nvFont(.uiStrong)123                    .foregroundStyle(NVColor.fg)124                    .accessibilityAddTraits(.isHeader)125                if let subtitle {126                    Text(verbatim: subtitle).nvFont(.micro).foregroundStyle(NVColor.fgMuted)127                }128            }129            Spacer(minLength: NVSpace.sm)130            accessory131        }132        .padding(.vertical, NVSpace.xs)133    }134}135136extension NVSectionHeader where Accessory == EmptyView {137    public init(_ title: String, subtitle: String? = nil) {138        self.init(title, subtitle: subtitle) { EmptyView() }139    }140}141142// MARK: - NVEmptyState143144/// A centred empty state: symbol, serif title, message and an optional action.145public struct NVEmptyState: View {146    let systemImage: String147    let title: String148    let message: String?149    let actionTitle: String?150    let action: (() -> Void)?151152    public init(systemImage: String, title: String, message: String? = nil, actionTitle: String? = nil, action: (() -> Void)? = nil) {153        self.systemImage = systemImage154        self.title = title155        self.message = message156        self.actionTitle = actionTitle157        self.action = action158    }159160    public var body: some View {161        VStack(spacing: NVSpace.md) {162            Image(systemName: systemImage)163                .font(.system(size: 34, weight: .light))164                .foregroundStyle(NVColor.lumen)165                .accessibilityHidden(true)166            Text(verbatim: title)167                .nvFont(.h3)168                .foregroundStyle(NVColor.fg)169                .multilineTextAlignment(.center)170            if let message {171                Text(verbatim: message)172                    .nvFont(.ui)173                    .foregroundStyle(NVColor.fgMuted)174                    .multilineTextAlignment(.center)175            }176            if let actionTitle, let action {177                Button(action: action) {178                    Text(verbatim: actionTitle)179                        .nvFont(.uiStrong)180                        .foregroundStyle(NVColor.accentFg)181                        .padding(.horizontal, NVSpace.lg)182                        .padding(.vertical, NVSpace.sm)183                        .background(NVColor.accent, in: Capsule())184                }185                .buttonStyle(.plain)186                .padding(.top, NVSpace.xs)187            }188        }189        .padding(NVSpace.xl)190        .frame(maxWidth: 420)191        .frame(maxWidth: .infinity, maxHeight: .infinity)192        .accessibilityElement(children: .contain)193    }194}195196// MARK: - NVAIBadge197198/// The visible marker of AI-generated content (« Généré par IA »), in the AI violet. Mandatory next to every199/// summary, suggestion or answer produced by a model.200public struct NVAIBadge: View {201    /// How much the badge shows.202    public enum Style: Sendable, Hashable {203        /// Symbol + « Généré par IA » (+ the model name when given).204        case full205        /// Symbol + « IA ».206        case compact207    }208209    let model: String?210    let style: Style211212    public init(model: String? = nil, style: Style = .full) {213        self.model = model214        self.style = style215    }216217    /// The text shown (and read by VoiceOver in its long form).218    public static func label(model: String?, style: Style) -> String {219        switch style {220        case .compact: return "IA"221        case .full:222            if let model, !model.isEmpty { return "Généré par IA · \(model)" }223            return "Généré par IA"224        }225    }226227    public var body: some View {228        HStack(spacing: NVSpace.xs) {229            Image(systemName: "sparkles").imageScale(.small).accessibilityHidden(true)230            Text(verbatim: Self.label(model: model, style: style)).lineLimit(1)231        }232        .nvFont(.micro)233        .foregroundStyle(NVColor.ai)234        .padding(.horizontal, NVSpace.sm)235        .padding(.vertical, NVSpace.xxs + 1)236        .background(NVColor.aiSoft, in: Capsule())237        .accessibilityElement(children: .ignore)238        .accessibilityLabel(Text(verbatim: Self.label(model: model, style: .full)))239    }240}241242// MARK: - NVCitationChip243244/// A numbered source of a grounded answer or a research citation: `[n] label`.245public struct NVCitationChip: View {246    let index: Int?247    let label: String248    let action: (() -> Void)?249250    public init(index: Int? = nil, label: String, action: (() -> Void)? = nil) {251        self.index = index252        self.label = label253        self.action = action254    }255256    /// The VoiceOver label.257    public static func accessibilityText(index: Int?, label: String) -> String {258        if let index { return "Source \(index) : \(label)" }259        return "Source : \(label)"260    }261262    public var body: some View {263        Group {264            if let action {265                Button(action: action) { chip }.buttonStyle(.plain)266            } else {267                chip268            }269        }270        .accessibilityElement(children: .ignore)271        .accessibilityLabel(Text(verbatim: Self.accessibilityText(index: index, label: label)))272        .accessibilityAddTraits(action == nil ? [] : .isButton)273    }274275    private var chip: some View {276        HStack(spacing: NVSpace.xs) {277            if let index {278                Text(verbatim: "\(index)")279                    .nvFont(.micro)280                    .monospacedDigit()281                    .foregroundStyle(NVColor.accentFg)282                    .padding(.horizontal, 5)283                    .padding(.vertical, 1)284                    .background(NVColor.accent, in: Capsule())285            }286            Text(verbatim: label).nvFont(.micro).foregroundStyle(NVColor.accent).lineLimit(1)287        }288        .padding(.leading, index == nil ? NVSpace.sm : NVSpace.xxs + 1)289        .padding(.trailing, NVSpace.sm)290        .padding(.vertical, NVSpace.xxs + 1)291        .background(NVColor.accentSoft, in: Capsule())292        .contentShape(Capsule())293    }294}295296// MARK: - NVToolbarGlass297298/// Translucent toolbar chrome: a horizontal row of controls on the glass elevation.299public struct NVToolbarGlass<Content: View>: View {300    let spacing: CGFloat301    let radius: CGFloat302    let content: Content303304    public init(spacing: CGFloat = NVSpace.sm, radius: CGFloat = NVRadius.xl, @ViewBuilder content: () -> Content) {305        self.spacing = spacing306        self.radius = radius307        self.content = content()308    }309310    public var body: some View {311        HStack(spacing: spacing) { content }312            .padding(.horizontal, NVSpace.md)313            .padding(.vertical, NVSpace.sm)314            .nvGlass(radius: radius)315    }316}317318// MARK: - NVProgressDots319320/// Progress as dots: determinate (`count` steps, `current` done) or indeterminate (three dots breathing in turn;321/// static under Reduce Motion).322public struct NVProgressDots: View {323    let count: Int324    let current: Int?325    let tone: NVTone326    @Environment(\.accessibilityReduceMotion) private var reduceMotion327    @State private var animating = false328329    /// Determinate: `current` of `count` steps are done.330    public init(count: Int, current: Int, tone: NVTone = .accent) {331        self.count = max(1, count)332        self.current = min(max(0, current), max(1, count))333        self.tone = tone334    }335336    /// Indeterminate activity.337    public init(tone: NVTone = .accent) {338        count = 3339        current = nil340        self.tone = tone341    }342343    public var body: some View {344        HStack(spacing: NVSpace.xs) {345            ForEach(0..<count, id: \.self) { i in346                Circle()347                    .fill(fill(i))348                    .frame(width: 6, height: 6)349                    .opacity(opacity(i))350                    .scaleEffect(current == nil && animating && !reduceMotion ? 1 : (current == nil && !reduceMotion ? 0.7 : 1))351                    .animation(pulse(i), value: animating)352            }353        }354        .onAppear { if current == nil { animating = true } }355        .accessibilityElement(children: .ignore)356        .accessibilityLabel(Text(verbatim: current == nil ? "En cours" : "Progression"))357        .accessibilityValue(Text(verbatim: current.map { "\($0) sur \(count)" } ?? ""))358    }359360    private func fill(_ i: Int) -> Color {361        guard let current else { return tone.foreground }362        return i < current ? tone.foreground : NVColor.lineStrong363    }364365    private func opacity(_ i: Int) -> Double {366        guard current == nil else { return 1 }367        if reduceMotion { return 0.7 }368        return animating ? 1 : 0.3369    }370371    private func pulse(_ i: Int) -> Animation? {372        guard current == nil, !reduceMotion else { return nil }373        return NVMotion.Curve.standard.animation(duration: NVMotion.Token.ambient.duration / 2)374            .repeatForever(autoreverses: true)375            .delay(Double(i) * NVMotion.Token.fast.duration)376    }377}378
Files main
Menu

Theme