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
+81 -7
View File
@@ -55,6 +55,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
private let shoeGaze: ShoeGaze
private let window: LyrebirdWindow
/// Multi-level undo / redo for structural rig edits. The same instance backs both the Edit-menu
/// items here and `ShoeGazeBuilderView`'s own edits, so ⌘Z works from the menu and the canvas.
private let undo: UndoCoordinator
/// Foot-controller MIDI input. The macOS engine is never rebuilt, so this is created once and
/// started at launch.
private let midi: MIDIController
@@ -73,6 +77,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
/// match the current bypass state (which may also change from the UI or MIDI).
private var bypassMenuItem: NSMenuItem?
/// Undo / Redo items, retained so `validateMenuItem(_:)` can enable them and refresh their
/// titles to the next action (e.g. "Undo Add Reverb").
private var undoMenuItem: NSMenuItem?
private var redoMenuItem: NSMenuItem?
/// The Rigs ▸ Set List submenu, retained so its item titles can be refreshed to the current
/// library order each time it opens (via `menuNeedsUpdate(_:)`).
private var setListMenu: NSMenu?
@@ -109,6 +118,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
self.coreDriver = coreDriver
self.engine = engine
self.shoeGaze = shoeGaze
self.undo = UndoCoordinator(shoeGaze: shoeGaze)
self.midi = MIDIController(engine: engine, shoeGaze: shoeGaze)
self.window = LyrebirdWindow(
title: "ShoeGaze Builder — Lyrebird Pedalboard",
@@ -158,12 +168,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
let driver: CoreAudioDriver = coreDriver
let rigLibrary: RigLibraryStore = library
let midiController: MIDIController = midi
let undoCoordinator: UndoCoordinator = undo
window.open {
ShoeGazeBuilderView(
shoeGaze: session,
library: rigLibrary,
hardwareSampleRate: { driver.deviceSampleRate },
midi: midiController
midi: midiController,
undo: undoCoordinator
)
}
anchorWindowLeft()
@@ -302,6 +314,26 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
addItem(to: fileMenu, title: "Export Archive…", action: #selector(exportArchiveAction(_:)), key: "e")
addItem(to: fileMenu, title: "Import Archive…", action: #selector(importArchiveAction(_:)), key: "i")
// Edit menu — multi-level undo / redo of structural rig edits (add / remove / move pedals,
// boards, routing). ⌘Z / ⇧⌘Z; titles + enabled state refresh in `validateMenuItem(_:)`.
let editMenuItem: NSMenuItem = NSMenuItem()
mainMenu.addItem(editMenuItem)
let editMenu: NSMenu = NSMenu(title: "Edit")
editMenuItem.submenu = editMenu
self.undoMenuItem = addItem(
to: editMenu,
title: "Undo",
action: #selector(undoAction(_:)),
key: "z"
)
self.redoMenuItem = addItem(
to: editMenu,
title: "Redo",
action: #selector(redoAction(_:)),
key: "z"
)
self.redoMenuItem?.keyEquivalentModifierMask = [.command, .shift]
// Board menu — add board + transport.
let boardMenuItem: NSMenuItem = NSMenuItem()
mainMenu.addItem(boardMenuItem)
@@ -311,13 +343,13 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
// config and the mismatch alert), so the Board menu only adds boards.
addItem(to: boardMenu, title: "Add Board", action: #selector(addBoardAction(_:)), key: "b")
boardMenu.addItem(NSMenuItem.separator())
// ⌘Z toggles rig mute (a hard silence of all output; distinct from Bypass Rig's dry
// passthrough). Owns the ⌘Z key equivalent on macOS; the in-window view binds it on iOS.
// ⌘M toggles rig mute (a hard silence of all output; distinct from Bypass Rig's dry
// passthrough). ⌘Z / ⇧⌘Z are Undo / Redo (Edit menu); the in-window view binds ⌘M on iOS.
self.muteMenuItem = addItem(
to: boardMenu,
title: "Mute Rig",
action: #selector(toggleMuteAction(_:)),
key: "z"
key: "m"
)
// Bypass Rig routes every board to its dry input (pedals skipped) — distinct from Mute's
// hard silence. No key equivalent: ⌘B is Add Board, and Bypass has a prominent on-screen button.
@@ -401,6 +433,28 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
// MARK: - Menu actions
/// Reverts the most recent structural rig edit — unless a text field is being edited, in which
/// case its own field editor handles ⌘Z (so typing a rig name keeps normal text undo).
@objc private func undoAction(_ sender: Any?) {
if let editor: NSText = NSApp.keyWindow?.firstResponder as? NSText,
let manager: UndoManager = editor.undoManager {
manager.undo()
return
}
undo.undo()
}
/// Re-applies the most recently undone structural rig edit, deferring to a text field's own redo
/// while one is being edited (mirrors ``undoAction(_:)``).
@objc private func redoAction(_ sender: Any?) {
if let editor: NSText = NSApp.keyWindow?.firstResponder as? NSText,
let manager: UndoManager = editor.undoManager {
manager.redo()
return
}
undo.redo()
}
/// Opens (or re-opens) the Help window hosting the shared ``HelpView``.
@objc private func showHelpAction(_ sender: Any?) {
let helpWidth: CGFloat = 560
@@ -476,9 +530,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
@objc private func addBoardAction(_ sender: Any?) {
var created: PedalBoard?
do {
let board: PedalBoard = try shoeGaze.addBoard()
created = board
try shoeGaze.finalizeBoard(board)
// Record as an undo checkpoint, matching the in-window Add Board.
try undo.perform("Add Board") {
let board: PedalBoard = try shoeGaze.addBoard()
created = board
try shoeGaze.finalizeBoard(board)
}
} catch {
if let board: PedalBoard = created { shoeGaze.removeBoard(board) }
print("[ShoeGazeBuilder] addBoard failed: \(error)")
@@ -547,6 +604,23 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
menuItem.state = shoeGaze.rigBypassed ? .on : .off
return true
}
if menuItem == undoMenuItem {
// Name the next action so the menu reads "Undo Add Reverb"; disable when there's none.
if let label: String = undo.undoActionLabel {
menuItem.title = "Undo \(label)"
} else {
menuItem.title = "Undo"
}
return undo.canUndo
}
if menuItem == redoMenuItem {
if let label: String = undo.redoActionLabel {
menuItem.title = "Redo \(label)"
} else {
menuItem.title = "Redo"
}
return undo.canRedo
}
if setListMenuItems.contains(menuItem) {
// A slot is live only when the library actually holds a rig at that position.
return menuItem.tag < library.entries.count