Cotton code
143 lines · 6.4 KB · SwiftRaw
// The ground a deck's card lies on (owner, 29 Oct 2026: an object face// stands bare on its tool's own ground; paper stays for paper things). A// face declares its ground beside it (CottonFaces' `FaceGround`, kept by id// in `FaceRegistry.ground`): a paper face lies on the page's own ground, and// the shell keeps paper; a face on a ground of its own — a list's// cassette on the music's tolex — reports that ground to the shell. The// shell paints its tile once across the window, behind the transparent// page and fan, cross-fading as the deck moves between grounds.//// On a dark ground the deck's own words set on the page — its count// ("1 / 3"), the crumbs where a whole is opened — take the light the theme// sets on a dark fill: its cream, as the crossing's word is set on its ink// capsule (`DeckCrossing`), the pencil as thin as the print's. The search// and add discs keep their own light disc on any ground. Nothing changes on// paper. In an app that holds one tool, the deck keeps that tool's ground// with no card up (`DeckGround.laid`).//// An app that stands on a ground of its own (`FlavourManifest.ground`: the// calendar's kitchen tile, the people's amethyst desk; 12 Nov 2026) lays it// wherever its deck would lie on paper: a paper face, a list, an empty// deck. Cotton names none and keeps paper.import CottonFacesimport CottonKitimport CottonPaperimport CottonValuesimport SwiftUIenum DeckGroundKnobs {    /// The cross-fade between two grounds, in seconds.    static let fade: Double = 0.3}enum DeckGround {    /// The ground laid behind `card`: the one its face declares when it is    /// not the page's paper; nil on paper, where nothing is laid.    @MainActor static func of(_ card: DeckCard) -> FaceGround? {        guard let ground = FaceRegistry.ground(DeckFace.faceID(card)), ground != .paper else { return FaceGround.app }        return ground    }    /// The ground laid behind a deck: its front card's (`of`); with no card    /// up, in an app that holds one tool (Cotton Books, Cotton Music: a kit    /// of one, `installed` the tools the database holds), the ground of the    /// face the deck's table draws its cards by, so an empty Books deck lies    /// on the navy felt its books do, never on cream paper (Round 17's fan,    /// 5 Nov 2026). An app of many tools (Cotton, Rigby) keeps paper under an    /// empty deck, as many tools' tables have no one ground.    @MainActor static func laid(front: DeckCard?, table: DefinitionCardFace?, installed: [String]) -> FaceGround? {        if let front { return of(front) }        guard installed.count == 1, let face = table?.face.rawValue,              let ground = FaceRegistry.ground(face), ground != .paper else { return FaceGround.app }        return ground    }    /// An ink the deck sets on its page, on `ground`: the theme's print on    /// paper; on a dark ground of its own the theme's cream, the pencil as    /// much of it as the pencil is of the print; on a light ground of its own    /// (the kitchen tile, the pegboard, the rose linen, the terrazzo) the print's ink whole, the pencil too: paper's greys vanish    /// into a painted ground's grain (12 Nov 2026 walk).    static func ink(_ ink: PaperInk, on ground: FaceGround?, theme: Theme) -> Color {        guard let ground, ground != .paper else { return theme.print(ink) }        // Story's flat ground follows the phone's appearance, and its words with it.        if ground == .blank, ink == .ink || ink == .pencil {            return ink == .ink ? StoryMaterial.ink.colour : StoryMaterial.quiet.colour        }        switch ink {        case .ink: return ground.dark ? theme.sheet(.cream) : theme.print(.ink)        case .pencil: return ground.dark ? theme.sheet(.cream).opacity(theme.colors.pencil) : theme.print(.ink)        case .red, .green: return theme.print(ink)        }    }    /// Whether words beside the card take the ground's own ink    /// (`DeckGround.ink`) rather than the theme's paper inks: on any ground    /// of its own, dark or light.    static func own(_ ground: FaceGround?) -> Bool {        ground.map { $0 != .paper } ?? false    }}extension FaceGround {    /// The ground this app stands on (`FlavourManifest.ground`); nil, the    /// shell's paper (Cotton).    @MainActor static let app: FaceGround? = named(by: FlavourManifest.current)    /// The ground this app launches on: the one it names for the launch    /// alone (`FlavourManifest.launch`), else the one it stands on. The    /// splash lays it, as the launch storyboard before it did.    @MainActor static let launch: FaceGround? = launched(by: FlavourManifest.current)    static func launched(by manifest: FlavourManifest?) -> FaceGround? {        named(manifest?.launch) ?? named(by: manifest)    }    /// The ground `manifest` names. A name that is no ground is a broken    /// build, said out loud rather than drawn on paper.    static func named(by manifest: FlavourManifest?) -> FaceGround? { named(manifest?.ground) }    private static func named(_ id: String?) -> FaceGround? {        guard let id else { return nil }        guard let ground = FaceGround(rawValue: id) else {            fatalError("flavour.json names ground \(id), which is no FaceGround")        }        return ground    }}/// The shell's one ground, behind both page and fan: nothing over the base/// paper; a face's own ground fades as the card in front moves between grounds.struct DeckGroundLaid: View {    let ground: FaceGround?    var body: some View {        ZStack {            if let ground {                FaceGroundSurface(ground)                    .id(ground)                    .transition(.opacity)                    .accessibilityHidden(true)            }        }        .animation(.easeInOut(duration: DeckGroundKnobs.fade), value: ground)        .allowsHitTesting(false)    }}/// A page reports inside its own SwiftUI host; FanCover carries the reading/// out through the environment. An absent sibling is neutral, distinct from/// an unread deck and from an explicit paper ground.enum DeckGroundReading: Equatable, Sendable {    case unspecified    case pending    case known(FaceGround?)}struct DeckGroundPreference: PreferenceKey {    static let defaultValue = DeckGroundReading.unspecified    static func reduce(value: inout DeckGroundReading, nextValue: () -> DeckGroundReading) {        let next = nextValue()        if next != .unspecified { value = next }    }}