SPB Git

Repository Public License

RootView.swift 13.6 KB 281 lines swift

1import PrismProjectStore2import PrismRenderer3import SwiftUI45struct RootView: View {6    @Bindable var model: StudioModel7    @Binding var showPalette: Bool8    @State private var deleting: ProjectRecord?910    var body: some View {11        NavigationSplitView {12            List(selection: $model.selection) {13                Label("New App", systemImage: "plus.square.on.square").tag(SidebarItem.welcome)14                Section("Apps") {15                    if model.projects.isEmpty {16                        Text("No apps yet").foregroundStyle(.secondary).font(.callout)17                    }18                    ForEach(model.projects) { project in19                        Label {20                            VStack(alignment: .leading, spacing: 1) {21                                Text(project.name).lineLimit(1)22                                if project.status == "draft" { Text("Not built yet").font(.caption).foregroundStyle(.secondary) }23                            }24                        } icon: { Image(systemName: project.status == "draft" ? "square.dashed" : "app.fill") }25                        .tag(SidebarItem.project(project.id))26                        .contextMenu { Button("Delete…", role: .destructive) { deleting = project } }27                    }28                }29                Section("PRISM") {30                    Label("Library", systemImage: "square.grid.2x2").tag(SidebarItem.library)31                    Label("Monitor", systemImage: "gauge.with.dots.needle.33percent").tag(SidebarItem.monitor)32                }33            }34            .navigationSplitViewColumnWidth(min: 190, ideal: 220, max: 300)35            .safeAreaInset(edge: .bottom) { KeyStatusBadge(model: model).padding(10) }36        } detail: {37            // Size-isolated so no screen's content can feed changing size limits back into the window's split view.38            SizeIsolated { detail.frame(maxWidth: .infinity, maxHeight: .infinity) }39        }40        .sheet(isPresented: $showPalette) { CommandPalette(model: model, isPresented: $showPalette) }41        .alert("PRISM", isPresented: Binding(get: { model.lastError != nil }, set: { if !$0 { model.lastError = nil } })) {42            Button("OK") { model.lastError = nil }43        } message: { Text(model.lastError ?? "") }44        .confirmationDialog("Delete “\(deleting?.name ?? "")”?", isPresented: Binding(get: { deleting != nil }, set: { if !$0 { deleting = nil } }), presenting: deleting) { project in45            Button("Delete App and Its Data", role: .destructive) { model.deleteProject(project.id) }46        } message: { _ in Text("The app's definition, saved versions and data are removed from PRISM. A published desktop copy is not affected.") }47    }4849    @ViewBuilder50    private var detail: some View {51        switch model.selection {52        case .project(let id):53            if let session = model.session(for: id) { ProjectView(model: model, session: session).id(id) }54            else { ContentUnavailableView("App not found", systemImage: "questionmark.app") }55        case .library: LibraryView(model: model)56        case .monitor: MonitorView(model: model)57        default: WelcomeView(model: model)58        }59    }60}6162/// Always-visible connection state, so it is never a surprise whether Claude features will work.63struct KeyStatusBadge: View {64    let model: StudioModel65    @Environment(\.openSettings) private var openSettings6667    var body: some View {68        let (symbol, color, text): (String, Color, String) = {69            switch model.keyStatus {70            case .missing: return ("key.slash", .orange, "No API key")71            case .stored: return ("key", .secondary, "Key stored, not checked")72            case .verified(_, let n): return ("checkmark.seal.fill", .green, "Claude connected · \(n) models")73            case .invalid: return ("xmark.seal.fill", .red, "Key rejected")74            case .unreachable: return ("wifi.slash", .orange, "Claude unreachable")75            }76        }()77        Button { openSettings() } label: {78            HStack(spacing: 6) {79                Image(systemName: symbol).foregroundStyle(color)80                Text(text).font(.callout).lineLimit(1)81                Spacer(minLength: 0)82            }83            .padding(.horizontal, 10).padding(.vertical, 7)84            .background(.quaternary.opacity(0.5), in: RoundedRectangle(cornerRadius: 8))85        }86        .buttonStyle(.plain)87        .help("Open Settings to manage the Anthropic API key")88        .accessibilityLabel("Claude connection: \(text). Opens Settings.")89    }90}9192/// The prompt composer: Return submits, Shift-Return inserts a newline.93struct Composer: View {94    @Binding var text: String95    var placeholder: String96    var actionTitle: String97    var isBusy = false98    var large = false99    var onSubmit: () -> Void100    var onStop: (() -> Void)?101    @FocusState private var focused: Bool102103    var body: some View {104        HStack(alignment: .bottom, spacing: 10) {105            ZStack(alignment: .topLeading) {106                if text.isEmpty {107                    Text(placeholder).foregroundStyle(.tertiary).padding(.horizontal, 5).padding(.vertical, 1).allowsHitTesting(false)108                }109                TextEditor(text: $text)110                    .scrollContentBackground(.hidden)111                    .focused($focused)112                    .frame(minHeight: large ? 84 : 22, maxHeight: large ? 160 : 90)113                    .fixedSize(horizontal: false, vertical: true)114                    .onKeyPress(keys: [.return]) { press in115                        if press.modifiers.contains(.shift) { return .ignored }116                        submit()117                        return .handled118                    }119                    .accessibilityLabel(placeholder)120            }121            .font(large ? .title3 : .body)122            if isBusy, let onStop {123                Button(role: .cancel) { onStop() } label: { Label("Stop", systemImage: "stop.fill") }124                    .controlSize(large ? .large : .regular)125                    .help("Stop the build (⌘.)")126            } else {127                Button { submit() } label: { Label(actionTitle, systemImage: "arrow.up.circle.fill") }128                    .buttonStyle(.borderedProminent)129                    .controlSize(large ? .large : .regular)130                    .disabled(text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || isBusy)131                    .help("\(actionTitle) (Return). Shift-Return adds a line.")132            }133        }134        .padding(large ? 16 : 10)135        .background(.background, in: RoundedRectangle(cornerRadius: large ? 16 : 12))136        .overlay(RoundedRectangle(cornerRadius: large ? 16 : 12).strokeBorder(focused ? Color.accentColor.opacity(0.7) : Color.secondary.opacity(0.3), lineWidth: focused ? 1.5 : 1))137        .onAppear { if large { focused = true } }138    }139140    private func submit() {141        guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, !isBusy else { return }142        onSubmit()143    }144}145146struct WelcomeView: View {147    @Bindable var model: StudioModel148    @Environment(\.openSettings) private var openSettings149150    var body: some View {151        @Bindable var model = model152        ScrollView {153            VStack(spacing: 22) {154                VStack(spacing: 6) {155                    Image(systemName: "triangle.fill").font(.system(size: 34)).foregroundStyle(LinearGradient(colors: [.cyan, .indigo, .pink], startPoint: .topLeading, endPoint: .bottomTrailing)).accessibilityHidden(true)156                    Text("What do you want to build?").font(.largeTitle.bold())157                    Text("Describe it. Build it. Use it. Evolve it.").font(.title3).foregroundStyle(.secondary)158                }159                .padding(.top, 36)160161                Composer(text: $model.welcomeDraft, placeholder: "Describe the app you need…", actionTitle: "Build", large: true) {162                    let text = model.welcomeDraft163                    model.welcomeDraft = ""164                    model.createProject(prompt: text)165                }166                .frame(maxWidth: 720)167168                if !model.keyStatus.canBuild {169                    HStack(spacing: 8) {170                        Image(systemName: "info.circle").foregroundStyle(.orange)171                        Text("Claude is not connected. Your request will be saved; add an API key to build it.")172                        Button("Open Settings") { openSettings() }173                    }174                    .font(.callout)175                }176177                VStack(alignment: .leading, spacing: 8) {178                    Text("Try one of these").font(.headline)179                    ForEach(StudioModel.suggestedPrompts, id: \.self) { suggestion in180                        Button { model.welcomeDraft = suggestion } label: {181                            HStack {182                                Image(systemName: "sparkles").foregroundStyle(.tint)183                                Text(suggestion).multilineTextAlignment(.leading)184                                Spacer(minLength: 0)185                            }186                            .padding(10)187                            .background(.quaternary.opacity(0.4), in: RoundedRectangle(cornerRadius: 10))188                            .contentShape(Rectangle())189                        }190                        .buttonStyle(.plain)191                        .accessibilityHint("Puts this request in the composer")192                    }193                }194                .frame(maxWidth: 720, alignment: .leading)195196                if !model.templateNames.isEmpty {197                    VStack(alignment: .leading, spacing: 8) {198                        Text("Start from a recipe").font(.headline)199                        Text("Verified apps assembled by hand from the catalog. No Claude call, works offline.").font(.callout).foregroundStyle(.secondary)200                        HStack {201                            ForEach(model.templateNames, id: \.self) { name in202                                Button { model.createFromTemplate(name) } label: { Label(name.replacingOccurrences(of: "-", with: " ").capitalized, systemImage: "doc.on.doc") }203                            }204                        }205                    }206                    .frame(maxWidth: 720, alignment: .leading)207                }208209                if !model.projects.isEmpty {210                    VStack(alignment: .leading, spacing: 8) {211                        Text("Recent").font(.headline)212                        ForEach(model.projects.prefix(5)) { project in213                            Button { model.selection = .project(project.id) } label: {214                                HStack {215                                    Image(systemName: project.status == "draft" ? "square.dashed" : "app.fill").foregroundStyle(.secondary)216                                    Text(project.name)217                                    Spacer()218                                    Text(project.updatedAt, format: .relative(presentation: .named)).foregroundStyle(.secondary).font(.callout)219                                }220                                .contentShape(Rectangle())221                            }222                            .buttonStyle(.plain)223                        }224                    }225                    .frame(maxWidth: 720, alignment: .leading)226                }227            }228            .padding(.horizontal, 32).padding(.bottom, 40)229            .frame(maxWidth: .infinity)230        }231        .navigationTitle("New App")232    }233}234235struct CommandPalette: View {236    @Bindable var model: StudioModel237    @Binding var isPresented: Bool238    @State private var query = ""239    @FocusState private var focused: Bool240    @Environment(\.openSettings) private var openSettings241242    struct Entry: Identifiable {243        let id: String244        let title: String245        let symbol: String246        let run: @MainActor () -> Void247    }248249    private var entries: [Entry] {250        var out: [Entry] = [251            Entry(id: "new", title: "New App", symbol: "plus.square.on.square") { model.selection = .welcome },252            Entry(id: "library", title: "Open Library", symbol: "square.grid.2x2") { model.selection = .library },253            Entry(id: "monitor", title: "Open Monitor", symbol: "gauge.with.dots.needle.33percent") { model.selection = .monitor },254            Entry(id: "settings", title: "Open Settings", symbol: "gearshape") { openSettings() },255        ]256        if case .project(let id) = model.selection, let session = model.session(for: id), !session.isDraft {257            for mode in StudioMode.allCases { out.append(Entry(id: "mode." + mode.rawValue, title: "Switch to \(mode.title)", symbol: mode.symbol) { session.mode = mode }) }258        }259        for p in model.projects { out.append(Entry(id: "p." + p.id, title: "Open “\(p.name)”", symbol: "app") { model.selection = .project(p.id) }) }260        let q = query.trimmingCharacters(in: .whitespaces).lowercased()261        return q.isEmpty ? out : out.filter { $0.title.lowercased().contains(q) }262    }263264    var body: some View {265        VStack(spacing: 0) {266            TextField("Type a command or an app name", text: $query)267                .textFieldStyle(.plain).font(.title3).padding(14).focused($focused)268                .onSubmit { if let first = entries.first { first.run(); isPresented = false } }269            Divider()270            List(entries) { entry in271                Button { entry.run(); isPresented = false } label: { Label(entry.title, systemImage: entry.symbol).frame(maxWidth: .infinity, alignment: .leading).contentShape(Rectangle()) }272                    .buttonStyle(.plain)273            }274            .listStyle(.inset)275        }276        .frame(width: 520, height: 360)277        .onAppear { focused = true }278        .onExitCommand { isPresented = false }279    }280}281
Files main
Menu

Theme