9a8e5461c0
Move the remaining synchronous main-thread I/O off the main actor: - RigLibraryStore now constructs instantly with a local placeholder and exposes an awaitable async start(): it resolves the iCloud-or-local location off-main (the blocking ubiquity lookup), loads entries off-main via the shared ioQueue (so a reload can't drop a just-saved rig), then sets isLoaded. reload() is async; iCloud metadata updates re-read via it. - Seeding moves into RigLibraryBootstrap.start(_:seedFrom:), which seeds a "Default" rig only AFTER the (possibly iCloud) library has loaded and is still empty — preventing a duplicate "Default" on every synced device on first launch. - Sidebar shows a Loading state until isLoaded; the builder restores the persisted current-rig highlight when isLoaded flips true (initialRigID param dropped). - Shells construct the store synchronously and start it asynchronously (macOS via Task in didFinishLaunching; iOS via .task). Codec + file helpers are nonisolated static so background work needs no actor hop. - Tests await store.start(). Full suite: 206 passing. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
168 lines
6.2 KiB
Swift
168 lines
6.2 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) }
|
|
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)
|
|
}
|
|
}
|