diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift index 1e0f8cc..a11b6bb 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift @@ -699,6 +699,9 @@ public struct ShoeGazeBuilderView: View { /// survives a restart even without an explicit Save; the app shells also save on background. private func autosave() { RigStore.save(shoeGaze) + // Pair the current-rig id with every graph write so the persisted highlight can never drift + // from the autosaved graph (the source of the highlight/graph mismatch on restart). + persistCurrentRigID() } /// Removes every board, then seeds one fresh empty board so the builder is never boardless. @@ -874,21 +877,21 @@ public struct ShoeGazeBuilderView: View { // MARK: - Rig library - /// Highlights the library entry that matches the restored working rig, once the library has - /// loaded. Runs once. + /// Highlights the rig the working session derives from, once the library has loaded. Runs once. /// - /// The working graph is restored from the autosave file independently of any persisted - /// selection, so the highlight is derived from the *actual* loaded graph rather than a separate - /// stored id (which could point at a different rig and mislabel what's shown). If the working - /// graph is an unsaved/edited session that matches no saved entry, nothing is highlighted. + /// The current rig id is written paired with every autosave (see ``autosave()``), so the stored + /// id always tracks the loaded graph — including after edits such as learning a MIDI binding, + /// which change the graph but not which saved rig it came from. Only highlights when nothing is + /// selected yet and the saved id still exists in the library. private func restoreCurrentRig() { guard !didRestoreRig, library.isLoaded else { return } didRestoreRig = true - guard currentRigID == nil else { return } - let working: ShoeGazeState = shoeGaze.captureState() - if let match: RigEntry = library.entries.first(where: { $0.state == working }) { - currentRigID = match.id - } + guard currentRigID == nil, + let stored: String = UserDefaults.standard.string( + forKey: ShoeGazeBuilderView.currentRigDefaultsKey), + let id: UUID = UUID(uuidString: stored), + library.entries.contains(where: { $0.id == id }) else { return } + currentRigID = id } /// Persists `currentRigID` so the same rig is highlighted on the next launch.