Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift
T
josh 1893576718 Add MIDI foot-controller control; board/rig bypass; iOS fixes
Headline: MIDI control for a Behringer FCB1010 (four phases).
- MIDIController (Lyrebird MIDI client): connection indicator via source
  polling, background-queue->main-actor hop, MIDI Learn.
- Volume and Wah pedals (Wah reuses the SVF bandpass sweep, driven by
  pedal position instead of an envelope follower).
- CC -> any pedal knob (0-127 mapped across the knob range).
- Program Change -> pedal/board/rig toggles: software latch for booleans,
  exclusive radio for board solo. Positional addressing with display-name
  hints; stale/out-of-range bindings are skipped, not fired.
- Per-rig MIDIMap embedded in ShoeGazeState as an optional field (old rigs
  decode to nil; no preset version bump). Central MIDI Mapping sheet with
  Learn/Clear per control.

Also included (same working branch):
- Full board bypass and rig bypass (dry passthrough via cascaded
  crossfades in BoardOutputMaster), with UI toggles.
- Horizontal pedal picker grouped by category in filter order.
- Drag a palette pedal onto a chain card to insert before it
  (ShoeGaze.insertPedal); tap still appends.
- iOS: guard macOS-only .menuStyle(.borderlessButton) to silence the
  _UIReparentingView warning; iCloud-unavailable alert; keep the device
  awake while audio runs.

Tests: 24 added (Volume/Wah, MIDIMap Codable incl. legacy decode,
CC and PC controller behavior, insert/bypass API). Full suite green;
macOS and iOS app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 08:18:09 -07:00

198 lines
7.3 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
/// Invoked when the local-only (iCloud-unavailable) status icon is tapped, so the host can
/// explain how to re-enable iCloud sync. Defaults to a no-op.
var onShowCloudStatus: () -> 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()
cloudStatusIcon
}
}
/// Cloud icon when the library is iCloud-backed; a tappable slash when local-only (tap explains
/// how to re-enable sync).
@ViewBuilder
private var cloudStatusIcon: some View {
if store.isICloud {
Image(systemName: "icloud")
.font(.caption)
.foregroundStyle(Color.white.opacity(RigLibrarySidebarView.headerOpacity))
.help("Synced with iCloud across your devices")
} else {
Button { onShowCloudStatus() } label: {
Image(systemName: "icloud.slash")
.font(.caption)
.foregroundStyle(Color.white.opacity(RigLibrarySidebarView.headerOpacity))
}
.buttonStyle(.plain)
.help("iCloud unavailable — tap for details. 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")
}
#if os(macOS)
.menuStyle(.borderlessButton)
.fixedSize()
#endif
.help("Archive and library options")
}
.buttonStyle(.bordered)
.tint(.orange)
.padding(.top, 2)
}
}