Cotton code
280 lines · 10.8 KB · SwiftRaw
// The one confirm dialog — a decision that cannot be taken back, asked once.//// ## The dress (phase 20, 2026-09-14)//// A question is a SLIP OF PAPER put down over the screen: a small torn cream// slip, lifted, on the screen's own plaster dimmed behind it. The question is// set in the serif — it is the slip's title — and the sentence under it is// typed. Under a pencil line sit the two verbs, typed: Cancel in pencil, and// the act itself in RED INK when it erases something, in ink when it does not.//// What went: the 56pt glyph disc and the two filled buttons. A disc with a// trash can in it is a badge, not a thing on paper, and a filled red button on// a slip is a sticker. The red INK is what says danger now, and it says it on// the one word that does the damage.//// ## Two rules the caller does not get to choose////   * **The dialog IS the decision.** Nothing is erased by the tap that opens//     it, and `onConfirm` is the only path to the act.//   * **Neither verb works while `isLoading`.** A double-tapped Empty is two//     erasures, and the second one cannot be undone any more than the first.//     `confirm()` and `cancel()` hold that rule, and the test calls them.//// A full-screen overlay rather than a `.sheet`, because this is a question// ABOVE the screen you are reading and a sheet is a surface you go INTO.// `.alert` is not this: an alert is the platform's own card in the platform's// own voice.import SwiftUIpublic enum ConfirmDecisionTone: Sendable {    /// The act erases something. The verb is red ink, and the button carries    /// the destructive role so VoiceOver says so.    case danger    /// Everything else. The verb is ink.    case primary    /// The ink the act's verb is typed in.    var verbInk: PaperInk {        switch self {        case .danger: return .red        case .primary: return .ink        }    }    /// The button's role — what a screen reader announces before the word.    var role: ButtonRole? {        switch self {        case .danger: return .destructive        case .primary: return nil        }    }}/// The slip's measurements, by name.enum ConfirmDecisionSlip {    static let maxWidth: CGFloat = 320    static let inset: CGFloat = 24    static let titleSize: CGFloat = 21    static let messageSize: CGFloat = 14    static let verbSize: CGFloat = 16    /// How much the plaster behind the slip is dimmed.    static let scrim: Double = 0.30}public struct ConfirmDecisionDialog: View {    private let title: String    private let message: String    private let confirmLabel: String    private let cancelLabel: String    private let tone: ConfirmDecisionTone    private let isLoading: Bool    private let identifier: String?    private let onConfirm: () -> Void    private let onCancel: () -> Void    /// A second answer beside the first ("Only this one" / "Every one"): the    /// verbs then stand one under the other, Cancel last.    private var otherLabel: String?    private var onOther: (() -> Void)?    @Environment(\.theme) private var theme    public init(        title: String,        message: String,        confirmLabel: String,        cancelLabel: String? = nil,        tone: ConfirmDecisionTone = .danger,        isLoading: Bool = false,        identifier: String? = nil,        onConfirm: @escaping () -> Void,        onCancel: @escaping () -> Void    ) {        self.title = title        self.message = message        self.confirmLabel = confirmLabel        self.cancelLabel = cancelLabel ?? "Cancel"        self.tone = tone        self.isLoading = isLoading        self.identifier = identifier        self.onConfirm = onConfirm        self.onCancel = onCancel    }    /// Take the act — unless it is already in flight.    public func confirm() {        guard !isLoading else { return }        onConfirm()    }    /// The slip with a second answer under the first.    public func other(_ label: String?, _ act: @escaping () -> Void) -> ConfirmDecisionDialog {        var copy = self        copy.otherLabel = label        copy.onOther = act        return copy    }    /// Take the second answer — unless an act is already in flight.    public func chooseOther() {        guard !isLoading else { return }        onOther?()    }    /// Put the slip away — unless the act is already in flight, when walking    /// away would leave it finishing behind a screen that no longer says so.    public func cancel() {        guard !isLoading else { return }        onCancel()    }    /// The slip's seed: the caller's identifier when it has one, so the same    /// question is the same slip every time it is asked.    var seed: String { "question-\(identifier ?? title)" }    public var body: some View {        ZStack {            CottonColors.ink.opacity(ConfirmDecisionSlip.scrim)                .ignoresSafeArea()                // The scrim dismisses: the one gesture a modal question must                // always answer.                .onTapGesture { cancel() }            slip        }        .accessibilityIdentifier(identifier ?? "")        .accessibilityAddTraits(.isModal)    }    private var slip: some View {        Paper(dye: .cream, edge: .torn, seed: seed, shadow: .lifted, inset: ConfirmDecisionSlip.inset) {            VStack(spacing: 0) {                Text(title)                    .paperTitle(ConfirmDecisionSlip.titleSize)                    .multilineTextAlignment(.center)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.top, 6)                    .padding(.bottom, 10)                Text(message)                    .paperBody(ConfirmDecisionSlip.messageSize)                    .lineSpacing(3)                    .multilineTextAlignment(.center)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.bottom, 20)                PencilRule(seed: seed, theme: theme)                    .stroke(theme.print(.pencil), lineWidth: 0.9)                    .blendMode(theme.printBlend)                    .frame(height: 2)                if let otherLabel {                    VStack(spacing: 0) {                        verb(confirmLabel, "confirm", action: confirm)                        verb(otherLabel, "other", action: chooseOther)                        Button(action: cancel) {                            Text(cancelLabel)                                .paperBody(ConfirmDecisionSlip.verbSize, ink: .pencil)                                .frame(maxWidth: .infinity, minHeight: 48)                                .contentShape(Rectangle())                        }                        .buttonStyle(OpacityButtonStyle(pressed: 0.6))                        .accessibilityIdentifier(identifier.map { "\($0)-cancel" } ?? "")                    }                    .padding(.top, 4)                } else {                HStack(spacing: 0) {                    Button(action: cancel) {                        Text(cancelLabel)                            .paperBody(ConfirmDecisionSlip.verbSize, ink: .pencil)                            .frame(maxWidth: .infinity, minHeight: 48)                            .contentShape(Rectangle())                    }                    .buttonStyle(OpacityButtonStyle(pressed: 0.6))                    .disabled(isLoading)                    .accessibilityIdentifier(identifier.map { "\($0)-cancel" } ?? "")                    Button(role: tone.role, action: confirm) {                        ZStack {                            if isLoading {                                // The ONE place this app draws a native                                // indicator outside the migration screen: the                                // slip is already drawn, and what is in flight                                // is the act rather than a screen.                                ProgressView().tint(theme.print(tone.verbInk))                            } else {                                Text(confirmLabel)                                    .paperBody(ConfirmDecisionSlip.verbSize, ink: tone.verbInk)                            }                        }                        .frame(maxWidth: .infinity, minHeight: 48)                        .contentShape(Rectangle())                    }                    .buttonStyle(OpacityButtonStyle(pressed: 0.6))                    .disabled(isLoading)                    .accessibilityIdentifier(identifier.map { "\($0)-confirm" } ?? "")                }                .padding(.top, 4)                .opacity(isLoading ? 0.6 : 1)                }            }        }        // The slip is as tall as its words. `Paper` fills a height it is        // PROPOSED, and the ZStack over the screen proposes the whole screen.        .fixedSize(horizontal: false, vertical: true)        .frame(maxWidth: ConfirmDecisionSlip.maxWidth)        .padding(.horizontal, 28)    }}extension ConfirmDecisionDialog {    /// One of two answers, typed in the act's ink.    fileprivate func verb(_ label: String, _ name: String, action: @escaping () -> Void) -> some View {        Button(role: tone.role, action: action) {            Text(label)                .paperBody(ConfirmDecisionSlip.verbSize, ink: tone.verbInk)                .frame(maxWidth: .infinity, minHeight: 48)                .contentShape(Rectangle())        }        .buttonStyle(OpacityButtonStyle(pressed: 0.6))        .accessibilityIdentifier(identifier.map { "\($0)-\(name)" } ?? "")    }}extension View {    /// Ask a question that cannot be taken back, over this screen.    ///    /// Public for the Mac's own Settings pages, which ask their one question    /// (the recovery key) on the same slip — the reason the slip is the    /// look's (`CottonPaper`) and asks nothing of a record. How far a write to    /// a repeating record reaches is `CottonUI`'s question (`scopeDecision`).    public func confirmDecision(        isPresented: Bool,        title: String,        message: String,        confirmLabel: String,        cancelLabel: String? = nil,        tone: ConfirmDecisionTone = .danger,        isLoading: Bool = false,        identifier: String? = nil,        onConfirm: @escaping () -> Void,        onCancel: @escaping () -> Void    ) -> some View {        overlay {            if isPresented {                ConfirmDecisionDialog(                    title: title,                    message: message,                    confirmLabel: confirmLabel,                    cancelLabel: cancelLabel,                    tone: tone,                    isLoading: isLoading,                    identifier: identifier,                    onConfirm: onConfirm,                    onCancel: onCancel                )                .transition(.opacity)            }        }        .animation(.easeInOut(duration: 0.15), value: isPresented)    }}