feat(undo): parameter undo for knobs, sliders, switches, selectors

Tier 2 of the rig-undo feature, on the same undo stack as the structural
(tier 1) edits. Records one undo checkpoint when the user finishes changing a
pedal control, so ⌘Z / the ↶ button step back through knob turns and switch
flips as well as add/remove/move.

- UndoEntry.parameter + PedalControlGroup (knob/advancedKnob/slider/switch/
  selector); UndoCoordinator.recordParameterEdit + .parameter handling in
  undo()/redo(). applyParameter re-resolves the LIVE node by (pedalID, group,
  controlLabel) via ShoeGaze.pedal(id:), so an undo lands on the right control
  even after an intervening structural rebuild replaced the pedal's node
  (pedal ids persist through rebuild). old == new records nothing.
- Knobs record via Lyrebird 2.3.0's ParameterKnob.onEditingEnded (one edit per
  drag): PedalView.recordingKnob (main / advanced / mix / level / looper) and
  TempoSyncKnobView.
- Self-contained views (VerticalParameterSlider, PedalSwitchView,
  PedalSelectorKnobView) record via a new \.pedalParameterEditRecorder
  environment closure PedalView installs, capturing pedal.id + the coordinator.
  Slider/selector snapshot node.value at drag start; switch records old→new on
  the committing tap.
