// PedalPaletteView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdShoeGaze /// A vertical catalog of every available pedal plus a reference list of the routing/branching /// options, sized to sit in a narrow left sidebar. /// /// The pedal rows are tappable: selecting one invokes `onAdd(type)` with the pedal's /// `displayName`, which the host wraps in a `PedalType` for `ShoeGaze.appendPedal(_:to:)`. The routing cards /// (Parallel split, Stereo split, Chain router, Merge to mono) are informational only — they /// describe the topologies the engine supports, which are built programmatically rather than via /// linear add (see ``BoardBuilderView`` for the read-only display of existing branches). /// /// ## Usage /// /// ```swift /// PedalPaletteView { type in /// try? shoeGaze.appendPedal(PedalType(type), to: board) /// } /// ``` public struct PedalPaletteView: View { // MARK: - Layout constants private static let sectionSpacing: CGFloat = 14 private static let rowSpacing: CGFloat = 6 private static let titleFontSize: CGFloat = 12 private static let titleOpacity: Double = 0.7 private static let cardCornerRadius: CGFloat = 7 private static let cardPaddingH: CGFloat = 10 private static let cardPaddingV: CGFloat = 7 private static let cardBorderOpacity: Double = 0.4 private static let cardBorderWidth: CGFloat = 1 private static let pedalCardBrightness: Double = 0.18 private static let routingCardBrightness: Double = 0.1 private static let routingCaptionOpacity: Double = 0.5 private static let routingCaptionFontSize: CGFloat = 10 private static let addGlyphOpacity: Double = 0.4 /// Vertical gap between a pedal row's name and its description subtitle. private static let rowSubtitleSpacing: CGFloat = 2 /// Opacity of the pedal-row description subtitle. private static let summaryOpacity: Double = 0.55 /// Fixed card width when laid out horizontally (the vertical layout stretches to fill instead). private static let horizontalCardWidth: CGFloat = 150 /// Preferred display order for the built-in pedals. Any registered pedal not listed here is /// appended afterwards (sorted) so the palette never silently drops a newly added pedal. private static let preferredOrder: [String] = [ "Overdrive", "Distortion", "Compressor", "Sustainer", "Limiter", "Noise Gate", "Fuzz", "Graphic EQ", "Tremolo", "Phaser", "Modulation", "Chorus", "Flanger", "Vibrato", "Rotary", "Auto-Wah", "Mutant", "Ring Modulator", "Diffuser", "Bass Octaver", "Octave Splitter", "Harmonizer", "Delay", "Feedback Delay", "Multi-Tap Delay", "Ping-Pong", "Reverse Delay", "Reverb", ] /// Routing/branching topologies the user can insert into the board. private static let routingOptions: [(title: String, caption: String, action: RoutingAction)] = [ ("Stereo Split", "L/R independent lanes", .stereoSplit), ("Parallel Split", "A/B branches, mixed", .parallelSplit), ("Merge to Mono", "Collapse stereo → mono", .mergeToMono), ] /// Routing actions a host can perform from the palette. public enum RoutingAction: Equatable, Sendable { case stereoSplit case parallelSplit case mergeToMono case chainRouter } // MARK: - Properties private let onAdd: (String) -> Void private let onRouting: (RoutingAction) -> Void private let axis: Axis // MARK: - Init /// Creates a pedal palette. /// /// - Parameters: /// - axis: `.vertical` (default) lays the catalog out as a full-width column for a sidebar; /// `.horizontal` lays it out as a fixed-width-card row for a horizontal scroller. The host /// wraps the view in a `ScrollView` of the matching axis. /// - onAdd: Invoked with a pedal's `displayName` when its row is tapped (append to main chain). /// Rows are also `.draggable` carrying `"add:"` for drop-onto-lane targeting. /// - onRouting: Invoked when a routing row is tapped. Defaults to a no-op. public init( axis: Axis = .vertical, onAdd: @escaping (String) -> Void, onRouting: @escaping (RoutingAction) -> Void = { _ in } ) { self.axis = axis self.onAdd = onAdd self.onRouting = onRouting } /// Registered pedal names in preferred order, with any unlisted registered pedals appended. private var orderedPedalNames: [String] { let registered: Set = Set(PedalRegistry.builtIn.keys) let ordered: [String] = PedalPaletteView.preferredOrder.filter { registered.contains($0) } let extras: [String] = registered .subtracting(ordered) .sorted() return ordered + extras } // MARK: - Body public var body: some View { switch axis { case .horizontal: horizontalBody default: verticalBody } } /// Full-width column for a left sidebar. private var verticalBody: some View { VStack(alignment: .leading, spacing: PedalPaletteView.sectionSpacing) { VStack(alignment: .leading, spacing: PedalPaletteView.rowSpacing) { sectionTitle("PEDALS") ForEach(orderedPedalNames, id: \.self) { name in pedalRow(name) } } VStack(alignment: .leading, spacing: PedalPaletteView.rowSpacing) { sectionTitle("ROUTING") ForEach(PedalPaletteView.routingOptions, id: \.title) { option in routingCard(title: option.title, caption: option.caption, action: option.action) } } } } /// Fixed-width-card row for a horizontal scroller: leading PEDALS group, a divider, then the /// ROUTING group. private var horizontalBody: some View { HStack(alignment: .top, spacing: PedalPaletteView.rowSpacing) { sectionTitle("PEDALS") ForEach(orderedPedalNames, id: \.self) { name in pedalRow(name) } Divider().overlay(Color.white.opacity(PedalPaletteView.cardBorderOpacity)) sectionTitle("ROUTING") ForEach(PedalPaletteView.routingOptions, id: \.title) { option in routingCard(title: option.title, caption: option.caption, action: option.action) } } } // MARK: - Subviews private func sectionTitle(_ text: String) -> some View { Text(text) .font(.system(size: PedalPaletteView.titleFontSize, weight: .semibold)) .foregroundStyle(Color.white.opacity(PedalPaletteView.titleOpacity)) } /// A full-width pedal row: tap appends to the main chain; drag carries `"add:"` so it can /// be dropped onto a stereo/parallel lane. private func pedalRow(_ name: String) -> some View { let summary: String = PedalRegistry.summary(for: name) return Button { onAdd(name) } label: { VStack(alignment: .leading, spacing: PedalPaletteView.rowSubtitleSpacing) { HStack(spacing: 4) { Text(name) .font(.callout) .foregroundStyle(Color.white) .lineLimit(1) Spacer(minLength: 0) Image(systemName: "plus") .font(.caption2) .foregroundStyle(Color.white.opacity(PedalPaletteView.addGlyphOpacity)) } if !summary.isEmpty { Text(summary) .font(.caption2) .foregroundStyle(Color.white.opacity(PedalPaletteView.summaryOpacity)) .lineLimit(2) .multilineTextAlignment(.leading) .fixedSize(horizontal: false, vertical: true) } } .padding(.horizontal, PedalPaletteView.cardPaddingH) .padding(.vertical, PedalPaletteView.cardPaddingV) .modifier(CardFrame(axis: axis, width: PedalPaletteView.horizontalCardWidth)) .background(cardBackground(brightness: PedalPaletteView.pedalCardBrightness)) } .buttonStyle(.plain) .draggable("add:\(name)") } /// A tappable routing row that inserts a region/node into the board. private func routingCard(title: String, caption: String, action: RoutingAction) -> some View { Button { onRouting(action) } label: { VStack(alignment: .leading, spacing: 2) { Text(title) .font(.callout) .foregroundStyle(Color.white) Text(caption) .font(.system(size: PedalPaletteView.routingCaptionFontSize)) .foregroundStyle(Color.white.opacity(PedalPaletteView.routingCaptionOpacity)) } .padding(.horizontal, PedalPaletteView.cardPaddingH) .padding(.vertical, PedalPaletteView.cardPaddingV) .modifier(CardFrame(axis: axis, width: PedalPaletteView.horizontalCardWidth)) .background(cardBackground(brightness: PedalPaletteView.routingCardBrightness)) } .buttonStyle(.plain) } /// Stretches a card to fill width in a vertical column, or pins it to a fixed width in a /// horizontal row. private struct CardFrame: ViewModifier { let axis: Axis let width: CGFloat func body(content: Content) -> some View { switch axis { case .horizontal: content.frame(width: width, alignment: .leading) default: content.frame(maxWidth: .infinity, alignment: .leading) } } } private func cardBackground(brightness: Double) -> some View { RoundedRectangle(cornerRadius: PedalPaletteView.cardCornerRadius) .fill(Color(white: brightness)) .overlay( RoundedRectangle(cornerRadius: PedalPaletteView.cardCornerRadius) .stroke( Color.white.opacity(PedalPaletteView.cardBorderOpacity), lineWidth: PedalPaletteView.cardBorderWidth ) ) } }