// The sheet an act asks for its words on: the act's question as the title, // its message under it, a typed line on a pencil rule per field (a few lines // for a body), and the form's sage Keep card — held back while a required // field is blank — beside Discard. One sheet for every act that asks // (`RecordAction.fields`), on a list, a section or a page, so a pull request's // title and a workflow's branch are asked for the same way. // // It runs nothing itself: it hands back what was typed (`RecordAction.answers`) // and the surface that holds it runs the act, as the yes of a question does. // // **The sheet stays up until the act lands**, with everything typed in it: // Keep stands faint while the act runs, a refusal is printed in red under the // fields in the crate's own sentence, and only an act that was done closes the // sheet. A long description is never lost to "no such branch". // // A field that is `verbatim` (a branch, a tag) is typed exactly as typed: no // capital at its start and no correction of a word the keyboard does not know. import SwiftUI struct ActFieldsSheet: View { let action: RecordAction /// `-fields`: each line's identifier is this and its key. let identifier: String /// Runs the act with what was typed (trimmed, a blank field left out): /// nil once it was done, which closes the sheet, else the sentence why /// not, which the sheet prints and stays up with. let onRun: @MainActor ([String: String]) async -> String? @State private var typed: [String: String] @State private var running = false @State private var problem: String? @Environment(\.dismiss) private var dismiss @Environment(\.closeCard) private var closeCard @Environment(\.theme) private var theme init(action: RecordAction, identifier: String, onRun: @escaping @MainActor ([String: String]) async -> String?) { self.action = action self.identifier = identifier self.onRun = onRun _typed = State(initialValue: action.seeded) } var body: some View { VStack(alignment: .leading, spacing: ActFieldsSheetKnobs.actFieldsSheetGap) { Text(action.confirmation?.title ?? action.label) .paperTitle(theme.scale.titleSize + 2) .accessibilityAddTraits(.isHeader) if let message = action.confirmation?.message, !message.isEmpty { Text(message) .paperMeta(theme.scale.metaSize + 0.5) .fixedSize(horizontal: false, vertical: true) } ForEach(action.fields) { field in PaperFormSection(field.label) { if !field.choices.isEmpty { // Picked, not typed: one slip per answer, the chosen // one ticked. VStack(alignment: .leading, spacing: ActFieldsSheetKnobs.actFieldsChoiceGap) { ForEach(field.choices) { choice in let chosen = typed[field.key] == choice.id Button { typed[field.key] = choice.id } label: { Paper(dye: chosen ? .sage : .cream, edge: .cut, seed: "act-\(field.key)-\(choice.id)", tilt: false, inset: 0) { HStack { Text(choice.label).paperBody(theme.scale.bodySize + 1) Spacer(minLength: ActFieldsSheetKnobs.actFieldsChoiceSpacerMin) if chosen { Image(systemName: "checkmark").foregroundStyle(theme.print(.ink)) } } .padding(ActFieldsSheetKnobs.actFieldsChoiceInset) } } .buttonStyle(OpacityButtonStyle()) .accessibilityAddTraits(chosen ? .isSelected : []) .accessibilityIdentifier("\(identifier)-\(field.key)-\(choice.id)") } } } else if field.secure { // A password is typed blind: dots on the line, as // `EnteredValueField` types a sealed value. VStack(alignment: .leading, spacing: ActFieldsSheetKnobs.actFieldsSecureGap) { SecureField( "", text: line(field.key), prompt: Text(field.placeholder ?? "") .font(CottonFonts.type(theme.scale.titleSize - 1)) .foregroundStyle(theme.print(.pencil)) ) .textInputAutocapitalization(.never) .autocorrectionDisabled() .font(CottonFonts.type(theme.scale.titleSize - 1)) .foregroundStyle(theme.print(.ink)) .tint(theme.print(.red)) PaperFormRule(seed: "act-\(field.key)") } .accessibilityIdentifier("\(identifier)-\(field.key)") } else { PaperTextField( field.placeholder ?? "", text: line(field.key), axis: field.multiline ? .vertical : .horizontal, lines: field.multiline ? ActFieldsSheetKnobs.actFieldsMultilineMinLines...ActFieldsSheetKnobs.actFieldsMultilineMaxLines : ActFieldsSheetKnobs.actFieldsSinglelineMinLines...ActFieldsSheetKnobs.actFieldsSinglelineMaxLines ) .textInputAutocapitalization(field.verbatim ? .never : nil) .autocorrectionDisabled(field.verbatim) .accessibilityIdentifier("\(identifier)-\(field.key)") } } } if let problem { Text(problem) .paperBody(theme.scale.bodySize - 1, ink: .red) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("\(identifier)-problem") } // A form's two acts (r4): Keep on sage, held back while a needed // field is blank or the act runs; Discard, grey, writing nothing. CardRows(scale: theme.scale) { ActCard(act: ActFieldsSheetKnobs.keep, onFire: { _ in keep() }) .disabled(running || action.answers(typed) == nil) .opacity(running || action.answers(typed) == nil ? Double(ActFieldsSheetKnobs.actFieldsHeldBackOpacity) : 1) .accessibilityIdentifier("\(identifier)-run") ActCard(act: ActFieldsSheetKnobs.discard, onFire: { _ in CardClosing.close(closeCard, else: dismiss) }) .accessibilityIdentifier("\(identifier)-discard") } } .frame(maxWidth: .infinity, alignment: .leading) .accessibilityIdentifier(identifier) } private func keep() { guard !running, let answers = action.answers(typed) else { return } running = true problem = nil Task { let refused = await onRun(answers) running = false if let refused { problem = refused } else { CardClosing.close(closeCard, else: dismiss) } } } private func line(_ key: String) -> Binding { Binding(get: { typed[key] ?? "" }, set: { typed[key] = $0 }) } } /// The sizes, counts and timings ActFieldsSheet draws with: Swift's own, beside /// their reader — a theme sets only its tokens (`scale.tokens`). enum ActFieldsSheetKnobs { /// The form's two acts (r4): Keep, the main act on sage, and Discard. static let keep = ActCardReading(id: "form-keep", verb: CardWords.keep, object: .enamel(.done), main: true, does: .completes) static let discard = ActCardReading(id: "form-discard", verb: "Discard", object: .enamel(.bin), does: .putsAway) static let actFieldsChoiceGap: CGFloat = 8 static let actFieldsChoiceInset: CGFloat = 14 static let actFieldsChoiceSpacerMin: CGFloat = 8 static let actFieldsHeldBackOpacity: CGFloat = 0.45 static let actFieldsMultilineMaxLines = 8 static let actFieldsMultilineMinLines = 3 static let actFieldsSecureGap: CGFloat = 5 static let actFieldsSheetGap: CGFloat = 14 static let actFieldsSinglelineMaxLines = 6 static let actFieldsSinglelineMinLines = 1 }