Cotton code
232 lines · 9.2 KB · SwiftRaw
import CottonKitimport CottonPaperimport SwiftUI#if canImport(UIKit)import UIKit#endif/// A list's fan tile, made from what it holds (5 Nov 2026), for every tool/// that draws lists (Books, Cinemas, Music, a Calendar list): the view tiles'/// own worn cream card with no object on it (`list-tile-ground`), its name/// and count typed beneath in Courier as a view tile's are (`ViewTile`), and/// where a view tile's painted object stands, one of three things:////// - the person's own picture (`record_group.picture`), a print pinned inside///   the card's frame;/// - else a small stack of its first three members' pictures (a book's///   cover, a film's poster), the way a playlist's cover is built from its///   albums — fewer members, fewer in the stack;/// - else three blank sheets laid the same way: the list's default tile,///   never the blank card.////// Every picture is the thumbnail this phone already holds (`read.mediaPath`,/// the 256 px rendition the pictures sweep collects): nothing here asks the/// network. A fan tile is printed once, never live (`FanPrints`), so the arc/// decodes a list's pictures into `ListTilePictureCache` before it prints/// the tile, and prints it again as more land (`FanTierArc`); the tile draws/// what the cache holds. Until the own picture's thumbnail lands the tile/// draws the stack, and until a cover's lands the stack goes without it.struct ListTile: View {    let pictures: ListTilePictures    let label: String    let count: Int?    let width: Double    let height: Double    var growth: CGFloat = 1    /// The catalogue name of the card the tile is laid on.    nonisolated static let ground = "list-tile-ground"    var body: some View {        ZStack(alignment: .top) {            ViewTile(painted: Self.ground, label: label, count: count, width: width, height: height, growth: growth)            #if canImport(UIKit)            ListTileArt(drawn: ListTileArt.drawn(own: pictures.cover.flatMap(ListTilePictureCache.image),                                                 stack: pictures.stack.map(ListTilePictureCache.image)),                        width: width, height: height)            #else            ListTileArt(drawn: .plain, width: width, height: height)            #endif        }        .frame(width: width, height: height)    }}#if canImport(UIKit)/// The lists' pictures this phone has decoded for their tiles, by media key:/// filled by the arc before it prints a list's tile (`load`), read by the/// tile as it is printed. A key not on this phone yet is asked again when/// bytes land.@MainActorenum ListTilePictureCache {    private static var held: [String: UIImage] = [:]    static func image(_ key: String) -> UIImage? { held[key] }    /// For tests and the specimen: a picture in hand under `key`.    static func keep(_ image: UIImage, as key: String) { held[key] = image }    /// Decodes each of `keys` not held yet from this phone (`read.mediaPath`,    /// never the network) at `edge` pixels; whether any came.    static func load(_ keys: [String], core: any CottonCalling, edge: CGFloat) async -> Bool {        var came = false        for key in keys where held[key] == nil {            if case .ready(let image) = await MediaImages.load(from: core, key: key, edge: edge) {                held[key] = image                came = true            }        }        return came    }    /// The keys of `pictures` the cache does not hold.    static func missing(_ pictures: ListTilePictures) -> [String] {        ([pictures.cover].compactMap { $0 } + pictures.stack).filter { held[$0] == nil }    }}#endif/// What a list's tile is made from (`shell.fan`): the person's own picture/// and its first members' pictures, as media keys.struct ListTilePictures: Equatable, Sendable {    var cover: String?    var stack: [String]}/// What stands in the list tile's object place, drawn from pictures in hand.struct ListTileArt: View {    #if canImport(UIKit)    typealias Picture = UIImage    #else    typealias Picture = Never    #endif    enum Drawn {        /// The person's own picture, pinned.        case own(Picture)        /// The first members' pictures, the first on top.        case stack([Picture])        /// No picture in hand: three blank sheets.        case plain        var name: String {            switch self {            case .own: "own"            case .stack(let pictures): "stack \(pictures.count)"            case .plain: "plain"            }        }    }    /// RULE (5 Nov 2026): the person's own picture wins when it is on this phone; else the members' pictures that are, in order, at most three; else the blank sheets — never the blank card.    static func drawn(own: Picture?, stack: [Picture?]) -> Drawn {        if let own { return .own(own) }        let held = Array(stack.compactMap { $0 }.prefix(ListTileKnobs.lay.count))        return held.isEmpty ? .plain : .stack(held)    }    let drawn: Drawn    let width: Double    let height: Double    @Environment(\.theme) private var theme    var body: some View {        ZStack {            switch drawn {            case .own(let picture):                pinned(picture)            case .stack(let pictures):                // Drawn last first, so the list's first member is on top.                ForEach(Array(pictures.enumerated()).reversed(), id: \.offset) { index, picture in                    laid(index) { cover(picture) }                }            case .plain:                ForEach((0..<ListTileKnobs.lay.count).reversed(), id: \.self) { index in                    laid(index) { sheet }                }            }        }        .frame(width: width, height: height * ListTileKnobs.place)        .position(x: width / 2, y: height * ListTileKnobs.centre)        .frame(width: width, height: height)        .allowsHitTesting(false)        .accessibilityHidden(true)    }    /// One of the stack's places: turned and moved as the knobs lay it.    private func laid(_ index: Int, @ViewBuilder _ content: () -> some View) -> some View {        let lay = ListTileKnobs.lay[index]        return content()            .rotationEffect(.degrees(lay.turn))            .offset(x: width * lay.x, y: height * lay.y)    }    private var coverSize: CGSize {        CGSize(width: width * ListTileKnobs.coverWidth, height: width * ListTileKnobs.coverWidth / ListTileKnobs.coverAspect)    }    @ViewBuilder private func cover(_ picture: Picture) -> some View {        #if canImport(UIKit)        Image(uiImage: picture)            .resizable()            .scaledToFill()            .frame(width: coverSize.width, height: coverSize.height)            .clipShape(RoundedRectangle(cornerRadius: 1.5))            .shadow(color: .black.opacity(0.28), radius: 1.5, x: 0.5, y: 1)        #endif    }    /// A blank sheet where a cover would be: the default list tile.    private var sheet: some View {        RoundedRectangle(cornerRadius: 1.5)            .fill(theme.sheet(.cream))            .overlay(RoundedRectangle(cornerRadius: 1.5).strokeBorder(theme.print(.pencil).opacity(0.35), lineWidth: 0.6))            .frame(width: coverSize.width, height: coverSize.height)            .shadow(color: .black.opacity(0.18), radius: 1.2, x: 0.5, y: 1)    }    /// The person's picture as a print: a white border round it, turned a    /// little, a red pin at its head.    @ViewBuilder private func pinned(_ picture: Picture) -> some View {        #if canImport(UIKit)        let side = width * ListTileKnobs.printWidth        let border = side * ListTileKnobs.printBorder        ZStack(alignment: .top) {            Image(uiImage: picture)                .resizable()                .scaledToFill()                .frame(width: side - border * 2, height: side - border * 2)                .clipped()                .padding(border)                .background(PhotosMaterial.border.colour)                .shadow(color: .black.opacity(0.3), radius: 2, x: 0.5, y: 1.5)            Circle()                .fill(theme.print(.red))                .frame(width: side * ListTileKnobs.pin, height: side * ListTileKnobs.pin)                .shadow(color: .black.opacity(0.35), radius: 0.8, x: 0.4, y: 0.8)                .offset(y: -side * ListTileKnobs.pin / 3)        }        .rotationEffect(.degrees(ListTileKnobs.printTurn))        #endif    }}/// Measured against the view tiles (`ViewTileKnobs`): the object place is/// the card's upper half, the words start a little past its middle.enum ListTileKnobs {    /// The object place's centre and height, against the card's height.    static let centre = 0.29    static let place = 0.46    /// A cover's width against the card's, and its paperback's aspect.    static let coverWidth = 0.36    static let coverAspect = 2.0 / 3.0    /// Each place of the stack, the first on top: turned (degrees) and moved    /// (against the card's width and height).    static let lay: [(turn: Double, x: Double, y: Double)] = [(0, 0, 0.012), (-9, -0.2, -0.008), (8, 0.2, -0.004)]    /// The print's side against the card's width, its border against its    /// side, its turn, and its pin against its side.    static let printWidth = 0.6    static let printBorder = 0.06    static let printTurn = -2.5    static let pin = 0.11}