Repository Public License
1import AppKit2import PrismRenderer3import PrismSecurity4import SwiftUI56final class StudioDelegate: NSObject, NSApplicationDelegate {7 var onTerminate: (() -> Void)?8 func applicationWillTerminate(_ notification: Notification) { onTerminate?() }9 func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false }10}1112/// Finds resources shipped inside PRISM.app, falling back to the repository layout when run with `swift run`.13enum StudioResources {14 static func locate(_ name: String) -> URL? {15 var candidates: [URL] = []16 if let r = Bundle.main.resourceURL { candidates.append(r.appendingPathComponent(name)) }17 var dir = URL(fileURLWithPath: CommandLine.arguments[0]).deletingLastPathComponent()18 for _ in 0..<7 {19 candidates.append(dir.appendingPathComponent(name))20 candidates.append(dir.appendingPathComponent("build/debug").appendingPathComponent(name == "SatelliteHost.app" ? "PrismSatelliteHost.app" : name))21 dir = dir.deletingLastPathComponent()22 }23 return candidates.first { FileManager.default.fileExists(atPath: $0.path) }24 }25}2627public struct PrismStudioApp: App {28 @NSApplicationDelegateAdaptor(StudioDelegate.self) private var delegate29 @State private var boot: Result<StudioModel, Error>30 @State private var palette = false3132 public init() {33 let dialogs = AppKitDialogs()34 _boot = State(initialValue: Result {35 if StudioSelfTest.isRequested { return try StudioSelfTest.makeModel() }36 return try StudioModel(hostTemplate: StudioResources.locate("SatelliteHost.app"), templatesDirectory: StudioResources.locate("templates"), dialogs: dialogs)37 })38 }3940 public var body: some Scene {41 WindowGroup("PRISM", id: "main") {42 switch boot {43 case .success(let model):44 RootView(model: model, showPalette: $palette)45 .frame(minWidth: 980, minHeight: 620)46 .onAppear { delegate.onTerminate = { MainActor.assumeIsolated { model.shutdown() } } }47 .task {48 if StudioSelfTest.isRequested { await StudioSelfTest.run(model: model) }49 else if model.keyStatus != .missing { await model.testConnection() }50 }51 case .failure(let error):52 ContentUnavailableView("PRISM could not open its library", systemImage: "externaldrive.badge.xmark", description: Text(error.localizedDescription))53 .frame(minWidth: 600, minHeight: 400)54 }55 }56 .defaultSize(width: 1280, height: 800)57 .commands {58 if case .success(let model) = boot { StudioCommands(model: model, showPalette: $palette) }59 }6061 Settings {62 if case .success(let model) = boot { SettingsView(model: model) }63 }64 }65}6667struct StudioCommands: Commands {68 @Bindable var model: StudioModel69 @Binding var showPalette: Bool7071 private var session: ProjectSession? {72 if case .project(let id) = model.selection { return model.session(for: id) }73 return nil74 }7576 var body: some Commands {77 CommandGroup(replacing: .newItem) {78 Button("New App") { model.selection = .welcome }.keyboardShortcut("n")79 Divider()80 Button("Import Project…") {81 if let url = model.dialogs?.chooseFileToOpen(allowedExtensions: ["prismproject", "json"]) { model.importProject(from: url) }82 }.keyboardShortcut("i", modifiers: [.command, .shift])83 Button("Export Project…") {84 guard let s = session, let url = model.dialogs?.chooseFileToSave(suggestedName: "\(s.record.name).prismproject") else { return }85 model.exportProject(s.id, to: url)86 }87 .keyboardShortcut("e", modifiers: [.command, .shift])88 .disabled(session?.blueprint == nil)89 }90 CommandGroup(replacing: .undoRedo) {91 Button(session?.history?.undoLabel.map { "Undo \($0)" } ?? "Undo") { session?.undo() }.keyboardShortcut("z").disabled(session?.canUndo != true)92 Button(session?.history?.redoLabel.map { "Redo \($0)" } ?? "Redo") { session?.redo() }.keyboardShortcut("z", modifiers: [.command, .shift]).disabled(session?.canRedo != true)93 }94 CommandMenu("Go") {95 Button("Command Palette…") { showPalette = true }.keyboardShortcut("k")96 Divider()97 ForEach(Array(StudioMode.allCases.enumerated()), id: \.element) { index, mode in98 Button(mode.title) { session?.mode = mode }99 .keyboardShortcut(KeyEquivalent(Character("\(index + 1)")))100 .disabled(session == nil || (session?.isDraft == true && mode != .create))101 }102 Divider()103 Button("Library") { model.selection = .library }.keyboardShortcut("l", modifiers: [.command, .shift])104 Button("Monitor") { model.selection = .monitor }.keyboardShortcut("m", modifiers: [.command, .shift])105 }106 CommandMenu("Build") {107 Button("Stop Build") { session?.cancelBuild() }.keyboardShortcut(".").disabled(session?.isBuilding != true)108 Button("Build Last Request Again") { session?.retryLastRequest() }109 .keyboardShortcut("r").disabled(session == nil || session?.isBuilding == true || !model.keyStatus.canBuild)110 }111 }112}113