Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift
T
josh 9e09eb284a Add .shoegaze document type and rig/board sharing
Introduce a single shareable document type for rigs and boards:

- ShoeGazeDocument: versioned JSON envelope wrapping either a RigArchive
  (one or many rigs) or a BoardVersion, with a tolerant decoder that also
  reads the legacy bare-RigArchive and bare-ShoeGazeState shapes.
- Register the .shoegaze UTType (net.realizedsound.shoegaze, conforms to
  public.json) in all four demo app Info.plists.
- Migrate library export/import onto the envelope: exportDocumentData() and
  a decoder-tolerant importArchive(); RigFileDocument and the Builder
  exporter/importer now use .shoegaze. Internal per-rig and board-version
  files stay app-private JSON.
- Add OS share-sheet entries via SwiftUI ShareLink: Share per rig in the
  library sidebar, Share per version in the board editor, backed by the
  Transferable wrappers RigShareItem / BoardShareItem.

Tests: ShoeGazeDocument round-trip and legacy decode, library .shoegaze
export/import round-trip, and a board-document-imports-no-rigs case.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 17:49:20 -07:00

178 lines
6.7 KiB
Swift

// RigLibrarySidebarView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
/// The rig library: a persistent left-hand listing of every saved rig, with controls to start a
/// new rig, save / save-as, duplicate, rename, delete, erase all, and import / export an archive.
///
/// Selecting a rig replaces the current working session (the host stops the engine, rebuilds the
/// graph, and restarts). This view is purely presentational — every action is delegated to the
/// host (`ShoeGazeBuilderView`) via closures so engine lifecycle and naming prompts stay in one
/// place.
struct RigLibrarySidebarView: View {
// MARK: - Layout constants
private static let width: CGFloat = 200
private static let rowSpacing: CGFloat = 4
private static let rowPadding: CGFloat = 8
private static let rowCorner: CGFloat = 6
private static let selectedOpacity: Double = 0.22
private static let subtitleOpacity: Double = 0.5
private static let headerOpacity: Double = 0.65
private static let emptyOpacity: Double = 0.4
// MARK: - Inputs
let store: RigLibraryStore
/// The library entry currently loaded into the session, or `nil` for an unsaved/new rig.
@Binding var currentRigID: UUID?
let onSelect: (RigEntry) -> Void
let onNew: () -> Void
let onSave: () -> Void
let onSaveAs: () -> Void
let onDuplicate: (RigEntry) -> Void
let onRename: (RigEntry) -> Void
let onDelete: (RigEntry) -> Void
let onEraseAll: () -> Void
let onImportArchive: () -> Void
let onExportArchive: () -> Void
private static let timestampFormat: Date.FormatStyle =
Date.FormatStyle(date: .abbreviated, time: .shortened)
// MARK: - Body
var body: some View {
VStack(alignment: .leading, spacing: RigLibrarySidebarView.rowSpacing) {
header
Divider().overlay(Color.white.opacity(0.12))
rigList
Divider().overlay(Color.white.opacity(0.12))
actionBar
}
.frame(width: RigLibrarySidebarView.width)
}
// MARK: - Header
private var header: some View {
HStack(spacing: 6) {
Text("Rigs")
.font(.headline)
.foregroundStyle(Color.white)
Spacer()
// Cloud icon when the library is iCloud-backed; a slash when local-only.
Image(systemName: store.isICloud ? "icloud" : "icloud.slash")
.font(.caption)
.foregroundStyle(Color.white.opacity(RigLibrarySidebarView.headerOpacity))
.help(store.isICloud
? "Synced with iCloud across your devices"
: "iCloud unavailable — rigs are stored on this device only")
}
}
// MARK: - List
private var rigList: some View {
ScrollView(.vertical, showsIndicators: true) {
LazyVStack(alignment: .leading, spacing: RigLibrarySidebarView.rowSpacing) {
if !store.isLoaded {
HStack(spacing: 6) {
ProgressView().controlSize(.small)
Text("Loading…")
.font(.caption)
.foregroundStyle(Color.white.opacity(RigLibrarySidebarView.emptyOpacity))
}
.padding(.vertical, RigLibrarySidebarView.rowPadding)
} else if store.entries.isEmpty {
Text("No saved rigs yet.\nTap New or Save to add one.")
.font(.caption)
.foregroundStyle(Color.white.opacity(RigLibrarySidebarView.emptyOpacity))
.padding(.vertical, RigLibrarySidebarView.rowPadding)
}
ForEach(store.entries) { entry in
rigRow(entry)
}
}
}
.frame(maxHeight: .infinity)
}
private func rigRow(_ entry: RigEntry) -> some View {
let isSelected: Bool = entry.id == currentRigID
return Button {
onSelect(entry)
} label: {
VStack(alignment: .leading, spacing: 2) {
Text(entry.name)
.font(.subheadline)
.foregroundStyle(Color.white)
.lineLimit(1)
Text(entry.savedAt.formatted(RigLibrarySidebarView.timestampFormat))
.font(.caption2)
.foregroundStyle(Color.white.opacity(RigLibrarySidebarView.subtitleOpacity))
.lineLimit(1)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(RigLibrarySidebarView.rowPadding)
.background(
RoundedRectangle(cornerRadius: RigLibrarySidebarView.rowCorner)
.fill(Color.orange.opacity(isSelected ? RigLibrarySidebarView.selectedOpacity : 0))
)
}
.buttonStyle(.plain)
.contextMenu {
Button("Rename…") { onRename(entry) }
Button("Duplicate…") { onDuplicate(entry) }
// Encoding a fully-concrete RigEntry to JSON does not fail in practice; on the
// unreachable failure path the Share item is simply omitted.
if let shareItem: RigShareItem = try? RigShareItem(rig: entry) {
ShareLink(
item: shareItem,
preview: SharePreview(entry.name)
) {
Label("Share…", systemImage: "square.and.arrow.up")
}
}
Divider()
Button("Delete", role: .destructive) { onDelete(entry) }
}
}
// MARK: - Action bar
private var actionBar: some View {
HStack(spacing: 6) {
Button { onNew() } label: { Image(systemName: "plus") }
.help("New rig")
Button { onSave() } label: { Image(systemName: "square.and.arrow.down") }
.help("Save the current rig")
Button { onSaveAs() } label: { Image(systemName: "doc.on.doc") }
.help("Save the current rig as a new entry")
Spacer()
Menu {
Button("Import Archive…") { onImportArchive() }
Button("Export Archive…") { onExportArchive() }
Divider()
Button("Erase All Rigs…", role: .destructive) { onEraseAll() }
} label: {
Image(systemName: "ellipsis.circle")
}
.menuStyle(.borderlessButton)
.fixedSize()
.help("Archive and library options")
}
.buttonStyle(.bordered)
.tint(.orange)
.padding(.top, 2)
}
}