Cotton code
145 lines · 6.0 KB · SwiftRaw
// One opened object and its acts. A deck's record opens IN PLACE// (`OpenedInPlace`); the page (`OpenedCardPage`) is what a new record's// blank card stands on, with the same container Settings' Backup card uses.import CottonFacesimport CottonKitimport CottonPaperimport CottonValuesimport SwiftUIstruct OpenedCardContent<Face: View, Acts: View>: View {    @ViewBuilder let face: () -> Face    @ViewBuilder let acts: () -> Acts    var body: some View {        VStack(alignment: .leading, spacing: BackupCardKnobs.gap) {            face()                .environment(\.faceOpened, true)                .frame(maxWidth: .infinity)            acts()        }    }}struct OpenedCardPage<Face: View, Acts: View>: View {    var ground: FaceGround? = nil    let close: () -> Void    @ViewBuilder let face: () -> Face    @ViewBuilder let acts: () -> Acts    var body: some View {        GeometryReader { room in            PaperScreen(faceGround: ground, showsBack: false, onBack: close) {                OpenedCardContent {                    face()                        .aspectRatio(CardPaperKnobs.aspect, contentMode: .fit)                        .frame(maxHeight: max(0, room.size.height - DeckWholeCardKnobs.top))                        .clipped()                } acts: {                    acts()                }            }        }        .simultaneousGesture(MagnifyGesture().onEnded { pinch in            if pinch.magnification < DeckWholeCardKnobs.closes { close() }        })        .environment(\.openedCardGround, ground)        .accessibilityElement(children: .contain)        .accessibilityIdentifier("opened-card")    }}/// AN OPENED CARD STAYS WHERE IT IS (owner, 3 Nov 2026: "Do we need to/// shift the todo out of the box of todos when the actions menu comes up?/// ... The objects are designed in such a way that the action menu won't/// overlap then anyways"; and of every kind: "None of them should change or/// shift on long press. Just the action menu should come up."). A tap that/// opens a record at rest, and every hold, open the card IN PLACE on its/// deck: the same frame, size, ground and neighbours, the face drawn as at/// rest (never its `faceOpened` look). The acts tier fans at the hub/// (`presentOpened`), the face's tagged parts go live (`deckEditable`), and/// the deck holds every move while it stands (each deck's own `opened`)./// Closing — the hub, Close, a pinch in, or the card leaving the screen —/// returns the deck to plain; nothing moved, so nothing animates back. One/// implementation for every deck's card, Home's sync card included.struct OpenedInPlace: ViewModifier {    let opened: Bool    let definition: TableDefinition    let record: TableRecord    let close: () -> Void    /// The face the card wears now: Face on the acts rests on it.    let worn: String    @Environment(\.deckEditor) private var editor    @Environment(\.cottonCore) private var core    @Environment(\.deckViewName) private var view    private var target: DeckEditTarget {        DeckEditTarget(table: definition.id, record: record.id, date: definition.whenColumn)    }    func body(content: Content) -> some View {        content            .deckEditable(definition: definition, record: record, live: opened)            .simultaneousGesture(MagnifyGesture().onEnded { pinch in                if pinch.magnification < DeckWholeCardKnobs.closes { close() }            }, including: opened ? .all : .subviews)            .onChange(of: opened, initial: true) { was, now in                if now {                    // Its tool's acts beside its own, as they stand on this view's deck.                    editor?.presentOpened(target, record: record, worn: worn, view: view, core: core, close: close)                } else if was { dismiss() }            }            // Another card's opening (or a new record's) took the editor:            // this one stands down.            .onChange(of: editor?.openedTarget) { was, now in                if opened, was == target, now != target { close() }            }            .onChange(of: record) { _, record in if opened { editor?.hold(record) } }            .onDisappear { if opened { dismiss(); close() } }    }    /// The editor lets go of this card, unless another has it already.    private func dismiss() {        guard editor?.openedTarget == nil || editor?.openedTarget == target else { return }        editor?.dismissOpened()    }}extension View {    /// The deck's card, opened in place while `opened` (`OpenedInPlace`).    func opensInPlace(_ opened: Bool, definition: TableDefinition, record: TableRecord, worn: String,                      close: @escaping () -> Void) -> some View {        modifier(OpenedInPlace(opened: opened, definition: definition, record: record, close: close, worn: worn))    }}private struct OpenedCardGroundKey: EnvironmentKey {    static let defaultValue: FaceGround? = nil}extension EnvironmentValues {    var openedCardGround: FaceGround? {        get { self[OpenedCardGroundKey.self] }        set { self[OpenedCardGroundKey.self] = newValue }    }}/// The plus card uses the same face and part editors, above the same fan.struct OpenedDraftCard: View {    let draft: DeckDraft    let editor: DeckEditor    var body: some View {        OpenedCardPage(ground: DeckGround.of(draft.card), close: { editor.close() }) {            DeckFace.view(for: draft.card, record: draft.record) {}                .deckEditable(definition: draft.definition, record: draft.record)                .onTapGesture {                    // Its title untyped, a tap asks it again (`shell.input`'s new).                    guard draft.isNew, let target = editor.openedTarget,                          let column = editor.part("title", on: target.record)?.column,                          draft.record[column].text?.isEmpty != false else { return }                    editor.write(target, column: column, store: nil, core: nil)                }        } acts: { EmptyView() }    }}