- rebuildsOnChange switches/selectors are deliberately NOT recorded (they alter
  graph topology and would fight the structural restore's live-value merge) —
  documented as a known gap, along with tempo-sync division changes.
- Bump the Lyrebird pin to 2.3.0 (the released tag with onEditingEnded), drop
  the dev-time local path override.

Tests: 18 UndoCoordinator tests (knob/slider/switch/selector round-trips,
post-rebuild re-resolution, + a catalogue test asserting unique control labels
per group so the by-label resolution can't mis-target). Full suite 837 green;
both demo schemes build against 2.3.0. External review + caveman sweep: no
blocking findings.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 14:23:00 -08:00
parent a8bd0242a3
commit 1f5b70edc7
14 changed files with 396 additions and 31 deletions
@@ -23,3 +23,20 @@ extension EnvironmentValues {
set { self[PedalRebuildActionKey.self] = newValue }
}
}
/// Environment action a leaf pedal control calls when the user finishes changing it, so the host
/// can record one undo checkpoint per edit. Arguments: the control's group, its label, and the
/// values before and after the edit. ``PedalView`` installs it capturing the pedal's id and the
/// undo coordinator; it defaults to a no-op, so a control outside the builder records nothing.
private struct PedalParameterEditRecorderKey: EnvironmentKey {
static let defaultValue: @MainActor @Sendable (PedalControlGroup, String, Double, Double) -> Void = { _, _, _, _ in }
}
extension EnvironmentValues {
/// Records a finished parameter edit (group, control label, old value, new value). No-op unless
/// the host installs it.
var pedalParameterEditRecorder: @MainActor @Sendable (PedalControlGroup, String, Double, Double) -> Void {
get { self[PedalParameterEditRecorderKey.self] }
set { self[PedalParameterEditRecorderKey.self] = newValue }
}
}
@@ -33,9 +33,15 @@ struct PedalSelectorKnobView: View {
/// rebuild for `rebuildsOnChange` selectors. `nil` for plain live-value selectors.
private let onCommit: (() -> Void)?
/// Records one undo checkpoint per drag that changes the stop. Installed by ``PedalView``.
@Environment(\.pedalParameterEditRecorder) private var recordEdit
// MARK: - Drag state
@State private var dragStartIndex: Int = 0
/// Node value captured when a drag begins — the undo checkpoint's "before" (read from the node,
/// not the stop table, so it can't index out of range if the stops change).
@State private var dragStartValue: Double = 0
@State private var dragInProgress: Bool = false
// MARK: - Init
@@ -94,6 +100,7 @@ struct PedalSelectorKnobView: View {
if !dragInProgress {
dragInProgress = true
dragStartIndex = currentIndex
dragStartValue = selector.node.value
}
let delta: CGFloat = gesture.translation.height // down = positive
// Up should advance (increase index), so subtract the downward delta.
@@ -107,7 +114,15 @@ struct PedalSelectorKnobView: View {
.onEnded { _ in
let changed: Bool = currentIndex != dragStartIndex
dragInProgress = false
if changed { onCommit?() }
if changed {
onCommit?()
// A rebuildsOnChange selector alters graph topology: it is NOT recorded as a
// parameter edit (and isn't undoable yet — recording it would fight the
// structural restore's live-value merge). Only plain live-value selectors record.
if !selector.rebuildsOnChange {
recordEdit(.selector, selector.label, dragStartValue, selector.node.value)
}
}
}
}
@@ -56,6 +56,9 @@ public struct PedalSwitchView: View {
@Environment(\.shoeGazeTempoEnabled) private var tempoEnabled: Bool
/// Records one undo checkpoint per committed change. Installed by ``PedalView``; no-op elsewhere.
@Environment(\.pedalParameterEditRecorder) private var recordEdit
// MARK: - Init
/// Creates a segmented switch view.
@@ -105,9 +108,18 @@ public struct PedalSwitchView: View {
private func segment(option: PedalSwitch.Option, isActive: Bool) -> some View {
Button(action: {
let changed: Bool = option.value != pedalSwitch.node.value
let oldValue: Double = pedalSwitch.node.value
let changed: Bool = option.value != oldValue
pedalSwitch.node.update(option.value)
if changed { onCommit?() }
if changed {
onCommit?()
// A rebuildsOnChange switch alters graph topology: it is NOT recorded as a parameter
// edit (and isn't undoable yet — recording it would fight the structural restore's
// live-value merge). Only plain live-value switches become parameter edits.
if !pedalSwitch.rebuildsOnChange {
recordEdit(.switch, pedalSwitch.label, oldValue, option.value)
}
}
}) {
Text(option.title)
.font(.system(size: PedalSwitchView.fontSize, weight: .semibold))
+52 -5
View File
@@ -246,6 +246,9 @@ struct TempoSyncKnobView: View {
let knob: PedalKnob
let param: TempoSyncableParam
/// Undo coordinator injected by the host; `nil` outside the builder.
@Environment(UndoCoordinator.self) private var undo: UndoCoordinator?
/// Presentation flag for the division-picker popover.
@State private var showDivisionPicker: Bool = false
@@ -270,6 +273,18 @@ struct TempoSyncKnobView: View {
VStack(spacing: TempoSyncKnobView.cellSpacing) {
ParameterKnob(knob.label, node: knob.node, range: knob.range)
.onEditingEnded { previous, newValue in
// Only reachable when free (synced knobs are disabled), so a recorded edit is a
// genuine manual turn of this time/rate knob.
undo?.recordParameterEdit(
label: knob.label,
pedalID: pedal.id,
group: .knob,
controlLabel: knob.label,
oldValue: previous,
newValue: newValue
)
}
.foregroundStyle(Color.white)
.disabled(isSynced)
.opacity(isSynced ? TempoSyncKnobView.syncedDialOpacity : 1.0)
@@ -513,6 +528,9 @@ public struct PedalView: View {
/// Mirrors ``ShoeGaze/tempoEnabled`` so tempo-sync knob affordances hide when tempo is off.
@Environment(\.shoeGazeTempoEnabled) private var tempoEnabled: Bool
/// Undo coordinator injected by the host; `nil` outside the builder (edits aren't recorded).
@Environment(UndoCoordinator.self) private var undo: UndoCoordinator?
// MARK: - Init
/// Creates a stompbox pedal view for the given pedal.
@@ -597,6 +615,18 @@ public struct PedalView: View {
PedalInfoView(pedal: pedal)
.presentationDetents([.medium, .large])
}
// Let the self-contained control views (switch / selector / slider) record one undo
// checkpoint per edit. Captures this pedal's id + the coordinator; a no-op when `undo` is nil.
.environment(\.pedalParameterEditRecorder) { @MainActor [pedal, undo] (group: PedalControlGroup, label: String, oldValue: Double, newValue: Double) in
undo?.recordParameterEdit(
label: label,
pedalID: pedal.id,
group: group,
controlLabel: label,
oldValue: oldValue,
newValue: newValue
)
}
}
// MARK: - Content layouts
@@ -670,7 +700,7 @@ public struct PedalView: View {
Spacer(minLength: 0)
ForEach(Array(rowPair.element.enumerated()), id: \.offset) { knobPair in
let knob: PedalKnob = knobPair.element
ParameterKnob(knob.label, node: knob.node, range: knob.range)
recordingKnob(knob)
.foregroundStyle(Color.white)
}
Spacer(minLength: 0)
@@ -866,7 +896,7 @@ public struct PedalView: View {
ForEach(Array(rows.enumerated()), id: \.offset) { rowPair in
HStack(alignment: .top, spacing: PedalView.advancedPopoverSpacing) {
ForEach(Array(rowPair.element.enumerated()), id: \.offset) { knobPair in
ParameterKnob(knobPair.element.label, node: knobPair.element.node, range: knobPair.element.range)
recordingKnob(knobPair.element, group: .advancedKnob)
.foregroundStyle(Color.white)
}
}
@@ -924,11 +954,28 @@ public struct PedalView: View {
if tempoEnabled, let param: TempoSyncableParam = syncableParam(for: knob) {
TempoSyncKnobView(pedal: pedal, knob: knob, param: param)
} else {
ParameterKnob(knob.label, node: knob.node, range: knob.range)
recordingKnob(knob)
.foregroundStyle(Color.white)
}
}
/// A ``ParameterKnob`` that records one undo checkpoint per drag (via the coordinator) when the
/// user finishes turning it. The control is addressed by the pedal's id, its group, and its
/// label so undo re-resolves the live node even after an intervening rebuild.
private func recordingKnob(_ knob: PedalKnob, group: PedalControlGroup = .knob) -> ParameterKnob {
ParameterKnob(knob.label, node: knob.node, range: knob.range)
.onEditingEnded { previous, newValue in
undo?.recordParameterEdit(
label: knob.label,
pedalID: pedal.id,
group: group,
controlLabel: knob.label,
oldValue: previous,
newValue: newValue
)
}
}
/// The tempo-syncable descriptor whose parameter node backs `knob`, or `nil` if the knob is
/// not syncable. Matched by `UIParamNode` identity so it is robust to label changes.
private func syncableParam(for knob: PedalKnob) -> TempoSyncableParam? {
@@ -1141,7 +1188,7 @@ public struct PedalView: View {
@ViewBuilder
private var levelControl: some View {
if let level: PedalKnob = pedal.knobs.last {
ParameterKnob(level.label, node: level.node, range: level.range)
recordingKnob(level)
.foregroundStyle(Color.white)
}
}
@@ -1152,7 +1199,7 @@ public struct PedalView: View {
@ViewBuilder
private var mixControl: some View {
if pedal.showsMixControl, let mix: PedalKnob = pedal.knobs.dropLast().last {
ParameterKnob(mix.label, node: mix.node, range: mix.range)
recordingKnob(mix)
.foregroundStyle(Color.white)
}
}
@@ -49,6 +49,13 @@ struct VerticalParameterSlider: View {
private let node: UIParamNode
private let range: ClosedRange<Double>
/// Records one undo checkpoint per drag. Installed by ``PedalView``; no-op elsewhere.
@Environment(\.pedalParameterEditRecorder) private var recordEdit
/// Node value captured when a drag begins, for the undo checkpoint's "before".
@State private var dragStartValue: Double = 0
@State private var dragInProgress: Bool = false
// MARK: - Init
/// Creates a vertical fader.
@@ -141,8 +148,16 @@ struct VerticalParameterSlider: View {
.gesture(
DragGesture(minimumDistance: 0)
.onChanged { drag in
if !dragInProgress {
dragInProgress = true
dragStartValue = node.value
}
updateValue(forY: drag.location.y, height: height)
}
.onEnded { _ in
dragInProgress = false
recordEdit(.slider, label, dragStartValue, node.value)
}
)
}
.frame(width: VerticalParameterSlider.controlWidth, height: VerticalParameterSlider.trackHeight)