Cotton code
123 lines · 5.7 KB · SwiftRaw
// The letterboard fan tile's specimen (Cotton Cinemas, Round 19, 6 Nov 2026):// the tile a tool's `fan_tile` names, drawn from the kit's own data — the// Cinemas tool folder the Loom app bundles (`project.yml`), its manifest's// `fan_tile` and the pictures it names — so what Loom shows is what the kit// ships, and a picture changed in the kit is the picture shown here.//// One row of tiles, as a tier deals them: a view (Films, centred, lifted), a// list with its own picture (a fixture cover, cover-fit), a list with none// (the `list` pictogram), a Plus door, an act, a red act (its ink red, the// board black), and the view again at its side. The words are the tile's// own: invented, live type.import CottonFacesimport CottonPaperimport Foundationimport SwiftUI#if canImport(UIKit)    import UIKit#else    import AppKit#endif/// A tool's fan tile read from its kit folder: its stock and each picture it/// names, by its part and id.public struct LetterboardKit {    public let stock: Image?    let named: [String: [String: String]]    let images: [String: Image]    /// The kit folder's tile (`<folder>/manifest.json`'s `fan_tile`, its    /// pictures `<folder>/pictures/<sha256>.png`); nil when it names none.    public static func load(_ folder: URL) -> LetterboardKit? {        guard let data = try? Data(contentsOf: folder.appendingPathComponent("manifest.json")),              let manifest = try? JSONSerialization.jsonObject(with: data) as? [String: Any],              let tile = manifest["fan_tile"] as? [String: Any], tile["type"] as? String == "letterboard"        else { return nil }        func reference(_ value: Any?) -> String? { (value as? [String: Any])?["photo"] as? String }        var named: [String: [String: String]] = [:]        for part in ["pictures", "views", "acts", "doors"] {            named[part] = (tile[part] as? [String: Any] ?? [:]).compactMapValues(reference)        }        var images: [String: Image] = [:]        let references = Set(named.values.flatMap(\.values) + [reference(tile["stock"])].compactMap { $0 })        for photo in references {            let file = folder.appendingPathComponent("pictures/\(photo.dropFirst("photo:".count)).png")            #if canImport(UIKit)                if let image = UIImage(contentsOfFile: file.path) { images[photo] = Image(uiImage: image) }            #else                if let image = NSImage(contentsOf: file) { images[photo] = Image(nsImage: image) }            #endif        }        return LetterboardKit(stock: reference(tile["stock"]).flatMap { images[$0] }, named: named, images: images)    }    /// The pictogram the tile names for `id` in `part` (`views`, `acts`, `doors`, `pictures`).    public func pictogram(_ part: String, _ id: String) -> Image? {        named[part]?[id].flatMap { images[$0] }    }}/// The specimen row's words: invented, as every face's catalogue entry is.enum LetterboardFaceCatalogue {    static let view = "Films"    static let pictured = "Synthetic nights"    static let plain = "Weekend list"    static let door = "Add files"    static let act = "Play from start"    static let away = "Remove"}struct SpecimenLetterboard: View {    @Environment(\.theme) private var theme    let kit: LetterboardKit    let tileWidth: Double?    let growth: CGFloat    /// `width`: one tile's, the worn theme's fan tile when none is given.    /// `growth`: how far the phone's text size grows the words (1, the    /// default size; about 3.4, the largest accessibility size).    public init(kit: LetterboardKit, width: Double? = nil, growth: CGFloat = 1) {        self.kit = kit        self.tileWidth = width        self.growth = growth    }    private var width: Double { tileWidth ?? Double(theme.scale[.fanTileW]) }    /// The Cinemas kit folder the Loom app bundles, read.    @MainActor public static let cinemas: LetterboardKit? = Bundle.main.url(forResource: "cinemas", withExtension: nil).flatMap(LetterboardKit.load)    /// The stock's own shape (474 x 696).    private var height: Double { width * 696 / 474 }    public var body: some View {        let red = theme.print(.red)        HStack(alignment: .top, spacing: width * 0.12) {            tile(LetterboardFaceCatalogue.view, "2", centred: true) { pictogram("views", "films") }            tile(LetterboardFaceCatalogue.pictured, "4") {                if let cover = FixturePictures.image("poster-ferry") {                    cover.resizable().scaledToFill()                        .frame(width: width * PaintedTileKnobs.markSide, height: width * PaintedTileKnobs.markSide)                        .clipShape(RoundedRectangle(cornerRadius: 6))                }            }            tile(LetterboardFaceCatalogue.plain, "0") { pictogram("pictures", "list") }            tile(LetterboardFaceCatalogue.door, nil) { pictogram("doors", "files") }            tile(LetterboardFaceCatalogue.act, nil) { pictogram("acts", "play_from_start") }            tile(LetterboardFaceCatalogue.away, nil, ink: red) { pictogram("acts", "bin", red: red) }            tile(LetterboardFaceCatalogue.view, "2") { pictogram("views", "films") }        }        .padding(width * 0.2)        .background(CinemasMaterial.velvet.colour)    }    private func tile(_ label: String, _ figure: String?, ink: Color = PaintedTileKnobs.cream, centred: Bool = false,                      @ViewBuilder mark: () -> some View) -> some View {        PaintedTile(stock: kit.stock, label: label, figure: figure, ink: ink, centred: centred, width: width, height: height, growth: growth, mark: mark)    }    @ViewBuilder private func pictogram(_ part: String, _ id: String, red: Color? = nil) -> some View {        if let image = kit.pictogram(part, id) { PaintedPictogram(image, red: red) }    }}