Cotton code
68 lines · 2.7 KB · SwiftRaw
// A record's tint: chosen once from the paper palette by a stable hash of its// id, so the same record is the same paper on every open and every device. A// tint never carries meaning — not the project, not lateness (rebuild note// 7b95efa4, "Slip tints").import CottonValuesimport SwiftUIpublic enum FaceTint {    /// The papers a slip is cut from — the dyes the lists already use, never    /// newsprint (a wash, not a sheet) nor buff (the todo index card's cream).    public static let slipDyes: [PaperDye] = [.cream, .sage, .straw, .peach, .pink, .blue]    public static func dye(for id: String) -> PaperDye {        slipDyes[Int(CapColour.fnv1a(id) % UInt64(slipDyes.count))]    }    public static func dye(for id: String, stock: SlipStock?) -> PaperDye {        switch stock {        case .plain: return .cream        case .rose: return ToolCardTint.rose.paper        case .sage: return ToolCardTint.sage.paper        case nil: return dye(for: id)        }    }    /// A cap's enamel, from the same hash, over every cap colour.    public static func cap(for id: String) -> CapColour {        CapColour.allCases[Int(CapColour.fnv1a(id) % UInt64(CapColour.allCases.count))]    }}/// The titles a link column's target ids read as, gathered by the surface that/// holds the records (a list of todos knows its projects' names). A face asks/// it and writes nothing for an id it does not know.public struct LinkTitles: Sendable {    public var byID: [String: String]    /// The picture each linked record heads its page with — its slip's photo    /// (a person's face) — by id, where it holds one, so a card that names    /// the person prints their photo (architect, r8).    public var pictures: [String: String]    public init(_ byID: [String: String] = [:], pictures: [String: String] = [:]) {        self.byID = byID        self.pictures = pictures    }    /// These titles and pictures under `newer`'s: the newer reading wins.    public func merging(_ newer: LinkTitles) -> LinkTitles {        LinkTitles(byID.merging(newer.byID) { _, read in read }, pictures: pictures.merging(newer.pictures) { _, read in read })    }    /// The picture `record` of `definition` heads its page with, when it has one.    static func picture(of record: TableRecord, in definition: TableDefinition) -> String? {        guard let key = definition.slip.photo, let picture = record[key].text, !picture.isEmpty else { return nil }        return picture    }}private struct LinkTitlesKey: EnvironmentKey {    static let defaultValue = LinkTitles()}extension EnvironmentValues {    public var linkTitles: LinkTitles {        get { self[LinkTitlesKey.self] }        set { self[LinkTitlesKey.self] = newValue }    }}