Cotton code
152 lines · 6.4 KB · SwiftRaw
// A MEASUREMENT as a BRASS TAG (Cotton Health, 13 Nov 2026): a brass tag on// a loop of waxed red cord, hung on the honed slate (`FaceGround.slate`), the// kind a clinic hangs on a scale or a cuff, a reading engraved in its face.////   THE TAG: what was measured engraved small at its head (`tag.kind`), the//   reading large (`tag.said`, a blood pressure upper over lower) with its//   unit (`tag.unit`), before or after a meal where the meter said//   (`tag.context`) and when it was taken (`tag.taken`). Engraved: dark//   brass with the light the cut catches a hair below it.//// Tagged: the kind (`title`), the reading (`amount`), when (`when`). Stepped// by a tap, it cuts. Every word is the core's (`health_card::tag`).import CottonPaperimport CottonValuesimport SwiftUI/// What a measurement says on its tag.public struct HealthTagWords: Equatable, Sendable {    public let kind: String?    public let said: String?    public let unit: String?    public let context: String?    public let taken: String?    public init(_ card: DeckCard) {        var fields: [String: RecordValue] = [:]        if case .object(let object)? = card.fields["tag"] { fields = object }        func said(_ key: String) -> String? {            fields[key]?.text.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.flatMap { $0.isEmpty ? nil : $0 }        }        kind = said("kind") ?? card.text("title")        self.said = said("said") ?? card.text("subtitle")        unit = said("unit")        context = said("context")        taken = said("taken") ?? card.text("date")    }}public struct HealthTagFace: View {    let card: DeckCard    public init(card: DeckCard) {        self.card = card    }    public var body: some View {        let words = HealthTagWords(card)        HealthInstrument(aspect: HealthInstrumentKnobs.tagAspect, foot: nil) { size in            BrassTag(words: words, size: size)        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("health-tag")    }}/// Words engraved in brass: dark in the cut, the cut's lower lip catching/// the light a hair below.private struct Engraved: View {    let text: String    let font: Font    var body: some View {        Text(verbatim: text)            .font(font)            .foregroundStyle(HealthMaterial.engraving.colour)            .background(                Text(verbatim: text).font(font)                    .foregroundStyle(HealthMaterial.engravingLight.colour.opacity(0.7))                    .offset(x: 0.6, y: 1.0)            )    }}private struct BrassTag: View {    let words: HealthTagWords    let size: CGSize    var body: some View {        let face = HealthInstrumentKnobs.tagFace        let w = size.width        ZStack {            HealthLayer(name: "health-tag", size: size, bare: HealthMaterial.brass.colour, round: false)            VStack(spacing: w * 0.03) {                if let kind = words.kind {                    Engraved(text: kind.uppercased(), font: CottonFonts.cap(w * 0.07))                        .tracking(w * 0.008)                        .lineLimit(1)                        .minimumScaleFactor(0.5)                        .accessibilityIdentifier("health-tag-kind")                        .facePart(.title)                }                if let said = words.said {                    Engraved(text: said, font: CottonFonts.display(w * 0.2))                        .lineLimit(1)                        .minimumScaleFactor(0.4)                        .accessibilityIdentifier("health-tag-reading")                        .facePart(.amount)                }                if let unit = words.unit, !unit.isEmpty {                    Engraved(text: unit, font: CottonFonts.serif(w * 0.075))                        .accessibilityIdentifier("health-tag-unit")                }                if let context = words.context {                    Engraved(text: context, font: CottonFonts.serif(w * 0.06))                        .lineLimit(1)                        .minimumScaleFactor(0.6)                        .accessibilityIdentifier("health-tag-context")                }                if let taken = words.taken {                    Engraved(text: taken, font: CottonFonts.type(w * 0.052))                        .lineLimit(1)                        .minimumScaleFactor(0.5)                        .accessibilityIdentifier("health-tag-taken")                        .facePart(.when)                }            }            .frame(width: face.width * w * 0.86, height: face.height * size.height)            .position(x: face.midX * w, y: face.midY * size.height)        }        .frame(width: size.width, height: size.height)    }}/// A measurement by its kind, and the readings it is reviewed with:/// invented readings only.public enum HealthTagFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .slate    public static let face = Face(id: "health.tag", specimens: [        Specimen("weight", weight),        Specimen("blood-pressure", bloodPressure),        Specimen("glucose", glucose),        Specimen("temperature", temperature),    ], steps: .cut) { card, _, _ in        HealthTagFace(card: card)    }    static func tag(_ words: [String: RecordValue]) -> DeckCard {        DeckCard(face: "health.tag", corner: nil, pressing: false, pace: nil, plate: nil,                 fields: ["tag": .object(words), "title": words["kind"] ?? .null])    }    public static let weight = tag(["measure": .text("weight"), "kind": .text("Weight"), "said": .text("71.6"), "unit": .text("kg"),                                    "taken": .text("Thu 12 Nov · 7:00 am")])    public static let bloodPressure = tag(["measure": .text("blood_pressure"), "kind": .text("Blood pressure"), "said": .text("118/76"),                                           "unit": .text("mmHg"), "taken": .text("Thu 12 Nov · 8:00 am")])    public static let glucose = tag(["measure": .text("glucose"), "kind": .text("Blood glucose"), "said": .text("96"), "unit": .text("mg/dL"),                                     "context": .text("Before a meal"), "taken": .text("Wed 11 Nov · 1:10 pm")])    public static let temperature = tag(["measure": .text("temperature"), "kind": .text("Temperature"), "said": .text("36.8"), "unit": .text("°C"),                                         "taken": .text("Tue 10 Nov · 9:30 pm")])}