From 44e6c35550fc6f30f92bc300aa35101ab3c2672e Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Mon, 10 Aug 2026 16:40:35 -0700 Subject: [PATCH] fix(rigs): don't flag a rig dirty over input/channel differences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switching between starter rigs prompted "save changes?" even when nothing was edited. The unsaved-changes check compared full ShoeGazeState snapshots (`captureState() != entry.state`), which include the live input source and engine channel counts — and those legitimately differ from a factory rig's saved values now that a rig switch preserves the player's current input instead of adopting the rig's saved one. Add ShoeGazeState.hasSameRigContent(as:) comparing only rig content (boards, routers, MIDI map, tempo) and use it for the dirty check, so session/engine settings no longer register as unsaved changes. Test added. Co-Authored-By: Claude Opus 4.8 --- .../Presets/ShoeGazeState.swift | 16 +++++++++++++ .../ShoeGazeBuilderView.swift | 6 ++++- Tests/LyrebirdShoeGazeTests/PresetTests.swift | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Sources/LyrebirdShoeGaze/Presets/ShoeGazeState.swift b/Sources/LyrebirdShoeGaze/Presets/ShoeGazeState.swift index 60df45d..3cbe95b 100644 --- a/Sources/LyrebirdShoeGaze/Presets/ShoeGazeState.swift +++ b/Sources/LyrebirdShoeGaze/Presets/ShoeGazeState.swift @@ -140,4 +140,20 @@ public struct ShoeGazeState: Codable, Equatable { self.numOutputChannels = try container.decodeIfPresent(Int.self, forKey: .numOutputChannels) self.routers = try container.decodeIfPresent([ChainRouterState].self, forKey: .routers) } + + /// Whether two snapshots describe the same **rig** — its boards, routers, MIDI map, and tempo — + /// ignoring live session settings that are not part of the recalled rig: the input source and the + /// engine's channel counts. + /// + /// Use this for unsaved-changes detection. Those session fields are deliberately not applied when + /// switching rigs (see ``ShoeGaze/loadRig(from:keepingCurrentInput:)``), so a plain `==` would + /// report a freshly-loaded rig as "changed" merely because the player's current input differs from + /// the input the rig was saved with — producing a spurious "save changes?" prompt. + public func hasSameRigContent(as other: ShoeGazeState) -> Bool { + boards == other.boards + && routers == other.routers + && midiMap == other.midiMap + && bpm == other.bpm + && tempoEnabled == other.tempoEnabled + } } diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift index 0f52b6b..9900a3e 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift @@ -1677,12 +1677,16 @@ public struct ShoeGazeBuilderView: View { /// Whether the working rig differs from the library entry it was loaded from. A working rig with /// no backing entry counts as dirty when it has any boards. + /// + /// Compares rig content only (boards, routers, MIDI, tempo) — not the live input source or engine + /// channel counts, which are intentionally kept across a rig switch and so must not register as + /// unsaved changes (otherwise switching between rigs always prompts to save). private func isCurrentRigDirty() -> Bool { guard let id: UUID = currentRigID, let entry: RigEntry = library.entries.first(where: { $0.id == id }) else { return !shoeGaze.boards.isEmpty } - return shoeGaze.captureState() != entry.state + return !shoeGaze.captureState().hasSameRigContent(as: entry.state) } /// Saves the current session over the loaded library entry, or prompts for a name when no entry diff --git a/Tests/LyrebirdShoeGazeTests/PresetTests.swift b/Tests/LyrebirdShoeGazeTests/PresetTests.swift index 7984625..d8c191e 100644 --- a/Tests/LyrebirdShoeGazeTests/PresetTests.swift +++ b/Tests/LyrebirdShoeGazeTests/PresetTests.swift @@ -427,6 +427,29 @@ struct PresetTests { #expect(sg.inputSource == .testLoop, "load leaves the current input source unchanged") } + @Test func rigContentComparisonIgnoresInputAndChannels() throws { + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( + duration: 0.05, numInputChannels: 1, numAudioBuses: 64 + ) + defer { try? FileManager.default.removeItem(at: url) } + let sg: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop) + let board: PedalBoard = try sg.addBoard() + try board.add(FuzzPedal()) + try sg.finalizeBoard(board) + let saved: ShoeGazeState = sg.captureState() + + // Changing only the live input source must NOT read as a rig change (the source of the + // spurious "save changes?" prompt when switching starter rigs). + sg.inputSource = .liveInput(channel: 0) + let afterInputChange: ShoeGazeState = sg.captureState() + #expect(afterInputChange.hasSameRigContent(as: saved), "input change must not be a rig change") + #expect(afterInputChange != saved, "but the raw snapshots still differ on the input field") + + // A genuine graph difference IS a content change. + let emptyRig: ShoeGazeState = ShoeGazeState(boards: []) + #expect(!saved.hasSameRigContent(as: emptyRig), "different boards must count as a rig change") + } + @Test func loadPreservesCurrentInputSourceNotTheSavedOne() throws { // A rig captured with live input still RECORDS its source in the snapshot... let (engineIn, urlIn): (SynthesisEngine, URL) = makeNRTEngine(