// 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. import SwiftUI /// One serif word over the lines it heads. public struct PaperFormSection: View { @Environment(\.theme) private var theme let title: String let content: Content public init(_ title: String, @ViewBuilder content: () -> Content) { self.title = title self.content = content() } public var body: some View { 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. public struct PaperTextField: View { @Environment(\.theme) private var theme let 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? /// 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.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? #endif public init( _ hint: String, text: Binding, size: CGFloat? = nil, axis: Axis = .horizontal, lines: ClosedRange? = nil, lineSpacing: CGFloat = 0, serif: Bool = false, focus: FocusState.Binding? = nil ) { self.lineSpacing = lineSpacing self.serif = serif self.focus = focus self.hint = hint self._text = text self.size = size self.axis = axis self.lines = lines #if os(iOS) self.selection = nil #endif } #if os(iOS) /// The same line, reporting where its caret stands. public init( _ hint: String, text: Binding, selection: Binding, size: CGFloat? = nil, axis: Axis = .horizontal, lines: ClosedRange? = nil, lineSpacing: CGFloat = 0, focus: FocusState.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). public var body: some View { field .padding(.bottom, PaperFormKnobs.paperFormFieldRuleBottomInset + PaperFormKnobs.paperFormRuleHeight) .overlay(alignment: .bottom) { PaperFormRule() } } @ViewBuilder private var field: some View { if let focus { typed.focused(focus) } else { typed } } @ViewBuilder private var input: some View { let prompt = Text(hint).font(face).foregroundStyle(theme.print(.pencil)) #if os(iOS) if let selection { TextField(hint, text: $text, selection: selection, prompt: prompt, axis: axis) } else { TextField(hint, text: $text, prompt: prompt, axis: axis) } #else TextField(hint, text: $text, prompt: prompt, axis: axis) #endif } private var typed: some View { 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() } private var lineRange: ClosedRange { lines ?? (1...PaperFormKnobs.paperFormFieldMaxLines) } private var typeSize: CGFloat { size ?? PaperFormKnobs.paperFormFieldSize } private var face: Font { serif ? theme.type.title(typeSize) : theme.type.body(typeSize) } } /// The pencil rule a typed line sits on. public struct PaperFormRule: View { @Environment(\.theme) private var theme let seed: String public init(seed: String = "form-rule") { self.seed = seed } public var body: some View { 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. public struct PaperInkCircle: View { @Environment(\.theme) private var theme let symbol: String let label: String let isEnabled: Bool let isBusy: Bool let action: () -> Void public init(symbol: String = "checkmark", label: String, isEnabled: Bool = true, isBusy: Bool = false, action: @escaping () -> Void) { self.symbol = symbol self.label = label self.isEnabled = isEnabled self.isBusy = isBusy self.action = action } public var body: some View { 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. public struct PaperTypedButton: View { @Environment(\.theme) private var theme let title: String let 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) private var isEnabled public init( _ title: String, destructive: Bool = false, size: CGFloat? = nil, action: @escaping () -> Void ) { self.title = title self.destructive = destructive self.size = size self.action = action } public var body: some View { 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. public struct PaperWrap: 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). public init(spacing: CGFloat) { self.spacing = spacing } public func 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() ?? 0 return CGSize(width: proposal.width ?? width, height: height) } public func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { var y = bounds.minY for row in arrange(width: bounds.width, subviews: subviews) { var x = bounds.minX for 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 } } private struct Row { var indices: [Int] = [] var width: CGFloat = 0 var height: CGFloat = 0 } private func constrainedProposal(for subview: LayoutSubview, width: CGFloat) -> ProposedViewSize { let naturalWidth = subview.sizeThatFits(.unspecified).width return ProposedViewSize(width: min(naturalWidth, max(0, width)), height: nil) } private func 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.width if 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`). enum PaperFormKnobs { static let paperFormButtonRuleBottomInset: CGFloat = 3 static let paperFormButtonRuleHeight: CGFloat = 3 static let paperFormButtonRuleOpacity: CGFloat = 0.7 static let paperFormButtonRuleWidth: CGFloat = 1.1 static let paperFormButtonVerticalInset: CGFloat = 8 static let paperFormChoiceDisabledOpacity: CGFloat = 0.4 static let paperFormChoiceGap: CGFloat = 6 static let paperFormChoiceHorizontalInset: CGFloat = 10 static let paperFormChoiceRuleWidth: CGFloat = 1.4 static let paperFormChoiceVerticalInset: CGFloat = 7 static let paperFormFieldMaxLines = 6 static let paperFormFieldRuleBottomInset: CGFloat = 5 public static let paperFormFieldSize: CGFloat = 16 static let paperFormInkCircleIconSize: CGFloat = 20 static let paperFormInkCircleSide: CGFloat = 56 static let paperFormPressedOpacity: CGFloat = 0.7 static let paperFormRowGap: CGFloat = 12 static let paperFormRowIconSize: CGFloat = 15 static let paperFormRowIconWidth: CGFloat = 22 static let paperFormRowStackGap: CGFloat = 5 static let paperFormRowValueLines = 2 static let paperFormRowVerticalInset: CGFloat = 4 static let paperFormRuleHeight: CGFloat = 2 static let paperFormRuleOpacity: CGFloat = 0.55 static let paperFormRuleWidth: CGFloat = 1 static let paperFormSectionGap: CGFloat = 8 }