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:
@@ -31,6 +31,15 @@ public struct ParameterXY: View {
|
||||
private let yNode: UIParamNode
|
||||
private let yRange: ClosedRange<Double>
|
||||
|
||||
/// Called once when a drag ends, with the `(x, y)` values when the drag began and when it ended.
|
||||
/// Set via ``onEditingEnded(_:)``; lets a host record one edit per gesture across both axes.
|
||||
private var onEditingEndedAction: ((_ previous: (x: Double, y: Double), _ new: (x: Double, y: Double)) -> Void)?
|
||||
|
||||
// Both nodes' values captured when a drag begins.
|
||||
@State private var dragStartX: Double = 0
|
||||
@State private var dragStartY: Double = 0
|
||||
@State private var dragInProgress: Bool = false
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates an XY pad bound to two `UIParamNode` values.
|
||||
@@ -59,6 +68,20 @@ public struct ParameterXY: View {
|
||||
self.yRange = yRange
|
||||
}
|
||||
|
||||
/// Returns a copy of the pad that calls `action` each time a drag ends — once per gesture — with
|
||||
/// the `(x, y)` values when the drag began and when it ended. Use it to record a single
|
||||
/// two-axis edit (for example an undo checkpoint). Matches ``ParameterKnob/onEditingEnded(_:)``.
|
||||
///
|
||||
/// - Parameter action: Receives `(previous, new)`, each an `(x, y)` value pair.
|
||||
/// - Returns: A pad that reports the end of each edit.
|
||||
public func onEditingEnded(
|
||||
_ action: @escaping (_ previous: (x: Double, y: Double), _ new: (x: Double, y: Double)) -> Void
|
||||
) -> ParameterXY {
|
||||
var copy: ParameterXY = self
|
||||
copy.onEditingEndedAction = action
|
||||
return copy
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
@@ -134,11 +157,20 @@ public struct ParameterXY: View {
|
||||
private func dragGesture(in size: CGSize) -> some Gesture {
|
||||
DragGesture(minimumDistance: 0)
|
||||
.onChanged { gesture in
|
||||
if !dragInProgress {
|
||||
dragInProgress = true
|
||||
dragStartX = xNode.value
|
||||
dragStartY = yNode.value
|
||||
}
|
||||
let nx: CGFloat = min(max(gesture.location.x / size.width, 0), 1)
|
||||
let ny: CGFloat = min(max(gesture.location.y / size.height, 0), 1)
|
||||
xNode.update(denormalise(nx, range: xRange))
|
||||
yNode.update(denormalise(1.0 - ny, range: yRange)) // flip Y
|
||||
}
|
||||
.onEnded { _ in
|
||||
dragInProgress = false
|
||||
onEditingEndedAction?((x: dragStartX, y: dragStartY), (x: xNode.value, y: yNode.value))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
@@ -203,6 +235,14 @@ public struct ParameterXY: View {
|
||||
private let padSize: Double = 160
|
||||
private let cursorRadius: Double = 6
|
||||
|
||||
/// Called once when a drag ends, with the `(x, y)` values at the drag's start and end. Set via
|
||||
/// ``onEditingEnded(_:)``; mirrors the macOS pad.
|
||||
private var onEditingEndedAction: ((_ previous: (x: Double, y: Double), _ new: (x: Double, y: Double)) -> Void)?
|
||||
|
||||
@State private var dragStartX: Double = 0
|
||||
@State private var dragStartY: Double = 0
|
||||
@State private var dragInProgress: Bool = false
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates an XY pad bound to two `UIParamNode` values.
|
||||
@@ -274,11 +314,20 @@ public struct ParameterXY: View {
|
||||
.onDrag(
|
||||
minimumDistance: 0,
|
||||
onChanged: { (drag: DragGestureValue) in
|
||||
if !dragInProgress {
|
||||
dragInProgress = true
|
||||
dragStartX = xNode.value
|
||||
dragStartY = yNode.value
|
||||
}
|
||||
// Absolute position: the touch point is the cursor.
|
||||
let nx: Double = min(max(drag.location.x / padSize, 0), 1)
|
||||
let ny: Double = min(max(drag.location.y / padSize, 0), 1)
|
||||
xNode.update(denormalise(nx, range: xRange))
|
||||
yNode.update(denormalise(1.0 - ny, range: yRange)) // flip Y
|
||||
},
|
||||
onEnded: { (_: DragGestureValue) in
|
||||
dragInProgress = false
|
||||
onEditingEndedAction?((x: dragStartX, y: dragStartY), (x: xNode.value, y: yNode.value))
|
||||
}
|
||||
)
|
||||
Text("\(xLabel): \(formattedParameterValue(xNode.value))")
|
||||
@@ -289,6 +338,19 @@ public struct ParameterXY: View {
|
||||
.padding()
|
||||
}
|
||||
|
||||
/// Returns a copy of the pad that calls `action` each time a drag ends — once per gesture — with
|
||||
/// the `(x, y)` values at the drag's start and end. Mirrors the macOS pad's ``onEditingEnded(_:)``.
|
||||
///
|
||||
/// - Parameter action: Receives `(previous, new)`, each an `(x, y)` value pair.
|
||||
/// - Returns: A pad that reports the end of each edit.
|
||||
public func onEditingEnded(
|
||||
_ action: @escaping (_ previous: (x: Double, y: Double), _ new: (x: Double, y: Double)) -> Void
|
||||
) -> ParameterXY {
|
||||
var copy: ParameterXY = self
|
||||
copy.onEditingEndedAction = action
|
||||
return copy
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func normalise(
|
||||
|
||||
Reference in New Issue
Block a user