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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user