// SplitterView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdSynth 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` 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 = 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 ) } } }