Cotton code
193 lines · 8.2 KB · SwiftRaw
// THE DECK MOVES BY TAP (owner, 29 Oct 2026): the screen's width in three.// A tap on the LEFT third is the record before, on the RIGHT third the next,// at once — a plain cut, the next card simply there, so records fly by as// fast as they are tapped; the MIDDLE third is the object's own tap (what// the card's tap opens: a whole's parts, a group's deck, a chat, the// viewer). The zones are the screen's, not the card's: the ground beside// and above the card counts as well. A tap that lands on a part the face// tagged (`FacePart`) or a region the shell keeps (`deckKeepsTaps`) never// moves: an editable part opens the record, an act still acts.// Past either end the deck rolls into the neighbouring view, as a swipe// does. VoiceOver reaches the same moves as the card's "Next record" and// "Previous record" actions.import CottonFacesimport CottonValuesimport SwiftUI/// The deck's three zones across the screen.struct DeckZones: Equatable {    /// The screen's span the zones divide, in global points.    var minX: CGFloat    var width: CGFloat    init(minX: CGFloat = 0, width: CGFloat) {        self.minX = minX        self.width = width    }    /// The zones of the page whose global frame is `frame`.    init(_ frame: CGRect) {        self.init(minX: frame.minX, width: frame.width)    }    enum Zone: Equatable {        case previous, own, next    }    func zone(at x: CGFloat) -> Zone {        guard width > 0 else { return .own }        let share = (x - minX) / width        if share < 1.0 / 3 { return .previous }        if share >= 2.0 / 3 { return .next }        return .own    }    /// The step a tap at `point` moves the deck: -1 or 1 on a side third,    /// nil in the middle third or on a region that keeps its own tap.    func step(at point: CGPoint, kept: [CGRect] = []) -> Int? {        guard !kept.contains(where: { $0.contains(point) }) else { return nil }        switch zone(at: point.x) {        case .previous: return -1        case .next: return 1        case .own: return nil        }    }    /// Editable parts open even outside the middle third. Acts and shell    /// controls retain their own tap wherever they stand.    func opens(at point: CGPoint, parts: [CGRect], acts: [CGRect], kept: [CGRect]) -> Bool {        !((acts + kept).contains { $0.contains(point) }) &&            (isOwn(point) || parts.contains { $0.contains(point) })    }    /// Is a tap at `point` the object's own (the middle third)?    func isOwn(_ point: CGPoint) -> Bool {        zone(at: point.x) == .own    }}/// Where a step from `at` among `count` cards lands: another card, or past/// an end, into the neighbouring view (`DeckSurface.roll`).enum DeckStep: Equatable {    case card(Int)    case past(forward: Bool)    static func of(_ step: Int, from at: Int, count: Int) -> DeckStep {        if count > 0, abs(step) > 1 { return .card(min(count - 1, max(0, at + step))) }        let next = at + step        return (0..<count).contains(next) ? .card(next) : .past(forward: step > 0)    }}/// The card's moves for VoiceOver, in the shell's words.enum DeckZoneWords {    static var next: String {        ShellLines.line("record", "side.next", ["name": ShellLines.line("home", "questions.a_record")])    }    static var previous: String {        ShellLines.line("record", "side.previous", ["name": ShellLines.line("home", "questions.a_record")])    }}/// The regions on a card the shell keeps the tap of, beside the face's/// tagged parts: Home's own part cards, whose rows and buttons are theirs.struct DeckKeptTapsKey: PreferenceKey {    static var defaultValue: [Anchor<CGRect>] { [] }    static func reduce(value: inout [Anchor<CGRect>], nextValue: () -> [Anchor<CGRect>]) {        value.append(contentsOf: nextValue())    }}extension View {    /// The front card, moved by the zones: a tap on a side third that lands    /// on no tagged part moves the deck by `move`, one in the middle third    /// is the object's own (`own`).    func deckZoned(_ zones: DeckZones, move: @escaping (Int) -> Void, own: @escaping () -> Void) -> some View {        modifier(DeckZonedCard(zones: zones, move: move, own: own))    }    /// The card's moves for VoiceOver, so the zones are not the only way:    /// "Next record" and "Previous record".    func deckMoveActions(_ move: @escaping (Int) -> Void) -> some View {        accessibilityAction(named: Text(DeckZoneWords.next)) { move(1) }            .accessibilityAction(named: Text(DeckZoneWords.previous)) { move(-1) }    }    /// The ground the card stands on, moved by the zones: a tap on a side    /// third moves the deck, the middle third's is nobody's.    func deckZonedGround(_ zones: DeckZones?, move: @escaping (Int) -> Void) -> some View {        modifier(DeckZonedGround(zones: zones, move: move))    }    /// This region keeps its taps: the deck's zones never move over it.    func deckKeepsTaps() -> some View {        transformAnchorPreference(key: DeckKeptTapsKey.self, value: .bounds) { $0.append($1) }    }}/// The front card under the zones. The side thirds' tap rides with every/// other gesture on the card (`simultaneousGesture`), so a face's own tap on/// its bare page — a todo's or an album's page turn — never holds the deck/// back; it moves only where no tagged part stands, the parts' rectangles/// read off the face's tap map (`FacePartsKey`). The middle third's tap is/// the card's own too, even when a face also turns its page there. Acts/// retain their tap without opening the record.private struct DeckZonedCard: ViewModifier {    let zones: DeckZones    let move: (Int) -> Void    let own: () -> Void    /// The tagged parts on the card and the regions the shell keeps, in    /// global points.    @State private var parts: [CGRect] = []    @State private var shell: [CGRect] = []    @State private var acts: [CGRect] = []    func body(content: Content) -> some View {        content            .overlayPreferenceValue(FacePartsKey.self) { tagged in                GeometryReader { proxy in                    let origin = proxy.frame(in: .global).origin                    let rects = tagged.map { proxy[$0.bounds].offsetBy(dx: origin.x, dy: origin.y) }                    let actions = tagged.filter { [.call, .message, .email, .play, .scrub, .next, .previous].contains($0.part) }                        .map { proxy[$0.bounds].offsetBy(dx: origin.x, dy: origin.y) }                    Color.clear                        .onChange(of: rects, initial: true) { _, now in parts = now }                        .onChange(of: actions, initial: true) { _, now in acts = now }                }                .allowsHitTesting(false)            }            .overlayPreferenceValue(DeckKeptTapsKey.self) { regions in                GeometryReader { proxy in                    let origin = proxy.frame(in: .global).origin                    let rects = regions.map { proxy[$0].offsetBy(dx: origin.x, dy: origin.y) }                    Color.clear.onChange(of: rects, initial: true) { _, now in shell = now }                }                .allowsHitTesting(false)            }            .contentShape(Rectangle())            .simultaneousGesture(SpatialTapGesture(coordinateSpace: .global).onEnded { tap in                if zones.opens(at: tap.location, parts: parts, acts: acts, kept: shell) { own() }                if let step = zones.step(at: tap.location, kept: parts + shell) { move(step) }            })    }}/// The ground around the card under the zones: only taps no card, line or/// button took reach it. No zones (an empty deck, a search) stands the tap/// down on the one view path: swapping the wrapper rebuilt everything under/// it, the ink ground's letter in progress with it, whenever the deck emptied/// or filled ("co" read "c", 8 Nov 2026).private struct DeckZonedGround: ViewModifier {    let zones: DeckZones?    let move: (Int) -> Void    func body(content: Content) -> some View {        content            .background { Color.clear.contentShape(Rectangle()).allowsHitTesting(zones != nil) }            .gesture(SpatialTapGesture(coordinateSpace: .global).onEnded { tap in                if let step = zones?.step(at: tap.location) { move(step) }            }, including: zones == nil ? .subviews : .all)    }}