// 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 /// Category-colour fill / border opacity for the colour-coded pedal cards. private static let categoryFillOpacity: Double = 0.22 private static let categoryBorderOpacity: Double = 0.65 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", "Volume", "Graphic EQ", "Tremolo", "Phaser", "Modulation", "Chorus", "Flanger", "Vibrato", "Rotary", "Auto-Wah", "Wah", "Mutant", "Ring Modulator", "Diffuser", "Bitcrusher", "Granulator", "Auto Granulator", "Bass Octaver", "Octave Splitter", "Harmonizer", "Delay", "Feedback Delay", "Multi-Tap Delay", "Ping-Pong", "Reverse Delay", "Reverb", "Shimmer Reverb", "Plate Reverb", "Room Reverb", "Spring 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 /// Active type filter; `nil` shows all pedals. @State private var filter: PedalCategory? // 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 } /// Pedal names after applying the active type filter. private var visiblePedalNames: [String] { guard let filter: PedalCategory = filter else { return orderedPedalNames } return orderedPedalNames.filter { PedalCategory.category(for: $0) == filter } } /// Visible pedals bucketed by category, in the same order the filter menu lists them /// (`PedalCategory.allCases`). Within a bucket the preferred order is preserved; empty /// buckets are dropped so the picker shows only categories that have visible pedals. private var groupedVisiblePedals: [(category: PedalCategory, names: [String])] { let visible: [String] = visiblePedalNames return PedalCategory.allCases.compactMap { category in let names: [String] = visible.filter { PedalCategory.category(for: $0) == category } return names.isEmpty ? nil : (category, names) } } // 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) { HStack { sectionTitle("PEDALS") Spacer(minLength: 0) filterMenu } ForEach(visiblePedalNames, 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) } } } } /// Horizontal scroller: a PEDALS row (leading filter + colour-coded cards) with the ROUTING /// row stacked beneath it. private var horizontalBody: some View { VStack(alignment: .leading, spacing: PedalPaletteView.sectionSpacing) { HStack(alignment: .top, spacing: PedalPaletteView.sectionSpacing) { VStack(alignment: .leading, spacing: PedalPaletteView.rowSpacing) { sectionTitle("PEDALS") filterMenu } ForEach(groupedVisiblePedals, id: \.category) { group in VStack(alignment: .leading, spacing: PedalPaletteView.rowSpacing) { categoryLabel(group.category) HStack(alignment: .top, spacing: PedalPaletteView.rowSpacing) { ForEach(group.names, id: \.self) { name in pedalRow(name) } } } } } HStack(alignment: .top, spacing: PedalPaletteView.rowSpacing) { 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 per-group header in the horizontal picker — the category name tinted in its accent colour. private func categoryLabel(_ category: PedalCategory) -> some View { Text(category.rawValue.uppercased()) .font(.system(size: PedalPaletteView.titleFontSize, weight: .semibold)) .foregroundStyle(category.color.opacity(0.9)) .lineLimit(1) } /// "Filter by type" menu — defaults to **All** (no filtering). private var filterMenu: some View { Menu { Button("All Types") { filter = nil } Divider() ForEach(PedalCategory.allCases) { category in Button(category.rawValue) { filter = category } } } label: { HStack(spacing: 3) { Image(systemName: "line.3.horizontal.decrease.circle") Text(filter?.rawValue ?? "All").lineLimit(1) } .font(.system(size: PedalPaletteView.titleFontSize)) .foregroundStyle((filter?.color ?? Color.white).opacity(0.9)) } #if os(macOS) .menuStyle(.borderlessButton) .fixedSize() #endif } /// 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(categoryCardBackground(PedalCategory.category(for: name).color)) } .buttonStyle(.plain) .draggable("add:\(name)") } /// Category-tinted card background — a low-opacity fill plus a stronger border in the category /// colour, so palette blocks are colour-coded by type. private func categoryCardBackground(_ color: Color) -> some View { RoundedRectangle(cornerRadius: PedalPaletteView.cardCornerRadius) .fill(color.opacity(PedalPaletteView.categoryFillOpacity)) .overlay( RoundedRectangle(cornerRadius: PedalPaletteView.cardCornerRadius) .stroke(color.opacity(PedalPaletteView.categoryBorderOpacity), lineWidth: PedalPaletteView.cardBorderWidth) ) } /// 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 ) ) } }