Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/BoardBarView.swift
T
josh b4b2f58762 Compact board mixer bar; align scope/CPU to palette width
Shrink BoardBarView to a fixed-height single horizontal scroll group with a
tighter two-row control column, so it wraps its elements and frees vertical
canvas for the pedals. Constrain the scope and CPU-meter views to the pedal
palette width, left-aligned, so they sit under the palette column.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 17:14:04 -07:00

311 lines
12 KiB
Swift

// BoardBarView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdUI
import LyrebirdShoeGaze
/// A horizontal board-mixer strip for switching between, or blending, multiple `PedalBoard`s.
///
/// Each board is a channel cell with an audible LED, a Solo button, and a level fader (the
/// board's master gain). A leading **Solo / Blend** mode switch chooses the interaction:
///
/// - **Solo** — tapping a cell's LED makes that board the only audible one
/// (``ShoeGaze/soloBoard(_:crossfadeSeconds:)``); the **All** button returns to the
/// summed state (``ShoeGaze/selectAllBoards(crossfadeSeconds:)``). Amp-channel feel.
/// - **Blend** — tapping a cell's LED toggles that board in or out of the mix
/// independently (``ShoeGaze/setBoard(_:audible:crossfadeSeconds:)``), so any subset can
/// play at once — the default summed behaviour, now controllable per board.
///
/// Tapping a cell's **name** selects it as the edit target (`editingBoardIndex`), which is
/// independent of what is audible — you can audition one board while editing another. A
/// cell's context menu deletes the board.
///
/// The same view serves macOS and iOS; on iOS the cell row scrolls horizontally and the
/// context-menu delete is reached by long-press.
public struct BoardBarView: View {
// MARK: - Play mode
/// How a cell's LED tap maps to board audibility.
private enum PlayMode: Hashable {
/// Tapping a board solos it exclusively.
case solo
/// Tapping a board toggles it independently in/out of the mix.
case blend
}
// MARK: - Layout constants
private static let barSpacing: CGFloat = 10
private static let barHeight: CGFloat = 92
private static let cellSpacing: CGFloat = 8
private static let cellWidth: CGFloat = 104
private static let cellInnerSpacing: CGFloat = 5
private static let cellPadding: CGFloat = 7
private static let cellCornerRadius: CGFloat = 8
private static let controlColumnWidth: CGFloat = 188
private static let modePickerWidth: CGFloat = 104
private static let crossfadeSliderMinWidth: CGFloat = 64
private static let ledDiameter: CGFloat = 12
private static let ledGlowRadius: CGFloat = 6
private static let ledGlowOpacity: Double = 0.75
private static let ledDimBrightness: Double = 0.16
private static let ledRimOpacity: Double = 0.30
private static let soloButtonSize: CGFloat = 22
private static let editingBorderWidth: CGFloat = 2
private static let idleBorderOpacity: Double = 0.18
private static let cellBackgroundBrightness: Double = 0.14
private static let labelOpacity: Double = 0.65
private static let nameFontSize: CGFloat = 12
private static let maxCrossfadeSeconds: Double = 10.0
private static let crossfadeStep: Double = 0.1
private static let addButtonSize: CGFloat = 30
// MARK: - Properties
private let shoeGaze: ShoeGaze
@Binding private var editingBoardIndex: Int
@Binding private var crossfadeSeconds: Double
private let onAddBoard: () -> Void
@State private var mode: PlayMode = .solo
// MARK: - Init
/// Creates a board-mixer bar.
///
/// - Parameters:
/// - shoeGaze: The manager whose `boards` are switched / blended.
/// - editingBoardIndex: The index of the board currently being edited (a cell's name
/// tap writes this).
/// - crossfadeSeconds: Crossfade duration applied to every board switch / toggle.
/// - onAddBoard: Called when the trailing **+** 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 {
// One horizontal scroll group so the whole bar hugs its content and stays a short,
// fixed-height band — leaving the canvas below for the pedals.
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .center, spacing: BoardBarView.barSpacing) {
controlColumn
Divider().overlay(Color.white.opacity(BoardBarView.idleBorderOpacity))
ForEach(Array(shoeGaze.boards.enumerated()), id: \.offset) { pair in
boardCell(index: pair.offset, board: pair.element)
}
addButton
}
.frame(height: BoardBarView.barHeight)
}
.frame(height: BoardBarView.barHeight)
}
// MARK: - Control column
private var controlColumn: some View {
VStack(alignment: .leading, spacing: BoardBarView.cellInnerSpacing) {
HStack(spacing: BoardBarView.cellInnerSpacing) {
Picker("Mode", selection: $mode) {
Text("Solo").tag(PlayMode.solo)
Text("Blend").tag(PlayMode.blend)
}
.pickerStyle(.segmented)
.labelsHidden()
.frame(width: BoardBarView.modePickerWidth)
Button("All") {
shoeGaze.selectAllBoards(crossfadeSeconds: clampedCrossfade)
}
.buttonStyle(.bordered)
.tint(.orange)
.help("Play all boards (summed)")
}
HStack(spacing: BoardBarView.cellInnerSpacing) {
Text("XFade")
.font(.caption2)
.foregroundStyle(Color.white.opacity(BoardBarView.labelOpacity))
Slider(
value: $crossfadeSeconds,
in: 0 ... BoardBarView.maxCrossfadeSeconds,
step: BoardBarView.crossfadeStep
)
.frame(minWidth: BoardBarView.crossfadeSliderMinWidth)
Text(String(format: "%.1fs", clampedCrossfade))
.font(.caption2)
.monospacedDigit()
.foregroundStyle(Color.white.opacity(BoardBarView.labelOpacity))
}
}
.frame(width: BoardBarView.controlColumnWidth)
}
// MARK: - Board cell
@ViewBuilder
private func boardCell(index: Int, board: PedalBoard) -> some View {
let isEditing: Bool = index == editingBoardIndex
let isAudible: Bool = shoeGaze.isBoardAudible(board)
let isSoloed: Bool = shoeGaze.activeBoardIndex == index
VStack(spacing: BoardBarView.cellInnerSpacing) {
Text(boardLabel(index: index, board: board))
.font(.system(size: BoardBarView.nameFontSize, weight: .semibold))
.foregroundStyle(Color.white)
.lineLimit(1)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.onTapGesture { editingBoardIndex = index }
HStack(spacing: BoardBarView.cellSpacing) {
ledButton(board: board, isAudible: isAudible)
soloButton(board: board, isSoloed: isSoloed)
}
levelFader(board: board)
}
.padding(BoardBarView.cellPadding)
.frame(width: BoardBarView.cellWidth)
.background(
RoundedRectangle(cornerRadius: BoardBarView.cellCornerRadius)
.fill(Color(white: BoardBarView.cellBackgroundBrightness))
)
.overlay(
RoundedRectangle(cornerRadius: BoardBarView.cellCornerRadius)
.stroke(
isEditing ? Color.orange : Color.white.opacity(BoardBarView.idleBorderOpacity),
lineWidth: isEditing ? BoardBarView.editingBorderWidth : 1
)
)
.contextMenu {
Button("Edit This Board") { editingBoardIndex = index }
Button(isAudible ? "Silence" : "Enable") {
shoeGaze.setBoard(board, audible: !isAudible, crossfadeSeconds: clampedCrossfade)
}
Divider()
Button("Delete Board", role: .destructive) {
shoeGaze.removeBoard(board)
clampEditingIndex()
}
}
}
/// The audible LED — tap behaviour depends on the current ``PlayMode``.
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: {
ZStack {
if isAudible {
Circle()
.fill(Color.green.opacity(BoardBarView.ledGlowOpacity))
.frame(
width: BoardBarView.ledDiameter + BoardBarView.ledGlowRadius,
height: BoardBarView.ledDiameter + BoardBarView.ledGlowRadius
)
.blur(radius: BoardBarView.ledGlowRadius / 2)
}
Circle()
.fill(isAudible ? Color.green : Color(white: BoardBarView.ledDimBrightness))
.frame(width: BoardBarView.ledDiameter, height: BoardBarView.ledDiameter)
.overlay(
Circle().stroke(Color.white.opacity(BoardBarView.ledRimOpacity), lineWidth: 0.5)
)
}
.frame(width: BoardBarView.ledDiameter + BoardBarView.ledGlowRadius,
height: BoardBarView.ledDiameter + BoardBarView.ledGlowRadius)
}
.buttonStyle(.plain)
.help(mode == .solo ? "Solo this board" : (isAudible ? "Silence this board" : "Add this board to the mix"))
}
/// Exclusive solo button — lit when this board is the soloed one.
private func soloButton(board: PedalBoard, isSoloed: Bool) -> some View {
Button {
shoeGaze.soloBoard(board, crossfadeSeconds: clampedCrossfade)
} label: {
Text("S")
.font(.system(size: BoardBarView.nameFontSize, weight: .bold))
.foregroundStyle(isSoloed ? Color.black : Color.white)
.frame(width: BoardBarView.soloButtonSize, height: BoardBarView.soloButtonSize)
.background(
RoundedRectangle(cornerRadius: 4)
.fill(isSoloed ? Color.yellow : Color(white: BoardBarView.ledDimBrightness))
)
}
.buttonStyle(.plain)
.help("Solo this board (exclusive)")
}
/// Board master-gain fader. Hidden until the board's output master is connected.
@ViewBuilder
private func levelFader(board: PedalBoard) -> some View {
if let gainNode: UIParamNode = board.masterGainNode {
Slider(
value: Binding(
get: { gainNode.value },
set: { gainNode.update($0) }
),
in: 0.0 ... 1.0
)
.controlSize(.mini)
}
}
// MARK: - Add button
private var addButton: some View {
Button(action: onAddBoard) {
Image(systemName: "plus")
.font(.system(size: BoardBarView.nameFontSize, weight: .bold))
.foregroundStyle(Color.white)
.frame(width: BoardBarView.addButtonSize, height: BoardBarView.addButtonSize)
.background(
RoundedRectangle(cornerRadius: BoardBarView.cellCornerRadius)
.stroke(Color.white.opacity(BoardBarView.idleBorderOpacity), lineWidth: 1)
)
}
.buttonStyle(.plain)
.help("Add a board")
}
// MARK: - Helpers
/// Display label for a board, falling back to a 1-based position when unnamed.
private func boardLabel(index: Int, board: PedalBoard) -> String {
board.label.isEmpty ? "Board \(index + 1)" : board.label
}
/// `crossfadeSeconds` clamped to the supported range.
private var clampedCrossfade: Double {
min(max(crossfadeSeconds, 0.0), BoardBarView.maxCrossfadeSeconds)
}
/// Keeps the edit target valid after a delete.
private func clampEditingIndex() {
let count: Int = shoeGaze.boards.count
guard count > 0 else { editingBoardIndex = 0; return }
editingBoardIndex = min(editingBoardIndex, count - 1)
}
}