Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/PedalBoardView.swift
T
2026-06-21 12:25:18 -07:00

167 lines
7.1 KiB
Swift

// PedalBoardView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdUI
import LyrebirdShoeGaze
/// A horizontal scrolling strip of ``PedalView`` cards, one per pedal in a `PedalBoard`.
///
/// Pedals appear left-to-right in signal-flow order (the same order as
/// `PedalBoard.pedals`). The board's `label` property is shown above the strip on a
/// dark "pedalboard" backdrop that evokes a wooden/metal board surface.
///
/// When `board.masterGainNode` is non-nil (i.e. after `ShoeGaze.finalizeBoard(_:)` has
/// been called), a small "Master" section appears at the right end of the scrollable
/// strip, containing a single output-level `ParameterKnob` bound to the board's gain node.
///
/// An optional `onRemove` callback, if provided, is invoked by a small remove button
/// rendered in the header row so `PedalBoardView` remains decoupled from `ShoeGaze`.
///
/// ## Usage
///
/// ```swift
/// PedalBoardView(board: pedalBoard, onRemove: { shoeGaze.removeBoard(pedalBoard) })
/// ```
public struct PedalBoardView: View {
// MARK: - Layout constants
private static let pedalSpacing: CGFloat = 20
private static let horizontalPadding: CGFloat = 16
private static let verticalPadding: CGFloat = 20
private static let sectionSpacing: CGFloat = 10
private static let boardCornerRadius: CGFloat = 8
private static let boardBorderOpacity: Double = 0.50
private static let boardBorderBrightness: Double = 0.30
private static let labelOpacity: Double = 0.60
private static let outerHorizontalPadding: CGFloat = 4
private static let boardBorderLineWidth: CGFloat = 1
/// Dark walnut board surface colour components.
private static let boardBackgroundRed: Double = 0.12
private static let boardBackgroundGreen: Double = 0.10
private static let boardBackgroundBlue: Double = 0.08
/// Separator between the pedal strip and the master section.
private static let masterSeparatorOpacity: Double = 0.25
private static let masterSeparatorWidth: CGFloat = 1
private static let masterSectionPaddingH: CGFloat = 12
private static let masterSectionPaddingV: CGFloat = 8
private static let masterLabelOpacity: Double = 0.45
private static let masterGainRange: ClosedRange<Double> = 0.0...1.0
/// Size of the header-area remove button.
private static let removeButtonFontSize: CGFloat = 11
private static let removeButtonOpacity: Double = 0.55
private static let headerSpacing: CGFloat = 6
// MARK: - Properties
private let board: PedalBoard
/// Called when the user taps the remove button in the board header.
/// If `nil`, no remove button is rendered.
private let onRemove: (() -> Void)?
// MARK: - Init
/// Creates a horizontal board view for the given `PedalBoard`.
///
/// - Parameters:
/// - board: The `PedalBoard` whose pedals will be rendered as stompbox cards.
/// The board's `label` property is used as the header text.
/// - onRemove: Optional closure invoked when the user taps the remove button.
/// Pass `nil` to suppress the button entirely. Defaults to `nil`.
public init(
board: PedalBoard,
onRemove: (() -> Void)? = nil
) {
self.board = board
self.onRemove = onRemove
}
// MARK: - Body
public var body: some View {
VStack(alignment: .leading, spacing: PedalBoardView.sectionSpacing) {
boardHeader
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .bottom, spacing: PedalBoardView.pedalSpacing) {
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pair in
let pedal: Pedal = pair.element
PedalView(pedal: pedal)
}
if let masterNode: UIParamNode = board.masterGainNode {
masterSection(node: masterNode)
}
}
.padding(.horizontal, PedalBoardView.horizontalPadding)
.padding(.vertical, PedalBoardView.verticalPadding)
}
}
.background(
RoundedRectangle(cornerRadius: PedalBoardView.boardCornerRadius)
.fill(Color(
red: PedalBoardView.boardBackgroundRed,
green: PedalBoardView.boardBackgroundGreen,
blue: PedalBoardView.boardBackgroundBlue
)) // dark walnut board surface
.overlay(
RoundedRectangle(cornerRadius: PedalBoardView.boardCornerRadius)
.stroke(
Color(white: PedalBoardView.boardBorderBrightness)
.opacity(PedalBoardView.boardBorderOpacity),
lineWidth: PedalBoardView.boardBorderLineWidth
)
)
)
.padding(.horizontal, PedalBoardView.outerHorizontalPadding)
}
// MARK: - Subviews
/// Label row with optional remove button, shown above the pedal strip.
private var boardHeader: some View {
HStack(spacing: PedalBoardView.headerSpacing) {
Text(board.label)
.font(.subheadline)
.foregroundStyle(Color.white.opacity(PedalBoardView.labelOpacity))
Spacer()
if let remove: () -> Void = onRemove {
Button(action: remove) {
Text("Remove")
.font(.system(size: PedalBoardView.removeButtonFontSize))
.foregroundStyle(Color.white.opacity(PedalBoardView.removeButtonOpacity))
}
.buttonStyle(.plain)
}
}
// Indent to align with the board interior, matching horizontalPadding.
.padding(.horizontal, PedalBoardView.horizontalPadding)
.padding(.top, PedalBoardView.masterSectionPaddingV)
}
/// Master-gain section rendered at the right end of the pedal strip.
///
/// A thin vertical separator visually divides the pedal chain from the
/// master output stage, matching the pattern used by hardware pedalboards.
private func masterSection(node: UIParamNode) -> some View {
HStack(alignment: .bottom, spacing: PedalBoardView.pedalSpacing) {
Rectangle()
.fill(Color.white.opacity(PedalBoardView.masterSeparatorOpacity))
.frame(width: PedalBoardView.masterSeparatorWidth)
.padding(.vertical, PedalBoardView.masterSectionPaddingV)
VStack(spacing: PedalBoardView.masterSectionPaddingV) {
Text("MASTER")
.font(.system(size: PedalBoardView.removeButtonFontSize, weight: .medium, design: .monospaced))
.foregroundStyle(Color.white.opacity(PedalBoardView.masterLabelOpacity))
ParameterKnob("Gain", node: node, range: PedalBoardView.masterGainRange)
.foregroundStyle(Color.white)
}
.padding(.horizontal, PedalBoardView.masterSectionPaddingH)
}
}
}