From ef20a0eb95aa2035e4ac7aa81e810a39f4f85475 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sun, 28 Jun 2026 18:11:44 -0700 Subject: [PATCH] Open .shoegaze files: wire document handlers, scope type to builders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Registering the document type made the apps claim .shoegaze, but without an open handler macOS reported "cannot open files in the ShoeGaze Document format." Wire actual opening: - macOS Builder: implement application(_:open:) — decode the document and import its rigs into the library (board files get an explanatory note), with a confirmation alert. Also migrate the File-menu Export/Import panels to .shoegaze (still importing legacy JSON). - iOS Builder: add .onOpenURL and an importDocument(at:) on the app model. - Drop the document-type declaration from the two minimal player demos (ShoeGazeDemo, ShoeGazeiOSDemo); they have no open/share UI, so only the two Builder apps should claim the type. Co-Authored-By: Claude Opus 4.8 --- Demo/ShoeGazeBuilder/AppDelegate.swift | 65 +++++++++++++++++-- .../ShoeGazeBuilderiOSApp.swift | 1 + .../ShoeGazeBuilderiOSAppModel.swift | 19 ++++++ Demo/ShoeGazeDemo/Info.plist | 39 ----------- Demo/ShoeGazeiOSDemo/Info.plist | 39 ----------- 5 files changed, 79 insertions(+), 84 deletions(-) diff --git a/Demo/ShoeGazeBuilder/AppDelegate.swift b/Demo/ShoeGazeBuilder/AppDelegate.swift index d7be091..2d61830 100644 --- a/Demo/ShoeGazeBuilder/AppDelegate.swift +++ b/Demo/ShoeGazeBuilder/AppDelegate.swift @@ -154,6 +154,57 @@ final class AppDelegate: NSObject, NSApplicationDelegate { true } + /// Opens `.shoegaze` files handed to the app by Finder, AirDrop, "Open With", or a double-click. + /// + /// Rig documents are imported into the library (they appear in the sidebar, ready to load); + /// board documents are surfaced with a note that boards are opened from a rig's board editor. + func application(_ application: NSApplication, open urls: [URL]) { + for url: URL in urls { + openShoeGazeDocument(at: url) + } + } + + /// Reads and imports a single `.shoegaze` document, reporting the outcome to the user. + private func openShoeGazeDocument(at url: URL) { + // A LaunchServices-supplied URL is security-scoped; start access to read it. + let scoped: Bool = url.startAccessingSecurityScopedResource() + defer { if scoped { url.stopAccessingSecurityScopedResource() } } + do { + let data: Data = try Data(contentsOf: url) + let document: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data) + if document.rigEntries?.isEmpty == false { + let added: Int = try library.importArchive(from: data) + presentOpenInfo( + title: added == 1 ? "Rig Imported" : "Rigs Imported", + message: added == 1 + ? "Added 1 rig to your library. Select it in the Rigs sidebar to load it." + : "Added \(added) rigs to your library. Select one in the Rigs sidebar to load it." + ) + } else if document.boardVersion != nil { + presentOpenInfo( + title: "Board Document", + message: "This file is a single board. Open a rig, then load the board from the " + + "board editor's Versions menu." + ) + } + } catch { + presentOpenInfo( + title: "Couldn’t Open File", + message: "“\(url.lastPathComponent)” is not a readable ShoeGaze document." + ) + print("[ShoeGazeBuilder] open document failed: \(error)") + } + } + + /// Shows a one-button informational alert for a document-open outcome. + private func presentOpenInfo(title: String, message: String) { + let alert: NSAlert = NSAlert() + alert.messageText = title + alert.informativeText = message + alert.addButton(withTitle: "OK") + alert.runModal() + } + func applicationWillTerminate(_ notification: Notification) { RigStore.save(shoeGaze) // Ensure any in-flight background rig-library writes finish before exit. @@ -267,20 +318,22 @@ final class AppDelegate: NSObject, NSApplicationDelegate { @objc private func exportArchiveAction(_ sender: Any?) { let panel: NSSavePanel = NSSavePanel() - panel.allowedContentTypes = [.json] - panel.nameFieldStringValue = "\(AppDelegate.defaultArchiveFileName).json" + panel.allowedContentTypes = [.shoeGaze] + panel.nameFieldStringValue = + "\(AppDelegate.defaultArchiveFileName).\(ShoeGazeDocument.fileExtension)" panel.canCreateDirectories = true let rigLibrary: RigLibraryStore = library panel.begin { response in guard response == .OK, let url: URL = panel.url else { return } - do { try rigLibrary.exportArchiveData().write(to: url, options: .atomic) } - catch { print("[ShoeGazeBuilder] export archive failed: \(error)") } + do { try rigLibrary.exportDocumentData().write(to: url, options: .atomic) } + catch { print("[ShoeGazeBuilder] export library failed: \(error)") } } } @objc private func importArchiveAction(_ sender: Any?) { let panel: NSOpenPanel = NSOpenPanel() - panel.allowedContentTypes = [.json] + // Accept the .shoegaze document plus legacy plain-JSON archives. + panel.allowedContentTypes = [.shoeGaze, .json] panel.canChooseDirectories = false panel.allowsMultipleSelection = false let rigLibrary: RigLibraryStore = library @@ -290,7 +343,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate { let data: Data = try Data(contentsOf: url) _ = try rigLibrary.importArchive(from: data) } catch { - print("[ShoeGazeBuilder] import archive failed: \(error)") + print("[ShoeGazeBuilder] import library failed: \(error)") } } } diff --git a/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSApp.swift b/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSApp.swift index e3b2479..2fadaf9 100644 --- a/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSApp.swift +++ b/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSApp.swift @@ -35,6 +35,7 @@ struct ShoeGazeBuilderiOSApp: App { .task { await model.startLibrary() } } .onDisappear { model.stop() } + .onOpenURL { url in model.importDocument(at: url) } } .onChange(of: scenePhase) { _, phase in // Persist the rig whenever the app leaves the foreground. diff --git a/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSAppModel.swift b/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSAppModel.swift index 2899ef4..644b65f 100644 --- a/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSAppModel.swift +++ b/Demo/ShoeGazeBuilderiOS/ShoeGazeBuilderiOSAppModel.swift @@ -93,6 +93,25 @@ final class ShoeGazeBuilderiOSAppModel { library.flushPendingWrites() } + // MARK: - Document open + + /// Imports a `.shoegaze` file opened from Files, AirDrop, or another app into the rig library. + /// Rig documents add their rigs (they appear in the sidebar, ready to load); board documents are + /// ignored here. Returns the number of rigs added. + @discardableResult + func importDocument(at url: URL) -> Int { + // A document opened from outside the sandbox is security-scoped; start access to read it. + let scoped: Bool = url.startAccessingSecurityScopedResource() + defer { if scoped { url.stopAccessingSecurityScopedResource() } } + do { + let data: Data = try Data(contentsOf: url) + return try library.importArchive(from: data) + } catch { + print("[ShoeGazeBuilderiOS] open document failed: \(error)") + return 0 + } + } + // MARK: - Audio session / engine construction /// Activates the play-and-record audio session once. Idempotent; safe to call before each diff --git a/Demo/ShoeGazeDemo/Info.plist b/Demo/ShoeGazeDemo/Info.plist index 61361cd..d23e0ba 100644 --- a/Demo/ShoeGazeDemo/Info.plist +++ b/Demo/ShoeGazeDemo/Info.plist @@ -28,44 +28,5 @@ ShoeGazeDemo can route a hardware input channel through your pedalboard. Enable microphone access to use Live Input mode. NSPrincipalClass NSApplication - CFBundleDocumentTypes - - - CFBundleTypeName - ShoeGaze Document - CFBundleTypeRole - Editor - LSHandlerRank - Owner - LSItemContentTypes - - net.realizedsound.shoegaze - - - - UTExportedTypeDeclarations - - - UTTypeIdentifier - net.realizedsound.shoegaze - UTTypeDescription - ShoeGaze Document - UTTypeConformsTo - - public.json - - UTTypeTagSpecification - - public.filename-extension - - shoegaze - - public.mime-type - - application/json - - - - diff --git a/Demo/ShoeGazeiOSDemo/Info.plist b/Demo/ShoeGazeiOSDemo/Info.plist index e73fcd5..6723281 100644 --- a/Demo/ShoeGazeiOSDemo/Info.plist +++ b/Demo/ShoeGazeiOSDemo/Info.plist @@ -41,44 +41,5 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight - CFBundleDocumentTypes - - - CFBundleTypeName - ShoeGaze Document - CFBundleTypeRole - Editor - LSHandlerRank - Owner - LSItemContentTypes - - net.realizedsound.shoegaze - - - - UTExportedTypeDeclarations - - - UTTypeIdentifier - net.realizedsound.shoegaze - UTTypeDescription - ShoeGaze Document - UTTypeConformsTo - - public.json - - UTTypeTagSpecification - - public.filename-extension - - shoegaze - - public.mime-type - - application/json - - - -