Cotton code
241 lines · 10.4 KB · SwiftRaw
// The PASSKEY card, Cotton Keys' first face (8 Nov 2026): a key tag on// paper, plain by design until its painted round. The site's own name typed// heavy with its address small under it, a lock drawn in pencil at the top// right, the account on a dotted rule, the owner's note in hand, and at the// foot the key's facts struck as stamps — when it was made, when it last// signed, how many times. Nothing of the key itself is on the card: no// credential id, no public key, never the sealed private half. Every word is// the core's (`DeckCard`); the tap map is the title (the site's name) and the// body (the note).import CottonPaperimport CottonValuesimport SwiftUIenum PasskeyCardFaceKnobs {    static let inset = EdgeInsets(top: 28, leading: 26, bottom: 24, trailing: 22)    static let siteSize: CGFloat = 30    /// The most a long site name shrinks before it takes its second line.    static let shrink: CGFloat = 0.6    static let addressSize: CGFloat = 15    static let addressGap: CGFloat = 6    static let accountSize: CGFloat = 19    static let accountTop: CGFloat = 22    static let ruleGap: CGFloat = 10    static let noteSize: CGFloat = 19    static let noteLines = 3    static let noteTop: CGFloat = 14    /// The lock's width; its height follows.    static let lockWidth: CGFloat = 34    static let lockGap: CGFloat = 12    static let stampLabel: CGFloat = 10    static let stampWords: CGFloat = 14    static let stampGap: CGFloat = 10    /// Each stamp struck a little off true, in turn.    static let turns: [Double] = [-3, 2, -1.5]}/// What a passkey's card reads off its `DeckCard`.public struct PasskeyReading: Equatable, Sendable {    public struct Stamp: Equatable, Sendable {        public let label: String?        public let words: String    }    /// The heading: the site's own name, else its address.    public let site: String?    /// The address under the heading, only when a name stands above it.    public let address: String?    public let account: String?    public let note: String?    public let stamps: [Stamp]    public init(_ card: DeckCard) {        let name = card.text("title")        let rp = card.text("subtitle")        site = name ?? rp        address = name == nil || rp == name ? nil : rp        account = card.text("holder")        note = card.text("body")        stamps = StudioReading.facts(card).map { Stamp(label: $0.label, words: $0.said) }    }}public struct PasskeyCardFace: View {    let card: DeckCard    public init(card: DeckCard) {        self.card = card    }    private var reading: PasskeyReading { PasskeyReading(card) }    public var body: some View {        VStack(alignment: .leading, spacing: 0) {            HStack(alignment: .top, spacing: PasskeyCardFaceKnobs.lockGap) {                VStack(alignment: .leading, spacing: PasskeyCardFaceKnobs.addressGap) {                    if let site = reading.site {                        HeavyType(words: site, size: PasskeyCardFaceKnobs.siteSize)                            .lineLimit(2)                            .minimumScaleFactor(PasskeyCardFaceKnobs.shrink)                            .fixedSize(horizontal: false, vertical: true)                            .accessibilityIdentifier("deck-title")                            .facePart(.title)                    }                    if let address = reading.address {                        Text(address)                            .font(CottonFonts.type(PasskeyCardFaceKnobs.addressSize))                            .lineLimit(1)                            .minimumScaleFactor(PasskeyCardFaceKnobs.shrink)                            .faceInk(.pencil)                            .accessibilityIdentifier("passkey-address")                    }                }                .frame(maxWidth: .infinity, alignment: .leading)                PasskeyLock()                    .frame(width: PasskeyCardFaceKnobs.lockWidth, height: PasskeyCardFaceKnobs.lockWidth * 1.2)            }            if let account = reading.account {                VStack(alignment: .leading, spacing: PasskeyCardFaceKnobs.ruleGap) {                    DottedRule()                    FaceFact(value: account, size: PasskeyCardFaceKnobs.accountSize, lines: 1)                        .accessibilityIdentifier("passkey-account")                    DottedRule()                }                .padding(.top, PasskeyCardFaceKnobs.accountTop)            }            if let note = reading.note {                FaceFact(value: note, hand: true, size: PasskeyCardFaceKnobs.noteSize, lines: PasskeyCardFaceKnobs.noteLines)                    .padding(.top, PasskeyCardFaceKnobs.noteTop)                    .facePart(.body)            }            Spacer(minLength: PasskeyCardFaceKnobs.stampGap)            if !reading.stamps.isEmpty {                ChipWrap(spacing: PasskeyCardFaceKnobs.stampGap) {                    ForEach(Array(reading.stamps.enumerated()), id: \.offset) { index, stamp in                        PasskeyStamp(stamp: stamp)                            .rotationEffect(.degrees(PasskeyCardFaceKnobs.turns[index % PasskeyCardFaceKnobs.turns.count]))                            .accessibilityIdentifier("passkey-stamp-\(index)")                    }                }                .padding(.top, 4)            }        }        .padding(PasskeyCardFaceKnobs.inset)        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)        .onCardPaper(PasskeyCardFaceCatalogue.stock.laid(card.pace))    }}/// One fact struck as a stamp: its label small over its words, in a ruled/// box, inked unevenly.struct PasskeyStamp: View {    let stamp: PasskeyReading.Stamp    @Environment(\.theme) private var theme    var body: some View {        VStack(alignment: .leading, spacing: 1) {            if let label = stamp.label {                Text(label.uppercased())                    .font(CottonFonts.type(PasskeyCardFaceKnobs.stampLabel))                    .lineLimit(1)            }            Text(stamp.words)                .font(CottonFonts.type(PasskeyCardFaceKnobs.stampWords))                .lineLimit(1)                .minimumScaleFactor(0.7)        }        .padding(.horizontal, 8)        .padding(.vertical, 5)        .overlay(RoundedRectangle(cornerRadius: 2).inset(by: 1).stroke(lineWidth: 1.2))        .foregroundStyle(theme.print(.ink))        .mask(StampWear(seed: stamp.words))        .blendMode(theme.printBlend)        .accessibilityElement(children: .combine)    }}/// A padlock drawn in pencil: its shackle, its body and the keyhole.struct PasskeyLock: View {    @Environment(\.theme) private var theme    var body: some View {        Canvas { context, size in            let ink = theme.print(.pencil)            let line = max(1.4, size.width * 0.07)            let body = CGRect(x: line / 2, y: size.height * 0.42, width: size.width - line, height: size.height * 0.58 - line / 2)            var shackle = Path()            let inset = size.width * 0.22            shackle.move(to: CGPoint(x: inset, y: body.minY))            shackle.addLine(to: CGPoint(x: inset, y: size.height * 0.28))            shackle.addArc(                center: CGPoint(x: size.width / 2, y: size.height * 0.28), radius: size.width / 2 - inset,                startAngle: .degrees(180), endAngle: .degrees(0), clockwise: false)            shackle.addLine(to: CGPoint(x: size.width - inset, y: body.minY))            context.stroke(shackle, with: .color(ink), style: StrokeStyle(lineWidth: line, lineCap: .round))            context.stroke(Path(roundedRect: body, cornerRadius: size.width * 0.1), with: .color(ink), lineWidth: line)            let hole = CGPoint(x: size.width / 2, y: body.midY - body.height * 0.08)            let r = size.width * 0.08            context.fill(Path(ellipseIn: CGRect(x: hole.x - r, y: hole.y - r, width: r * 2, height: r * 2)), with: .color(ink))            var stem = Path()            stem.move(to: hole)            stem.addLine(to: CGPoint(x: hole.x, y: hole.y + body.height * 0.3))            context.stroke(stem, with: .color(ink), style: StrokeStyle(lineWidth: line, lineCap: .round))        }        .blendMode(theme.printBlend)        .accessibilityHidden(true)    }}/// The passkey card, by the kind's name, and the cards it is reviewed in.public enum PasskeyCardFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "passkey.card", specimens: [        Specimen("used", used),        Specimen("unnamed", unnamed),        Specimen("never-used", neverUsed),        Specimen("largest", largest),    ]) { card, _, _ in        PasskeyCardFace(card: card)    }    static func passkey(_ fields: [String: RecordValue]) -> DeckCard {        DeckCard(face: .passkeyCard, fields: fields)    }    static func facts(_ said: [(String, String)]) -> RecordValue {        .list(said.map { .object(["label": .text($0.0), "said": .text($0.1)]) })    }    public static let used = passkey([        "title": .text("Example"),        "subtitle": .text("example.com"),        "holder": .text("ada@example.com"),        "body": .text("The work account."),        "facts": facts([("Made", "2 Oct 2026"), ("Last used", "5 Nov 2026"), ("Uses", "12 uses")]),    ])    /// A site that gave no name of its own: its address is the heading.    public static let unnamed = passkey([        "subtitle": .text("login.example.org"),        "holder": .text("ada.lovelace"),        "facts": facts([("Made", "18 Sep 2026"), ("Last used", "30 Oct 2026"), ("Uses", "3 uses")]),    ])    public static let neverUsed = passkey([        "title": .text("Example"),        "subtitle": .text("example.com"),        "holder": .text("work@example.com"),        "facts": facts([("Made", "21 Aug 2026"), ("Uses", "0 uses")]),    ])    /// Every place at its longest.    public static let largest = passkey([        "title": .text("The Example Cooperative Society of Imaginary Sites"),        "subtitle": .text("accounts.sign-in.example-cooperative.example"),        "holder": .text("ada.lovelace.countess.of.lovelace@example.com"),        "body": .text("A long note in the owner's own words: which device made it, why it is kept, and who else may use it on this account."),        "facts": facts([("Made", "Sat 2 Oct 2026, 09:14"), ("Last used", "Thu 5 Nov 2026, 18:40"), ("Uses", "1,204 uses")]),    ])}