// RigNamePrompt.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI /// A request to prompt the user for a rig name (New, Save As, Rename, Duplicate). /// /// Presented via the ``SwiftUI/View/rigNamePrompt(_:)`` modifier. Setting a non-`nil` value shows a /// text-entry alert seeded with `initialText`; committing a non-empty, trimmed name invokes /// `onCommit`. Cancelling dismisses without calling `onCommit`. struct RigNamePrompt: Identifiable { let id: UUID = UUID() /// Alert title, e.g. `"Save Rig As"`. let title: String /// Text the field is pre-filled with (e.g. the existing name for a rename). let initialText: String /// Confirm-button label, e.g. `"Save"` or `"Rename"`. let confirmLabel: String /// Invoked with the trimmed, non-empty name when the user confirms. let onCommit: (String) -> Void } private struct RigNamePromptModifier: ViewModifier { @Binding var prompt: RigNamePrompt? @State private var text: String = "" func body(content: Content) -> some View { content .onChange(of: prompt?.id) { // Seed the field whenever a new prompt is presented. text = prompt?.initialText ?? "" } .alert( prompt?.title ?? "", isPresented: isPresented, presenting: prompt ) { active in TextField("Name", text: $text) Button(active.confirmLabel) { let trimmed: String = text.trimmingCharacters(in: .whitespacesAndNewlines) if !trimmed.isEmpty { active.onCommit(trimmed) } } Button("Cancel", role: .cancel) { } } } private var isPresented: Binding { Binding( get: { prompt != nil }, set: { presented in if !presented { prompt = nil } } ) } } extension View { /// Presents a rig-name entry alert driven by `prompt`. Set the binding to a ``RigNamePrompt`` /// to show it; it clears itself on dismiss. func rigNamePrompt(_ prompt: Binding) -> some View { modifier(RigNamePromptModifier(prompt: prompt)) } }