Cotton code
334 lines · 13.8 KB · SwiftRaw
// Where the page meets Home's fan. The page area runs to the record// button's band and its scroll on under the band to the foot of the screen,// with ONE bottom inset — the band's — whether the fan stands or folds; the// standing fan lies over the page's foot and its rows run under it (291 rows// C and F; architect's ruling 01:30 IST 17 Oct 2026). Folding the fan into// the circle is a scale and an opacity on the arcs and the record button,// never a change of the page's frame or inset: a fold that moved the inset in// its spring laid the list out again in every frame of a fling. The fan's// room in every state is `FanRoom` (`FanEdgeTests`).import CottonFacesimport CottonWritingimport SwiftUI/// The room the fan takes at the foot of Home: the tiles' arc and, under it,/// the caps' arc, over the record button's band (`page`) — the same numbers/// the fan's own frames are laid out from. Where the standing fan covers the/// page, never the page's inset.enum FanRoom {    /// The gap the board's foot column keeps over the record button, under    /// the receipt's slot — there with no receipt shown, so the button    /// stands this far under the fan's foot.    static let recordGap: CGFloat = 8    /// The tiles' frame: none while the fan is put away under the thought    /// field or the keyboard. Collapsed, the arcs keep their frame — they    /// shrink into the circle as drawn, not as laid out.    static func deck(height: Double, tucked: Bool, keyboardUp: Bool) -> Double {        putAway(tucked: tucked, keyboardUp: keyboardUp) ? 0 : height    }    /// The fan is put away — under Home's thought field, a page's own    /// composer, or the keyboard: no tiles, no caps, and the page's scroll    /// leaves its collapse as it was.    static func putAway(tucked: Bool, keyboardUp: Bool) -> Bool { tucked || keyboardUp }    /// The caps' room: their own while the tiles stand, none once the tiles    /// are put away — they go with them.    static func caps(natural: Double, deck: Double, height: Double) -> Double { deck == height ? natural : 0 }    /// The fan is folded into the circle and not put away: the arcs are    /// drawn shrunk into the circle and faded, standing where they stood.    static func folded(collapsed: Bool, tucked: Bool, keyboardUp: Bool) -> Bool { collapsed && !tucked && !keyboardUp }    /// Where the bloomed fan covers the page, in the window — the page    /// area's foot, the fan's top — and nil while the fan is folded or put    /// away: under the folded band rows read as they are, so VoiceOver's    /// swipe to the next row scrolls it in (architect's rulings 14:50 and    /// 21:15 IST 16 Oct 2026).    static func cover(collapsed: Bool, tucked: Bool, keyboardUp: Bool, areaFoot: Double?) -> Double? {        collapsed || putAway(tucked: tucked, keyboardUp: keyboardUp) ? nil : areaFoot    }    /// Where the standing fan's top lies in the window (`FanCover.standingEdge`):    /// the page area's foot, as measured while the fan was out, less the    /// standing fan's room. It reads no fold, tuck or keyboard: the fan    /// hovers over the page, and the card under it never moves.    static func standingEdge(areaFoot: Double?, standingRoom: Double) -> Double? {        areaFoot.map { $0 - standingRoom }    }    /// The fan's room over the band: the ring of views over the tiles, the    /// tiles and the caps, none while the fan is put away. Folded or not it    /// is the same — a fold lays nothing out. The ring's band is room on every    /// tile, whether or not its tool has a ring yet, so the page's picture    /// region never changes as the fan turns.    static func page(height: Double, caps natural: Double, tucked: Bool, keyboardUp: Bool) -> Double {        let deck = deck(height: height, tucked: tucked, keyboardUp: keyboardUp)        return deck + caps(natural: natural, deck: deck, height: height)    }}/// A tile's page as the fan shows it, live or painted (`FanHome.framed`), in/// a page area that runs to the band. A concrete view, so the/// fan's page passes through no `AnyView` of its own (NavigationSweepTests).struct FramedPage: View {    let page: AnyView    /// The bloomed fan's cover (`FanCover`).    var cover: FanCover?    /// The views the ring over the fan chose (`ToolViews`).    var views: ToolViews?    var tiers: FanTiers?    /// Only the live page lays the shell ground; offscreen painters never do.    var laysGround = false    /// UIKit stacks report inside their hosting controller.    var hosted = false    var arrival: FanPageArrival?    var body: some View {        page.modifier(FanGroundReport(enabled: !hosted))            .environment(\.fanPageArrival, arrival)            .environment(\.fanCover, cover)            .environment(\.fanLaysGround, laysGround)            .environment(views)            .environment(tiers)    }}/// Preferences do not cross a UIHostingController. Read the page's ground/// inside its host, then hand it to the shell through the shared environment.struct FanGroundReport: ViewModifier {    var enabled = true    @Environment(\.fanCover) private var cover    @Environment(\.fanLaysGround) private var laysGround    @Environment(\.fanPageArrival) private var arrival    func body(content: Content) -> some View {        content            .backgroundPreferenceValue(DeckGroundPreference.self) { reading in                Color.clear.preference(key: FanPageReadingPreference.self, value: FanPageReading(ground: reading))            }            .overlayPreferenceValue(FanPageReadingPreference.self) { report in                Color.clear.task(id: FanPageReport(reading: report, arrival: arrival?.id, active: enabled && laysGround)) {                    guard enabled && laysGround else { return }                    await Task.yield()                    guard !Task.isCancelled else { return }                    if let arrival { arrival.report(report) }                    else if laysGround { cover?.lay(report.ground) }                }.allowsHitTesting(false)            }    }}/// Where the bloomed fan covers the page, in the window (`FanRoom.cover`):/// one box per fan, handed to its pages once. A fold changes only `edge`,/// which only the rows' own `UnreadUnderTheFan` read — never the page's/// environment, whose change drew every page of the card's stack again in/// the fold's first frame (291 row F).@MainActor @Observablefinal class FanCover {    /// Before any page reports, the app's own ground (`FaceGround.app`), so    /// the first frame after the splash stands on what the splash did.    var ground: FaceGround? = FaceGround.app    /// The colour a page on the plain ground (`FaceGround.blank`) draws its    /// own theme on (Notes' look, from the core), laid through the status    /// bar and under the home indicator so the page meets the window's edges    /// in one colour; nil, the plain ground's own (`deckPlainGround`).    var plainGround: Color?    /// A new deck has not read its first card yet: keep the leaving ground.    /// A known nil is paper, including an empty deck and a non-deck page.    func lay(_ reading: DeckGroundReading) {        switch reading {        case .pending: break        case .known(let ground): self.ground = ground        case .unspecified: ground = nil        }    }    /// Stable room above the record band; folding never relays the page.    var scrollClearance: Double = 0    /// Where the bloomed fan covers the page now (`FanRoom.cover`), nil while    /// it is folded or put away: what a row reads to leave the accessibility    /// tree under the fan. Never a page's layout: the fan hovers.    var edge: Double?    /// Where the standing fan's top lies in the window, whatever the fan is    /// doing now — open, closed into the hub, tucked or under the keyboard    /// (`FanRoom.standingEdge`): the foot a deck stands its card above. The    /// fan hovers over the page, so opening or closing it moves neither    /// this nor the card by a point (owner, 12 Nov 2026).    var standingEdge: Double?    /// The keyboard is up and the fan put away under it (`FanHome`).    var keyboardUp = false}extension EnvironmentValues {    /// The fan's cover; nil off the fan.    @Entry var fanCover: FanCover? = nil    @Entry var fanLaysGround = false}/// A row under the bloomed fan is covered — a tap there lands on the fan —/// so it is neither announced nor tappable: it leaves the accessibility tree/// (walker pass 4 row 3; walker on 289; architect's ruling 21:15 IST 16 Oct/// 2026, form (i)). The fan's state decides: folded or put away there is no/// cover and every row reads as it is. The test is the row's middle against/// the cover, read only when it flips, so a scroll redraws no row. One chain/// whether a cover stands or not: a branch on it gave every row on the page/// a new identity at each fold and bloom, and built them all again mid-fling/// (291 row F).struct UnreadUnderTheFan: ViewModifier {    @Environment(\.fanCover) private var cover    @State private var under = false    static func covered(midY: Double, cover: Double?) -> Bool {        guard let cover else { return false }        return midY > cover    }    func body(content: Content) -> some View {        let edge = cover?.edge        content            .onGeometryChange(for: Bool.self, of: { Self.covered(midY: Double($0.frame(in: .global).midY), cover: edge) }) { under = $0 }            .accessibilityHidden(edge != nil && under)    }}/// The fan's clip: its own frame, and while `hangs` the fan's own height/// again below its foot — the outer caps lean down beside the record button/// whole; only a fan sliding away is cut at its foot.struct FanFootClip: Shape {    var hangs: Bool    func path(in rect: CGRect) -> Path {        Path(hangs ? rect.union(rect.offsetBy(dx: 0, dy: rect.height)) : rect)    }}/// The first page frame is ready only after both its list and its card fill.struct FanPageReading: Equatable, Sendable {    var ground: DeckGroundReading = .unspecified    var filled = true    var ready: Bool { ground != .pending && filled }}struct FanPageReadingPreference: PreferenceKey {    static let defaultValue = FanPageReading()    static func reduce(value: inout FanPageReading, nextValue: () -> FanPageReading) {        let next = nextValue()        if next.ground != .unspecified { value.ground = next.ground }        value.filled = value.filled && next.filled    }}/// A report belongs to the mounted page, including its request generation.private struct FanPageReport: Equatable {    let reading: FanPageReading    let arrival: String?    let active: Bool}struct FanPageArrival {    let id: String    let report: @MainActor (FanPageReading) -> Void}extension EnvironmentValues {    @Entry var fanPageArrival: FanPageArrival? = nil}#if os(iOS)import UIKit/// A tapped destination is mounted under the leaving frame. The fan starts/// only when its first card and ground are ready, then one fade reveals it.@MainActor @Observablefinal class FanPageTurn {    private(set) var key: String?    private(set) var generation = 0    private(set) var ready = false    private(set) var snapshot: UIView?    private(set) var opacity = 1.0    @ObservationIgnored private var start: ((DeckGroundReading) -> Void)?    @ObservationIgnored private var duration = 0.0    @ObservationIgnored private var faded = false    @ObservationIgnored private var settled = false    func prepare(key: String, snapshot: UIView, duration: Double, start: @escaping (DeckGroundReading) -> Void) {        generation += 1        self.key = key        self.snapshot = snapshot        self.duration = duration        self.start = start        ready = false        faded = false        settled = false        opacity = 1    }    func report(_ reading: FanPageReading, key: String, generation: Int) {        guard self.key == key, self.generation == generation, !ready, reading.ready else { return }        ready = true        let start = start        self.start = nil        start?(reading.ground)        withAnimation(.easeInOut(duration: duration)) { opacity = 0 } completion: { [weak self] in            guard let self, self.generation == generation else { return }            self.faded = true            self.finish()        }    }    func didSettle(on key: String) {        guard self.key == key, ready else { return }        settled = true        finish()    }    private func finish() {        guard faded && settled else { return }        key = nil        snapshot = nil    }    func cancel() {        generation += 1        key = nil        snapshot = nil        start = nil        ready = false    }}#endif/// Selecting another tool leaves both page pictures valid. Only a change/// of path on the same tool makes its old picture stale.struct FanPageStand: Equatable {    var key = ""    var path = ""    static func changedPage(from old: Self, to new: Self) -> String? {        old.key == new.key && old.path != new.path ? new.key : nil    }}extension View {    /// Names the colour this page draws its plain ground in (a theme's    /// `#RRGGBB`), for the shell to lay outside the page's host    /// (`FanCover.plainGround`), while it shows.    func deckPlainGround(_ hex: String?) -> some View { modifier(DeckPlainGround(hex: hex)) }}private struct DeckPlainGround: ViewModifier {    let hex: String?    @Environment(\.fanCover) private var cover    func body(content: Content) -> some View {        // `WritingTheme.ui` is UIKit's; a macOS host build (the UI-tests stub) draws no theme ground.        #if canImport(UIKit)        content            .onChange(of: hex, initial: true) { _, now in cover?.plainGround = now.map { Color(uiColor: WritingTheme.ui($0)) } }            .onDisappear { cover?.plainGround = nil }        #else        content        #endif    }}