1.0 release
This commit is contained in:
@@ -0,0 +1,298 @@
|
||||
// BoardEditorView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// An interactive editor for a single `PedalBoard`.
|
||||
///
|
||||
/// `BoardEditorView` binds to `ShoeGaze` (an `@Observable` class) and reads the live
|
||||
/// board via `shoeGaze.boards[boardIndex]`. Because `ShoeGaze.boards` is an `@Observable`
|
||||
/// tracked property, loading a version — which calls `shoeGaze.rebuildBoard(_:from:)` and
|
||||
/// mutates `boards` in-place at the same index — triggers an automatic SwiftUI re-render
|
||||
/// with the new board and its new pedal list without any manual refresh.
|
||||
///
|
||||
/// ## Board-load re-binding
|
||||
///
|
||||
/// `rebuildBoard(_:from:)` tears down the old board, calls `addBoard()`, and moves the
|
||||
/// result back to `oldIndex`. The `boards` array is mutated on the main actor, so
|
||||
/// `shoeGaze.boards[boardIndex]` — read fresh in `body` — always resolves to the current
|
||||
/// board after a load. `BoardEditorView` never caches the `PedalBoard` reference itself;
|
||||
/// the index is the stable anchor.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// BoardEditorView(
|
||||
/// shoeGaze: shoeGaze,
|
||||
/// boardIndex: 0,
|
||||
/// versionStore: versionStore
|
||||
/// )
|
||||
/// ```
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - shoeGaze: The `ShoeGaze` manager that owns the board.
|
||||
/// - boardIndex: Index into `shoeGaze.boards`. If out of range, a small placeholder
|
||||
/// is shown instead.
|
||||
/// - versionStore: The `BoardVersionStore` that manages named snapshots.
|
||||
public struct BoardEditorView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let outerPadding: CGFloat = 16
|
||||
private static let headerSpacing: CGFloat = 10
|
||||
private static let sectionSpacing: CGFloat = 14
|
||||
private static let boardBackgroundRed: Double = 0.12
|
||||
private static let boardBackgroundGreen: Double = 0.10
|
||||
private static let boardBackgroundBlue: Double = 0.08
|
||||
private static let boardCornerRadius: CGFloat = 10
|
||||
private static let boardBorderBrightness: Double = 0.30
|
||||
private static let boardBorderOpacity: Double = 0.50
|
||||
private static let boardBorderLineWidth: CGFloat = 1
|
||||
private static let boardPedalAreaPadding: CGFloat = 16
|
||||
private static let labelFontSize: CGFloat = 17
|
||||
private static let placeholderOpacity: Double = 0.55
|
||||
private static let versionMenuMaxWidth: CGFloat = 280
|
||||
private static let versionRowSpacing: CGFloat = 6
|
||||
private static let versionDateOpacity: Double = 0.55
|
||||
private static let versionDateFontSize: CGFloat = 11
|
||||
private static let toolbarButtonSpacing: CGFloat = 8
|
||||
private static let textFieldMinWidth: CGFloat = 160
|
||||
private static let dividerOpacity: Double = 0.20
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let shoeGaze: ShoeGaze
|
||||
private let boardIndex: Int
|
||||
private let versionStore: BoardVersionStore
|
||||
|
||||
/// The text typed into the version-name field before saving.
|
||||
@State private var newVersionName: String = ""
|
||||
|
||||
// MARK: - Date formatting
|
||||
|
||||
/// Short date + time formatter used on version menu rows.
|
||||
private static let savedAtStyle: Date.FormatStyle = Date.FormatStyle()
|
||||
.month(.abbreviated)
|
||||
.day(.defaultDigits)
|
||||
.year(.defaultDigits)
|
||||
.hour(.defaultDigits(amPM: .abbreviated))
|
||||
.minute(.twoDigits)
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a board editor view.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - shoeGaze: The `ShoeGaze` manager that owns the board at `boardIndex`.
|
||||
/// - boardIndex: Index into `shoeGaze.boards`. Renders a placeholder when out of range.
|
||||
/// - versionStore: The `BoardVersionStore` that manages named snapshots for this board.
|
||||
public init(
|
||||
shoeGaze: ShoeGaze,
|
||||
boardIndex: Int,
|
||||
versionStore: BoardVersionStore
|
||||
) {
|
||||
self.shoeGaze = shoeGaze
|
||||
self.boardIndex = boardIndex
|
||||
self.versionStore = versionStore
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
if boardIndex < 0 || boardIndex >= shoeGaze.boards.count {
|
||||
outOfRangePlaceholder
|
||||
} else {
|
||||
let board: PedalBoard = shoeGaze.boards[boardIndex]
|
||||
editorContent(board: board)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Main editor layout
|
||||
|
||||
/// The primary editor layout when `boardIndex` is in range.
|
||||
private func editorContent(board: PedalBoard) -> some View {
|
||||
VStack(alignment: .leading, spacing: BoardEditorView.sectionSpacing) {
|
||||
header(board: board)
|
||||
Divider()
|
||||
.overlay(Color.white.opacity(BoardEditorView.dividerOpacity))
|
||||
pedalArea(board: board)
|
||||
}
|
||||
.padding(BoardEditorView.outerPadding)
|
||||
}
|
||||
|
||||
// MARK: - Header
|
||||
|
||||
/// Toolbar row: board label on the left, version controls on the right.
|
||||
private func header(board: PedalBoard) -> some View {
|
||||
VStack(alignment: .leading, spacing: BoardEditorView.headerSpacing) {
|
||||
HStack(spacing: BoardEditorView.headerSpacing) {
|
||||
Text(board.label)
|
||||
.font(.system(size: BoardEditorView.labelFontSize, weight: .semibold))
|
||||
.foregroundStyle(Color.white)
|
||||
Spacer()
|
||||
diskButtons
|
||||
}
|
||||
HStack(spacing: BoardEditorView.toolbarButtonSpacing) {
|
||||
versionNameField(board: board)
|
||||
versionsMenu
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// "Save to Disk" and "Load from Disk" buttons. Cross-platform; no file picker used.
|
||||
private var diskButtons: some View {
|
||||
HStack(spacing: BoardEditorView.toolbarButtonSpacing) {
|
||||
Button("Save to Disk") {
|
||||
try? versionStore.writeAllToDisk(
|
||||
in: BoardVersionStore.defaultDirectory()
|
||||
)
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
|
||||
Button("Load from Disk") {
|
||||
try? versionStore.loadAllFromDisk(
|
||||
in: BoardVersionStore.defaultDirectory()
|
||||
)
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
}
|
||||
}
|
||||
|
||||
/// Version-name `TextField` + "Save Version" button.
|
||||
private func versionNameField(board: PedalBoard) -> some View {
|
||||
HStack(spacing: BoardEditorView.toolbarButtonSpacing) {
|
||||
TextField("Version name…", text: $newVersionName)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.frame(minWidth: BoardEditorView.textFieldMinWidth)
|
||||
|
||||
Button("Save Version") {
|
||||
let effectiveName: String = newVersionName.isEmpty
|
||||
? "Version \(versionStore.versions.count + 1)"
|
||||
: newVersionName
|
||||
versionStore.capture(board, name: effectiveName)
|
||||
newVersionName = ""
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.tint(.orange)
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Menu` listing all saved versions.
|
||||
///
|
||||
/// Each version is represented by a nested `Menu` whose title is the version name.
|
||||
/// Opening a version's sub-menu exposes two reliably-reachable actions:
|
||||
/// - **Load** — calls `shoeGaze.rebuildBoard(_:from:)`, which tears down the old board,
|
||||
/// builds a fresh one from the saved `BoardState`, and moves it back to `boardIndex`.
|
||||
/// Because `ShoeGaze.boards` is `@Observable`, every view reading `shoeGaze.boards`
|
||||
/// re-renders automatically with the replacement board.
|
||||
/// - **Delete** — removes the version from memory AND from disk so that the deletion
|
||||
/// survives the next `loadAllFromDisk` call.
|
||||
private var versionsMenu: some View {
|
||||
Menu {
|
||||
if versionStore.versions.isEmpty {
|
||||
Text("No saved versions")
|
||||
} else {
|
||||
ForEach(versionStore.versions) { version in
|
||||
versionSubmenu(version: version)
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Label("Versions", systemImage: "clock.arrow.circlepath")
|
||||
}
|
||||
.menuStyle(.automatic)
|
||||
.frame(maxWidth: BoardEditorView.versionMenuMaxWidth, alignment: .leading)
|
||||
}
|
||||
|
||||
/// A nested `Menu` for a single version with Load and Delete actions.
|
||||
///
|
||||
/// Using a sub-menu rather than an `HStack` inside a single menu row ensures that
|
||||
/// both buttons are independently hittable on every platform.
|
||||
private func versionSubmenu(version: BoardVersion) -> some View {
|
||||
let dateString: String = version.savedAt.formatted(BoardEditorView.savedAtStyle)
|
||||
return Menu {
|
||||
Button("Load") {
|
||||
loadVersion(version)
|
||||
}
|
||||
Button("Delete", role: .destructive) {
|
||||
versionStore.remove(version)
|
||||
try? versionStore.deleteFromDisk(version, in: BoardVersionStore.defaultDirectory())
|
||||
}
|
||||
} label: {
|
||||
VStack(alignment: .leading, spacing: BoardEditorView.versionRowSpacing) {
|
||||
Text(version.name)
|
||||
Text(dateString)
|
||||
.font(.system(size: BoardEditorView.versionDateFontSize))
|
||||
.foregroundStyle(Color.secondary.opacity(BoardEditorView.versionDateOpacity))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Pedal area
|
||||
|
||||
/// Scrollable wrap-to-fit pedal area backed by `FlowLayout`.
|
||||
private func pedalArea(board: PedalBoard) -> some View {
|
||||
ScrollView(.vertical, showsIndicators: true) {
|
||||
FlowLayout(
|
||||
horizontalSpacing: FlowLayout.defaultHorizontalSpacing,
|
||||
verticalSpacing: FlowLayout.defaultVerticalSpacing
|
||||
) {
|
||||
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pair in
|
||||
let pedal: Pedal = pair.element
|
||||
PedalView(pedal: pedal)
|
||||
}
|
||||
}
|
||||
.padding(BoardEditorView.boardPedalAreaPadding)
|
||||
}
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: BoardEditorView.boardCornerRadius)
|
||||
.fill(
|
||||
Color(
|
||||
red: BoardEditorView.boardBackgroundRed,
|
||||
green: BoardEditorView.boardBackgroundGreen,
|
||||
blue: BoardEditorView.boardBackgroundBlue
|
||||
)
|
||||
)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: BoardEditorView.boardCornerRadius)
|
||||
.stroke(
|
||||
Color(white: BoardEditorView.boardBorderBrightness)
|
||||
.opacity(BoardEditorView.boardBorderOpacity),
|
||||
lineWidth: BoardEditorView.boardBorderLineWidth
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Out-of-range placeholder
|
||||
|
||||
/// Shown when `boardIndex` is outside `shoeGaze.boards`.
|
||||
private var outOfRangePlaceholder: some View {
|
||||
Text("No board at index \(boardIndex).")
|
||||
.foregroundStyle(Color.secondary.opacity(BoardEditorView.placeholderOpacity))
|
||||
.padding(BoardEditorView.outerPadding)
|
||||
}
|
||||
|
||||
// MARK: - Version loading
|
||||
|
||||
/// Loads `version` back into the board at `boardIndex`.
|
||||
///
|
||||
/// `rebuildBoard(_:from:)` tears down the old board, allocates a new one populated
|
||||
/// from `version.state`, finalises it, and moves it back to `boardIndex`. Because
|
||||
/// `ShoeGaze` is `@Observable` and `boards` is a tracked property, the mutation
|
||||
/// causes this view to re-render immediately with the replacement board — no manual
|
||||
/// state refresh needed.
|
||||
private func loadVersion(_ version: BoardVersion) {
|
||||
guard boardIndex >= 0, boardIndex < shoeGaze.boards.count else { return }
|
||||
let board: PedalBoard = shoeGaze.boards[boardIndex]
|
||||
do {
|
||||
_ = try shoeGaze.rebuildBoard(board, from: version.state)
|
||||
} catch {
|
||||
print("[BoardEditorView] rebuildBoard failed: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
// ChainRouterView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// Renders a ``ChainRouter`` as a vertical stack with transport controls, two labelled
|
||||
/// board rows, and a merger + optional master-gain output stage.
|
||||
///
|
||||
/// Layout (top → bottom):
|
||||
/// 1. **Header row** — ``SplitterView`` on the left; if `router.masterGainNode` is
|
||||
/// non-nil, a "Master" ``ParameterKnob`` on the right.
|
||||
/// 2. **Board A row** — label "A" followed by ``PedalBoardView`` for `router.boardA`.
|
||||
/// 3. **Board B row** — label "B" followed by ``PedalBoardView`` for `router.boardB`.
|
||||
/// 4. **Footer row** — ``MergerView`` for `router.merger` (shown only once finalize
|
||||
/// has been called and `router.merger` is non-nil).
|
||||
///
|
||||
/// `merger` is optional on `ChainRouter` (nil before `finalize()`) — the footer is
|
||||
/// conditionally shown so the view is usable at any lifecycle stage.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// ChainRouterView(router: chainRouter)
|
||||
/// ```
|
||||
public struct ChainRouterView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let outerSpacing: CGFloat = 16
|
||||
private static let headerSpacing: CGFloat = 14
|
||||
private static let boardRowSpacing: CGFloat = 8
|
||||
private static let boardLabelFontSize: CGFloat = 11
|
||||
private static let boardLabelOpacity: Double = 0.55
|
||||
private static let masterLabelFontSize: CGFloat = 11
|
||||
private static let masterLabelOpacity: Double = 0.45
|
||||
private static let masterKnobSpacing: CGFloat = 6
|
||||
private static let footerSpacing: CGFloat = 14
|
||||
private static let outerPaddingH: CGFloat = 12
|
||||
private static let outerPaddingV: CGFloat = 12
|
||||
private static let sectionDividerOpacity: Double = 0.12
|
||||
private static let sectionDividerHeight: CGFloat = 1
|
||||
/// Master gain range: 0 = silence, 1 = unity output.
|
||||
private static let masterGainRange: ClosedRange<Double> = 0.0...1.0
|
||||
/// Background colour for the router enclosure (darker than stage floor).
|
||||
private static let backgroundRed: Double = 0.11
|
||||
private static let backgroundGreen: Double = 0.10
|
||||
private static let backgroundBlue: Double = 0.13
|
||||
private static let backgroundCornerRadius: CGFloat = 10
|
||||
private static let backgroundBorderOpacity: Double = 0.30
|
||||
private static let backgroundBorderLineWidth: CGFloat = 1.0
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let router: ChainRouter
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a chain router view for the given ``ChainRouter``.
|
||||
///
|
||||
/// - Parameter router: The `ChainRouter` to display. Board A/B pedal views, the
|
||||
/// splitter mode picker, the merger mix knobs, and the optional master gain knob
|
||||
/// are all derived from this instance. The view correctly handles the pre-`finalize()`
|
||||
/// state where `router.merger` and `router.masterGainNode` are `nil`.
|
||||
public init(router: ChainRouter) {
|
||||
self.router = router
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
VStack(alignment: .leading, spacing: ChainRouterView.outerSpacing) {
|
||||
headerRow
|
||||
sectionDivider
|
||||
boardRow(label: "A", board: router.boardA)
|
||||
sectionDivider
|
||||
boardRow(label: "B", board: router.boardB)
|
||||
if let builtMerger: Merger = router.merger {
|
||||
sectionDivider
|
||||
footerRow(merger: builtMerger)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, ChainRouterView.outerPaddingH)
|
||||
.padding(.vertical, ChainRouterView.outerPaddingV)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: ChainRouterView.backgroundCornerRadius)
|
||||
.fill(Color(
|
||||
red: ChainRouterView.backgroundRed,
|
||||
green: ChainRouterView.backgroundGreen,
|
||||
blue: ChainRouterView.backgroundBlue
|
||||
))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: ChainRouterView.backgroundCornerRadius)
|
||||
.stroke(
|
||||
Color.white.opacity(ChainRouterView.backgroundBorderOpacity),
|
||||
lineWidth: ChainRouterView.backgroundBorderLineWidth
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
/// Top row: splitter mode selector + optional master gain knob.
|
||||
private var headerRow: some View {
|
||||
HStack(alignment: .top, spacing: ChainRouterView.headerSpacing) {
|
||||
SplitterView(splitter: router.splitter)
|
||||
Spacer()
|
||||
if let masterNode: UIParamNode = router.masterGainNode {
|
||||
masterKnob(node: masterNode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Master gain knob with a small label above.
|
||||
private func masterKnob(node: UIParamNode) -> some View {
|
||||
VStack(spacing: ChainRouterView.masterKnobSpacing) {
|
||||
Text("MASTER")
|
||||
.font(.system(
|
||||
size: ChainRouterView.masterLabelFontSize,
|
||||
weight: .medium,
|
||||
design: .monospaced
|
||||
))
|
||||
.foregroundStyle(Color.white.opacity(ChainRouterView.masterLabelOpacity))
|
||||
ParameterKnob("Gain", node: node, range: ChainRouterView.masterGainRange)
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
}
|
||||
|
||||
/// A labelled board row.
|
||||
private func boardRow(label: String, board: PedalBoard) -> some View {
|
||||
HStack(alignment: .top, spacing: ChainRouterView.boardRowSpacing) {
|
||||
Text(label)
|
||||
.font(.system(
|
||||
size: ChainRouterView.boardLabelFontSize,
|
||||
weight: .semibold,
|
||||
design: .monospaced
|
||||
))
|
||||
.foregroundStyle(Color.white.opacity(ChainRouterView.boardLabelOpacity))
|
||||
.frame(width: 14, alignment: .leading)
|
||||
.padding(.top, 4) // optically align with board content
|
||||
PedalBoardView(board: board)
|
||||
}
|
||||
}
|
||||
|
||||
/// Footer row with merger mix knobs.
|
||||
private func footerRow(merger: Merger) -> some View {
|
||||
HStack(spacing: ChainRouterView.footerSpacing) {
|
||||
MergerView(merger: merger)
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
|
||||
/// A thin horizontal rule between sections.
|
||||
private var sectionDivider: some View {
|
||||
Rectangle()
|
||||
.fill(Color.white.opacity(ChainRouterView.sectionDividerOpacity))
|
||||
.frame(height: ChainRouterView.sectionDividerHeight)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
// FlowLayout.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
|
||||
/// A SwiftUI `Layout` that arranges subviews left-to-right and wraps to the next
|
||||
/// row whenever the remaining horizontal space in the current row is exhausted.
|
||||
///
|
||||
/// Each row is filled greedily: subviews are placed in insertion order until
|
||||
/// the next subview would exceed the proposed width, at which point a new row
|
||||
/// begins. The final layout height is the sum of all row heights plus the
|
||||
/// inter-row spacing.
|
||||
///
|
||||
/// ## Infinite / nil proposed width
|
||||
///
|
||||
/// When the container proposes an infinite or `nil` width (e.g. inside a
|
||||
/// horizontal `ScrollView`), `FlowLayout` falls back to `FlowLayout.fallbackMaxWidth`
|
||||
/// and lays out subviews on a single logical strip up to that cap. This avoids
|
||||
/// an unbounded single-row layout that would make `sizeThatFits` report infinite
|
||||
/// height.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// FlowLayout(horizontalSpacing: 12, verticalSpacing: 16) {
|
||||
/// ForEach(pedals, id: \.id) { pedal in
|
||||
/// PedalView(pedal: pedal)
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - horizontalSpacing: Gap between adjacent subviews in the same row.
|
||||
/// Defaults to `FlowLayout.defaultHorizontalSpacing`.
|
||||
/// - verticalSpacing: Gap between rows.
|
||||
/// Defaults to `FlowLayout.defaultVerticalSpacing`.
|
||||
public struct FlowLayout: Layout {
|
||||
|
||||
// MARK: - Named constants
|
||||
|
||||
/// Default gap between subviews within the same row (points).
|
||||
public static let defaultHorizontalSpacing: CGFloat = 16
|
||||
|
||||
/// Default gap between rows (points).
|
||||
public static let defaultVerticalSpacing: CGFloat = 16
|
||||
|
||||
/// Maximum width used when the container proposes infinite or zero width,
|
||||
/// preventing an unbounded single-row layout.
|
||||
public static let fallbackMaxWidth: CGFloat = 1200
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
/// Gap between adjacent subviews in the same row.
|
||||
public let horizontalSpacing: CGFloat
|
||||
|
||||
/// Gap between rows.
|
||||
public let verticalSpacing: CGFloat
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a flow layout with the given spacings.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - horizontalSpacing: Gap between subviews within a row (points).
|
||||
/// Defaults to `FlowLayout.defaultHorizontalSpacing`.
|
||||
/// - verticalSpacing: Gap between rows (points).
|
||||
/// Defaults to `FlowLayout.defaultVerticalSpacing`.
|
||||
public init(
|
||||
horizontalSpacing: CGFloat = FlowLayout.defaultHorizontalSpacing,
|
||||
verticalSpacing: CGFloat = FlowLayout.defaultVerticalSpacing
|
||||
) {
|
||||
self.horizontalSpacing = horizontalSpacing
|
||||
self.verticalSpacing = verticalSpacing
|
||||
}
|
||||
|
||||
// MARK: - Layout protocol
|
||||
|
||||
public func sizeThatFits(
|
||||
proposal: ProposedViewSize,
|
||||
subviews: Subviews,
|
||||
cache: inout ()
|
||||
) -> CGSize {
|
||||
let rows: [[RowItem]] = computeRows(
|
||||
subviews: subviews,
|
||||
availableWidth: resolvedWidth(from: proposal)
|
||||
)
|
||||
return totalSize(rows: rows)
|
||||
}
|
||||
|
||||
public func placeSubviews(
|
||||
in bounds: CGRect,
|
||||
proposal: ProposedViewSize,
|
||||
subviews: Subviews,
|
||||
cache: inout ()
|
||||
) {
|
||||
let rows: [[RowItem]] = computeRows(
|
||||
subviews: subviews,
|
||||
availableWidth: resolvedWidth(from: proposal)
|
||||
)
|
||||
|
||||
var y: CGFloat = bounds.minY
|
||||
for (rowIndex, row): (Int, [RowItem]) in rows.enumerated() {
|
||||
var x: CGFloat = bounds.minX
|
||||
let rowHeight: CGFloat = row.max(by: { $0.size.height < $1.size.height })?.size.height ?? 0
|
||||
|
||||
for item: RowItem in row {
|
||||
subviews[item.index].place(
|
||||
at: CGPoint(x: x, y: y),
|
||||
anchor: .topLeading,
|
||||
proposal: ProposedViewSize(item.size)
|
||||
)
|
||||
x += item.size.width + horizontalSpacing
|
||||
}
|
||||
y += rowHeight
|
||||
if rowIndex < rows.count - 1 {
|
||||
y += verticalSpacing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private helpers
|
||||
|
||||
/// A single subview's natural (ideal) size and its index into `subviews`.
|
||||
private struct RowItem {
|
||||
let index: Int
|
||||
let size: CGSize
|
||||
}
|
||||
|
||||
/// Resolves the available width from the container proposal.
|
||||
///
|
||||
/// Falls back to `FlowLayout.fallbackMaxWidth` when the proposed width is
|
||||
/// `nil`, zero, or infinite so the layout algorithm always has a finite bound
|
||||
/// to wrap against.
|
||||
private func resolvedWidth(from proposal: ProposedViewSize) -> CGFloat {
|
||||
guard let width: CGFloat = proposal.width,
|
||||
width > 0,
|
||||
width.isFinite else {
|
||||
return FlowLayout.fallbackMaxWidth
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
/// Partitions all subviews into rows given the available width.
|
||||
///
|
||||
/// Each subview is measured at its ideal size (`unspecified` proposal).
|
||||
/// Subviews are appended to the current row while they fit; when adding the
|
||||
/// next subview would overflow `availableWidth`, a new row is started.
|
||||
///
|
||||
/// A subview wider than `availableWidth` is placed alone on its own row so
|
||||
/// it is always visible rather than silently clipped.
|
||||
private func computeRows(subviews: Subviews, availableWidth: CGFloat) -> [[RowItem]] {
|
||||
var rows: [[RowItem]] = []
|
||||
var currentRow: [RowItem] = []
|
||||
var currentRowWidth: CGFloat = 0
|
||||
|
||||
for index: Int in subviews.indices {
|
||||
let size: CGSize = subviews[index].sizeThatFits(.unspecified)
|
||||
let neededWidth: CGFloat = currentRow.isEmpty
|
||||
? size.width
|
||||
: currentRowWidth + horizontalSpacing + size.width
|
||||
|
||||
if !currentRow.isEmpty, neededWidth > availableWidth {
|
||||
// The subview doesn't fit — commit the current row and start fresh.
|
||||
rows.append(currentRow)
|
||||
currentRow = [RowItem(index: index, size: size)]
|
||||
currentRowWidth = size.width
|
||||
} else {
|
||||
// Fits in the current row (or is the first item on a new row).
|
||||
currentRow.append(RowItem(index: index, size: size))
|
||||
currentRowWidth = neededWidth
|
||||
}
|
||||
}
|
||||
|
||||
if !currentRow.isEmpty {
|
||||
rows.append(currentRow)
|
||||
}
|
||||
|
||||
return rows
|
||||
}
|
||||
|
||||
/// Computes the bounding size of the finished row layout.
|
||||
///
|
||||
/// Width is the widest row; height is the sum of all row heights plus
|
||||
/// inter-row spacing. Returns `.zero` for an empty subview list.
|
||||
private func totalSize(rows: [[RowItem]]) -> CGSize {
|
||||
guard !rows.isEmpty else { return .zero }
|
||||
|
||||
var totalHeight: CGFloat = 0
|
||||
var maxWidth: CGFloat = 0
|
||||
|
||||
for (rowIndex, row): (Int, [RowItem]) in rows.enumerated() {
|
||||
let rowHeight: CGFloat = row.max(by: { $0.size.height < $1.size.height })?.size.height ?? 0
|
||||
let rowWidth: CGFloat = row.reduce(0) { acc, item in
|
||||
acc + item.size.width
|
||||
} + CGFloat(max(row.count - 1, 0)) * horizontalSpacing
|
||||
|
||||
totalHeight += rowHeight
|
||||
if rowIndex < rows.count - 1 {
|
||||
totalHeight += verticalSpacing
|
||||
}
|
||||
maxWidth = max(maxWidth, rowWidth)
|
||||
}
|
||||
|
||||
return CGSize(width: maxWidth, height: totalHeight)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// MergerView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// A compact routing-box view for a ``Merger`` showing two `ParameterKnob` controls
|
||||
/// that adjust the per-branch mix levels.
|
||||
///
|
||||
/// Branch A and B knobs are each bound to their respective `UIParamNode` (obtained via
|
||||
/// `merger.$levelA` / `merger.$levelB`) and accept values in ``MergerView/levelRange``.
|
||||
///
|
||||
/// The enclosure uses a slate/steel tint to visually distinguish routing boxes from
|
||||
/// effect pedals.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// MergerView(merger: router.merger)
|
||||
/// ```
|
||||
public struct MergerView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let containerCornerRadius: CGFloat = 10
|
||||
private static let containerPaddingH: CGFloat = 14
|
||||
private static let containerPaddingV: CGFloat = 10
|
||||
private static let containerBorderOpacity: Double = 0.55
|
||||
private static let containerBorderLineWidth: CGFloat = 1.0
|
||||
private static let sectionSpacing: CGFloat = 8
|
||||
private static let knobRowSpacing: CGFloat = 12
|
||||
private static let titleFontSize: CGFloat = 11
|
||||
private static let titleOpacity: Double = 0.55
|
||||
/// Background colour components for the routing-box enclosure (steel).
|
||||
private static let backgroundRed: Double = 0.22
|
||||
private static let backgroundGreen: Double = 0.22
|
||||
private static let backgroundBlue: Double = 0.26
|
||||
/// Level knob range: 0 = muted, 1 = unity gain, 2 = double gain.
|
||||
private static let levelRange: ClosedRange<Double> = 0.0...2.0
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let merger: Merger
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a merger view for the given ``Merger``.
|
||||
///
|
||||
/// - Parameter merger: The `Merger` whose branch level parameters will be bound to
|
||||
/// the two knobs.
|
||||
public init(merger: Merger) {
|
||||
self.merger = merger
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
VStack(spacing: MergerView.sectionSpacing) {
|
||||
Text("MIX")
|
||||
.font(.system(size: MergerView.titleFontSize, weight: .semibold, design: .monospaced))
|
||||
.foregroundStyle(Color.white.opacity(MergerView.titleOpacity))
|
||||
|
||||
HStack(spacing: MergerView.knobRowSpacing) {
|
||||
ParameterKnob("A", node: merger.$levelA, range: MergerView.levelRange)
|
||||
.foregroundStyle(Color.white)
|
||||
ParameterKnob("B", node: merger.$levelB, range: MergerView.levelRange)
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, MergerView.containerPaddingH)
|
||||
.padding(.vertical, MergerView.containerPaddingV)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: MergerView.containerCornerRadius)
|
||||
.fill(Color(
|
||||
red: MergerView.backgroundRed,
|
||||
green: MergerView.backgroundGreen,
|
||||
blue: MergerView.backgroundBlue
|
||||
))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: MergerView.containerCornerRadius)
|
||||
.stroke(
|
||||
Color.white.opacity(MergerView.containerBorderOpacity),
|
||||
lineWidth: MergerView.containerBorderLineWidth
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
// ParallelSectionView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// Renders a ``ParallelSection`` as a horizontal parallel rack.
|
||||
///
|
||||
/// Layout (left → right):
|
||||
/// 1. ``SplitterView`` — mode selector and branch LED indicators.
|
||||
/// 2. A central `VStack` with two labelled branch rows:
|
||||
/// - **A** row: one ``PedalView`` per pedal in `section.branchA`.
|
||||
/// - **B** row: one ``PedalView`` per pedal in `section.branchB`.
|
||||
/// Empty branches show a placeholder label.
|
||||
///
|
||||
/// `ParallelSection` does not expose a `Merger`; the merger is owned by `PedalBoard`
|
||||
/// and is not part of the section object. Only the splitter and branch pedal rows are
|
||||
/// rendered here. The view wraps its central panel in a horizontal `ScrollView` so
|
||||
/// wide branch chains do not overflow the window.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// ParallelSectionView(section: parallelSection)
|
||||
/// ```
|
||||
public struct ParallelSectionView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let outerSpacing: CGFloat = 16
|
||||
private static let branchStackSpacing: CGFloat = 12
|
||||
private static let pedalSpacing: CGFloat = 16
|
||||
private static let branchLabelFontSize: CGFloat = 11
|
||||
private static let branchLabelOpacity: Double = 0.55
|
||||
private static let emptyBranchOpacity: Double = 0.30
|
||||
private static let emptyBranchFontSize: CGFloat = 11
|
||||
private static let scrollPaddingH: CGFloat = 12
|
||||
private static let scrollPaddingV: CGFloat = 8
|
||||
private static let branchSeparatorOpacity: Double = 0.15
|
||||
private static let branchSeparatorHeight: CGFloat = 1
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let section: ParallelSection
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a parallel section view for the given ``ParallelSection``.
|
||||
///
|
||||
/// - Parameter section: The `ParallelSection` to render. Both branch pedal arrays
|
||||
/// and the splitter's enable nodes are read from this instance.
|
||||
public init(section: ParallelSection) {
|
||||
self.section = section
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
HStack(alignment: .top, spacing: ParallelSectionView.outerSpacing) {
|
||||
SplitterView(splitter: section.splitter)
|
||||
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
VStack(alignment: .leading, spacing: ParallelSectionView.branchStackSpacing) {
|
||||
branchRow(label: "A", pedals: section.branchA.pedals)
|
||||
|
||||
Rectangle()
|
||||
.fill(Color.white.opacity(ParallelSectionView.branchSeparatorOpacity))
|
||||
.frame(height: ParallelSectionView.branchSeparatorHeight)
|
||||
|
||||
branchRow(label: "B", pedals: section.branchB.pedals)
|
||||
}
|
||||
.padding(.horizontal, ParallelSectionView.scrollPaddingH)
|
||||
.padding(.vertical, ParallelSectionView.scrollPaddingV)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
/// A labelled row of pedal cards for one branch.
|
||||
private func branchRow(label: String, pedals: [Pedal]) -> some View {
|
||||
HStack(alignment: .bottom, spacing: ParallelSectionView.pedalSpacing) {
|
||||
Text(label)
|
||||
.font(.system(
|
||||
size: ParallelSectionView.branchLabelFontSize,
|
||||
weight: .semibold,
|
||||
design: .monospaced
|
||||
))
|
||||
.foregroundStyle(Color.white.opacity(ParallelSectionView.branchLabelOpacity))
|
||||
.frame(width: 14, alignment: .leading)
|
||||
|
||||
if pedals.isEmpty {
|
||||
Text("(empty)")
|
||||
.font(.system(size: ParallelSectionView.emptyBranchFontSize))
|
||||
.foregroundStyle(Color.white.opacity(ParallelSectionView.emptyBranchOpacity))
|
||||
} else {
|
||||
ForEach(Array(pedals.enumerated()), id: \.offset) { pair in
|
||||
let pedal: Pedal = pair.element
|
||||
PedalView(pedal: pedal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
// PedalView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
// MARK: - Accent-color mapping (UI-only, never in the core target)
|
||||
|
||||
/// Returns a stompbox enclosure tint colour keyed on the pedal's display name.
|
||||
///
|
||||
/// The core `Pedal` type cannot carry a SwiftUI `Color`, so the mapping lives
|
||||
/// here in the UI target. Unrecognised names fall through to `.gray`.
|
||||
private func enclosureColor(for displayName: String) -> Color {
|
||||
switch displayName {
|
||||
case "Overdrive": return Color(red: 0.85, green: 0.42, blue: 0.08) // warm orange
|
||||
case "Delay": return Color(red: 0.10, green: 0.55, blue: 0.55) // teal
|
||||
case "Reverb": return Color(red: 0.30, green: 0.18, blue: 0.65) // indigo
|
||||
case "Modulation": return Color(red: 0.12, green: 0.58, blue: 0.22) // green
|
||||
case "Fuzz": return Color(red: 0.55, green: 0.10, blue: 0.10) // deep crimson
|
||||
case "Tremolo": return Color(red: 0.20, green: 0.38, blue: 0.72) // cobalt blue
|
||||
case "Phaser": return Color(red: 0.60, green: 0.22, blue: 0.62) // violet
|
||||
case "Compressor": return Color(red: 0.18, green: 0.42, blue: 0.32) // forest green
|
||||
default: return Color(red: 0.36, green: 0.36, blue: 0.38) // steel gray
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Screw decoration
|
||||
|
||||
/// A small circular "screw head" decoration placed in each corner of the enclosure.
|
||||
private struct ScrewDot: View {
|
||||
|
||||
private static let size: CGFloat = 8
|
||||
private static let strokeWidth: CGFloat = 1
|
||||
private static let slotWidth: CGFloat = 1
|
||||
private static let slotHeight: CGFloat = 4
|
||||
private static let fillOpacity: Double = 0.25
|
||||
private static let rimOpacity: Double = 0.18
|
||||
private static let slotOpacity: Double = 0.50
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Circle()
|
||||
.fill(Color.black.opacity(ScrewDot.fillOpacity))
|
||||
.frame(width: ScrewDot.size, height: ScrewDot.size)
|
||||
Circle()
|
||||
.stroke(Color.white.opacity(ScrewDot.rimOpacity), lineWidth: ScrewDot.strokeWidth)
|
||||
.frame(width: ScrewDot.size, height: ScrewDot.size)
|
||||
// Slot line — Phillips-style cross
|
||||
Rectangle()
|
||||
.fill(Color.black.opacity(ScrewDot.slotOpacity))
|
||||
.frame(width: ScrewDot.slotWidth, height: ScrewDot.slotHeight)
|
||||
Rectangle()
|
||||
.fill(Color.black.opacity(ScrewDot.slotOpacity))
|
||||
.frame(width: ScrewDot.slotHeight, height: ScrewDot.slotWidth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - LED
|
||||
|
||||
/// A small circular LED that glows green when the effect is engaged.
|
||||
///
|
||||
/// Bypass polarity: `0` = engaged (effect ON), `1` = bypassed.
|
||||
/// The LED is lit when `bypassValue < 0.5`.
|
||||
private struct LEDIndicator: View {
|
||||
|
||||
private static let diameter: CGFloat = 10
|
||||
private static let glowRadius: CGFloat = 6
|
||||
private static let glowOpacity: Double = 0.75
|
||||
private static let rimLineWidth: CGFloat = 0.5
|
||||
private static let dimBrightness: Double = 0.15
|
||||
private static let rimStrokeOpacity: Double = 0.30
|
||||
|
||||
let bypassValue: Double
|
||||
|
||||
private var isEngaged: Bool { bypassValue < 0.5 }
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
if isEngaged {
|
||||
Circle()
|
||||
.fill(Color.green.opacity(LEDIndicator.glowOpacity))
|
||||
.frame(
|
||||
width: LEDIndicator.diameter + LEDIndicator.glowRadius,
|
||||
height: LEDIndicator.diameter + LEDIndicator.glowRadius
|
||||
)
|
||||
.blur(radius: LEDIndicator.glowRadius / 2)
|
||||
}
|
||||
Circle()
|
||||
.fill(isEngaged ? Color.green : Color(white: LEDIndicator.dimBrightness))
|
||||
.frame(width: LEDIndicator.diameter, height: LEDIndicator.diameter)
|
||||
.overlay(
|
||||
Circle()
|
||||
.stroke(Color.white.opacity(LEDIndicator.rimStrokeOpacity), lineWidth: LEDIndicator.rimLineWidth)
|
||||
.frame(width: LEDIndicator.diameter, height: LEDIndicator.diameter)
|
||||
)
|
||||
}
|
||||
.frame(width: LEDIndicator.diameter + LEDIndicator.glowRadius,
|
||||
height: LEDIndicator.diameter + LEDIndicator.glowRadius)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Footswitch
|
||||
|
||||
/// A round footswitch-style button that toggles bypass on/off.
|
||||
private struct FootswitchButton: View {
|
||||
|
||||
private static let diameter: CGFloat = 44
|
||||
private static let strokeWidth: CGFloat = 2.5
|
||||
private static let innerDiameter: CGFloat = 36
|
||||
private static let outerFillBrightness: Double = 0.18
|
||||
private static let ringBrightness: Double = 0.50
|
||||
private static let innerFillBrightness: Double = 0.25
|
||||
|
||||
let node: UIParamNode
|
||||
|
||||
var body: some View {
|
||||
Button(action: {
|
||||
let engaged: Bool = node.value < 0.5
|
||||
// Toggle: engaged (0) → bypassed (1) and vice-versa.
|
||||
node.update(engaged ? 1.0 : 0.0)
|
||||
}) {
|
||||
ZStack {
|
||||
Circle()
|
||||
.fill(Color(white: FootswitchButton.outerFillBrightness))
|
||||
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
|
||||
Circle()
|
||||
.stroke(Color(white: FootswitchButton.ringBrightness),
|
||||
lineWidth: FootswitchButton.strokeWidth)
|
||||
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
|
||||
Circle()
|
||||
.fill(Color(white: FootswitchButton.innerFillBrightness))
|
||||
.frame(width: FootswitchButton.innerDiameter, height: FootswitchButton.innerDiameter)
|
||||
}
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - PedalView
|
||||
|
||||
/// A stompbox-style enclosure view that renders all controls for a single `Pedal`.
|
||||
///
|
||||
/// The enclosure shows:
|
||||
/// - Corner screw decorations.
|
||||
/// - A contrasting name-plate capsule near the top with `displayName`.
|
||||
/// - A grid of ``ParameterKnob`` controls (up to ``knobColumnCount`` per row) drawn from
|
||||
/// the effect + `Mix` knobs of ``Pedal/knobs``.
|
||||
/// - A centred bottom row pairing the `Level` knob with the bypass switch (LED + footswitch).
|
||||
///
|
||||
/// ## Bypass LED polarity
|
||||
///
|
||||
/// `Pedal.bypass` is `0` = engaged (effect active), `1` = bypassed.
|
||||
/// The LED is **green/lit** when `bypass < 0.5` (effect is on) and dark when bypassed.
|
||||
///
|
||||
/// ## Accent color
|
||||
///
|
||||
/// The enclosure tint is assigned by `enclosureColor(for:)` — a private helper in
|
||||
/// this file keyed on `displayName`. Core `Pedal` never imports SwiftUI.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// PedalView(pedal: overdrivePedal)
|
||||
/// ```
|
||||
public struct PedalView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
/// Wide enough for three ~55 pt knobs (dial + label + value) plus padding in a
|
||||
/// single row, so the effect controls never overlap.
|
||||
private static let enclosureWidth: CGFloat = 220
|
||||
private static let enclosureCornerRadius: CGFloat = 12
|
||||
private static let enclosurePadding: CGFloat = 14
|
||||
private static let screwInset: CGFloat = 6
|
||||
private static let knobColumnCount: Int = 3
|
||||
private static let knobSpacing: CGFloat = 10
|
||||
/// Horizontal gap between the Level knob and the bypass control in the bottom row.
|
||||
private static let bottomControlsSpacing: CGFloat = 18
|
||||
private static let sectionSpacing: CGFloat = 14
|
||||
private static let namePlateCornerRadius: CGFloat = 8
|
||||
private static let namePlatePaddingH: CGFloat = 10
|
||||
private static let namePlatePaddingV: CGFloat = 4
|
||||
private static let outerBorderOpacity: Double = 0.60
|
||||
private static let innerHighlightOpacity: Double = 0.22
|
||||
private static let shadowRadius: CGFloat = 8
|
||||
private static let shadowYOffset: CGFloat = 4
|
||||
private static let shadowOpacity: Double = 0.60
|
||||
private static let enclosureGradientDarkFraction: Double = 0.30
|
||||
private static let bypassSectionSpacing: CGFloat = 6
|
||||
/// Half the screw-dot size — used to centre screws on the corner position.
|
||||
private static let screwHalfSize: CGFloat = 4
|
||||
private static let namePlateBackgroundOpacity: Double = 0.35
|
||||
private static let fontSizeNamePlate: CGFloat = 13
|
||||
private static let outerBorderLineWidth: CGFloat = 1.5
|
||||
private static let innerHighlightLineWidth: CGFloat = 1
|
||||
private static let innerHighlightPadding: CGFloat = 1
|
||||
private static let shadowXOffset: CGFloat = 0
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let pedal: Pedal
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a stompbox pedal view for the given pedal.
|
||||
///
|
||||
/// - Parameter pedal: The `Pedal` whose parameters will be displayed.
|
||||
public init(pedal: Pedal) {
|
||||
self.pedal = pedal
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
let accent: Color = enclosureColor(for: pedal.displayName)
|
||||
let darkAccent: Color = accent.opacity(PedalView.enclosureGradientDarkFraction)
|
||||
|
||||
ZStack(alignment: .topLeading) {
|
||||
// Enclosure fill with top-to-bottom gradient
|
||||
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius)
|
||||
.fill(
|
||||
LinearGradient(
|
||||
gradient: Gradient(colors: [accent, darkAccent]),
|
||||
startPoint: .top,
|
||||
endPoint: .bottom
|
||||
)
|
||||
)
|
||||
|
||||
// Outer border
|
||||
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius)
|
||||
.stroke(Color.black.opacity(PedalView.outerBorderOpacity),
|
||||
lineWidth: PedalView.outerBorderLineWidth)
|
||||
|
||||
// Inner highlight rim (subtle bevel)
|
||||
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius - PedalView.innerHighlightPadding)
|
||||
.stroke(Color.white.opacity(PedalView.innerHighlightOpacity),
|
||||
lineWidth: PedalView.innerHighlightLineWidth)
|
||||
.padding(PedalView.innerHighlightPadding)
|
||||
|
||||
// Content stack
|
||||
VStack(spacing: PedalView.sectionSpacing) {
|
||||
namePlate
|
||||
knobGrid
|
||||
Spacer(minLength: 0)
|
||||
bottomControls
|
||||
}
|
||||
.padding(PedalView.enclosurePadding)
|
||||
|
||||
// Corner screws — overlaid at the four corners
|
||||
screwCorners
|
||||
}
|
||||
.frame(width: PedalView.enclosureWidth)
|
||||
.shadow(
|
||||
color: Color.black.opacity(PedalView.shadowOpacity),
|
||||
radius: PedalView.shadowRadius,
|
||||
x: PedalView.shadowXOffset,
|
||||
y: PedalView.shadowYOffset
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
/// Capsule name plate near the top of the enclosure.
|
||||
private var namePlate: some View {
|
||||
Text(pedal.displayName)
|
||||
.font(.system(size: PedalView.fontSizeNamePlate, weight: .bold, design: .rounded))
|
||||
.foregroundStyle(Color.white)
|
||||
.padding(.horizontal, PedalView.namePlatePaddingH)
|
||||
.padding(.vertical, PedalView.namePlatePaddingV)
|
||||
.background(Color.black.opacity(PedalView.namePlateBackgroundOpacity))
|
||||
.clipShape(RoundedRectangle(cornerRadius: PedalView.namePlateCornerRadius))
|
||||
}
|
||||
|
||||
/// Grid of the effect + `Mix` knobs, in rows of up to `knobColumnCount`.
|
||||
///
|
||||
/// The `Level` knob (the last entry of `pedal.knobs`) is dropped here and rendered in
|
||||
/// the bottom control row next to the bypass switch, leaving more room for the effect
|
||||
/// controls at the top.
|
||||
private var knobGrid: some View {
|
||||
let knobs: [PedalKnob] = Array(pedal.knobs.dropLast())
|
||||
let rows: [[PedalKnob]] = stride(
|
||||
from: 0,
|
||||
to: knobs.count,
|
||||
by: PedalView.knobColumnCount
|
||||
).map { startIndex in
|
||||
Array(knobs[startIndex ..< min(startIndex + PedalView.knobColumnCount, knobs.count)])
|
||||
}
|
||||
|
||||
return VStack(spacing: PedalView.knobSpacing) {
|
||||
ForEach(Array(rows.enumerated()), id: \.offset) { rowPair in
|
||||
let row: [PedalKnob] = rowPair.element
|
||||
HStack(spacing: PedalView.knobSpacing) {
|
||||
ForEach(Array(row.enumerated()), id: \.offset) { knobPair in
|
||||
let knob: PedalKnob = knobPair.element
|
||||
ParameterKnob(knob.label, node: knob.node, range: knob.range)
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
// Pad short rows so knobs stay aligned in their columns.
|
||||
if row.count < PedalView.knobColumnCount {
|
||||
ForEach(0 ..< (PedalView.knobColumnCount - row.count), id: \.self) { _ in
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bottom control row: the `Level` knob and the bypass switch side-by-side, centred
|
||||
/// with equal padding below.
|
||||
private var bottomControls: some View {
|
||||
HStack(alignment: .center, spacing: PedalView.bottomControlsSpacing) {
|
||||
levelControl
|
||||
bypassControl
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
|
||||
/// The `Level` knob — the last entry of `pedal.knobs`.
|
||||
@ViewBuilder
|
||||
private var levelControl: some View {
|
||||
if let level: PedalKnob = pedal.knobs.last {
|
||||
ParameterKnob(level.label, node: level.node, range: level.range)
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
}
|
||||
|
||||
/// LED + footswitch bypass control, paired beside the Level knob.
|
||||
private var bypassControl: some View {
|
||||
// Reading `pedal.$bypass.value` here registers this view for automatic
|
||||
// re-renders whenever the bypass parameter changes on the main thread
|
||||
// (UIParamNode is @Observable).
|
||||
let bypassValue: Double = pedal.$bypass.value
|
||||
|
||||
return VStack(spacing: PedalView.bypassSectionSpacing) {
|
||||
LEDIndicator(bypassValue: bypassValue)
|
||||
FootswitchButton(node: pedal.$bypass)
|
||||
}
|
||||
}
|
||||
|
||||
/// Four screw dots overlaid at the enclosure corners.
|
||||
private var screwCorners: some View {
|
||||
GeometryReader { geometry in
|
||||
let w: CGFloat = geometry.size.width
|
||||
let h: CGFloat = geometry.size.height
|
||||
let inset: CGFloat = PedalView.screwInset
|
||||
|
||||
let half: CGFloat = PedalView.screwHalfSize
|
||||
|
||||
ZStack(alignment: .topLeading) {
|
||||
// Top-left
|
||||
ScrewDot()
|
||||
.position(x: inset + half, y: inset + half)
|
||||
// Top-right
|
||||
ScrewDot()
|
||||
.position(x: w - inset - half, y: inset + half)
|
||||
// Bottom-left
|
||||
ScrewDot()
|
||||
.position(x: inset + half, y: h - inset - half)
|
||||
// Bottom-right
|
||||
ScrewDot()
|
||||
.position(x: w - inset - half, y: h - inset - half)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// ShoeGazeUI.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// LyrebirdShoeGazeUI views are implemented in:
|
||||
// PedalView.swift — single pedal card
|
||||
// PedalBoardView.swift — horizontal board strip
|
||||
// ShoeGazeView.swift — top-level session view with transport
|
||||
// FlowLayout.swift — wrap-to-fit Layout (left-to-right, row-wrapping)
|
||||
// BoardEditorView.swift — per-board editor with FlowLayout pedal area + version store
|
||||
@@ -0,0 +1,381 @@
|
||||
// ShoeGazeView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(AppKit)
|
||||
import AppKit
|
||||
#endif
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// The top-level view for a `ShoeGaze` session.
|
||||
///
|
||||
/// Displays a transport header (Start / Stop engine buttons and an Add Board button),
|
||||
/// a vertically scrollable list of ``PedalBoardView`` strips (one per board), and a
|
||||
/// ``LyrebirdStatusView`` CPU meter at the bottom, all on a dark backdrop that matches
|
||||
/// the stompbox enclosure aesthetic.
|
||||
///
|
||||
/// ## Board identity keying
|
||||
///
|
||||
/// The boards `ForEach` uses `id: \.offset` on an enumerated copy of `shoeGaze.boards`.
|
||||
/// `PedalBoard` is a `@MainActor` class and is neither `Identifiable` nor `Hashable`
|
||||
/// (adding conformance to the core type would violate the module-layering constraint).
|
||||
/// Index keying is stable for the simple append/remove-by-position pattern this demo
|
||||
/// uses and avoids any protocol requirements on the core `PedalBoard` type.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// ShoeGazeView(shoeGaze: shoeGaze)
|
||||
/// ```
|
||||
public struct ShoeGazeView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let outerPadding: CGFloat = 20
|
||||
private static let sectionSpacing: CGFloat = 16
|
||||
private static let transportSpacing: CGFloat = 12
|
||||
private static let boardSpacing: CGFloat = 20
|
||||
private static let minWidth: CGFloat = 400
|
||||
private static let dividerOpacity: Double = 0.12
|
||||
/// Stage-floor dark background colour components (very dark warm black).
|
||||
private static let backgroundRed: Double = 0.08
|
||||
private static let backgroundGreen: Double = 0.07
|
||||
private static let backgroundBlue: Double = 0.06
|
||||
/// Default crossfade duration in seconds for board-select transitions.
|
||||
private static let defaultCrossfadeSeconds: Double = 0.3
|
||||
/// Maximum crossfade duration accepted by the crossfade seconds stepper.
|
||||
private static let maxCrossfadeSeconds: Double = 10.0
|
||||
/// Stepper increment for the crossfade seconds control.
|
||||
private static let crossfadeStepIncrement: Double = 0.1
|
||||
/// Width of the crossfade-seconds text field.
|
||||
private static let crossfadeFieldWidth: CGFloat = 60
|
||||
/// Spacing inside the board-select row.
|
||||
private static let boardSelectSpacing: CGFloat = 8
|
||||
/// Opacity for the board-select row labels.
|
||||
private static let labelOpacity: Double = 0.65
|
||||
/// Diameter of the recording-in-progress indicator dot.
|
||||
private static let recordDotSize: CGFloat = 10
|
||||
/// Opacity of the secondary folder-name label shown next to the folder picker button.
|
||||
private static let folderLabelOpacity: Double = 0.55
|
||||
/// Spacing inside the recording controls sub-row.
|
||||
private static let recordingRowSpacing: CGFloat = 10
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let shoeGaze: ShoeGaze
|
||||
|
||||
/// Bumped on each transport button press to force a re-read of the engine's
|
||||
/// running state. The button labels/enabled-state read `shoeGaze.engine.isRunning`
|
||||
/// directly (the single source of truth) rather than a private mirror that could
|
||||
/// drift out of sync if the engine is started/stopped elsewhere.
|
||||
@State private var transportTick: Bool = false
|
||||
|
||||
/// Crossfade duration used for the next board-select transition.
|
||||
@State private var crossfadeSeconds: Double = ShoeGazeView.defaultCrossfadeSeconds
|
||||
|
||||
/// Controls presentation of the iOS `.fileImporter` directory picker.
|
||||
/// On macOS the picker is opened imperatively via `NSOpenPanel`; this flag is
|
||||
/// only meaningful on iOS / non-macOS targets.
|
||||
@State private var showFolderImporter: Bool = false
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a top-level ShoeGaze view.
|
||||
///
|
||||
/// - Parameter shoeGaze: The `ShoeGaze` manager whose boards and engine will
|
||||
/// be displayed. The caller is responsible for engine lifecycle; this view
|
||||
/// provides Start and Stop transport buttons as a convenience.
|
||||
public init(shoeGaze: ShoeGaze) {
|
||||
self.shoeGaze = shoeGaze
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
VStack(alignment: .leading, spacing: ShoeGazeView.sectionSpacing) {
|
||||
transportHeader
|
||||
recordingRow
|
||||
Divider()
|
||||
.overlay(Color.white.opacity(ShoeGazeView.dividerOpacity))
|
||||
boardSelectRow
|
||||
Divider()
|
||||
.overlay(Color.white.opacity(ShoeGazeView.dividerOpacity))
|
||||
boardList
|
||||
Divider()
|
||||
.overlay(Color.white.opacity(ShoeGazeView.dividerOpacity))
|
||||
LyrebirdStatusView(engine: shoeGaze.engine)
|
||||
}
|
||||
.padding(ShoeGazeView.outerPadding)
|
||||
.frame(minWidth: ShoeGazeView.minWidth)
|
||||
.background(
|
||||
Color(
|
||||
red: ShoeGazeView.backgroundRed,
|
||||
green: ShoeGazeView.backgroundGreen,
|
||||
blue: ShoeGazeView.backgroundBlue
|
||||
)
|
||||
)
|
||||
#if !os(macOS)
|
||||
.fileImporter(
|
||||
isPresented: $showFolderImporter,
|
||||
allowedContentTypes: [.folder]
|
||||
) { result in
|
||||
switch result {
|
||||
case .success(let url):
|
||||
// A `.fileImporter` directory URL is security-scoped on iOS; access must
|
||||
// be started before `FileManager` can write into it. Kept open for the
|
||||
// session (released when the app exits).
|
||||
if url.startAccessingSecurityScopedResource() {
|
||||
shoeGaze.recordingFolder = url
|
||||
} else {
|
||||
print("[ShoeGazeView] could not access selected folder")
|
||||
}
|
||||
case .failure(let error):
|
||||
print("[ShoeGazeView] folder picker failed: \(error)")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
/// Transport bar: title, Start/Stop engine buttons, and Add Board button.
|
||||
///
|
||||
/// The button state reads `shoeGaze.engine.isRunning` (the single source of truth)
|
||||
/// so it stays correct even when the engine is started/stopped by another control.
|
||||
/// Audio only flows once the user presses Start — adding boards does not auto-start
|
||||
/// the engine.
|
||||
private var transportHeader: some View {
|
||||
// Referencing `transportTick` makes a press (which toggles it) re-evaluate this
|
||||
// view so `engine.isRunning` is re-read; toggling any @State invalidates the view.
|
||||
_ = transportTick
|
||||
let isRunning: Bool = shoeGaze.engine.isRunning
|
||||
return HStack(spacing: ShoeGazeView.transportSpacing) {
|
||||
Text("ShoeGaze")
|
||||
.font(.title2)
|
||||
.bold()
|
||||
.foregroundStyle(Color.white)
|
||||
Spacer()
|
||||
Button("Add Board") {
|
||||
addBoard()
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.tint(.orange)
|
||||
|
||||
Button("Start") {
|
||||
do {
|
||||
try shoeGaze.engine.start()
|
||||
} catch {
|
||||
// Surface start errors to the console; the UI remains in the
|
||||
// non-running state so the user can retry.
|
||||
print("[ShoeGazeView] 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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Recording controls: folder picker, chosen-folder label, and record/stop button.
|
||||
///
|
||||
/// The folder picker opens an `NSOpenPanel` on macOS or a `.fileImporter` sheet on iOS.
|
||||
/// The Record button is disabled until a folder has been chosen; it turns into a Stop
|
||||
/// button (red) while recording is in progress.
|
||||
private var recordingRow: some View {
|
||||
HStack(spacing: ShoeGazeView.recordingRowSpacing) {
|
||||
// --- Folder picker button ---
|
||||
Button("Choose Folder…") {
|
||||
chooseFolderAction()
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.tint(.blue)
|
||||
|
||||
// --- Chosen folder label ---
|
||||
let folderName: String = shoeGaze.recordingFolder?.lastPathComponent ?? "No folder"
|
||||
Text(folderName)
|
||||
.font(.caption)
|
||||
.foregroundStyle(Color.white.opacity(ShoeGazeView.folderLabelOpacity))
|
||||
.lineLimit(1)
|
||||
.truncationMode(.middle)
|
||||
|
||||
Spacer()
|
||||
|
||||
// --- Recording-in-progress indicator dot ---
|
||||
if shoeGaze.isRecording {
|
||||
Circle()
|
||||
.fill(Color.red)
|
||||
.frame(
|
||||
width: ShoeGazeView.recordDotSize,
|
||||
height: ShoeGazeView.recordDotSize
|
||||
)
|
||||
}
|
||||
|
||||
// --- Record / Stop button ---
|
||||
Button(shoeGaze.isRecording ? "Stop" : "Record") {
|
||||
if shoeGaze.isRecording {
|
||||
shoeGaze.stopRecording()
|
||||
} else {
|
||||
do {
|
||||
try shoeGaze.startRecording()
|
||||
} catch {
|
||||
print("[ShoeGazeView] startRecording() failed: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.tint(shoeGaze.isRecording ? .red : .accentColor)
|
||||
.disabled(shoeGaze.recordingFolder == nil)
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Binding<Int>` that maps between the Picker's integer tag and `ShoeGaze` select API.
|
||||
///
|
||||
/// Tag `-1` represents "All boards audible" (`activeBoardIndex == nil`).
|
||||
/// Tags `0 ..< boards.count` represent the soloed board at that index.
|
||||
/// Setting the binding calls `selectBoard(_:crossfadeSeconds:)` or
|
||||
/// `selectAllBoards(crossfadeSeconds:)` with the current `crossfadeSeconds` value.
|
||||
private var boardSelectBinding: Binding<Int> {
|
||||
Binding(
|
||||
get: { shoeGaze.activeBoardIndex ?? -1 },
|
||||
set: { newValue in
|
||||
// Clamp here so a value typed into the crossfade text field (which has no
|
||||
// built-in bounds) can never pass a negative or oversized lag time.
|
||||
let seconds: Double = min(max(crossfadeSeconds, 0.0), ShoeGazeView.maxCrossfadeSeconds)
|
||||
if newValue == -1 {
|
||||
shoeGaze.selectAllBoards(crossfadeSeconds: seconds)
|
||||
} else if newValue >= 0, newValue < shoeGaze.boards.count {
|
||||
shoeGaze.selectBoard(shoeGaze.boards[newValue], crossfadeSeconds: seconds)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/// Board-select row: an "Active" Picker to solo a board (or hear all), a crossfade-
|
||||
/// seconds text field, and a companion Stepper for convenient adjustment.
|
||||
///
|
||||
/// The row is always visible. When `boards.count <= 1` the Picker has only one or two
|
||||
/// options, which is still useful for the "All" / single-board toggle.
|
||||
private var boardSelectRow: some View {
|
||||
HStack(spacing: ShoeGazeView.boardSelectSpacing) {
|
||||
Text("Active:")
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Color.white.opacity(ShoeGazeView.labelOpacity))
|
||||
|
||||
Picker("Active", selection: boardSelectBinding) {
|
||||
Text("All").tag(-1)
|
||||
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)
|
||||
|
||||
Spacer()
|
||||
|
||||
Text("Crossfade (s):")
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Color.white.opacity(ShoeGazeView.labelOpacity))
|
||||
|
||||
TextField(
|
||||
"s",
|
||||
value: $crossfadeSeconds,
|
||||
format: .number.precision(.fractionLength(1))
|
||||
)
|
||||
.frame(width: ShoeGazeView.crossfadeFieldWidth)
|
||||
#if os(macOS)
|
||||
.textFieldStyle(.squareBorder)
|
||||
#endif
|
||||
|
||||
Stepper(
|
||||
"",
|
||||
value: $crossfadeSeconds,
|
||||
in: 0...ShoeGazeView.maxCrossfadeSeconds,
|
||||
step: ShoeGazeView.crossfadeStepIncrement
|
||||
)
|
||||
.labelsHidden()
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertically scrollable list of board strips.
|
||||
///
|
||||
/// Keyed by `\.offset` on an enumerated snapshot; see the doc-comment on
|
||||
/// `ShoeGazeView` for the rationale.
|
||||
private var boardList: some View {
|
||||
ScrollView(.vertical, showsIndicators: true) {
|
||||
VStack(alignment: .leading, spacing: ShoeGazeView.boardSpacing) {
|
||||
ForEach(Array(shoeGaze.boards.enumerated()), id: \.offset) { pair in
|
||||
let board: PedalBoard = pair.element
|
||||
PedalBoardView(board: board) {
|
||||
shoeGaze.removeBoard(board)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Recording
|
||||
|
||||
/// Opens a directory picker so the user can choose the recording destination folder.
|
||||
///
|
||||
/// On macOS an `NSOpenPanel` is presented imperatively (directories only, no file
|
||||
/// selection, no multiple selection). On iOS/non-macOS the `.fileImporter` modifier
|
||||
/// attached to the view hierarchy is toggled via `showFolderImporter`.
|
||||
private func chooseFolderAction() {
|
||||
#if os(macOS)
|
||||
let panel: NSOpenPanel = NSOpenPanel()
|
||||
panel.canChooseDirectories = true
|
||||
panel.canChooseFiles = false
|
||||
panel.allowsMultipleSelection = false
|
||||
panel.canCreateDirectories = true // show the "New Folder" button
|
||||
panel.prompt = "Choose"
|
||||
panel.message = "Select a folder for recording output"
|
||||
// `begin` is non-blocking (unlike `runModal`, which would freeze the SwiftUI
|
||||
// main-thread update loop while the panel is open).
|
||||
panel.begin { response in
|
||||
if response == .OK, let url: URL = panel.url {
|
||||
shoeGaze.recordingFolder = url
|
||||
}
|
||||
}
|
||||
#else
|
||||
showFolderImporter = true
|
||||
#endif
|
||||
}
|
||||
|
||||
// MARK: - Board management
|
||||
|
||||
/// Adds a new board with one `OverdrivePedal` as a default pedal so the board
|
||||
/// produces audible output immediately after being finalized. If any step after
|
||||
/// `shoeGaze.addBoard()` throws, the partially-built board is removed via
|
||||
/// `shoeGaze.removeBoard(_:)` before logging, leaving the rig in a consistent state.
|
||||
private func addBoard() {
|
||||
var createdBoard: PedalBoard?
|
||||
do {
|
||||
let board: PedalBoard = try shoeGaze.addBoard()
|
||||
createdBoard = board
|
||||
try board.add(OverdrivePedal())
|
||||
try shoeGaze.finalizeBoard(board)
|
||||
} catch {
|
||||
if let board: PedalBoard = createdBoard {
|
||||
shoeGaze.removeBoard(board)
|
||||
}
|
||||
print("[ShoeGazeView] addBoard() failed: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
// SplitterView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// A compact routing-box view for a ``Splitter`` that exposes a 3-way mode selector
|
||||
/// and two live LED indicators reflecting each branch's current enable value.
|
||||
///
|
||||
/// The segmented `Picker` is driven by a computed `Binding<SplitMode>` whose `get` reads
|
||||
/// `splitter.mode` directly and whose `set` writes back to `splitter.mode`, so the picker
|
||||
/// stays in sync whether the mode is changed via the UI or programmatically.
|
||||
///
|
||||
/// Because `body` already reads `splitter.$branchAEnabled.value` and
|
||||
/// `splitter.$branchBEnabled.value` (both `@Observable`), SwiftUI re-renders the view on
|
||||
/// any external mode change and the binding getter returns the correct value.
|
||||
///
|
||||
/// LED polarity: a branch LED is **lit** (blue/teal) when its `UIParamNode` value is
|
||||
/// ≥ 0.5, and dark when the branch is muted.
|
||||
///
|
||||
/// ## Usage
|
||||
///
|
||||
/// ```swift
|
||||
/// SplitterView(splitter: section.splitter)
|
||||
/// ```
|
||||
public struct SplitterView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let containerCornerRadius: CGFloat = 10
|
||||
private static let containerPaddingH: CGFloat = 14
|
||||
private static let containerPaddingV: CGFloat = 10
|
||||
private static let containerBorderOpacity: Double = 0.55
|
||||
private static let containerBorderLineWidth: CGFloat = 1.0
|
||||
private static let sectionSpacing: CGFloat = 8
|
||||
private static let ledRowSpacing: CGFloat = 16
|
||||
private static let titleFontSize: CGFloat = 11
|
||||
private static let titleOpacity: Double = 0.55
|
||||
private static let ledLabelFontSize: CGFloat = 10
|
||||
private static let ledLabelOpacity: Double = 0.60
|
||||
private static let ledDiameter: CGFloat = 8
|
||||
private static let ledGlowRadius: CGFloat = 5
|
||||
private static let ledGlowOpacity: Double = 0.80
|
||||
private static let ledDimBrightness: Double = 0.12
|
||||
private static let ledRimOpacity: Double = 0.30
|
||||
private static let ledRimLineWidth: CGFloat = 0.5
|
||||
private static let ledPairSpacing: CGFloat = 6
|
||||
/// Threshold above which a UIParamNode enable value is considered "on".
|
||||
private static let enableOnThreshold: Double = 0.5
|
||||
/// Background colour components for the routing-box enclosure (slate).
|
||||
private static let backgroundRed: Double = 0.20
|
||||
private static let backgroundGreen: Double = 0.23
|
||||
private static let backgroundBlue: Double = 0.28
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let splitter: Splitter
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a splitter view for the given ``Splitter``.
|
||||
///
|
||||
/// - Parameter splitter: The `Splitter` whose mode and branch enable values will be
|
||||
/// displayed and edited. The picker reads and writes `splitter.mode` directly via
|
||||
/// a computed binding, so external mode changes are always reflected correctly.
|
||||
public init(splitter: Splitter) {
|
||||
self.splitter = splitter
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
// Read branch enable values — because UIParamNode is @Observable, SwiftUI
|
||||
// automatically re-renders this view whenever either value changes on the main thread.
|
||||
// The mode binding getter also reads splitter.mode directly, so the picker stays in
|
||||
// sync with any programmatic mode change.
|
||||
let aValue: Double = splitter.$branchAEnabled.value
|
||||
let bValue: Double = splitter.$branchBEnabled.value
|
||||
let modeBinding: Binding<SplitMode> = Binding(
|
||||
get: { splitter.mode },
|
||||
set: { splitter.mode = $0 }
|
||||
)
|
||||
|
||||
VStack(spacing: SplitterView.sectionSpacing) {
|
||||
Text("SPLIT")
|
||||
.font(.system(size: SplitterView.titleFontSize, weight: .semibold, design: .monospaced))
|
||||
.foregroundStyle(Color.white.opacity(SplitterView.titleOpacity))
|
||||
|
||||
Picker("Mode", selection: modeBinding) {
|
||||
ForEach(SplitMode.allCases, id: \.self) { splitMode in
|
||||
Text(splitMode.rawValue.uppercased())
|
||||
.tag(splitMode)
|
||||
}
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
|
||||
branchLEDs(aValue: aValue, bValue: bValue)
|
||||
}
|
||||
.padding(.horizontal, SplitterView.containerPaddingH)
|
||||
.padding(.vertical, SplitterView.containerPaddingV)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: SplitterView.containerCornerRadius)
|
||||
.fill(Color(
|
||||
red: SplitterView.backgroundRed,
|
||||
green: SplitterView.backgroundGreen,
|
||||
blue: SplitterView.backgroundBlue
|
||||
))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: SplitterView.containerCornerRadius)
|
||||
.stroke(
|
||||
Color.white.opacity(SplitterView.containerBorderOpacity),
|
||||
lineWidth: SplitterView.containerBorderLineWidth
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
/// Pair of LED indicators showing live branch enable state.
|
||||
private func branchLEDs(aValue: Double, bValue: Double) -> some View {
|
||||
HStack(spacing: SplitterView.ledRowSpacing) {
|
||||
branchLED(label: "A", value: aValue)
|
||||
branchLED(label: "B", value: bValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// A single labelled LED dot for one branch.
|
||||
private func branchLED(label: String, value: Double) -> some View {
|
||||
let isOn: Bool = value >= SplitterView.enableOnThreshold
|
||||
|
||||
return VStack(spacing: SplitterView.ledPairSpacing) {
|
||||
Text(label)
|
||||
.font(.system(size: SplitterView.ledLabelFontSize, weight: .medium, design: .monospaced))
|
||||
.foregroundStyle(Color.white.opacity(SplitterView.ledLabelOpacity))
|
||||
ZStack {
|
||||
if isOn {
|
||||
Circle()
|
||||
.fill(Color.cyan.opacity(SplitterView.ledGlowOpacity))
|
||||
.frame(
|
||||
width: SplitterView.ledDiameter + SplitterView.ledGlowRadius,
|
||||
height: SplitterView.ledDiameter + SplitterView.ledGlowRadius
|
||||
)
|
||||
.blur(radius: SplitterView.ledGlowRadius / 2)
|
||||
}
|
||||
Circle()
|
||||
.fill(isOn ? Color.cyan : Color(white: SplitterView.ledDimBrightness))
|
||||
.frame(width: SplitterView.ledDiameter, height: SplitterView.ledDiameter)
|
||||
.overlay(
|
||||
Circle()
|
||||
.stroke(
|
||||
Color.white.opacity(SplitterView.ledRimOpacity),
|
||||
lineWidth: SplitterView.ledRimLineWidth
|
||||
)
|
||||
.frame(width: SplitterView.ledDiameter, height: SplitterView.ledDiameter)
|
||||
)
|
||||
}
|
||||
.frame(
|
||||
width: SplitterView.ledDiameter + SplitterView.ledGlowRadius,
|
||||
height: SplitterView.ledDiameter + SplitterView.ledGlowRadius
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user