// StereoRouteControlView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdUI import LyrebirdShoeGaze /// A compact routing box for a ``StereoSplitSection`` exposing a 4-way ``StereoRoute`` /// selector and live indicators for the active route and which output channels carry signal. /// /// The segmented `Picker` is driven by a computed `Binding` whose `get`/`set` /// read and write `section.route`, so it stays in sync with programmatic changes. When the /// section has been collected (its `collector` exists), the view also reads the collector's /// `@Observable` route gains so the channel LEDs re-render as the route crossfades. /// /// Source LEDs show what is feeding the output for the active route: /// - `stereo` → L and R branch LEDs lit (both chains feed their channel). /// - `leftOnly` → L lit (left chain, centered to both channels). /// - `rightOnly` → R lit (right chain, centered to both channels). /// - `bypass` → DRY lit (clean pre-split signal; both branches muted). /// /// ## Usage /// /// ```swift /// StereoRouteControlView(section: stereoSplitSection) /// ``` public struct StereoRouteControlView: 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 titleFontSize: CGFloat = 11 private static let titleOpacity: Double = 0.55 private static let ledRowSpacing: CGFloat = 16 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 /// 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 /// Width allotted to each segmented label so the four route names fit. private static let pickerMinWidth: CGFloat = 220 /// Short uppercase label shown in the segmented control for each route. private static func segmentLabel(_ route: StereoRoute) -> String { switch route { case .stereo: return "STEREO" case .leftOnly: return "L" case .rightOnly: return "R" case .bypass: return "BYP" } } // MARK: - Properties private let section: StereoSplitSection // MARK: - Init /// Creates a route control view for the given ``StereoSplitSection``. /// /// - Parameter section: The stereo split section whose ``StereoSplitSection/route`` is /// displayed and edited. public init(section: StereoSplitSection) { self.section = section } // MARK: - Body /// The active route, after touching the collector's `@Observable` route gains so SwiftUI /// re-renders the LEDs as the route crossfades or changes programmatically. private var liveRoute: StereoRoute { _ = section.collector?.$gainStereo.value _ = section.collector?.$gainLeftSolo.value _ = section.collector?.$gainRightSolo.value _ = section.collector?.$gainBypass.value return section.route } public var body: some View { let routeBinding: Binding = Binding( get: { section.route }, set: { section.route = $0 } ) let active: StereoRoute = liveRoute VStack(spacing: StereoRouteControlView.sectionSpacing) { Text("STEREO ROUTE") .font(.system(size: StereoRouteControlView.titleFontSize, weight: .semibold, design: .monospaced)) .foregroundStyle(Color.white.opacity(StereoRouteControlView.titleOpacity)) Picker("Route", selection: routeBinding) { ForEach(StereoRoute.allCases, id: \.self) { route in Text(StereoRouteControlView.segmentLabel(route)) .tag(route) } } .pickerStyle(.segmented) .frame(minWidth: StereoRouteControlView.pickerMinWidth) sourceLEDs(active: active) } .padding(.horizontal, StereoRouteControlView.containerPaddingH) .padding(.vertical, StereoRouteControlView.containerPaddingV) .background( RoundedRectangle(cornerRadius: StereoRouteControlView.containerCornerRadius) .fill(Color( red: StereoRouteControlView.backgroundRed, green: StereoRouteControlView.backgroundGreen, blue: StereoRouteControlView.backgroundBlue )) .overlay( RoundedRectangle(cornerRadius: StereoRouteControlView.containerCornerRadius) .stroke( Color.white.opacity(StereoRouteControlView.containerBorderOpacity), lineWidth: StereoRouteControlView.containerBorderLineWidth ) ) ) } // MARK: - Subviews /// L / R / DRY source LEDs reflecting what feeds the output for the active route. private func sourceLEDs(active: StereoRoute) -> some View { let leftLit: Bool = (active == .stereo || active == .leftOnly) let rightLit: Bool = (active == .stereo || active == .rightOnly) let dryLit: Bool = (active == .bypass) return HStack(spacing: StereoRouteControlView.ledRowSpacing) { sourceLED(label: "L", lit: leftLit) sourceLED(label: "R", lit: rightLit) sourceLED(label: "DRY", lit: dryLit) } } /// A single labelled LED dot for one signal source. private func sourceLED(label: String, lit: Bool) -> some View { VStack(spacing: StereoRouteControlView.ledPairSpacing) { Text(label) .font(.system(size: StereoRouteControlView.ledLabelFontSize, weight: .medium, design: .monospaced)) .foregroundStyle(Color.white.opacity(StereoRouteControlView.ledLabelOpacity)) ZStack { if lit { Circle() .fill(Color.cyan.opacity(StereoRouteControlView.ledGlowOpacity)) .frame( width: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius, height: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius ) .blur(radius: StereoRouteControlView.ledGlowRadius / 2) } Circle() .fill(lit ? Color.cyan : Color(white: StereoRouteControlView.ledDimBrightness)) .frame(width: StereoRouteControlView.ledDiameter, height: StereoRouteControlView.ledDiameter) .overlay( Circle() .stroke( Color.white.opacity(StereoRouteControlView.ledRimOpacity), lineWidth: StereoRouteControlView.ledRimLineWidth ) .frame(width: StereoRouteControlView.ledDiameter, height: StereoRouteControlView.ledDiameter) ) } .frame( width: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius, height: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius ) } } }