Cotton code
250 lines · 10.2 KB · SwiftRaw
// The one empty/notice state, and the one skeleton.//// `QuietState` is a THIN WRAPPER over the kit's one empty state (phase 20,// 2026-09-14): a blank sheet — `ObjectEmpty(sheet:)`, picture 37 — with the// title as its first typed line, the sentence as its second, and AT MOST ONE// forward verb. Omitting the verb's closure types it as a statement (a sent// state) rather than drawing a control that does nothing. A screen that holds// something other than sheets (people, photos, projects) passes its own blank// object to `ObjectEmpty` directly.//// A TOOL'S EMPTY LIST STANDS ITS OWN PICTURE (walker294 rows 84, 108–110:// Members and Social read "No … yet" under a blank grey card that said// nothing about the tool). Given the definition's `picture`, the empty state// draws that object — the stories' bundle, the guest book — in place of the// blank sheet; with none it keeps the sheet.//// `SkeletonText` is the other half of this file. A screen's loading state is a// SKELETON behind `isLoading`, never a spinner: the shape of what is coming is// information, and a spinner is not. On paper the shape is blank paper — slips// at the size of the lines that are coming, or pencil-ruled lines when the// skeleton already stands on a sheet.import CottonValuesimport SwiftUI/// What a `QuietState` hands the empty state — pure, so a test can read it.struct QuietStateLines: Equatable {    let line1: String    let line2: String?    let actionLabel: String?    /// A verb with no closure is a statement, not a control.    let isStatement: Bool    init(title: String, message: String, actionLabel: String?, hasAction: Bool) {        line1 = title        // An empty sentence is no second line, not a blank one.        line2 = message.isEmpty ? nil : message        self.actionLabel = actionLabel        isStatement = actionLabel != nil && !hasAction    }}public struct QuietState: View {    private let lines: QuietStateLines    /// The action's own name, which is a SEPARATE identifier from the state's    /// — every caller passes `<state>-retry`. A retry a test cannot press by    /// name is a retry no test presses.    private let actionIdentifier: String?    private let action: (() -> Void)?    /// The tool's own picture and name, stood in place of the blank sheet.    private let picture: ToolPicture?    private let name: String    public init(        title: String,        message: String,        actionLabel: String? = nil,        actionIdentifier: String? = nil,        picture: ToolPicture? = nil,        name: String = "",        action: (() -> Void)? = nil    ) {        self.lines = QuietStateLines(            title: title, message: message, actionLabel: actionLabel, hasAction: action != nil)        self.actionIdentifier = actionIdentifier        self.picture = picture        self.name = name        self.action = action    }    public var body: some View {        Group {            if let picture {                ObjectEmpty(                    object: ToolObject(name: name, picture: picture, printing: .contentOnly)                        .frame(height: QuietStateKnobs.pictureSide),                    line1: lines.line1,                    line2: lines.line2,                    actionLabel: lines.actionLabel,                    actionIdentifier: actionIdentifier,                    action: action                )            } else {                ObjectEmpty(                    sheet: .cream,                    line1: lines.line1,                    line2: lines.line2,                    actionLabel: lines.actionLabel,                    actionIdentifier: actionIdentifier,                    action: action                )            }        }        .padding(.horizontal, 28)    }}/// The sizes QuietState draws with: Swift's own, beside their reader.enum QuietStateKnobs {    /// The tool's picture stands in the blank sheet's height.    static let pictureSide: CGFloat = 108}/// A skeleton is shown only when the answer is SLOW.////// A `read.*` over the local file answers in single-digit milliseconds, and a/// skeleton painted on a screen's first frame — before the read has even been/// asked — is a flash of grey bars over an answer that was about to arrive./// Every skeleton on the phone is therefore invisible for its first/// `SkeletonRule.delay` and paints only if it is still standing after that:/// an answer that lands inside the window never presents one, and a slow one/// (the first open after a restore, a page behind a fold) says "reading" as/// it should. Applied INSIDE the skeleton components rather than at each of/// the hundred call sites, so a screen cannot forget it.////// The number: a local answer is milliseconds — `read.people` in 3 ms, two/// hundred messages in 32 ms, measured on the Mac over the owner's own file —/// a frame is 16, and the perceptual floor for "something is happening" is/// about 100. Under that a skeleton reads as flicker; at about 200 a page that/// has drawn nothing starts to read as stuck. So the window is 150 (architect,/// 11 Oct): every local answer lands inside it many times over — reads no/// longer queue behind one connection, so a board's tiles do not push the/// list's answer out of it — and only a read that is genuinely slow — the/// first open after a restore, a page behind a fold — ever presents one. One/// constant for the whole phone, which the board's first draw waits no longer/// than either (`BoardSurface.read`); `SkeletonRuleTests` holds it.enum SkeletonRule {    public static let delay: Duration = .milliseconds(150)}/// Invisible until it has stood for `SkeletonRule.delay`, keeping its layout/// so the content that replaces it does not jump. The two skeleton components/// below wear it, and ONLY they do: `SkeletonSweepTests` holds that the/// modifier is spelled nowhere else and that every `skeleton` a screen draws/// is one of the two — a hand-built skeleton is a hand-built chance to paint/// on the first frame.struct SkeletonDelay: ViewModifier {    @State private var due = false    func body(content: Content) -> some View {        content            .opacity(due ? 1 : 0)            .task {                try? await Task.sleep(for: SkeletonRule.delay)                guard !Task.isCancelled else { return }                due = true            }    }}extension View {    /// A skeleton that waits `SkeletonRule.delay` before it paints.    func skeletonDelayed() -> some View { modifier(SkeletonDelay()) }}/// The one page-shaped skeleton: a few paragraphs of `SkeletonText`, so every/// screen waits under the same shape and the same delay.////// It replaced a skeleton per screen — blank cards at half opacity shaped like/// each list's own cells — most of which forgot the delay and painted on the/// first frame. The skeleton is a RARE fallback now (`AnswerCache` seeds a/// screen the person has been to; a first visit answers inside the window),/// and a fallback nobody should see is not worth a shape per screen.public struct SkeletonPage: View {    private let rows: Int    private let identifier: String?    /// - Parameters:    ///   - rows: how many two-line paragraphs stand in — a list's worth.    ///   - identifier: the accessibility identifier a screen's tests look for.    public init(rows: Int = 4, identifier: String? = nil) {        self.rows = rows        self.identifier = identifier    }    public var body: some View {        VStack(alignment: .leading, spacing: 20) {            ForEach(0..<max(1, rows), id: \.self) { _ in SkeletonText(lines: 2) }        }        .padding(.top, 8)        .frame(maxWidth: .infinity, alignment: .leading)        .accessibilityElement(children: .ignore)        .accessibilityIdentifier(identifier ?? "skeleton-page")    }}public struct SkeletonText: View {    private let lines: Int    private let lineWidth: CGFloat    private let lastLineWidth: CGFloat    private let lineHeight: CGFloat    @Environment(\.isOnPaper) private var isOnPaper    @Environment(\.theme) private var theme    /// - Parameters:    ///   - lineWidth: the fraction every line but the last takes. A paragraph    ///     skeleton wants full-width lines with a short last one; a skeleton    ///     standing in for a LIST of short things (a project name) wants every    ///     bar the same short width    ///   - lineHeight: the bar's height. The default is a body line; a row of    ///     `rowTitle` text is 20 (`h-5`).    public init(        lines: Int = 2,        lastLineWidth: CGFloat = 0.45,        lineWidth: CGFloat = 1,        lineHeight: CGFloat = 14    ) {        self.lines = lines        self.lastLineWidth = lastLineWidth        self.lineWidth = lineWidth        self.lineHeight = lineHeight    }    /// The fraction line `index` of `lines` takes.    static func width(index: Int, lines: Int, lineWidth: CGFloat, lastLineWidth: CGFloat) -> CGFloat {        index == max(1, lines) - 1 ? lastLineWidth : lineWidth    }    public var body: some View {        VStack(alignment: .leading, spacing: 8) {            ForEach(0..<max(1, lines), id: \.self) { index in                bar(index)                    .frame(height: lineHeight)                    .frame(maxWidth: .infinity, alignment: .leading)                    .scaleEffect(                        x: SkeletonText.width(                            index: index, lines: lines, lineWidth: lineWidth, lastLineWidth: lastLineWidth),                        y: 1,                        anchor: .leading                    )            }        }        .accessibilityHidden(true)        .skeletonDelayed()    }    /// A blank slip of paper at the line's size — or, already on a sheet, a    /// pencil rule where the line will be typed.    @ViewBuilder private func bar(_ index: Int) -> some View {        if isOnPaper {            PencilRule(seed: "skeleton-\(index)", theme: theme)                .stroke(theme.print(.pencil), lineWidth: 1)                .blendMode(theme.printBlend)                .opacity(0.5)        } else {            PaperSurface(dye: .cream, edge: .cut, seed: "skeleton-\(index)")                .modifier(PaperLift(shadow: .resting))        }    }}