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:
@@ -27,6 +27,11 @@ public struct ParameterKnob: View {
|
||||
/// Pixels of vertical drag that span the full parameter range. Default 200.
|
||||
public var dragSensitivity: Double = 200
|
||||
|
||||
/// Called once when a drag gesture ends, with the value at the start of the drag and the final
|
||||
/// value. Set via ``onEditingEnded(_:)``. Lets a host record a single edit per gesture (e.g. for
|
||||
/// undo) rather than one per intermediate `node.update(_:)`.
|
||||
private var onEditingEndedAction: ((Double, Double) -> Void)?
|
||||
|
||||
// MARK: - Private drag state
|
||||
|
||||
@State private var dragStartPosition: CGFloat = 0
|
||||
@@ -116,12 +121,30 @@ public struct ParameterKnob: View {
|
||||
node.update(clamped)
|
||||
}
|
||||
.onEnded { _ in
|
||||
let previousValue: Double = dragStartValue
|
||||
dragInProgress = false
|
||||
dragStartPosition = 0
|
||||
dragStartValue = 0
|
||||
onEditingEndedAction?(previousValue, node.value)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Modifiers
|
||||
|
||||
/// Returns a copy of the knob that calls `action` each time a drag gesture ends — once per
|
||||
/// gesture, not per intermediate update — with the value at the start of the drag and the final
|
||||
/// value. Use it to record a single edit (for example an undo checkpoint) when the user finishes
|
||||
/// turning the knob.
|
||||
///
|
||||
/// - Parameter action: Receives `(previousValue, newValue)` — the knob's value when the drag
|
||||
/// began and when it ended.
|
||||
/// - Returns: A knob that reports the end of each edit.
|
||||
public func onEditingEnded(_ action: @escaping (Double, Double) -> Void) -> ParameterKnob {
|
||||
var copy: ParameterKnob = self
|
||||
copy.onEditingEndedAction = action
|
||||
return copy
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func normalise(_ value: Double) -> Double {
|
||||
@@ -173,6 +196,10 @@ public struct ParameterKnob: View {
|
||||
/// macOS knob's default.
|
||||
public var dragSensitivity: Double = 200
|
||||
|
||||
/// Called once when a drag ends, with the drag's start value and final value. Set via
|
||||
/// ``onEditingEnded(_:)``; mirrors the macOS knob so a host can record one edit per gesture.
|
||||
private var onEditingEndedAction: ((Double, Double) -> Void)?
|
||||
|
||||
// Drag origin, captured on the first `onChanged` of each gesture. An
|
||||
// explicit flag avoids a sentinel collision when the value is genuinely at
|
||||
// the range's lower bound.
|
||||
@@ -235,7 +262,9 @@ public struct ParameterKnob: View {
|
||||
node.update(target)
|
||||
},
|
||||
onEnded: { (_: DragGestureValue) in
|
||||
let previousValue: Double = dragStartValue
|
||||
dragInProgress = false
|
||||
onEditingEndedAction?(previousValue, node.value)
|
||||
}
|
||||
)
|
||||
Text(label)
|
||||
@@ -243,6 +272,17 @@ public struct ParameterKnob: View {
|
||||
.padding()
|
||||
}
|
||||
|
||||
/// Returns a copy of the knob that calls `action` each time a drag ends — once per gesture —
|
||||
/// with the drag's start value and final value. Mirrors the macOS knob's ``onEditingEnded(_:)``.
|
||||
///
|
||||
/// - Parameter action: Receives `(previousValue, newValue)`.
|
||||
/// - Returns: A knob that reports the end of each edit.
|
||||
public func onEditingEnded(_ action: @escaping (Double, Double) -> Void) -> ParameterKnob {
|
||||
var copy: ParameterKnob = self
|
||||
copy.onEditingEndedAction = action
|
||||
return copy
|
||||
}
|
||||
|
||||
private func normalise(_ value: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
|
||||
Reference in New Issue
Block a user