62 lines
1.6 KiB
Swift
62 lines
1.6 KiB
Swift
// ParameterLabel.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import SwiftUI
|
|
import LyrebirdSynth
|
|
|
|
/// A read-only display label that shows the current value of a `UIParamNode`.
|
|
///
|
|
/// Updates automatically via `@Observable` on the backing node — no polling.
|
|
///
|
|
/// ```swift
|
|
/// ParameterLabel("Level", node: synth.$amplitude)
|
|
/// ParameterLabel("Freq (Hz)", node: synth.$frequency, decimalPlaces: 1)
|
|
/// ```
|
|
public struct ParameterLabel: View {
|
|
|
|
// MARK: - Properties
|
|
|
|
private let label: String
|
|
private let node: UIParamNode
|
|
private let decimalPlaces: Int
|
|
|
|
// MARK: - Init
|
|
|
|
/// Creates a read-only parameter label.
|
|
///
|
|
/// - Parameters:
|
|
/// - label: Descriptive text displayed before the value.
|
|
/// - node: The `UIParamNode` to monitor (pass `synth.$paramName`).
|
|
/// - decimalPlaces: Number of decimal places shown. Default 3.
|
|
public init(
|
|
_ label: String,
|
|
node: UIParamNode,
|
|
decimalPlaces: Int = 3
|
|
) {
|
|
self.label = label
|
|
self.node = node
|
|
self.decimalPlaces = decimalPlaces
|
|
}
|
|
|
|
// MARK: - Body
|
|
|
|
public var body: some View {
|
|
HStack {
|
|
Text(label)
|
|
.frame(minWidth: 80, alignment: .leading)
|
|
Text(formattedValue)
|
|
.monospacedDigit()
|
|
.frame(minWidth: 60, alignment: .trailing)
|
|
}
|
|
}
|
|
|
|
// MARK: - Formatting
|
|
|
|
private var formattedValue: String {
|
|
String(format: "%.\(decimalPlaces)f", node.value)
|
|
}
|
|
}
|