SPB Git

Repository Public MIT

example-glass-prompt-bar.txt 2.3 KB 56 lines text

1import SwiftUI2import SPGlassKit34public struct GlassPromptBarExample: GlassExample {5    public static let componentID = "glass-prompt-bar"6    @State private var draft = ""7    @State private var isResponding = false8    @State private var sent: [String] = []9    @State private var attachments = [10        GlassPromptAttachment(id: "a1", name: "sample-report.pdf", systemImage: "doc.richtext"),11        GlassPromptAttachment(id: "a2", name: "a-sample-image-with-a-rather-long-file-name.png", systemImage: "photo"),12    ]13    @State private var nextAttachment = 31415    public init() {}1617    public var body: some View {18        VStack(alignment: .leading, spacing: 12) {19            Text("Sample data · nothing is sent anywhere; prompts are only listed below.")20                .font(.caption)21                .foregroundStyle(.secondary)22            ScrollView {23                VStack(alignment: .leading, spacing: 8) {24                    ForEach(Array(sent.enumerated()), id: \.offset) { _, prompt in25                        Text(prompt)26                            .frame(maxWidth: .infinity, alignment: .leading)27                    }28                    if isResponding {29                        GlassThinkingIndicator(.writing, label: "Simulating a response")30                    }31                }32            }33            GlassPromptBar(text: $draft, placeholder: "Ask the sample assistant", isResponding: isResponding, attachments: attachments) { prompt in34                sent.append(prompt)35                isResponding = true36            } onStop: {37                isResponding = false38            } onAttach: {39                // A real host presents its own file importer or photo picker here.40                attachments.append(GlassPromptAttachment(id: "a\(nextAttachment)", name: "sample-\(nextAttachment).txt"))41                nextAttachment += 142            } onRemoveAttachment: { attachment in43                attachments.removeAll { $0.id == attachment.id }44            }45        }46        .padding()47        .task(id: isResponding) {48            guard isResponding else { return }49            try? await Task.sleep(for: .seconds(3))50            if !Task.isCancelled { isResponding = false }51        }52    }53}5455#Preview("GlassPromptBar") { GlassPromptBarExample().glassTheme(.aurora) }56
Files main
Menu

Theme