Cotton code
260 lines · 12.5 KB · SwiftRaw
// A CONTACT by the DESK TELEPHONE (Cotton Calling's Contacts, 13 Nov 2026):// on the calling desk's oxblood leather (`FaceGround.leatherDesk`) a// person's ivory card lies a hair askew, and the desk telephone stands in// front of it, its corner over the card's, ready to call them.////   THE CARD: ivory card stock, its shadow on the leather; on its left the//   person's photograph pasted in its white border, or their initials typed//   on a blank print where they have none; on its right their NAME printed//   in Caslon, one line or two (`title`), a printed rule, and their first//   number typed under it (`reach`, else the `phone` place), where they//   have one.//   THE TELEPHONE, painted three-quarter from the front right//   (`telephone`, by `scripts/paint-telephone.py`): its HANDSET on the//   cradle is the CALL (`FacePart.call`), and its camera key, the third of//   the four line keys with a camera moulded into it, is the VIDEO CALL//   (`FacePart.videoCall`). The shell routes both to the call; the face//   only draws them and says them.//// Named on Cotton Calling's Contacts view, never on the person table: other// apps draw a person their own way. Stepped by a tap, it cuts. Print on an// object scales with the object, never with Dynamic Type. Every word is the// core's (`DeckCard`) but the parts' own, which say themselves in the cards// set's lines (`faces.call`, `faces.video_call`).import CottonPaperimport CottonValuesimport SwiftUIenum PersonTelephoneFaceKnobs {    // THE TELEPHONE, as painted (`telephone`): its width over its height;    // the handset's bounds as shares of the painting (x, y, width, height,    // y down); the camera key's face as its four corners clockwise from    // the upper left.    static let aspect: CGFloat = 1.2961    static let handset = CGRect(x: 0.3163, y: 0.1156, width: 0.5173, height: 0.3532)    static let cameraKey = PaintedKey(corners: [        CGPoint(x: 0.4047, y: 0.6707), CGPoint(x: 0.4668, y: 0.7), CGPoint(x: 0.4514, y: 0.7355), CGPoint(x: 0.3892, y: 0.7056),    ])    /// The telephone's width as a share of the card's room, and how far    /// right of the room's middle it stands, as a share of its own width.    static let phoneWidth: CGFloat = 0.9    static let phoneShift: CGFloat = 0.04    /// The camera glyph, as a share of its key face's height; the least the    /// key's tap reaches, in points.    static let glyph: CGFloat = 0.66    static let reach: CGFloat = 44    // THE CARD: its width as a share of the room's, its width over its    // height, how far askew it lies; as shares of its width, its margin,    // the print's side and border, the name's size and lines, the number's    // size and the rule's weight.    static let cardWidth: CGFloat = 0.88    static let cardAspect: CGFloat = 1.6    static let cardTurn: Double = -3    static let margin: CGFloat = 0.06    static let print: CGFloat = 0.3    static let border: CGFloat = 0.012    static let nameSize: CGFloat = 0.075    static let nameLines = 2    static let numberSize: CGFloat = 0.052    static let rule: CGFloat = 0.004    /// How much of the card the telephone stands over, as a share of the    /// card's height.    static let overlap: CGFloat = 0.22}/// What a contact's card says.public struct PersonTelephoneWords: Equatable, Sendable {    public let name: String?    public let number: String?    public let initials: String    public init(_ card: DeckCard) {        name = card.text("title").map { $0.split(whereSeparator: \.isWhitespace).joined(separator: " ") }.flatMap { $0.isEmpty ? nil : $0 }        let reach = card.reach.phones.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.first { !$0.isEmpty }        number = reach ?? card.words("phone").first        initials = CallTelephoneSetting.initials(name ?? "")    }}public struct PersonTelephoneFace: View {    let card: DeckCard    let photo: FacePicture?    public init(card: DeckCard, photo: FacePicture?) {        self.card = card        self.photo = photo    }    public var body: some View {        let knobs = PersonTelephoneFaceKnobs.self        GeometryReader { box in            // The card and the telephone stand in one column; the room's            // narrower way sets their size.            let cardW0 = box.size.width * knobs.cardWidth            let phoneW0 = box.size.width * knobs.phoneWidth            let tall = cardW0 / knobs.cardAspect * (1 - knobs.overlap) + phoneW0 / knobs.aspect            let s = min(1, box.size.height * 0.98 / tall)            let card = CGSize(width: cardW0 * s, height: cardW0 * s / knobs.cardAspect)            let phone = CGSize(width: phoneW0 * s, height: phoneW0 * s / knobs.aspect)            let top = (box.size.height - (card.height * (1 - knobs.overlap) + phone.height)) / 2            ZStack(alignment: .topLeading) {                ContactCard(words: PersonTelephoneWords(self.card), photo: photo, size: card)                    .frame(width: card.width, height: card.height)                    .rotationEffect(.degrees(knobs.cardTurn))                    .offset(x: (box.size.width - card.width) / 2, y: top)                CardTelephone(size: phone)                    .frame(width: phone.width, height: phone.height)                    .offset(x: (box.size.width - phone.width) / 2 + phone.width * knobs.phoneShift,                            y: top + card.height * (1 - knobs.overlap))            }            .frame(width: box.size.width, height: box.size.height, alignment: .topLeading)        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("person-telephone")    }}/// The person's ivory card: the photograph, the name and the number.private struct ContactCard: View {    let words: PersonTelephoneWords    let photo: FacePicture?    let size: CGSize    var body: some View {        let knobs = PersonTelephoneFaceKnobs.self        let w = size.width        let side = knobs.print * w        let ink = CallingMaterial.cardInk.colour        ZStack(alignment: .topLeading) {            RoundedRectangle(cornerRadius: w * 0.012)                .fill(CallingMaterial.card.colour)                .shadow(color: .black.opacity(0.45), radius: 5, x: 1.5, y: 4)            HStack(alignment: .center, spacing: knobs.margin * w) {                ZStack {                    CallingMaterial.border.colour                    Group {                        if let photo {                            photo                        } else {                            Text(verbatim: words.initials)                                .font(CottonFonts.caslon(side * 0.36))                                .foregroundStyle(ink.opacity(0.7))                                .frame(maxWidth: .infinity, maxHeight: .infinity)                                .background(CallingMaterial.card.colour)                                .accessibilityHidden(true)                        }                    }                    .frame(width: side - knobs.border * w * 2, height: side * 1.2 - knobs.border * w * 2)                    .clipped()                }                .frame(width: side, height: side * 1.2)                .shadow(color: .black.opacity(0.18), radius: 1, x: 0.5, y: 1)                .accessibilityIdentifier("person-telephone-picture")                .facePart(.picture)                VStack(alignment: .leading, spacing: w * 0.03) {                    if let name = words.name {                        Text(verbatim: name)                            .font(CottonFonts.caslon(knobs.nameSize * w))                            .foregroundStyle(ink)                            .lineLimit(knobs.nameLines)                            .minimumScaleFactor(0.6)                            .fixedSize(horizontal: false, vertical: true)                            .accessibilityIdentifier("person-telephone-name")                            .facePart(.title)                    }                    Rectangle()                        .fill(ink.opacity(0.55))                        .frame(height: max(0.5, knobs.rule * w))                        .accessibilityHidden(true)                    if let number = words.number {                        Text(verbatim: number)                            .font(CottonFonts.type(knobs.numberSize * w))                            .foregroundStyle(ink.opacity(0.85))                            .lineLimit(1)                            .minimumScaleFactor(0.6)                            .accessibilityIdentifier("person-telephone-number")                    }                }                .blendMode(.multiply)            }            .padding(.horizontal, knobs.margin * w)            .padding(.top, knobs.margin * w)            .padding(.bottom, knobs.overlap * size.height)            .frame(width: size.width, height: size.height, alignment: .topLeading)        }    }}/// The desk telephone in front of the card: its handset the call, its/// camera key the video call.private struct CardTelephone: View {    let size: CGSize    var body: some View {        let knobs = PersonTelephoneFaceKnobs.self        let hand = knobs.handset        let key = knobs.cameraKey.outline(in: size).boundingRect        let reach = CGSize(width: max(key.width, knobs.reach), height: max(key.height, knobs.reach))        ZStack(alignment: .topLeading) {            if let painted = CottonMedia.image("telephone", scale: 3) {                painted.resizable().interpolation(.high)                    .frame(width: size.width, height: size.height)                    .accessibilityHidden(true)            } else {                RoundedRectangle(cornerRadius: size.width * 0.04)                    .fill(CallingMaterial.cream.colour)                    .frame(width: size.width * 0.5, height: size.height * 0.6)                    .offset(x: size.width * 0.25, y: size.height * 0.25)                    .accessibilityHidden(true)            }            MouldedGlyph(symbol: "video.fill", key: knobs.cameraKey, size: size, share: knobs.glyph,                         ink: CallingMaterial.keyInk.colour.opacity(0.75), multiply: true)            Color.clear                .frame(width: hand.width * size.width, height: hand.height * size.height)                .contentShape(Rectangle())                .accessibilityElement()                .accessibilityLabel(Text(verbatim: ShellLines.line("cards", "faces.call")))                .accessibilityIdentifier("person-telephone-call")                .facePart(.call)                .offset(x: hand.minX * size.width, y: hand.minY * size.height)            Color.clear                .frame(width: reach.width, height: reach.height)                .contentShape(Rectangle())                .accessibilityElement()                .accessibilityLabel(Text(verbatim: ShellLines.line("cards", "faces.video_call")))                .accessibilityIdentifier("person-telephone-video")                .facePart(.videoCall)                .offset(x: key.midX - reach.width / 2, y: key.midY - reach.height / 2)        }        .frame(width: size.width, height: size.height, alignment: .topLeading)    }}/// The contact by the telephone, by its id, and the people it is reviewed/// in: invented people and impossible numbers only.public enum PersonTelephoneFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .leatherDesk    public static let face = Face(id: "person.telephone", specimens: [        Specimen("friend", friend, picture: "rolodex-friend"),        Specimen("colleague", colleague, picture: "rolodex-ananya"),        Specimen("no-picture", noPicture),        Specimen("long-name", longName, picture: "portrait"),        Specimen("no-number", noNumber),    ], steps: .cut) { card, picture, _ in        PersonTelephoneFace(card: card, photo: picture)    }    static func person(_ name: String, phones: [String]) -> DeckCard {        var card = DeckCard(face: "person.telephone", corner: nil, pressing: false, pace: nil, plate: nil, fields: ["title": .text(name)])        card.reach = CardReach(phones: phones, call: !phones.isEmpty, message: !phones.isEmpty)        return card    }    public static let friend = person("Kabir Menon", phones: ["+1 555 0122"])    public static let colleague = person("Aditi Sen", phones: ["+1 555 0121", "+1 555 0131"])    public static let noPicture = person("Devika Kapur", phones: ["+1 555 0125"])    public static let longName = person("Arundhati Venkatalakshmi Krishnamurthy", phones: ["+1 555 0124"])    public static let noNumber = person("Ravi Iyer", phones: [])}