v2.3.0 release: onEditingEnded edit-ended callback on continuous controls

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 13:59:52 -08:00
parent 24ea32e47c
commit 9f244b1939
8 changed files with 325 additions and 2 deletions
@@ -35,6 +35,13 @@ public struct ParameterSlider: View {
private let range: ClosedRange<Double>
private let taper: Taper
/// Called once when a drag ends, with the value when the drag began and the final value. Set via
/// ``onEditingEnded(_:)``; lets a host record one edit per gesture (e.g. for undo).
private var onEditingEndedAction: ((Double, Double) -> Void)?
/// Value captured when a drag begins, so ``onEditingEnded(_:)`` can report `(previous, new)`.
@State private var dragStartValue: Double = 0
// MARK: - Init
/// Creates a slider bound to a `UIParamNode`.
@@ -63,13 +70,33 @@ public struct ParameterSlider: View {
HStack {
Text(label)
.frame(minWidth: 80, alignment: .leading)
Slider(value: binding, in: linearRange) { _ in }
Slider(value: binding, in: linearRange) { editing in
if editing {
dragStartValue = node.value
} else {
onEditingEndedAction?(dragStartValue, node.value)
}
}
Text(formattedValue)
.frame(minWidth: 60, alignment: .trailing)
.monospacedDigit()
}
}
// MARK: - Modifiers
/// Returns a copy of the slider that calls `action` each time a drag ends — once per gesture —
/// with the value when the drag began and the final value. Use it to record a single edit (for
/// example an undo checkpoint). Matches ``ParameterKnob/onEditingEnded(_:)``.
///
/// - Parameter action: Receives `(previousValue, newValue)`.
/// - Returns: A slider that reports the end of each edit.
public func onEditingEnded(_ action: @escaping (Double, Double) -> Void) -> ParameterSlider {
var copy: ParameterSlider = self
copy.onEditingEndedAction = action
return copy
}
// MARK: - Private helpers
/// Linear range always 0...1; taper mapping happens inside the Binding.
@@ -176,6 +203,14 @@ public struct ParameterSlider: View {
/// visible at both ends.
private let thumbRadius: Double = 7
/// Called once when a drag ends, with the drag's start value and final value. Set via
/// ``onEditingEnded(_:)``; mirrors the macOS slider.
private var onEditingEndedAction: ((Double, Double) -> Void)?
// Drag origin, captured on the first `onChanged`.
@State private var dragStartValue: Double = 0
@State private var dragInProgress: Bool = false
// MARK: - Init
/// Creates a slider bound to a `UIParamNode`.
@@ -237,11 +272,19 @@ public struct ParameterSlider: View {
.onDrag(
minimumDistance: 0,
onChanged: { (drag: DragGestureValue) in
if !dragInProgress {
dragInProgress = true
dragStartValue = node.value
}
// Absolute: the thumb follows the pointer x within the track.
let x0: Double = thumbRadius
let usable: Double = max(trackWidth - 2 * thumbRadius, 1)
let pos: Double = min(1, max(0, (drag.location.x - x0) / usable))
node.update(fromLinear(pos))
},
onEnded: { (_: DragGestureValue) in
dragInProgress = false
onEditingEndedAction?(dragStartValue, node.value)
}
)
Text(formattedValue)
@@ -250,6 +293,17 @@ public struct ParameterSlider: View {
}
}
/// Returns a copy of the slider that calls `action` each time a drag ends — once per gesture —
/// with the drag's start value and final value. Mirrors the macOS slider's ``onEditingEnded(_:)``.
///
/// - Parameter action: Receives `(previousValue, newValue)`.
/// - Returns: A slider that reports the end of each edit.
public func onEditingEnded(_ action: @escaping (Double, Double) -> Void) -> ParameterSlider {
var copy: ParameterSlider = self
copy.onEditingEndedAction = action
return copy
}
// MARK: - Private helpers
private func toLinear(_ value: Double) -> Double {