Add signal-flow pedalboard builder UI
Interactive builder views in LyrebirdShoeGazeUI: - PedalPaletteView: vertical sidebar listing every pedal (tap to append to the main chain, drag "add:<type>" onto a lane) plus routing actions (Stereo Split / Parallel Split / Merge to Mono). - BoardFlowView: left-to-right signal-flow board driven by board.orderedSegments. Pedal cards joined by pink connector lines (double for stereo width); split regions render as two stacked L/R (A/B) lanes you fill independently by dropping pedals; per-card trash, drag-to-reorder within a lane, region route/mode controls, and a region trash. Reads shoeGaze.boards[index] fresh each body (index is the stable anchor across rebuilds). - ShoeGazeBuilderView: sidebar + vertically-centered flow board, with transport, input picker, board picker, and JSON save/load (NSSavePanel / NSOpenPanel on macOS, fileExporter/Importer on iOS via RigFileDocument). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,359 @@
|
||||
// ShoeGazeBuilderView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(AppKit)
|
||||
import AppKit
|
||||
#endif
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
import LyrebirdSynth
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// A self-contained pedalboard builder: browse all pedals, build a board by adding / removing /
|
||||
/// reordering pedals, tweak live controls, choose the input source, start the chain, and save or
|
||||
/// load a board setup.
|
||||
///
|
||||
/// This view ties together ``PedalPaletteView`` (the catalog) and ``BoardBuilderView`` (the
|
||||
/// editable chain) with a transport bar, an input-source picker, a board selector, and save/load.
|
||||
/// It is the shared screen hosted by both the macOS and iOS builder app shells.
|
||||
///
|
||||
/// Save/load uses an `NSSavePanel`/`NSOpenPanel` on macOS and `.fileExporter`/`.fileImporter` on
|
||||
/// iOS, both reading/writing the JSON produced by `ShoeGaze.rigData()` / consumed by
|
||||
/// `loadRig(from:)`. Loading and input-source changes stop the engine first (they rebuild the
|
||||
/// graph) and restart it if it was running.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// ShoeGazeBuilderView(shoeGaze: shoeGaze)
|
||||
/// ```
|
||||
public struct ShoeGazeBuilderView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let outerPadding: CGFloat = 16
|
||||
private static let sectionSpacing: CGFloat = 12
|
||||
private static let rowSpacing: CGFloat = 10
|
||||
private static let dividerOpacity: Double = 0.12
|
||||
private static let labelOpacity: Double = 0.65
|
||||
private static let minWidth: CGFloat = 720
|
||||
private static let minHeight: CGFloat = 460
|
||||
private static let sidebarWidth: CGFloat = 168
|
||||
private static let columnSpacing: CGFloat = 14
|
||||
private static let backgroundRed: Double = 0.08
|
||||
private static let backgroundGreen: Double = 0.07
|
||||
private static let backgroundBlue: Double = 0.06
|
||||
private static let testLoopTag: Int = -1
|
||||
private static let defaultRigFileName: String = "ShoeGazeRig"
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let shoeGaze: ShoeGaze
|
||||
|
||||
/// Index of the board currently being edited.
|
||||
@State private var selectedBoardIndex: Int = 0
|
||||
|
||||
/// Bumped on transport/edit actions to force a re-read of `engine.isRunning`.
|
||||
@State private var transportTick: Bool = false
|
||||
|
||||
/// iOS file-dialog presentation flags + the document to export.
|
||||
@State private var showImporter: Bool = false
|
||||
@State private var showExporter: Bool = false
|
||||
@State private var exportDocument: RigFileDocument = RigFileDocument(data: Data())
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a builder view.
|
||||
///
|
||||
/// - Parameter shoeGaze: The `ShoeGaze` manager whose boards and engine are edited. The
|
||||
/// caller should create at least one board before presenting this view.
|
||||
public init(shoeGaze: ShoeGaze) {
|
||||
self.shoeGaze = shoeGaze
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
VStack(alignment: .leading, spacing: ShoeGazeBuilderView.sectionSpacing) {
|
||||
transportRow
|
||||
ioRow
|
||||
Divider().overlay(Color.white.opacity(ShoeGazeBuilderView.dividerOpacity))
|
||||
HStack(alignment: .top, spacing: ShoeGazeBuilderView.columnSpacing) {
|
||||
paletteSidebar
|
||||
Divider().overlay(Color.white.opacity(ShoeGazeBuilderView.dividerOpacity))
|
||||
boardArea
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
LyrebirdStatusView(engine: shoeGaze.engine)
|
||||
}
|
||||
.padding(ShoeGazeBuilderView.outerPadding)
|
||||
.frame(minWidth: ShoeGazeBuilderView.minWidth, minHeight: ShoeGazeBuilderView.minHeight)
|
||||
.background(
|
||||
Color(
|
||||
red: ShoeGazeBuilderView.backgroundRed,
|
||||
green: ShoeGazeBuilderView.backgroundGreen,
|
||||
blue: ShoeGazeBuilderView.backgroundBlue
|
||||
)
|
||||
)
|
||||
#if !os(macOS)
|
||||
.fileImporter(isPresented: $showImporter, allowedContentTypes: [.json]) { result in
|
||||
handleImport(result)
|
||||
}
|
||||
.fileExporter(
|
||||
isPresented: $showExporter,
|
||||
document: exportDocument,
|
||||
contentType: .json,
|
||||
defaultFilename: ShoeGazeBuilderView.defaultRigFileName
|
||||
) { result in
|
||||
if case .failure(let error) = result {
|
||||
print("[ShoeGazeBuilderView] export failed: \(error)")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// MARK: - Transport row
|
||||
|
||||
private var transportRow: some View {
|
||||
_ = transportTick
|
||||
let isRunning: Bool = shoeGaze.engine.isRunning
|
||||
return HStack(spacing: ShoeGazeBuilderView.rowSpacing) {
|
||||
Text("ShoeGaze Builder")
|
||||
.font(.title2).bold()
|
||||
.foregroundStyle(Color.white)
|
||||
Spacer()
|
||||
Button("Start") {
|
||||
do { try shoeGaze.engine.start() }
|
||||
catch { print("[ShoeGazeBuilderView] engine.start() failed: \(error)") }
|
||||
transportTick.toggle()
|
||||
}
|
||||
.buttonStyle(.borderedProminent).tint(.green).disabled(isRunning)
|
||||
|
||||
Button("Stop") {
|
||||
shoeGaze.engine.stop()
|
||||
transportTick.toggle()
|
||||
}
|
||||
.buttonStyle(.bordered).tint(.red).disabled(!isRunning)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Input + board + save/load row
|
||||
|
||||
private var ioRow: some View {
|
||||
HStack(spacing: ShoeGazeBuilderView.rowSpacing) {
|
||||
Text("Input:")
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Color.white.opacity(ShoeGazeBuilderView.labelOpacity))
|
||||
Picker("Input", selection: inputBinding) {
|
||||
Text("Test Loop").tag(ShoeGazeBuilderView.testLoopTag)
|
||||
ForEach(0..<shoeGaze.engine.numInputChannels, id: \.self) { channel in
|
||||
Text("In \(channel)").tag(channel)
|
||||
}
|
||||
}
|
||||
.pickerStyle(.menu)
|
||||
.foregroundStyle(Color.white)
|
||||
|
||||
Text("Board:")
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Color.white.opacity(ShoeGazeBuilderView.labelOpacity))
|
||||
Picker("Board", selection: $selectedBoardIndex) {
|
||||
ForEach(Array(shoeGaze.boards.enumerated()), id: \.offset) { pair in
|
||||
let label: String = pair.element.label.isEmpty
|
||||
? "Board \(pair.offset + 1)"
|
||||
: pair.element.label
|
||||
Text(label).tag(pair.offset)
|
||||
}
|
||||
}
|
||||
.pickerStyle(.menu)
|
||||
.foregroundStyle(Color.white)
|
||||
|
||||
Button("Add Board") { addBoard() }
|
||||
.buttonStyle(.bordered).tint(.orange)
|
||||
|
||||
Spacer()
|
||||
|
||||
Button("Save…") { saveAction() }
|
||||
.buttonStyle(.bordered).tint(.blue)
|
||||
Button("Load…") { loadAction() }
|
||||
.buttonStyle(.bordered).tint(.blue)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Palette sidebar
|
||||
|
||||
private var paletteSidebar: some View {
|
||||
ScrollView(.vertical, showsIndicators: true) {
|
||||
PedalPaletteView(
|
||||
onAdd: { type in addPedal(type: type) },
|
||||
onRouting: { action in performRouting(action) }
|
||||
)
|
||||
.padding(.trailing, 2)
|
||||
}
|
||||
.frame(width: ShoeGazeBuilderView.sidebarWidth)
|
||||
}
|
||||
|
||||
// MARK: - Board area
|
||||
|
||||
private var boardArea: some View {
|
||||
// Vertically center the signal-flow board in the available space.
|
||||
VStack(spacing: 0) {
|
||||
Spacer(minLength: 0)
|
||||
BoardFlowView(shoeGaze: shoeGaze, boardIndex: clampedBoardIndex)
|
||||
Spacer(minLength: 0)
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
|
||||
/// Performs a routing action from the palette against the selected board.
|
||||
private func performRouting(_ action: PedalPaletteView.RoutingAction) {
|
||||
guard !shoeGaze.boards.isEmpty else { return }
|
||||
let board: PedalBoard = shoeGaze.boards[clampedBoardIndex]
|
||||
do {
|
||||
switch action {
|
||||
case .stereoSplit: _ = try shoeGaze.appendStereoSplit(to: board)
|
||||
case .parallelSplit: _ = try shoeGaze.appendParallelSplit(to: board)
|
||||
case .mergeToMono: _ = try shoeGaze.appendMergeToMono(to: board)
|
||||
case .chainRouter: break // multi-board routing; not handled in the single-board builder
|
||||
}
|
||||
} catch {
|
||||
print("[ShoeGazeBuilderView] routing \(action) failed: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
/// `selectedBoardIndex` clamped to a valid board (or `0`); guards against deletes/loads that
|
||||
/// change the board count out from under the picker.
|
||||
private var clampedBoardIndex: Int {
|
||||
guard !shoeGaze.boards.isEmpty else { return 0 }
|
||||
return min(max(selectedBoardIndex, 0), shoeGaze.boards.count - 1)
|
||||
}
|
||||
|
||||
// MARK: - Input binding
|
||||
|
||||
private var inputBinding: Binding<Int> {
|
||||
Binding(
|
||||
get: {
|
||||
switch shoeGaze.inputSource {
|
||||
case .testLoop: return ShoeGazeBuilderView.testLoopTag
|
||||
case .liveInput(let channel): return channel
|
||||
}
|
||||
},
|
||||
set: { newValue in
|
||||
let source: InputSource = newValue == ShoeGazeBuilderView.testLoopTag
|
||||
? .testLoop
|
||||
: .liveInput(channel: newValue)
|
||||
switchInput(to: source)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
private func addPedal(type: String) {
|
||||
guard !shoeGaze.boards.isEmpty else { return }
|
||||
let board: PedalBoard = shoeGaze.boards[clampedBoardIndex]
|
||||
do {
|
||||
_ = try shoeGaze.appendPedal(type: type, to: board)
|
||||
} catch {
|
||||
print("[ShoeGazeBuilderView] appendPedal failed: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
private func addBoard() {
|
||||
var created: PedalBoard?
|
||||
do {
|
||||
let board: PedalBoard = try shoeGaze.addBoard()
|
||||
created = board
|
||||
try shoeGaze.finalizeBoard(board)
|
||||
selectedBoardIndex = shoeGaze.boards.count - 1
|
||||
} catch {
|
||||
if let board: PedalBoard = created { shoeGaze.removeBoard(board) }
|
||||
print("[ShoeGazeBuilderView] addBoard failed: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Switches input source, stopping/restarting the engine around the graph rebuild.
|
||||
private func switchInput(to source: InputSource) {
|
||||
let wasRunning: Bool = shoeGaze.engine.isRunning
|
||||
if wasRunning { shoeGaze.engine.stop() }
|
||||
do {
|
||||
try shoeGaze.setInputSource(source)
|
||||
} catch {
|
||||
print("[ShoeGazeBuilderView] setInputSource failed: \(error)")
|
||||
}
|
||||
if wasRunning {
|
||||
try? shoeGaze.engine.start()
|
||||
}
|
||||
transportTick.toggle()
|
||||
}
|
||||
|
||||
// MARK: - Save / Load
|
||||
|
||||
private func saveAction() {
|
||||
#if os(macOS)
|
||||
let panel: NSSavePanel = NSSavePanel()
|
||||
panel.allowedContentTypes = [.json]
|
||||
panel.nameFieldStringValue = "\(ShoeGazeBuilderView.defaultRigFileName).json"
|
||||
panel.canCreateDirectories = true
|
||||
panel.begin { response in
|
||||
guard response == .OK, let url: URL = panel.url else { return }
|
||||
do { try shoeGaze.saveRig(to: url) }
|
||||
catch { print("[ShoeGazeBuilderView] saveRig failed: \(error)") }
|
||||
}
|
||||
#else
|
||||
do {
|
||||
exportDocument = RigFileDocument(data: try shoeGaze.rigData())
|
||||
showExporter = true
|
||||
} catch {
|
||||
print("[ShoeGazeBuilderView] rigData failed: \(error)")
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private func loadAction() {
|
||||
#if os(macOS)
|
||||
let panel: NSOpenPanel = NSOpenPanel()
|
||||
panel.allowedContentTypes = [.json]
|
||||
panel.canChooseDirectories = false
|
||||
panel.allowsMultipleSelection = false
|
||||
panel.begin { response in
|
||||
guard response == .OK, let url: URL = panel.url else { return }
|
||||
loadRig(from: url)
|
||||
}
|
||||
#else
|
||||
showImporter = true
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Loads a rig from a URL, stopping/restarting the engine around the rebuild and resetting the
|
||||
/// board selection to a valid index (load replaces all boards).
|
||||
private func loadRig(from url: URL) {
|
||||
let wasRunning: Bool = shoeGaze.engine.isRunning
|
||||
if wasRunning { shoeGaze.engine.stop() }
|
||||
do {
|
||||
try shoeGaze.loadRig(from: url)
|
||||
selectedBoardIndex = 0
|
||||
} catch {
|
||||
print("[ShoeGazeBuilderView] loadRig failed: \(error)")
|
||||
}
|
||||
if wasRunning { try? shoeGaze.engine.start() }
|
||||
transportTick.toggle()
|
||||
}
|
||||
|
||||
#if !os(macOS)
|
||||
private func handleImport(_ result: Result<URL, Error>) {
|
||||
switch result {
|
||||
case .success(let url):
|
||||
// A `.fileImporter` URL is security-scoped on iOS; access must be started to read it.
|
||||
let scoped: Bool = url.startAccessingSecurityScopedResource()
|
||||
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
|
||||
loadRig(from: url)
|
||||
case .failure(let error):
|
||||
print("[ShoeGazeBuilderView] import failed: \(error)")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user