// The pieces a form on paper is made of — every sheet a person types into.//// A FORM IS A SHEET BEING FILLED IN. Nothing on a paper form is a grey input// box: a field is a typed line on a pencil rule, its hint is the same line in// pencil, and a section is one serif word above its lines (picture 04's printed// row, picture 31's key · value sheet). A choice between a few words is the// words printed in a row with the chosen one ringed in red pencil — the// passbook's own mark. What stays DIGITAL stays digital on purpose: the// keyboard, the date and time pickers, the photo picker (spec §1.6 — the input// devices are the phone's, the record is the paper's).//// THE ACTIONS HAVE TWO SHAPES. The one thing a form is for — Save, Add — is the// ink circle Home's New thought is drawn in, so a person finds the same dark// shape at the foot of every form. Everything else is a typed word, underlined// in pencil, and a destructive one is typed in red ink.//// THE SHEET ITSELF IS NOT DRAWN HERE. `cottonSheet` owns the sheet's material// (`Chrome/Sheets.swift`); these pieces are the contents, and they read on the// sheet's ground whatever it is dressed as.importSwiftUI/// One serif word over the lines it heads.publicstructPaperFormSection<Content: View>: View { @Environment(\.theme) privatevar themelet title: Stringlet content: Contentpublicinit(_ title: String, @ViewBuilder content: () -> Content) {self.title = titleself.content = content() }publicvar body: someView { VStack(alignment: .leading, spacing: PaperFormKnobs.paperFormSectionGap) {Text(title) .paperTitle(theme.scale.titleSize + 2) .accessibilityAddTraits(.isHeader) content } .frame(maxWidth: .infinity, alignment: .leading) }}/// A typed line on a pencil rule. The hint is the same line in pencil, so an/// empty field reads as a line waiting to be typed on rather than a box.publicstructPaperTextField: View { @Environment(\.theme) privatevar themelet hint: String @Binding var text: String/// The typed size; nil is the form's field size.let size: CGFloat?let axis: Axis/// The lines a vertical field grows through; nil is the form's own range.let lines: ClosedRange<Int>?/// Room between wrapped lines — a long text types with its page's, so its/// lines break where they break when read (`PageMarkdown.voice`).var lineSpacing: CGFloat = 0/// The object's own name is set in the serif; every other line is typed.let serif: Bool/// The form's own focus, when the form raises the keyboard on arrival.let focus: FocusState<Bool>.Binding? #if os(iOS)/// Where the caret stands, for a field that puts something there (a long/// text's attach). `TextSelection` is iOS 18 / macOS 15; on the Mac's 14/// floor there is none and the caret reads as the end of the text.private(set) var selection: Binding<TextSelection?>? #endifpublicinit( _ hint: String, text: Binding<String>, size: CGFloat? = nil, axis: Axis = .horizontal, lines: ClosedRange<Int>? = nil, lineSpacing: CGFloat = 0, serif: Bool = false, focus: FocusState<Bool>.Binding? = nil ) {self.lineSpacing = lineSpacingself.serif = serifself.focus = focusself.hint = hintself._text = textself.size = sizeself.axis = axisself.lines = lines #if os(iOS)self.selection = nil #endif } #if os(iOS)/// The same line, reporting where its caret stands.publicinit( _ hint: String, text: Binding<String>, selection: Binding<TextSelection?>, size: CGFloat? = nil, axis: Axis = .horizontal, lines: ClosedRange<Int>? = nil, lineSpacing: CGFloat = 0, focus: FocusState<Bool>.Binding? = nil ) {self.init(hint, text: text, size: size, axis: axis, lines: lines, lineSpacing: lineSpacing, focus: focus)self.selection = selection } #endif/// The rule is an OVERLAY under the typed line, not a sibling in a stack:/// a caller's `.accessibilityLabel` / `.accessibilityIdentifier` then lands/// on the text field itself, which reads as a text field with its label and/// its value. A stack made it a group with a label and no value, and a/// walker's describe could not find the field (architect, 5 Oct 2026).publicvar body: someView { field .padding(.bottom, PaperFormKnobs.paperFormFieldRuleBottomInset + PaperFormKnobs.paperFormRuleHeight) .overlay(alignment: .bottom) { PaperFormRule() } } @ViewBuilder privatevar field: someView {iflet focus { typed.focused(focus) } else { typed } } @ViewBuilder privatevar input: someView {let prompt = Text(hint).font(face).foregroundStyle(theme.print(.pencil)) #if os(iOS)iflet selection {TextField(hint, text: $text, selection: selection, prompt: prompt, axis: axis) } else {TextField(hint, text: $text, prompt: prompt, axis: axis) } #elseTextField(hint, text: $text, prompt: prompt, axis: axis) #endif }privatevar typed: someView { input .font(face) .foregroundStyle(theme.print(.ink)) .tint(theme.print(.red)) .lineLimit(axis == .vertical ? lineRange : 1...1) .lineSpacing(lineSpacing)// A typed line on a pencil rule, never a bordered platform field with// a blue ring around it (`PaperFocus.swift`). What says the line has// the keyboard is the caret in the red ink above. .cottonPaperField() }privatevar lineRange: ClosedRange<Int> { lines ?? (1...PaperFormKnobs.paperFormFieldMaxLines) }privatevar typeSize: CGFloat { size ?? PaperFormKnobs.paperFormFieldSize }privatevar face: Font { serif ? theme.type.title(typeSize) : theme.type.body(typeSize) }}/// The pencil rule a typed line sits on.publicstructPaperFormRule: View { @Environment(\.theme) privatevar themelet seed: Stringpublicinit(seed: String = "form-rule") {self.seed = seed }publicvar body: someView {PencilRule(seed: seed, theme: theme) .stroke( theme.print(.pencil).opacity(Double(PaperFormKnobs.paperFormRuleOpacity)), style: StrokeStyle(lineWidth: PaperFormKnobs.paperFormRuleWidth, lineCap: .round))// The rule's own height, which a field lays under its typed line. .frame(height: PaperFormKnobs.paperFormRuleHeight) .accessibilityHidden(true) }}/// The form's one purpose, as the ink circle.publicstructPaperInkCircle: View { @Environment(\.theme) privatevar themelet symbol: Stringlet label: Stringlet isEnabled: Boollet isBusy: Boollet action: () -> Voidpublicinit(symbol: String = "checkmark", label: String, isEnabled: Bool = true, isBusy: Bool = false, action: @escaping () -> Void) {self.symbol = symbolself.label = labelself.isEnabled = isEnabledself.isBusy = isBusyself.action = action }publicvar body: someView {Button(action: action) { ZStack {Circle().fill(theme.colors.ink.color)if isBusy {ProgressView().tint(theme.colors.ground.color) } else {Image(systemName: Glyph.symbol(symbol) ?? symbol) .font(theme.type.interface(PaperFormKnobs.paperFormInkCircleIconSize, weight: .medium)) .foregroundStyle(theme.colors.ground.color) } } .frame(width: PaperFormKnobs.paperFormInkCircleSide, height: PaperFormKnobs.paperFormInkCircleSide) .liftShadow() .opacity(isEnabled ? 1 : DisabledOpacity.actionPill) .contentShape(Circle()) } .buttonStyle(MotionButtonStyle()) .disabled(!isEnabled || isBusy) .accessibilityLabel(label) }}/// A secondary action as a typed word, underlined in pencil — in red ink when/// it destroys something.publicstructPaperTypedButton: View { @Environment(\.theme) privatevar themelet title: Stringlet destructive: Bool/// The typed size; nil is the form's field size.let size: CGFloat?let action: () -> Void/// A verb that cannot act right now is typed in pencil at a third off —/// a custom button style is not dimmed by `.disabled` on its own. @Environment(\.isEnabled) privatevar isEnabledpublicinit( _ title: String, destructive: Bool = false, size: CGFloat? = nil, action: @escaping () -> Void ) {self.title = titleself.destructive = destructiveself.size = sizeself.action = action }publicvar body: someView {Button(action: action) {Text(title) .paperBody(size ?? PaperFormKnobs.paperFormFieldSize, ink: destructive ? .red : .ink) .padding(.bottom, PaperFormKnobs.paperFormButtonRuleBottomInset) .overlay(alignment: .bottom) {PencilUnderline(seed: "typed-\(title)", theme: theme) .stroke( theme.print(destructive ? .red : .pencil) .opacity(Double(PaperFormKnobs.paperFormButtonRuleOpacity)), style: StrokeStyle(lineWidth: PaperFormKnobs.paperFormButtonRuleWidth, lineCap: .round)) .frame(height: PaperFormKnobs.paperFormButtonRuleHeight) } .padding(.vertical, PaperFormKnobs.paperFormButtonVerticalInset) .contentShape(Rectangle()) .opacity(isEnabled ? 1 : DisabledOpacity.actionPill) } .buttonStyle(OpacityButtonStyle(pressed: Double(PaperFormKnobs.paperFormPressedOpacity))) }}/// A wrapping row of equal-spaced children, the sizes their own.publicstructPaperWrap: Layout {var spacing: CGFloat/// A `Layout` has no environment to read a theme from, so the caller/// passes its worn theme's gap (`paper_form_wrap_gap` when it has none of/// its own).publicinit(spacing: CGFloat) {self.spacing = spacing }publicfunc sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {let rows = arrange(width: proposal.width ?? .infinity, subviews: subviews)let height = rows.reduce(0) { $0 + $1.height } + spacing * CGFloat(max(0, rows.count - 1))let width = rows.map(\.width).max() ?? 0return CGSize(width: proposal.width ?? width, height: height) }publicfunc placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {var y = bounds.minYfor row in arrange(width: bounds.width, subviews: subviews) {var x = bounds.minXfor index in row.indices {let childProposal = constrainedProposal(for: subviews[index], width: bounds.width)let size = subviews[index].sizeThatFits(childProposal) subviews[index].place(at: CGPoint(x: x, y: y + (row.height - size.height) * 0.5), proposal: childProposal) x += size.width + spacing } y += row.height + spacing } }privatestructRow {var indices: [Int] = []var width: CGFloat = 0var height: CGFloat = 0 }privatefunc constrainedProposal(for subview: LayoutSubview, width: CGFloat) -> ProposedViewSize {let naturalWidth = subview.sizeThatFits(.unspecified).widthreturnProposedViewSize(width: min(naturalWidth, max(0, width)), height: nil) }privatefunc arrange(width: CGFloat, subviews: Subviews) -> [Row] {var rows: [Row] = [Row()]for index in subviews.indices {let size = subviews[index].sizeThatFits(constrainedProposal(for: subviews[index], width: width))let needed = rows[rows.count - 1].indices.isEmpty ? size.width : rows[rows.count - 1].width + spacing + size.widthif needed > width, !rows[rows.count - 1].indices.isEmpty { rows.append(Row()) }var row = rows[rows.count - 1] row.width = row.indices.isEmpty ? size.width : row.width + spacing + size.width row.height = max(row.height, size.height) row.indices.append(index) rows[rows.count - 1] = row }return rows.filter { !$0.indices.isEmpty } }}/// The sizes, counts and timings PaperForm draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enumPaperFormKnobs {staticlet paperFormButtonRuleBottomInset: CGFloat = 3staticlet paperFormButtonRuleHeight: CGFloat = 3staticlet paperFormButtonRuleOpacity: CGFloat = 0.7staticlet paperFormButtonRuleWidth: CGFloat = 1.1staticlet paperFormButtonVerticalInset: CGFloat = 8staticlet paperFormChoiceDisabledOpacity: CGFloat = 0.4staticlet paperFormChoiceGap: CGFloat = 6staticlet paperFormChoiceHorizontalInset: CGFloat = 10staticlet paperFormChoiceRuleWidth: CGFloat = 1.4staticlet paperFormChoiceVerticalInset: CGFloat = 7staticlet paperFormFieldMaxLines = 6staticlet paperFormFieldRuleBottomInset: CGFloat = 5publicstaticlet paperFormFieldSize: CGFloat = 16staticlet paperFormInkCircleIconSize: CGFloat = 20staticlet paperFormInkCircleSide: CGFloat = 56staticlet paperFormPressedOpacity: CGFloat = 0.7staticlet paperFormRowGap: CGFloat = 12staticlet paperFormRowIconSize: CGFloat = 15staticlet paperFormRowIconWidth: CGFloat = 22staticlet paperFormRowStackGap: CGFloat = 5staticlet paperFormRowValueLines = 2staticlet paperFormRowVerticalInset: CGFloat = 4staticlet paperFormRuleHeight: CGFloat = 2staticlet paperFormRuleOpacity: CGFloat = 0.55staticlet paperFormRuleWidth: CGFloat = 1staticlet paperFormSectionGap: CGFloat = 8}