Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/BoardsSheetView.swift
T
josh 4627b3b800 Boards sheet, ⌘1–9 switching, unified player bar, input persistence
Boards sheet (BoardsSheetView): rename, drag-reorder, swipe-delete, plus
per-board enable/solo/level and a Solo/Blend + crossfade header. Opened from a
"Boards…" button in the builder; presented with medium/large detents on iOS.
Makes ShoeGaze.moveBoard public to back the reorder.

Keyboard switching: hidden ⌘1…⌘9 buttons in the builder solo (live-switch to)
the matching board and select it as the edit target.

Unified player: ShoeGazeView drops its bespoke Active picker + crossfade field
and hosts the shared BoardBarView, matching the builder.

Input persistence: the builder saves the input choice (test loop vs live
channel) to UserDefaults and restores it on appear. With no saved choice it
defaults to live input when the mic is already authorised and an input channel
exists, else stays on the test loop. The iOS builder now opens one input
channel so live input is actually available there.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 20:38:43 -07:00

244 lines
8.5 KiB
Swift

// BoardsSheetView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdUI
import LyrebirdShoeGaze
/// A full board manager presented as a sheet — the richer companion to the inline
/// ``BoardBarView``.
///
/// Each board is a `List` row with an audible toggle, a Solo button, a master-gain fader, an
/// inline rename field, and an "Edit" action that selects it as the builder's edit target and
/// dismisses. Rows reorder by drag (`.onMove`) and delete by swipe (`.onDelete`). A header
/// pairs the **Solo / Blend** mode switch with the crossfade slider, mirroring the board bar.
///
/// Primarily used on iOS/iPadOS (presented with `.presentationDetents`) where the inline bar
/// has no room for rename / reorder, but the view is platform-neutral.
public struct BoardsSheetView: View {
// MARK: - Play mode
private enum PlayMode: Hashable {
case solo
case blend
}
// MARK: - Layout constants
private static let rowSpacing: CGFloat = 12
private static let faderWidth: CGFloat = 120
private static let soloButtonSize: CGFloat = 28
private static let ledDiameter: CGFloat = 14
private static let ledDimBrightness: Double = 0.16
private static let ledRimOpacity: Double = 0.30
private static let maxCrossfadeSeconds: Double = 10.0
private static let crossfadeStep: Double = 0.1
private static let headerSpacing: CGFloat = 10
// MARK: - Properties
private let shoeGaze: ShoeGaze
@Binding private var editingBoardIndex: Int
@Binding private var crossfadeSeconds: Double
private let onAddBoard: () -> Void
@Environment(\.dismiss) private var dismiss
@State private var mode: PlayMode = .solo
// MARK: - Init
/// Creates a board-manager sheet.
///
/// - Parameters:
/// - shoeGaze: The manager whose `boards` are edited.
/// - editingBoardIndex: The builder's edit target; a row's "Edit" action sets this.
/// - crossfadeSeconds: Crossfade duration applied to switches / toggles.
/// - onAddBoard: Called when the **Add Board** button is tapped.
public init(
shoeGaze: ShoeGaze,
editingBoardIndex: Binding<Int>,
crossfadeSeconds: Binding<Double>,
onAddBoard: @escaping () -> Void
) {
self.shoeGaze = shoeGaze
self._editingBoardIndex = editingBoardIndex
self._crossfadeSeconds = crossfadeSeconds
self.onAddBoard = onAddBoard
}
// MARK: - Body
public var body: some View {
NavigationStack {
VStack(spacing: BoardsSheetView.headerSpacing) {
header
List {
ForEach(Array(shoeGaze.boards.enumerated()), id: \.offset) { pair in
boardRow(index: pair.offset, board: pair.element)
}
.onMove(perform: moveBoards)
.onDelete(perform: deleteBoards)
}
}
.padding(.top, BoardsSheetView.headerSpacing)
.navigationTitle("Boards")
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") { dismiss() }
}
ToolbarItem(placement: .primaryAction) {
Button {
onAddBoard()
} label: {
Label("Add Board", systemImage: "plus")
}
}
}
}
}
// MARK: - Header
private var header: some View {
HStack(spacing: BoardsSheetView.headerSpacing) {
Picker("Mode", selection: $mode) {
Text("Solo").tag(PlayMode.solo)
Text("Blend").tag(PlayMode.blend)
}
.pickerStyle(.segmented)
.frame(maxWidth: 180)
Button("All") {
shoeGaze.selectAllBoards(crossfadeSeconds: clampedCrossfade)
}
.buttonStyle(.bordered)
Spacer()
Text("XFade")
Slider(
value: $crossfadeSeconds,
in: 0 ... BoardsSheetView.maxCrossfadeSeconds,
step: BoardsSheetView.crossfadeStep
)
.frame(width: BoardsSheetView.faderWidth)
Text(String(format: "%.1fs", clampedCrossfade))
.monospacedDigit()
}
.padding(.horizontal)
}
// MARK: - Board row
@ViewBuilder
private func boardRow(index: Int, board: PedalBoard) -> some View {
let isAudible: Bool = shoeGaze.isBoardAudible(board)
let isSoloed: Bool = shoeGaze.activeBoardIndex == index
HStack(spacing: BoardsSheetView.rowSpacing) {
ledButton(board: board, isAudible: isAudible)
soloButton(board: board, isSoloed: isSoloed)
TextField(
"Board \(index + 1)",
text: Binding(
get: { board.label },
set: { board.label = $0 }
)
)
.textFieldStyle(.roundedBorder)
if let gainNode: UIParamNode = board.masterGainNode {
Slider(
value: Binding(
get: { gainNode.value },
set: { gainNode.update($0) }
),
in: 0.0 ... 1.0
)
.frame(width: BoardsSheetView.faderWidth)
}
Button("Edit") {
editingBoardIndex = index
dismiss()
}
.buttonStyle(.borderless)
}
.padding(.vertical, 4)
}
private func ledButton(board: PedalBoard, isAudible: Bool) -> some View {
Button {
switch mode {
case .solo:
shoeGaze.soloBoard(board, crossfadeSeconds: clampedCrossfade)
case .blend:
shoeGaze.setBoard(board, audible: !isAudible, crossfadeSeconds: clampedCrossfade)
}
} label: {
Circle()
.fill(isAudible ? Color.green : Color(white: BoardsSheetView.ledDimBrightness))
.frame(width: BoardsSheetView.ledDiameter, height: BoardsSheetView.ledDiameter)
.overlay(
Circle().stroke(Color.white.opacity(BoardsSheetView.ledRimOpacity), lineWidth: 0.5)
)
}
.buttonStyle(.plain)
}
private func soloButton(board: PedalBoard, isSoloed: Bool) -> some View {
Button {
shoeGaze.soloBoard(board, crossfadeSeconds: clampedCrossfade)
} label: {
Text("S")
.font(.system(size: 13, weight: .bold))
.foregroundStyle(isSoloed ? Color.black : Color.primary)
.frame(width: BoardsSheetView.soloButtonSize, height: BoardsSheetView.soloButtonSize)
.background(
RoundedRectangle(cornerRadius: 5)
.fill(isSoloed ? Color.yellow : Color.secondary.opacity(0.2))
)
}
.buttonStyle(.plain)
}
// MARK: - Mutations
/// Reorders boards, translating SwiftUI's `onMove` offsets to `ShoeGaze.moveBoard`.
private func moveBoards(from source: IndexSet, to destination: Int) {
// List drag delivers a single source row; ignore any multi-selection move so a
// dropped row can never desync `shoeGaze.boards` from the displayed order.
guard source.count == 1, let sourceIndex: Int = source.first else { return }
let board: PedalBoard = shoeGaze.boards[sourceIndex]
// SwiftUI's destination is the insertion point in the pre-move list; moveBoard
// removes first, so shift down by one when moving forward.
let target: Int = destination > sourceIndex ? destination - 1 : destination
shoeGaze.moveBoard(board, to: target)
clampEditingIndex()
}
private func deleteBoards(_ offsets: IndexSet) {
for index: Int in offsets.sorted(by: >) where index < shoeGaze.boards.count {
shoeGaze.removeBoard(shoeGaze.boards[index])
}
clampEditingIndex()
}
// MARK: - Helpers
private var clampedCrossfade: Double {
min(max(crossfadeSeconds, 0.0), BoardsSheetView.maxCrossfadeSeconds)
}
private func clampEditingIndex() {
let count: Int = shoeGaze.boards.count
guard count > 0 else { editingBoardIndex = 0; return }
editingBoardIndex = min(editingBoardIndex, count - 1)
}
}