diff --git a/RELEASES.md b/RELEASES.md index a8ea1d0..cc8affe 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -7,6 +7,27 @@ functionality in a backward-compatible way. --- +## 2.3.0 — Edit-ended callback on continuous controls + +A small, additive, source-compatible release. The continuous `LyrebirdUI` +controls drive their parameter node(s) continuously while dragging, which gives +a host no single "the user finished this edit" signal. This adds one. + +**New — `.onEditingEnded` on the continuous controls.** `ParameterKnob`, +`ParameterSlider`, `ParameterXY`, and `MultiSlider` gain an `.onEditingEnded` +modifier (both the SwiftUI and the Linux/GTK4 variants) that fires **once per +drag gesture**, reporting the value(s) at the start and end of the drag. A host +can use it to record a single edit per gesture — an undo checkpoint, say — +instead of one per intermediate update. The payload matches each control: +`(previous, new)` doubles for the knob and slider, a pair of `(x, y)` tuples for +the XY pad, and the before/after cell-value arrays for the multi-slider. + +Additive and drop-in over 2.2.x: every control's public initializer is +unchanged (the callback is attached by the modifier), so existing call sites and +DocC links are unaffected. See `docs/ui/gui-system.md` for examples. + +--- + ## 2.2.0 — Live-parameter ramp origin primed at synth add A small-but-behavioral release. One fix to how `@UIParam` / `@LiveParam` values diff --git a/Sources/LyrebirdCLI/Version.swift b/Sources/LyrebirdCLI/Version.swift index 2dfac1f..132f7fd 100644 --- a/Sources/LyrebirdCLI/Version.swift +++ b/Sources/LyrebirdCLI/Version.swift @@ -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 = """ diff --git a/Sources/LyrebirdUI/Controls/MultiSlider.swift b/Sources/LyrebirdUI/Controls/MultiSlider.swift index 6a26aef..5158f73 100644 --- a/Sources/LyrebirdUI/Controls/MultiSlider.swift +++ b/Sources/LyrebirdUI/Controls/MultiSlider.swift @@ -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 diff --git a/Sources/LyrebirdUI/Controls/ParameterKnob.swift b/Sources/LyrebirdUI/Controls/ParameterKnob.swift index 0bfcb52..6f27b9f 100644 --- a/Sources/LyrebirdUI/Controls/ParameterKnob.swift +++ b/Sources/LyrebirdUI/Controls/ParameterKnob.swift @@ -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 diff --git a/Sources/LyrebirdUI/Controls/ParameterSlider.swift b/Sources/LyrebirdUI/Controls/ParameterSlider.swift index 5113d1b..be7827b 100644 --- a/Sources/LyrebirdUI/Controls/ParameterSlider.swift +++ b/Sources/LyrebirdUI/Controls/ParameterSlider.swift @@ -35,6 +35,13 @@ public struct ParameterSlider: View { private let range: ClosedRange 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 { diff --git a/Sources/LyrebirdUI/Controls/ParameterXY.swift b/Sources/LyrebirdUI/Controls/ParameterXY.swift index 200feed..6e0cfe5 100644 --- a/Sources/LyrebirdUI/Controls/ParameterXY.swift +++ b/Sources/LyrebirdUI/Controls/ParameterXY.swift @@ -31,6 +31,15 @@ public struct ParameterXY: View { private let yNode: UIParamNode private let yRange: ClosedRange + /// 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( diff --git a/Tests/LyrebirdUITests/OnEditingEndedTests.swift b/Tests/LyrebirdUITests/OnEditingEndedTests.swift new file mode 100644 index 0000000..037400e --- /dev/null +++ b/Tests/LyrebirdUITests/OnEditingEndedTests.swift @@ -0,0 +1,65 @@ +// OnEditingEndedTests.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import Testing +@testable import LyrebirdUI +import LyrebirdSynth + +/// Coverage for the `.onEditingEnded` edit-reporting modifier on the continuous controls. +/// +/// A SwiftUI gesture can't be driven from a unit test (same limitation noted in +/// ``MultiSliderTests``), so these are structural smokes: they construct each control, attach +/// `.onEditingEnded` with that control's payload shape, and confirm the builder returns a usable +/// value of the same concrete type. Their real job is to **lock the public API** — the per-control +/// closure signatures — so a later signature change breaks the build here rather than silently at a +/// call site. The gesture → `(previous, new)` firing itself is covered by the control's own drag +/// handler and is exercised in app use, not here. +@MainActor +@Suite("onEditingEnded") +struct OnEditingEndedTests { + + @Test func knobBuilderCarriesTheAction() { + let node: UIParamNode = UIParamNode(0.5) + var reported: (Double, Double)? + let knob: ParameterKnob = ParameterKnob("Cutoff", node: node, range: 0...1) + .onEditingEnded { previous, new in reported = (previous, new) } + _ = knob + // The closure has not been invoked (no gesture), so nothing is reported yet. + #expect(reported == nil) + } + + @Test func sliderBuilderCarriesTheAction() { + let node: UIParamNode = UIParamNode(0.25) + let slider: ParameterSlider = ParameterSlider("Amp", node: node, range: 0...1) + .onEditingEnded { _, _ in } + _ = slider + } + + @Test func xyBuilderCarriesTheAction() { + let xNode: UIParamNode = UIParamNode(0.0) + let yNode: UIParamNode = UIParamNode(0.0) + let pad: ParameterXY = ParameterXY( + xLabel: "Pan", xNode: xNode, xRange: -1...1, + yLabel: "Amp", yNode: yNode, yRange: 0...1 + ) + .onEditingEnded { previous, new in + // Payload is a pair of (x, y) tuples — reference the members so a shape change breaks here. + _ = (previous.x, previous.y, new.x, new.y) + } + _ = pad + } + + @Test func multiSliderBuilderCarriesTheAction() { + let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 8) + buffer.fill(with: 0.3) + let bars: MultiSlider = MultiSlider(buffer: buffer, count: 4, range: 0...1) + .onEditingEnded { previous, new in + // Payload is the cell-value arrays before / after the drag. + _ = (previous.count, new.count) + } + _ = bars + } +} diff --git a/docs/ui/gui-system.md b/docs/ui/gui-system.md index 4149033..830c24a 100644 --- a/docs/ui/gui-system.md +++ b/docs/ui/gui-system.md @@ -189,6 +189,34 @@ ParameterLabel("Level", node: synth.$amplitude) ParameterLabel("Freq (Hz)", node: synth.$frequency, decimalPlaces: 1) ``` +### Reporting the end of an edit (`onEditingEnded`) + +The continuous controls — `ParameterKnob`, `ParameterSlider`, `ParameterXY`, and `MultiSlider` — +drive their node(s) continuously while dragging. Attach `.onEditingEnded` to be told **once per +drag gesture**, with the value(s) at the start and end of the drag, so a host can record a single +edit (an undo checkpoint, say) instead of one per intermediate update: + +```swift +ParameterKnob("Cutoff", node: synth.$cutoff, range: 20...20000) + .onEditingEnded { previous, new in + undo.record(previous, new) // called once, when the drag ends + } +``` + +The payload differs per control: + +| Control | `onEditingEnded` closure | +|---|---| +| `ParameterKnob` / `ParameterSlider` | `(previous: Double, new: Double)` | +| `ParameterXY` | `(previous: (x: Double, y: Double), new: (x: Double, y: Double))` — one gesture drives both nodes | +| `MultiSlider` | `(previous: [Double], new: [Double])` — the `count` cell values before/after (a drag can paint several bars) | + +Notes: +- It fires once per gesture **even when the value didn't net-change** (e.g. a `MultiSlider` tap on an + unchanged bar). Filter on `previous == new` if your host wants to skip no-op edits. +- `ParameterSlider` (the SwiftUI variant) rides the native `Slider`'s editing-changed signal, so it + also fires for keyboard / accessibility adjustments, not only pointer drags. + ### LyrebirdStatusView Compact engine status display showing average and peak CPU load. Polls the engine at 10 Hz;