SPB Git

Repository Public

ContentComponents.swift 9.0 KB 192 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/Design/Components/ContentComponents.swift3// Description: Content components — attachment chip and tile (type glyph or Quick Look thumbnail), the labelled AI panel container and the4// dashboard widget card (both empty shells for later passes), section header and keyboard-shortcut hint.5import QuickLookThumbnailing6import SwiftUI7import UniformTypeIdentifiers89// MARK: - attachments1011/// What an attachment component shows, independent from where the file comes from (received part, outgoing attachment).12struct AttachmentInfo: Identifiable, Equatable, Sendable {13    let id: String14    var name: String15    var type: String16    var size: Int17    var isRisky = false18    /// The gallery also thumbnails small PDFs (the person opened a screen made for looking at attachments).19    var thumbnailsDocuments = false2021    var glyph: String {22        if isRisky { return "exclamationmark.triangle.fill" }23        let t = type.lowercased(), ext = (name as NSString).pathExtension.lowercased()24        if t.hasPrefix("image/") { return "photo" }25        if t == "application/pdf" || ext == "pdf" { return "doc.richtext" }26        if t.hasPrefix("audio/") { return "waveform" }27        if t.hasPrefix("video/") { return "film" }28        if t.hasPrefix("text/calendar") || ext == "ics" { return "calendar" }29        if ["zip", "gz", "tar", "7z", "rar"].contains(ext) { return "doc.zipper" }30        if ["xls", "xlsx", "csv", "numbers"].contains(ext) { return "tablecells" }31        if ["ppt", "pptx", "key"].contains(ext) { return "rectangle.on.rectangle" }32        if t.hasPrefix("text/") || ["doc", "docx", "pages", "txt", "md", "rtf"].contains(ext) { return "doc.text" }33        return "doc"34    }35    /// Thumbnails are only generated for small images: nothing else is downloaded before the person asks.36    var wantsThumbnail: Bool {37        guard !isRisky, size <= 3_000_000 else { return false }38        let t = type.lowercased()39        return t.hasPrefix("image/") || (thumbnailsDocuments && (t == "application/pdf" || (name as NSString).pathExtension.lowercased() == "pdf"))40    }41}4243/// A Quick Look thumbnail of a local file, or the type glyph while it loads (or when there is none).44struct AttachmentThumbnail: View {45    let info: AttachmentInfo46    var side: CGFloat = 3447    /// Returns a local copy of the file to thumbnail; nil keeps the glyph.48    var file: (@Sendable () async -> URL?)?49    @State private var image: CGImage?50    @Environment(\.displayScale) private var scale5152    var body: some View {53        ZStack {54            RoundedRectangle(cornerRadius: SPRadius.chip, style: .continuous).fill(info.isRisky ? SPColor.warnSoft : SPColor.surface3)55            if let image {56                Image(decorative: image, scale: scale).resizable().scaledToFill().frame(width: side, height: side).clipShape(RoundedRectangle(cornerRadius: SPRadius.chip, style: .continuous))57            } else {58                Image(systemName: info.glyph).font(.system(size: side * 0.42)).symbolRenderingMode(.hierarchical).foregroundStyle(info.isRisky ? SPColor.warn : SPColor.text2)59            }60        }61        .frame(width: side, height: side)62        .task(id: info.id) {63            guard info.wantsThumbnail, image == nil, let file, let url = await file() else { return }64            let request = QLThumbnailGenerator.Request(fileAt: url, size: CGSize(width: side * 2, height: side * 2), scale: scale, representationTypes: .thumbnail)65            image = try? await QLThumbnailGenerator.shared.generateBestRepresentation(for: request).cgImage66        }67        .accessibilityHidden(true)68    }69}7071/// Type glyph or thumbnail, name (middle-truncated), size, and up to two actions (quick look, save / remove).72struct AttachmentChip<Actions: View>: View {73    let info: AttachmentInfo74    var file: (@Sendable () async -> URL?)?75    @ViewBuilder var actions: () -> Actions7677    var body: some View {78        HStack(spacing: SPSpacing.m) {79            AttachmentThumbnail(info: info, file: file)80            VStack(alignment: .leading, spacing: 1) {81                Text(info.name).font(SPFont.callout).foregroundStyle(SPColor.text).lineLimit(1).truncationMode(.middle)82                Text(info.isRisky ? L("Type à risque — ") + info.size.byteLabel : info.size.byteLabel).font(SPFont.caption).foregroundStyle(info.isRisky ? SPColor.warn : SPColor.text3)83            }84            Spacer(minLength: SPSpacing.m)85            actions()86        }87        .buttonStyle(.borderless)88        .padding(SPSpacing.m)89        .background(SPColor.surface2, in: RoundedRectangle(cornerRadius: SPRadius.card, style: .continuous))90        .overlay(RoundedRectangle(cornerRadius: SPRadius.card, style: .continuous).stroke(SPColor.border, lineWidth: 0.5))91    }92}9394/// The gallery form: a square thumbnail above the name and size.95struct AttachmentTile: View {96    let info: AttachmentInfo97    var file: (@Sendable () async -> URL?)?98    var body: some View {99        VStack(alignment: .leading, spacing: SPSpacing.s) {100            AttachmentThumbnail(info: info, side: 96, file: file).frame(maxWidth: .infinity)101            Text(info.name).font(SPFont.caption).foregroundStyle(SPColor.text).lineLimit(2).truncationMode(.middle)102            Text(info.size.byteLabel).font(.caption2).foregroundStyle(SPColor.text3)103        }104        .padding(SPSpacing.m).frame(width: 128, alignment: .leading)105        .spCard()106        .accessibilityElement(children: .combine)107    }108}109110// MARK: - AI panel (shell)111112/// The container of every AI surface: tinted `info-soft`, always labelled with what produced the content. Empty shell in this pass —113/// summaries, suggested replies and the assistant arrive in a later pass and only render inside this container.114struct AIPanel<Content: View>: View {115    let title: String116    /// The provider and model that produced the content, shown to the person (« Claude · Anthropic »). Never omitted.117    let provider: String118    @ViewBuilder var content: () -> Content119120    var body: some View {121        VStack(alignment: .leading, spacing: SPSpacing.m) {122            HStack(spacing: SPSpacing.s) {123                Image(systemName: "sparkles").foregroundStyle(SPColor.info)124                Text(title).font(SPFont.headline).foregroundStyle(SPColor.text).lineLimit(1).layoutPriority(1)125                Spacer()126                Text(provider).font(SPFont.caption).foregroundStyle(SPColor.info).lineLimit(1).truncationMode(.middle).padding(.horizontal, 7).padding(.vertical, 2)127                    .overlay(Capsule().stroke(SPColor.info.opacity(0.4), lineWidth: 0.5))128                    .accessibilityLabel(Text("Généré par \(provider)"))129            }130            content()131        }132        .padding(SPSpacing.l)133        .background(SPColor.infoSoft, in: RoundedRectangle(cornerRadius: SPRadius.card, style: .continuous))134        .overlay(RoundedRectangle(cornerRadius: SPRadius.card, style: .continuous).stroke(SPColor.info.opacity(0.25), lineWidth: 0.5))135        .accessibilityElement(children: .contain)136    }137}138139// MARK: - dashboard widget (shell)140141/// A level-1 card with a header (title, optional action) and a body. Shell for the « Command Center » dashboard of a later pass;142/// widgets are laid out in a SwiftUI `Grid` in 1× / 2× widths.143struct DashboardWidgetCard<Content: View>: View {144    let title: String145    var icon: String?146    var actionTitle: String?147    var action: (() -> Void)?148    /// 1 or 2 grid columns.149    var span = 1150    @ViewBuilder var content: () -> Content151152    var body: some View {153        VStack(alignment: .leading, spacing: SPSpacing.l) {154            HStack(spacing: SPSpacing.s) {155                if let icon { Image(systemName: icon).foregroundStyle(SPColor.text3) }156                Text(title).font(SPFont.headline).foregroundStyle(SPColor.text).accessibilityAddTraits(.isHeader)157                Spacer()158                if let actionTitle, let action { Button(actionTitle, action: action).buttonStyle(.borderless).font(SPFont.callout).foregroundStyle(SPColor.accent) }159            }160            content().frame(maxWidth: .infinity, alignment: .leading)161        }162        .padding(SPSpacing.xl)163        .spCard()164        .gridCellColumns(max(1, min(2, span)))165    }166}167168// MARK: - small pieces169170/// A keyboard shortcut shown beside a command (command palette, tooltips).171struct ShortcutHint: View {172    let text: String173    var body: some View {174        Text(text).font(.caption.monospaced()).foregroundStyle(SPColor.text3)175            .padding(.horizontal, 5).padding(.vertical, 1)176            .background(SPColor.surface3, in: RoundedRectangle(cornerRadius: 4, style: .continuous))177            .accessibilityLabel(Text("Raccourci \(text)"))178    }179}180181/// A soft status badge (authentication results, delivery states).182struct StatusBadge: View {183    let text: String184    let icon: String185    let color: SPToken186    let soft: SPToken187    var body: some View {188        Label(text, systemImage: icon).font(SPFont.caption).padding(.horizontal, 7).padding(.vertical, 3)189            .background(soft, in: Capsule()).foregroundStyle(color)190    }191}192
Files main
Menu

Theme