diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift index 0804eb3..ed543b6 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift @@ -118,6 +118,16 @@ public struct ShoeGazeBuilderView: View { @State private var pendingSwitchEntry: RigEntry? @State private var showSwitchSaveDialog: Bool = false + /// A failed edit (add pedal / routing / board) to surface as an alert. `nil` = no alert. + @State private var actionError: ActionError? + + /// A user-facing failure message for the action-error alert. + private struct ActionError: Identifiable { + let id: UUID = UUID() + let title: String + let message: String + } + /// Archive export state. Cross-platform: backs the sidebar's Export Archive and the /// "Save & Clear" flow is separate (it saves to the library, not a file). @State private var showExporter: Bool = false @@ -193,6 +203,7 @@ public struct ShoeGazeBuilderView: View { .background(boardsSheetAnchor) .background(rigExportAnchor) .background(rigImportAnchor) + .background(actionErrorAnchor) .rigNamePrompt($namePrompt) .confirmationDialog( "Erase all rigs?", @@ -374,6 +385,43 @@ public struct ShoeGazeBuilderView: View { } } + /// Hosts the action-failure alert on its own node (one presentation per node). + private var actionErrorAnchor: some View { + Color.clear + .alert( + actionError?.title ?? "", + isPresented: Binding( + get: { actionError != nil }, + set: { presented in if !presented { actionError = nil } } + ), + presenting: actionError + ) { _ in + Button("OK", role: .cancel) { } + } message: { error in + Text(error.message) + } + } + + /// Surfaces a failed edit as an alert (and logs it). Maps the common stereo-width / merge errors + /// to player-facing wording (no API names); otherwise uses the error's `LocalizedError` text. + private func reportError(_ title: String, _ error: Error) { + actionError = ActionError(title: title, message: friendlyMessage(for: error)) + } + + private func friendlyMessage(for error: Error) -> String { + switch error as? ShoeGazeError { + case .stereoWidthExceeded, .cannotSplitStereoChain: + return "This board is already in stereo. Add a “Merge to Mono” before another stereo " + + "(widening) pedal such as Chorus, Rotary, or a Granulator." + case .cannotMergeMonoChain: + return "This board is already mono — there’s no stereo signal to merge." + case .cannotUnzipMonoChain: + return "This board is mono — there’s no stereo signal to split into left/right lanes." + default: + return (error as? LocalizedError)?.errorDescription ?? error.localizedDescription + } + } + #if !os(macOS) /// Hosts the recording-folder importer on its own node. private var recordingFolderImportAnchor: some View { @@ -472,6 +520,7 @@ public struct ShoeGazeBuilderView: View { } } catch { print("[ShoeGazeBuilderView] routing \(action) failed: \(error)") + reportError("Couldn’t add routing", error) } } @@ -516,6 +565,7 @@ public struct ShoeGazeBuilderView: View { autosave() } catch { print("[ShoeGazeBuilderView] appendPedal failed: \(error)") + reportError("Couldn’t add \(type)", error) } } @@ -530,6 +580,7 @@ public struct ShoeGazeBuilderView: View { } catch { if let board: PedalBoard = created { shoeGaze.removeBoard(board) } print("[ShoeGazeBuilderView] addBoard failed: \(error)") + reportError("Couldn’t add board", error) } }