// VerticalParameterSlider.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdUI /// A vertical fader bound to a single ``UIParamNode``. /// /// Drag anywhere on the track to set the value: dragging to the top maps to /// `range.upperBound`, dragging to the bottom maps to `range.lowerBound`. A filled bar /// grows up from the bottom to show the current position and a thin thumb marks the value. /// The control reads `node.value` (so it re-renders when the parameter changes elsewhere) /// and writes back via `node.update(_:)`. /// /// `PedalView` renders a row of these for the graphic-EQ-style fader bank a pedal /// publishes through `Pedal.effectSliders`. struct VerticalParameterSlider: View { // MARK: - Layout constants private static let trackWidth: CGFloat = 10 private static let trackCornerRadius: CGFloat = 5 private static let thumbHeight: CGFloat = 4 private static let thumbWidth: CGFloat = 20 private static let thumbCornerRadius: CGFloat = 2 private static let controlWidth: CGFloat = 28 private static let trackHeight: CGFloat = 110 private static let labelSpacing: CGFloat = 4 private static let valueSpacing: CGFloat = 4 private static let fontSizeLabel: CGFloat = 9 private static let fontSizeValue: CGFloat = 9 private static let trackBackgroundOpacity: Double = 0.30 private static let fillTopOpacity: Double = 0.95 private static let fillBottomOpacity: Double = 0.55 private static let thumbOpacity: Double = 0.95 private static let labelOpacity: Double = 0.85 private static let valueOpacity: Double = 0.70 /// Range span at or above which the value is shown as a whole number (dB bands) rather /// than with two decimals (narrow linear ranges like the master gain). private static let wideSpanThreshold: Double = 5.0 // MARK: - Properties private let label: String private let node: UIParamNode private let range: ClosedRange // MARK: - Init /// Creates a vertical fader. /// /// - Parameters: /// - label: Short caption shown beneath the fader. /// - node: The `UIParamNode` the fader reads and writes. /// - range: Inclusive value range the fader spans, in the parameter's units. init( _ label: String, node: UIParamNode, range: ClosedRange ) { self.label = label self.node = node self.range = range } // MARK: - Value mapping /// Current value clamped into `range`. private var clampedValue: Double { min(max(node.value, range.lowerBound), range.upperBound) } /// Current value as a `[0, 1]` fraction of the range (0 = bottom, 1 = top). private var fraction: CGFloat { let span: Double = range.upperBound - range.lowerBound guard span > 0 else { return 0 } return CGFloat((clampedValue - range.lowerBound) / span) } // MARK: - Body var body: some View { VStack(spacing: VerticalParameterSlider.labelSpacing) { track Text(label) .font(.system(size: VerticalParameterSlider.fontSizeLabel, weight: .semibold)) .foregroundStyle(Color.white.opacity(VerticalParameterSlider.labelOpacity)) .lineLimit(1) Text(valueText) .font(.system(size: VerticalParameterSlider.fontSizeValue, design: .monospaced)) .foregroundStyle(Color.white.opacity(VerticalParameterSlider.valueOpacity)) .lineLimit(1) } .frame(width: VerticalParameterSlider.controlWidth) } /// The draggable track with background, value fill, and thumb. private var track: some View { GeometryReader { geometry in let height: CGFloat = geometry.size.height let fillHeight: CGFloat = height * fraction ZStack(alignment: .bottom) { // Background track RoundedRectangle(cornerRadius: VerticalParameterSlider.trackCornerRadius) .fill(Color.black.opacity(VerticalParameterSlider.trackBackgroundOpacity)) .frame(width: VerticalParameterSlider.trackWidth) // Value fill growing up from the bottom RoundedRectangle(cornerRadius: VerticalParameterSlider.trackCornerRadius) .fill( LinearGradient( colors: [ Color.white.opacity(VerticalParameterSlider.fillTopOpacity), Color.white.opacity(VerticalParameterSlider.fillBottomOpacity), ], startPoint: .top, endPoint: .bottom ) ) .frame(width: VerticalParameterSlider.trackWidth, height: fillHeight) // Thumb marking the current value. Clamp its centre into the track so it // never overhangs the ends at the value extremes. let halfThumb: CGFloat = VerticalParameterSlider.thumbHeight / 2 let thumbCentre: CGFloat = min(max(fillHeight, halfThumb), height - halfThumb) RoundedRectangle(cornerRadius: VerticalParameterSlider.thumbCornerRadius) .fill(Color.white.opacity(VerticalParameterSlider.thumbOpacity)) .frame( width: VerticalParameterSlider.thumbWidth, height: VerticalParameterSlider.thumbHeight ) .offset(y: -(thumbCentre - halfThumb)) } .frame(maxWidth: .infinity, maxHeight: .infinity) .contentShape(Rectangle()) .gesture( DragGesture(minimumDistance: 0) .onChanged { drag in updateValue(forY: drag.location.y, height: height) } ) } .frame(width: VerticalParameterSlider.controlWidth, height: VerticalParameterSlider.trackHeight) } /// Formats the current value: integers when the span is wide (dB bands), two decimals otherwise. private var valueText: String { let span: Double = range.upperBound - range.lowerBound if span >= VerticalParameterSlider.wideSpanThreshold { return String(format: "%.0f", clampedValue) } return String(format: "%.2f", clampedValue) } // MARK: - Drag handling /// Converts a vertical drag position into a parameter value and writes it to the node. /// /// - Parameters: /// - y: Drag Y position in the track's coordinate space (0 = top). /// - height: Track height in points. private func updateValue(forY y: CGFloat, height: CGFloat) { guard height > 0 else { return } // Invert: top of the track (y = 0) is the maximum, bottom is the minimum. let clampedY: CGFloat = min(max(y, 0), height) let frac: Double = Double(1 - clampedY / height) let span: Double = range.upperBound - range.lowerBound node.update(range.lowerBound + frac * span) } }