Cotton code
209 lines · 9.4 KB · SwiftRaw
// The SECRET face (Passwords, references `passwords.png`, `passwords-id.png`,// the API key and card cases and the house references 19/49, 20/50, 72 and// 73), on the core's places (KINDS, 0a0a099c3): the framed cream card with// what kind of secret it is typed small where it is not a password (`state`:// "API key", "Card"), its name typed heavy, the site under it, then the facts// down the left on dotted rules — the username, a masked number under its// label, the card's other facts — and when it was last used by hand at the// foot under its label; the old brass key stands along the right, and an// ID's own picture (its scan) is mounted under the heading instead. The// secret itself is never on the card: copying it is an act of the fan.// Every word is the core's (`DeckCard`).import CottonPaperimport CottonValuesimport SwiftUIenum SecretFaceKnobs {    static let inset = EdgeInsets(top: 28, leading: 26, bottom: 22, trailing: 20)    static let nameSize: CGFloat = 28    /// The most a long name shrinks before it takes its second line.    static let nameShrink: CGFloat = 0.7    static let siteSize: CGFloat = 16    static let siteGap: CGFloat = 8    static let valueSize: CGFloat = 18    static let maskSize: CGFloat = 16    static let maskTracking: CGFloat = 1.5    static let factsTop: CGFloat = 20    static let factGap: CGFloat = 10    static let dateSize: CGFloat = 17    static let dateRuleGap: CGFloat = 4    /// The last-used rule's length, as a share of the card's width.    static let dateShare: CGFloat = 0.5    /// The words' column beside the key, as a share of the card's width.    static let wordsShare: CGFloat = 0.54    /// The name's width over the key's ring, as a share of the card's width.    static let titleShare: CGFloat = 0.62    static let artShare: CGFloat = 0.4    /// Where the key starts down the card, as a share of its height.    static let artTop: CGFloat = 0.17    static let pictureShare: CGFloat = 0.36    /// The vault's own line at the foot when this device cannot open it.    static let sealedSize: CGFloat = 13}public struct SecretFace<Picture: View>: View {    let card: DeckCard    /// An ID's scan filling its frame, when the record keeps one.    var picture: Picture?    public init(card: DeckCard, picture: Picture? = nil) {        self.card = card        self.picture = picture    }    public var body: some View {        GeometryReader { sheet in            let words = picture == nil ? sheet.size.width * SecretFaceKnobs.wordsShare : .infinity            ZStack(alignment: .topLeading) {                if picture == nil {                    FaceArt(name: "face-art-key")                        .frame(width: sheet.size.width * SecretFaceKnobs.artShare)                        .frame(maxWidth: .infinity, alignment: .trailing)                        .padding(.top, sheet.size.height * SecretFaceKnobs.artTop)                        .padding(.trailing, SecretFaceKnobs.inset.trailing)                }                VStack(alignment: .leading, spacing: 0) {                    if let kind = card.text("state") {                        FaceKind(words: kind)                            .padding(.bottom, 8)                            .facePart(.state)                    }                    if let name = card.text("title") {                        HeavyType(words: name, size: SecretFaceKnobs.nameSize)                            .lineLimit(2)                            .minimumScaleFactor(SecretFaceKnobs.nameShrink)                            .frame(maxWidth: picture == nil ? sheet.size.width * SecretFaceKnobs.titleShare : .infinity, alignment: .leading)                            .accessibilityIdentifier("deck-title")                            .facePart(.title)                    }                    if let site = card.text("subtitle") {                        Text(site)                            .font(CottonFonts.type(SecretFaceKnobs.siteSize))                            .lineLimit(1)                            .minimumScaleFactor(SecretFaceKnobs.nameShrink)                            .faceInk(.ink)                            .frame(maxWidth: words, alignment: .leading)                            .padding(.top, SecretFaceKnobs.siteGap)                            .facePart(.subtitle)                    }                    if let picture {                        MountedPrint(photo: picture)                            .frame(height: sheet.size.height * SecretFaceKnobs.pictureShare)                            .padding(.top, 14)                            .facePart(.picture)                    }                    facts                        .frame(maxWidth: words, alignment: .leading)                        .padding(.top, SecretFaceKnobs.factsTop)                    Spacer(minLength: 8)                    if let used = card.text("date") {                        VStack(alignment: .leading, spacing: SecretFaceKnobs.dateRuleGap) {                            FaceFact(label: card.label("date"), value: used, hand: true, size: SecretFaceKnobs.dateSize)                            FaceHairline()                        }                        .frame(maxWidth: sheet.size.width * SecretFaceKnobs.dateShare, alignment: .leading)                        .facePart(.when)                    }                    // The vault this device cannot open, in its own words:                    // the card names the secret, its value stays shut.                    if let sealed = card.text("sealed") {                        Text(sealed)                            .font(CottonFonts.type(SecretFaceKnobs.sealedSize))                            .lineLimit(3)                            .fixedSize(horizontal: false, vertical: true)                            .faceInk(.pencil)                            .frame(maxWidth: words, alignment: .leading)                            .padding(.top, SecretFaceKnobs.factGap)                            .accessibilityIdentifier("deck-sealed")                    }                }                .padding(SecretFaceKnobs.inset)                if let corner = card.corner {                    RubberStamp(words: corner)                        .facePart(.when)                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)                        .padding(SecretFaceKnobs.inset)                }            }        }        .onCardPaper(SecretFaceCatalogue.stock.laid(card.pace))    }    /// The username, a masked number under its label and the card's other    /// facts, each closed by a dotted rule.    private var facts: some View {        VStack(alignment: .leading, spacing: SecretFaceKnobs.factGap) {            if card.text("body") != nil || card.text("mask") != nil || !StudioReading.facts(card).isEmpty {                DottedRule()            }            if let user = card.text("body") {                FaceFact(value: user, size: SecretFaceKnobs.valueSize, lines: 1)                    .facePart(.body)                DottedRule()            }            if let mask = card.text("mask") {                VStack(alignment: .leading, spacing: FacePartsKnobs.labelGap) {                    if let label = card.label("mask") { FaceLabel(words: label) }                    Text(mask)                        .font(CottonFonts.type(SecretFaceKnobs.maskSize))                        .tracking(SecretFaceKnobs.maskTracking)                        .lineLimit(1)                        .minimumScaleFactor(SecretFaceKnobs.nameShrink)                        .faceInk(.ink)                }                .accessibilityElement(children: .combine)                DottedRule()            }            ForEach(Array(StudioReading.facts(card).enumerated()), id: \.offset) { _, fact in                FaceFact(label: fact.label, value: fact.said, size: SecretFaceKnobs.maskSize, lines: 1)                DottedRule()            }        }    }}/// A rule of dots across the card, between the facts.struct DottedRule: View {    @Environment(\.theme) private var theme    var body: some View {        Line()            .stroke(theme.print(.pencil).opacity(0.5), style: StrokeStyle(lineWidth: 0.9, dash: [1.5, 3]))            .frame(height: 1)            .blendMode(theme.printBlend)            .accessibilityHidden(true)    }    private struct Line: Shape {        func path(in rect: CGRect) -> Path {            var path = Path()            path.move(to: CGPoint(x: rect.minX, y: rect.midY))            path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))            return path        }    }}/// The secret face, by the kind's name, and the cards it is reviewed in. A/// deck draws a secret with no picture: an ID's scan is its own page's.public enum SecretFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "secret", specimens: [Specimen("login", login), Specimen("apiKey", apiKey)]) { card, _, _ in        SecretFace<EmptyView>(card: card, picture: nil)    }    public static let login = DeckCard(face: .secret, fields: [        "title": .text("Example Mail"),        "subtitle": .text("example.com"),        "body": .text("priya@example.com"),    ])    public static let apiKey = DeckCard(face: .secret, fields: [        "state": .text("API key"),        "title": .text("Example Labs"),        "subtitle": .text("console.example.com"),        "body": .text("priya-dev"),    ])}