Cotton code
99 lines · 4.1 KB · SwiftRaw
// The small object at the left of a conversation's strip, a call's strip and a// conversation's header: the print when there is a photograph, the typed// initial card when there is not.//// **The picture is a KEY, and bytes never cross the boundary.** `read.threads`,// `read.thread` and `read.callLog` answer the key (`phone/pictures.rs`), so// print-or-card is decided without touching a byte, and only the print that is// on screen asks `media.ensure` for a path, decoded at the edge it is drawn. A// print whose bytes have not reached this phone shows its empty window — a// window, never a grey photograph pretending to load.//// A GROUP is its own photograph, one print. Never a cluster of its members'// faces: that was the owner's correction to picture 10.import CottonKitimport SwiftUI#if canImport(UIKit)import UIKit#endif/// A picture by object key, filling whatever window it is put in — and/// picture-shaped before its bytes arrive: left to itself it is `edge` square,/// and until there is a picture its window holds a quiet picture mark.struct KeyedPicture: View {    @Environment(\.theme) private var theme    let key: String?    /// The edge the picture is drawn at, in points.    let edge: CGFloat    /// Told when the picture turns out not to be on this device (or is, after    /// all), so what holds the window can say so in words under it.    var onAbsent: ((Bool) -> Void)? = nil    /// The picture AND the key it is about — a re-ask over the same key keeps    /// it on screen, so a pop-back into the conversation list does not blink.    @State private var slot = MediaImageSlot()    var body: some View {        ZStack {            Rectangle().fill(theme.printWindow)            if let image = slot.image {                Image(uiImage: image)                    .resizable()                    .scaledToFill()            } else {                Image(systemName: "photo")                    .font(theme.type.interface(max(ChatPictureKnobs.faceChatPictureMarkMinSize, edge * ChatPictureKnobs.faceChatPictureMarkFraction)))                    .foregroundStyle(theme.print(.pencil).opacity(ChatPictureKnobs.faceChatPicturePencilOpacity))            }        }        .frame(idealWidth: edge, idealHeight: edge)        .accessibilityHidden(true)        .mediaSlot($slot, key: key, edge: edge)        .onChange(of: slot.isAbsent, initial: true) { _, absent in onAbsent?(absent) }    }}/// A conversation or a caller, as the object at the left of their strip.struct ChatFace: View {    @Environment(\.theme) private var theme    let seed: String    let name: String    let pictureKey: String?    /// nil: the worn theme's strip lead (`paper_strip_leading_side`).    var width: CGFloat? = nil    private var side: CGFloat { width ?? SlipFaceKnobs.paperStripLeadingSide }    var body: some View {        if let pictureKey, !pictureKey.isEmpty {            InstantPrint(seed: seed, size: .thumb) {                KeyedPicture(key: pictureKey, edge: side)            }            .frame(width: side)        } else {            TypedCard(name: ChatFace.initial(name), seed: seed, monogram: true)                .frame(width: side * ChatPictureKnobs.faceChatPictureCardFraction)        }    }    /// The one letter a typed card carries: the name's first letter, or a    /// dash for a conversation nobody named. Numbers and symbols are kept as    /// they are — a withheld caller is "+" rather than an invented letter.    static func initial(_ name: String) -> String {        guard let first = name.trimmingCharacters(in: .whitespacesAndNewlines).first else {            return "\u{2013}"        }        return String(first).uppercased()    }}/// The sizes, counts and timings ChatPicture draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum ChatPictureKnobs {    static let faceChatPictureCardFraction: CGFloat = 0.92    static let faceChatPictureMarkFraction: CGFloat = 0.2    static let faceChatPictureMarkMinSize: CGFloat = 9    static let faceChatPicturePencilOpacity: CGFloat = 0.35}