feat(undo): multi-level structural undo/redo for rig edits

Add UndoCoordinator: 50-deep undo/redo of structural edits (add/insert/
remove/move pedals in any lane, stereo-split/parallel/merge/unzip routing,
add/remove/move boards, Clear Board). Each edit records a named checkpoint
via a perform(label:) wrapper threaded through the builder, board-flow,
board-bar, and boards-sheet views (injected through the SwiftUI environment,
re-injected onto the boards sheet).

Restore is surgical, not a whole-rig reload: it reconciles board-by-board
(remove added / re-add removed / rebuild changed / reorder) using the same
per-board rebuild path as an ordinary edit, so undo never tears the rig down
— recording, looper buffers, other boards' reverb/delay tails, and
bypass/solo state all survive. It also merges live parameter values by pedal
id, so undoing a topology edit never reverts a knob turned afterwards; a
pedal the edit removed returns with its snapshot values. No-op edits record
nothing (compared via hasSameRigContent). History clears on rig load / new /
switch.

UI: macOS Edit menu (Undo ⌘Z / Redo ⇧⌘Z with dynamic "Undo <action>" titles,
deferring to a text field's own editor while typing); iOS visible ↶/↷ buttons
+ ⌘Z/⇧⌘Z. Rig mute moves from ⌘Z to ⌘M. Help text updated.

Fix: every pedal edit rebuilt the board via BoardState(label:segments:)
without id:, regenerating the board UUID and breaking MIDI board bindings
after any edit. Thread id: board.id through all 10 rebuild sites; add
editingPreservesBoardIdentity regression test.

Tests: 12 UndoCoordinator tests (restore correctness, param preservation,
untouched-board identity, multi-board re-add, depth cap, no-op, throwing
action). Full suite 830 green; both demo schemes build (macOS + iPad).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 10:22:51 -08:00
parent 4c09825c2e
commit a8bd0242a3
14 changed files with 924 additions and 69 deletions
+23 -9
View File
@@ -63,6 +63,10 @@ public struct BoardFlowView: View {
private let shoeGaze: ShoeGaze
private let boardIndex: Int
/// Undo coordinator injected by ``ShoeGazeBuilderView``; `nil` in lightweight hosts (previews),
/// where edits run directly without recording history.
@Environment(UndoCoordinator.self) private var undo: UndoCoordinator?
/// Identifier of the lane/card currently hovered as a drop target, for highlight.
@State private var dropTarget: String?
@@ -293,12 +297,12 @@ public struct BoardFlowView: View {
do {
if split {
guard !shoeGaze.isStereoUnzip(afterSegment: segmentIndex, in: board) else { return }
_ = try shoeGaze.insertStereoUnzip(afterSegment: segmentIndex, in: board)
try editing("Split L/R") { _ = try shoeGaze.insertStereoUnzip(afterSegment: segmentIndex, in: board) }
} else {
// Only remove the next segment if it is in fact this pedal's unzip region —
// it may have been removed via the region's own trash button.
guard shoeGaze.isStereoUnzip(afterSegment: segmentIndex, in: board) else { return }
_ = try shoeGaze.removeSegment(at: segmentIndex + 1, from: board)
try editing("Merge L/R") { _ = try shoeGaze.removeSegment(at: segmentIndex + 1, from: board) }
}
} catch {
print("[BoardFlowView] split toggle failed: \(error)")
@@ -507,7 +511,7 @@ public struct BoardFlowView: View {
sourceIndex = index
}
do {
_ = try shoeGaze.movePedal(from: sourceLane, at: sourceIndex, to: destination, in: board)
try editing("Move Pedal") { _ = try shoeGaze.movePedal(from: sourceLane, at: sourceIndex, to: destination, in: board) }
return true
} catch {
print("[BoardFlowView] cross-lane move failed: \(error)")
@@ -522,15 +526,25 @@ public struct BoardFlowView: View {
return shoeGaze.boards[boardIndex]
}
/// Runs a structural edit through the undo coordinator (recording a checkpoint when it changes
/// the rig), or directly when no coordinator is present.
@discardableResult
private func editing<T>(_ label: String, _ action: () throws -> T) rethrows -> T {
if let undo: UndoCoordinator = undo {
return try undo.perform(label, action)
}
return try action()
}
private func append(type: String, lane: PedalLane) {
guard let board: PedalBoard = board else { return }
do { _ = try shoeGaze.appendPedal(PedalType(type), to: board, lane: lane) }
do { try editing("Add \(type)") { _ = try shoeGaze.appendPedal(PedalType(type), to: board, lane: lane) } }
catch { print("[BoardFlowView] appendPedal failed: \(error)") }
}
private func insert(type: String, at index: Int, lane: PedalLane) {
guard let board: PedalBoard = board else { return }
do { _ = try shoeGaze.insertPedal(PedalType(type), at: index, to: board, lane: lane) }
do { try editing("Add \(type)") { _ = try shoeGaze.insertPedal(PedalType(type), at: index, to: board, lane: lane) } }
catch { print("[BoardFlowView] insertPedal failed: \(error)") }
}
@@ -539,20 +553,20 @@ public struct BoardFlowView: View {
// Convert "dropped ONTO the card at `target`" into movePedal's insert-before `toOffset`:
// a forward move lands after the target (target + 1); a backward move lands before it.
let destination: Int = source < target ? target + 1 : target
do { _ = try shoeGaze.movePedal(from: source, to: destination, in: board, lane: .main) }
do { try editing("Move Pedal") { _ = try shoeGaze.movePedal(from: source, to: destination, in: board, lane: .main) } }
catch { print("[BoardFlowView] movePedal(main) failed: \(error)") }
}
private func reorderBranch(from source: Int, onto target: Int, lane: PedalLane) {
guard source != target, let board: PedalBoard = board else { return }
let destination: Int = source < target ? target + 1 : target
do { _ = try shoeGaze.movePedal(from: source, to: destination, in: board, lane: lane) }
do { try editing("Move Pedal") { _ = try shoeGaze.movePedal(from: source, to: destination, in: board, lane: lane) } }
catch { print("[BoardFlowView] movePedal(branch) failed: \(error)") }
}
private func removePedal(at index: Int, lane: PedalLane) {
guard let board: PedalBoard = board else { return }
do { _ = try shoeGaze.removePedal(at: index, from: board, lane: lane) }
do { try editing("Remove Pedal") { _ = try shoeGaze.removePedal(at: index, from: board, lane: lane) } }
catch { print("[BoardFlowView] removePedal failed: \(error)") }
}
@@ -563,7 +577,7 @@ public struct BoardFlowView: View {
private func removeSegment(at segmentIndex: Int) {
guard let board: PedalBoard = board else { return }
do { _ = try shoeGaze.removeSegment(at: segmentIndex, from: board) }
do { try editing("Remove Section") { _ = try shoeGaze.removeSegment(at: segmentIndex, from: board) } }
catch { print("[BoardFlowView] removeSegment failed: \(error)") }
}
}