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
@@ -6,18 +6,25 @@
import Foundation
import Observation
import LyrebirdSynth
/// Multi-level undo / redo for structural rig edits.
/// Multi-level undo / redo for rig edits — both structural topology changes and parameter changes,
/// interleaved on one stack.
///
/// `UndoCoordinator` wraps every structural edit (add / remove / move a pedal, board, split-merge
/// region, or router) in ``perform(_:_:)``. Before running the edit it snapshots the whole rig as a
/// ``ShoeGazeState``; if the edit actually changed the rig it records that pre-edit snapshot on the
/// undo stack. ``undo()`` restores the recorded snapshot via `ShoeGaze.loadRig(from:)` and moves the
/// pre-undo state onto the redo stack; ``redo()`` is the mirror. Any new edit clears the redo stack.
/// **Structural edits** (add / remove / move a pedal, board, split-merge region) are wrapped in
/// ``perform(_:_:)``, which snapshots the whole rig as a ``ShoeGazeState`` before the edit and
/// records that pre-edit snapshot if the rig actually changed. ``undo()`` restores it *surgically*
/// (see ``restore(_:on:)`` — board-by-board reconciliation, not a whole-rig reload) and moves the
/// pre-undo state onto the redo stack; ``redo()`` is the mirror.
///
/// This tier covers topology only. Continuous parameter changes (knob / switch values) are far too
/// frequent to snapshot the whole rig per change and are handled separately with coalesced per-node
/// deltas — see the project's undo notes.
/// **Parameter edits** (one knob / slider / switch / selector the user finished changing) are
/// recorded by ``recordParameterEdit(label:pedalID:group:controlLabel:oldValue:newValue:)`` as the
/// control's identity plus its before/after values — undo sets it back, redo re-applies, re-resolving
/// the live node by id each time so it survives an intervening rebuild. Topology-changing
/// (`rebuildsOnChange`) controls are deliberately NOT recorded this way (they belong to the
/// structural path).
///
/// Any new edit of either kind clears the redo stack.
///
/// The coordinator holds the ``ShoeGaze`` instance weakly: it is a companion object the UI owns
/// alongside the rig, and the rig never refers back to it. All access is `@MainActor`, matching the
@@ -105,6 +112,37 @@ public final class UndoCoordinator {
return result
}
/// Records a single parameter edit (a knob / slider the user finished turning). Does nothing when
/// the value is unchanged. Clears the redo stack, matching standard undo semantics.
///
/// - Parameters:
/// - label: Short description (typically the control's label, e.g. `"Drive"`).
/// - pedalID: The id of the pedal the control belongs to.
/// - group: Which of the pedal's control collections holds it.
/// - controlLabel: The control's label, used to find it within the group.
/// - oldValue: The value when the edit began.
/// - newValue: The value when the edit ended.
public func recordParameterEdit(
label: String,
pedalID: UUID,
group: PedalControlGroup,
controlLabel: String,
oldValue: Double,
newValue: Double
) {
guard oldValue != newValue else { return }
appendUndo(.parameter(
label: label,
pedalID: pedalID,
group: group,
controlLabel: controlLabel,
oldValue: oldValue,
newValue: newValue
))
redoStack.removeAll()
refreshState()
}
/// Reverts the most recent recorded edit, restoring the rig to its state before that edit, and
/// moves the edit onto the redo stack. No-op when there is nothing to undo.
public func undo() {
@@ -116,6 +154,10 @@ public final class UndoCoordinator {
let afterState: ShoeGazeState = shoeGaze.captureState()
appendRedo(.structural(label: label, state: afterState))
restore(beforeState, on: shoeGaze)
case .parameter(_, let pedalID, let group, let controlLabel, let oldValue, _):
applyParameter(pedalID: pedalID, group: group, controlLabel: controlLabel,
value: oldValue, on: shoeGaze)
appendRedo(entry)
}
refreshState()
}
@@ -131,6 +173,10 @@ public final class UndoCoordinator {
let beforeState: ShoeGazeState = shoeGaze.captureState()
appendUndo(.structural(label: label, state: beforeState))
restore(afterState, on: shoeGaze)
case .parameter(_, let pedalID, let group, let controlLabel, _, let newValue):
applyParameter(pedalID: pedalID, group: group, controlLabel: controlLabel,
value: newValue, on: shoeGaze)
appendUndo(entry)
}
refreshState()
}
@@ -209,6 +255,33 @@ public final class UndoCoordinator {
// routers currently present are left exactly as they are.
}
/// Sets a parameter back to `value` by re-resolving the control's live node from the current rig
/// (by pedal id, group, and label), so a parameter undo/redo lands on the right control even when
/// an earlier structural edit has since rebuilt the pedal. A missing pedal or control is a no-op.
private func applyParameter(
pedalID: UUID,
group: PedalControlGroup,
controlLabel: String,
value: Double,
on shoeGaze: ShoeGaze
) {
guard let pedal: Pedal = shoeGaze.pedal(id: pedalID) else { return }
let node: UIParamNode?
switch group {
case .knob:
node = pedal.knobs.first { $0.label == controlLabel }?.node
case .advancedKnob:
node = pedal.advancedKnobs.first { $0.label == controlLabel }?.node
case .slider:
node = pedal.effectSliders.first { $0.label == controlLabel }?.node
case .switch:
node = pedal.effectSwitches.first { $0.label == controlLabel }?.node
case .selector:
node = pedal.effectSelectors.first { $0.label == controlLabel }?.node
}
node?.update(value)
}
/// Flattens every pedal in the current rig into an `id → PedalState` map, descending into
/// parallel / stereo-split / stereo-unzip branches.
private func currentPedalStatesByID(_ shoeGaze: ShoeGaze) -> [UUID: PedalState] {