Cotton code
217 lines · 8.9 KB · SwiftRaw
// How the shell takes a face's parts (CottonFaces' `FacePart`, the tap// map): a part names a card place, and while the card is held the core says// what a tap on that place opens (`shell.opened`'s `parts`,// `DeckEditor.part`) — a date the days fan (`deckCorner`, ringed;// `deckWhen`), the status word the statuses (`deckStatusWord`), the group the// groups, words the short-text input or the full-screen editor// (`deckWords`); a picture opens the one viewer — or, where the core says it// chooses a column (an event's kind), turns the fan into that column's// values — and a person's discs their apps. The done box and a disc are// toggles the face keeps. Built for each card the deck draws// (`DeckFace.view`); where no editor stands each part draws exactly as it// was. A part whose editor a tap opens is the card's selected part while// that editor stands (`DeckEditor.selected(on:)`).//// ONLY WHAT THE SHELL MAY WRITE, AS THE CORE SAYS. A place the core leaves// out of the held card's parts is drawn as it is and its tap is the card's// own: a read-only or computed column, a record closed to edits, a type no// writer writes. A thread's last line is its newest message, no column of// the thread's to write: its tap opens the chat (walk 332).import CottonFacesimport CottonValuesimport SwiftUIenum DeckFaceParts {    /// The card place a part shows.    static func slot(of part: FacePart) -> DefinitionCardSlot? {        switch part {        case .title: .title        case .subtitle: .subtitle        case .body: .body        case .quote: .quote        case .note: .note        case .corner, .when: .date        case .state: .state        case .done: .check        case .picture: .photo        case .amount: .amount        case .place: .place        case .people: .people        case .group: .group        case .stars: .count        case .seal: .seal        case .weather: .weather        case .call, .videoCall, .message, .email, .play, .scrub, .next, .previous: nil        }    }    /// The core's word for a part's place in the held card's tap map.    static func place(of part: FacePart) -> String? {        switch part {        case .corner, .when: "date"        default: slot(of: part)?.rawValue        }    }    /// The parts' taps on one card: `open` is what its picture's tap does    /// (the deck's viewer) where the core's tap map says `viewer`, nil where    /// the card stands outside a deck that opens it; `call`, `message` and `email` open its person's apps.    @MainActor static func taps(open: (() -> Void)?, call: (() -> Void)? = nil, videoCall: (() -> Void)? = nil,                     message: (() -> Void)? = nil,                     email: (() -> Void)? = nil, next: (() -> Void)? = nil, previous: (() -> Void)? = nil,                     play: (() -> Void)? = nil) -> FacePartTaps {        let acts = Acts(open: open, call: call, videoCall: videoCall, message: message, email: email, next: next, previous: previous, play: play)        return { part, content in            switch part {            case .corner, .when, .title, .subtitle, .body, .quote, .note, .amount, .place, .people, .stars:                AnyView(content.modifier(DeckColumnPart(part: part)))            case .state:                AnyView(content.deckStatusWord().deckSelects(part))            case .group:                AnyView(content.deckGroups().deckSelects(part))            case .picture:                AnyView(content.modifier(DeckPicturePart(open: acts.open)))            case .seal, .weather:                AnyView(content.modifier(DeckChoicePart(part: part)))            case .call:                tapped(content, acts.call)            case .videoCall:                tapped(content, acts.videoCall)            case .message:                tapped(content, acts.message)            case .email:                tapped(content, acts.email)            case .next:                tapped(content, acts.next)            case .previous:                tapped(content, acts.previous)            case .play:                tapped(content, acts.play)            case .done, .scrub:                content            }        }    }    /// A card's acts past its editors, held on the main actor the taps run on.    @MainActor private final class Acts {        let open, call, videoCall, message, email, next, previous, play: (() -> Void)?        init(open: (() -> Void)?, call: (() -> Void)?, videoCall: (() -> Void)?, message: (() -> Void)?, email: (() -> Void)?,             next: (() -> Void)?, previous: (() -> Void)?, play: (() -> Void)?) {            self.play = play            self.open = open            self.call = call            self.videoCall = videoCall            self.message = message            self.email = email            self.next = next            self.previous = previous        }    }    /// `content` doing `action` on a tap; as it is with none.    fileprivate static func tapped(_ content: AnyView, _ action: (() -> Void)?) -> AnyView {        guard let action else { return content }        return AnyView(content.contentShape(Rectangle()).onTapGesture(perform: action).accessibilityAddTraits(.isButton))    }}/// A part that shows a column, routed to the editor the core says a tap on/// its place opens while the card is held (`DeckEditor.part`); else drawn/// as it is, its tap the card's own.private struct DeckColumnPart: ViewModifier {    let part: FacePart    @Environment(\.deckEditor) private var editor    @Environment(\.deckCardTarget) private var card    func body(content: Content) -> some View {        if let card, let place = DeckFaceParts.place(of: part), let routed = editor?.part(place, on: card.target.record) {            switch (part, routed.does) {            case (.corner, "days"): content.deckCorner().deckSelects(part)            case (.when, "days"): content.deckWhen().deckSelects(part)            case (_, "line"), (_, "page"):                if let column = routed.column { content.deckWords(column: column).deckSelects(part) } else { content }            default: content            }        } else {            content        }    }}/// A part drawn by its choice column (a diary's seal, its weather): where the/// core says a tap chooses it (`options`), a tap turns the fan into its/// values and the part is selected while they stand; else drawn as it is,/// its tap the card's own.private struct DeckChoicePart: ViewModifier {    let part: FacePart    @Environment(\.deckEditor) private var editor    @Environment(\.deckCardTarget) private var card    @Environment(\.cottonHaptics) private var haptics    func body(content: Content) -> some View {        if let editor, let card, let place = DeckFaceParts.place(of: part), let routed = editor.part(place, on: card.target.record),           routed.does == "options", let column = routed.column {            content                .contentShape(Rectangle())                .onTapGesture {                    haptics.press()                    editor.openOptions(card.target, column: column, record: card.record)                }                .accessibilityAddTraits(.isButton)                .deckSelects(part)        } else {            content        }    }}/// A card's picture: where the core says it chooses a column (`options`), a/// tap turns the fan into its values and the picture is the selected part/// while they stand; anywhere else its tap is `open`, the viewer.private struct DeckPicturePart: ViewModifier {    let open: (() -> Void)?    @Environment(\.deckEditor) private var editor    @Environment(\.deckCardTarget) private var card    @Environment(\.cottonHaptics) private var haptics    func body(content: Content) -> some View {        if let editor, let card, let routed = editor.part("photo", on: card.target.record), routed.does == "options",           let column = routed.column {            content                .contentShape(Rectangle())                .onTapGesture {                    haptics.press()                    editor.openOptions(card.target, column: column, record: card.record)                }                .accessibilityAddTraits(.isButton)                .deckSelects(.picture)        } else {            DeckFaceParts.tapped(AnyView(content), open)        }    }}extension View {    /// A tap here selects the card's `part` for as long as the editor the    /// same tap opens stands.    fileprivate func deckSelects(_ part: FacePart) -> some View {        modifier(DeckPartSelecting(part: part))    }}private struct DeckPartSelecting: ViewModifier {    let part: FacePart    @Environment(\.deckEditor) private var editor    @Environment(\.deckCardTarget) private var card    func body(content: Content) -> some View {        if let editor, let card {            content.simultaneousGesture(TapGesture().onEnded { editor.select(part, record: card.target.record) })        } else {            content        }    }}