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:
@@ -5,7 +5,7 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/// The current Lyrebird framework and CLI version.
|
||||
public let lyrebirdVersion: String = "2.2.0"
|
||||
public let lyrebirdVersion: String = "2.3.0"
|
||||
|
||||
/// The banner printed when the interactive shell or REPL session starts.
|
||||
public let welcomeBanner: String = """
|
||||
|
||||
@@ -43,6 +43,14 @@ public struct MultiSlider: View {
|
||||
|
||||
@State private var values: [Double]
|
||||
|
||||
/// Snapshot of `values` captured when a drag begins, so ``onEditingEnded(_:)`` can report the
|
||||
/// cells' `(previous, new)` state — a drag can paint across several columns.
|
||||
@State private var dragStartValues: [Double]?
|
||||
|
||||
/// Called once when a drag ends, with the `count` cell values before and after the drag (aligned
|
||||
/// to `startIndex`). Set via ``onEditingEnded(_:)``; lets a host record one edit per gesture.
|
||||
private var onEditingEndedAction: ((_ previous: [Double], _ new: [Double]) -> Void)?
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// - Parameters:
|
||||
@@ -112,6 +120,7 @@ public struct MultiSlider: View {
|
||||
.gesture(
|
||||
DragGesture(minimumDistance: 0)
|
||||
.onChanged { drag in
|
||||
if dragStartValues == nil { dragStartValues = values }
|
||||
let column: Int = Int(drag.location.x / columnWidth)
|
||||
guard column >= 0 && column < count else { return }
|
||||
let yClamped: CGFloat = min(max(drag.location.y, 0), geo.size.height)
|
||||
@@ -126,6 +135,12 @@ public struct MultiSlider: View {
|
||||
values[column] = value
|
||||
buffer.setValue(value, at: startIndex + column)
|
||||
}
|
||||
.onEnded { _ in
|
||||
if let previous: [Double] = dragStartValues {
|
||||
onEditingEndedAction?(previous, values)
|
||||
}
|
||||
dragStartValues = nil
|
||||
}
|
||||
)
|
||||
}
|
||||
.frame(height: height)
|
||||
@@ -133,6 +148,18 @@ public struct MultiSlider: View {
|
||||
Rectangle().stroke(Color.lyrebirdSeparator, lineWidth: 1)
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns a copy that calls `action` when a drag ends — once per gesture — with the `count` cell
|
||||
/// values before and after the drag (aligned to `startIndex`). A drag may paint several columns,
|
||||
/// so both arrays are supplied for the host to diff. Matches ``ParameterKnob/onEditingEnded(_:)``.
|
||||
///
|
||||
/// - Parameter action: Receives `(previous, new)` cell-value arrays.
|
||||
/// - Returns: A slider row that reports the end of each edit.
|
||||
public func onEditingEnded(_ action: @escaping (_ previous: [Double], _ new: [Double]) -> Void) -> MultiSlider {
|
||||
var copy: MultiSlider = self
|
||||
copy.onEditingEndedAction = action
|
||||
return copy
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
@@ -168,6 +195,13 @@ public struct MultiSlider: View {
|
||||
|
||||
@State private var values: [Double]
|
||||
|
||||
/// Snapshot of `values` at drag start, so ``onEditingEnded(_:)`` can report `(previous, new)`.
|
||||
@State private var dragStartValues: [Double]?
|
||||
|
||||
/// Called once when a drag ends, with the `count` cell values before and after. Set via
|
||||
/// ``onEditingEnded(_:)``; mirrors the macOS multi-slider.
|
||||
private var onEditingEndedAction: ((_ previous: [Double], _ new: [Double]) -> Void)?
|
||||
|
||||
// Fixed per-column width (points). The Apple side fills the available width
|
||||
// via GeometryReader; on Linux the Canvas needs a concrete width, so each bar
|
||||
// gets a fixed column and the row is `count × columnWidth` wide.
|
||||
@@ -243,6 +277,7 @@ public struct MultiSlider: View {
|
||||
.onDrag(
|
||||
minimumDistance: 0,
|
||||
onChanged: { (drag: DragGestureValue) in
|
||||
if dragStartValues == nil { dragStartValues = values }
|
||||
let column: Int = Int(drag.location.x / columnWidth)
|
||||
guard column >= 0 && column < count else { return }
|
||||
let hgt: Double = Double(height)
|
||||
@@ -252,8 +287,26 @@ public struct MultiSlider: View {
|
||||
let value: Double = range.lowerBound + norm * span
|
||||
values[column] = value
|
||||
buffer.setValue(value, at: startIndex + column)
|
||||
},
|
||||
onEnded: { (_: DragGestureValue) in
|
||||
if let previous: [Double] = dragStartValues {
|
||||
onEditingEndedAction?(previous, values)
|
||||
}
|
||||
dragStartValues = nil
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns a copy that calls `action` when a drag ends — once per gesture — with the `count` cell
|
||||
/// values before and after (aligned to `startIndex`). Mirrors the macOS multi-slider's
|
||||
/// ``onEditingEnded(_:)``.
|
||||
///
|
||||
/// - Parameter action: Receives `(previous, new)` cell-value arrays.
|
||||
/// - Returns: A slider row that reports the end of each edit.
|
||||
public func onEditingEnded(_ action: @escaping (_ previous: [Double], _ new: [Double]) -> Void) -> MultiSlider {
|
||||
var copy: MultiSlider = self
|
||||
copy.onEditingEndedAction = action
|
||||
return copy
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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