Cotton code
193 lines · 8.7 KB · SwiftRaw
// The object faces: a tool's card, a project's cap, a person's polaroid. Each is// the owner's phone's own drawing (`ToolCard`, `ProjectCap`, `InstantPrint`)// fed from a definition and a record instead of a screen's model.import CottonValuesimport SwiftUI/// A tool's card on the board: the photographed stock, the object, the name./// The table's picture names the object (`tool-todos`); the tool's key is that/// name less its prefix, which is also how the card picks its stock.public struct CardFace: View {    private let definition: TableDefinition    private let record: TableRecord?    private let width: CGFloat?    private let now: Date    public init(definition: TableDefinition, record: TableRecord? = nil, width: CGFloat? = nil, now: Date = Date()) {        self.definition = definition        self.record = record        self.width = width        self.now = now    }    /// The tool's key: its tool block's, else "tool-todos" → "todos"; a table    /// with neither is keyed by its plural.    public static func toolKey(_ definition: TableDefinition) -> String {        if let key = definition.tool?.key { return key }        guard case .asset(let picture) = definition.picture, picture.hasPrefix("tool-") else {            return definition.plural.lowercased()        }        return String(picture.dropFirst("tool-".count))    }    static func objectPicture(_ definition: TableDefinition, record: TableRecord?) -> ToolPicture? {        guard let object = record.flatMap({ row in definition.slip.object.flatMap { row[$0].text } }) else {            return definition.picture        }        return .asset(ToolObjects.asset(for: object) ?? object)    }    public var body: some View {        ToolCard(            key: record?.id ?? Self.toolKey(definition),            name: record.map { $0.said?.slip.name ?? "" } ?? definition.plural,            picture: Self.objectPicture(definition, record: record),            stock: record == nil ? definition.tool?.cardStock : nil,            now: now, width: width)    }}/// A project's bottle cap, its enamel from the record's id.public struct CapFace: View {    private let definition: TableDefinition    private let record: TableRecord    private let side: CGFloat?    public init(definition: TableDefinition, record: TableRecord, side: CGFloat? = nil) {        self.definition = definition        self.record = record        self.side = side    }    public var body: some View {        ProjectCap(name: record.said?.slip.name ?? "", colour: FaceTint.cap(for: record.id), side: side)    }}/// A record as a polaroid: the print of its slip's picture, three across in a/// list's grid or on a tile.////// A table of pictures names its words (`polaroid`): the margin caption ("0:42"/// beside a film's play mark, "12 pictures" under an album's cover), the title/// in the serif under the print and a line by hand under that, then the/// slip's byline ("suggested", in red where it says so) and its answers (Keep/// / Not this) — and its print is always drawn, a picture whose bytes are not/// here showing the empty window rather than a card of words.////// Without `polaroid` it is a person's: the print captioned with the first/// name when its bytes are here, the typed card with the whole name otherwise./// Never initials.public struct PolaroidFace: View {    @Environment(\.theme) private var theme    private let definition: TableDefinition    private let record: TableRecord    /// The width the caller named; nil is the worn grid print's.    private let givenWidth: CGFloat?    private var width: CGFloat { givenWidth ?? PrintSize.grid.suggestedWidth(in: theme.scale) }    /// One of several across a list's grid: the object takes the cell's    /// width, `width` only sizing the picture it decodes.    private let fills: Bool    private let onAnswer: ((String) -> Void)?    @Environment(\.linkTitles) private var links    /// - Parameter onAnswer: what a tap on one of the slip's answers does;    ///   nil draws none (a tile).    public init(        definition: TableDefinition, record: TableRecord, width: CGFloat? = nil, fills: Bool = false,        onAnswer: ((String) -> Void)? = nil    ) {        self.definition = definition        self.record = record        self.givenWidth = width        self.fills = fills        self.onAnswer = onAnswer    }    /// A slip that opens a record: on a fan tile's page, the record grows out of it (`TilePage`).    public var body: some View { face.zoomSource() }    @ViewBuilder private var face: some View {        if definition.polaroid != nil {            picture        } else {            person        }    }    /// A table of pictures' polaroid.    private var picture: some View {        let badge = definition.slip.badge.flatMap { record.said?.slip.badge != nil ? $0 : nil }        return VStack(alignment: .leading, spacing: ObjectFacesKnobs.facePolaroidWordsGap) {            InstantPrint(                seed: record.id, caption: definition.polaroidCaption(of: record, links: links.byID),                captionGlyph: badge?.symbol, size: .grid            ) {                RecordPicture(avatarKey: record[definition.slip.photo].text, edge: width)            }            .accessibilityLabel(badge?.spoken ?? record.said?.slip.title ?? definition.name)            if let title = definition.polaroidTitle(of: record, links: links.byID) {                Text(title)                    .paperTitle(theme.scale.titleSize - 2)                    .lineLimit(ObjectFacesKnobs.facePolaroidTitleLineLimit)                    .padding(.top, ObjectFacesKnobs.facePolaroidTitleTopPadding)            }            if let line = definition.polaroidSubtitle(of: record, links: links.byID) {                Text(line).paperHand(theme.scale.handSize, ink: .pencil).lineLimit(1)            }            if let byline = record.said?.slip.byline?.words {                Text(byline)                    .paperHand(theme.scale.handSize, ink: definition.bylineIsRed ? .red : .pencil)                    .lineLimit(1)                    .accessibilityIdentifier("polaroid-\(definition.id)-byline-\(record.id)")            }            if let onAnswer {                AnswerRow(verbs: definition.drawnAnswers(record), identifier: "polaroid-\(definition.id)-\(record.id)") { onAnswer($0.value) }            }        }        .frame(width: fills ? nil : width, alignment: .leading)        .frame(maxWidth: fills ? .infinity : nil, alignment: .leading)    }    /// A person's polaroid.    @ViewBuilder private var person: some View {        let name = record.said?.slip.name ?? ""        // Keep the existing name card's one line under the full name.        let detail = (record.said?.slip.subtitle_one ?? record.said?.slip.subtitle)            ?? FaceText.slipLine(record[definition.slip.trailing], words: record.columnWords(definition.slip.trailing), column: definition.column(definition.slip.trailing), links: links.byID, currency: record[definition.column(definition.slip.trailing)?.currency].text, readsPlaceholder: false)        // A face, always: the photo, else the initials in the print's own        // window, so every card on the People list is a person's print        // rather than a typed name card (walker 604bcce6). The one line        // under it (the organisation) is caption size, never shrunk.        let initials = CardPerson.initials(name)        VStack(alignment: .leading, spacing: 4) {            RecordPrint(                key: record[definition.slip.photo].text, name: initials ?? name, detail: nil,                seed: record.id, caption: definition.polaroidCaption(of: record, links: links.byID), edge: width,                framesInitials: true, monogram: initials != nil            )            if let detail {                // Two lines, so an organisation stands whole at the caption                // size rather than cut mid-word on one ("Panasoni…", walker294).                Text(detail).paperMeta(theme.scale.metaSize)                    .lineLimit(ObjectFacesKnobs.facePolaroidDetailLineLimit)                    .multilineTextAlignment(.center)                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("polaroid-\(definition.id)-detail-\(record.id)")            }        }        .frame(width: fills ? nil : width)        .frame(maxWidth: fills ? .infinity : nil)    }}/// The sizes, counts and timings ObjectFaces draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum ObjectFacesKnobs {    static let facePolaroidTitleLineLimit = 2    static let facePolaroidDetailLineLimit = 2    static let facePolaroidTitleTopPadding: CGFloat = 4    static let facePolaroidWordsGap: CGFloat = 4}