Cotton code
177 lines · 8.8 KB · SwiftRaw
// Scrolling: the scroll view owns the finger, and there is one of it.//// The owner could not reach the bottom of an inner screen without scrolling to// the top first, and sometimes could not get back up. That shape is never// content size — it is a competing gesture or a nested scroll container. The// cause was `RecordRow`'s `DragGesture(minimumDistance: 0)`, which claimed the// touch before the scroller saw movement, so a list scrolled only from its// whitespace (lane I6). The rules that keep it from coming back, held here:////   1. No `DragGesture(minimumDistance: 0)` anywhere a finger might mean to//      scroll. The ones that stay are sequenced AFTER a hold (the drag exists//      only once the finger has rested), or sit on a view that floats OVER the//      page rather than in its scroll view. Each exemption carries its sentence//      and must still trip the sweep, or it is furniture.//   2. No vertical `ScrollView` inside another vertical `ScrollView`. Two//      nested vertical scrollers is two claimants for one drag.//   3. No `.gesture` / `.highPriorityGesture` on a `ScrollView` itself. A//      `.simultaneousGesture` may ride along (the day pager does, sideways//      only); a gesture that takes precedence over the pan is the bug by name.//   4. A scroll column STATES its width — `.cottonScrollColumn()`, spelled once//      in `Chrome/ScrollColumn.swift`. `.frame(maxWidth: .infinity)` is not a//      width but a permission to grow, and a column one sixth of a point wider//      than its scroll view is a scroll view that pans sideways: Home did, in//      TestFlight 145, at some Dynamic Type sizes and not others. The//      measurements are in `Chrome/ScrollColumn.swift`.//   5. A press lets go when the finger TRAVELS. A button cancels on leaving its//      bounds, and a full-width row has no sideways bounds to leave, so a drag//      across a Home row released as a tap and opened it. The travel is read by//      a watcher on the WINDOW that never recognises (`Chrome/Pressable.swift`,//      installed once by the root) — never by a `DragGesture`, however//      simultaneous: that shape was built, measured, and re-broke scrolling//      from a row.//// The rest of the rules are prose in Cotton note "The phone and Mac app — how a screen is built",// "Scrolling". Every fixture here is synthetic.import XCTest@testable import CottonUIfinal class ScrollingSweepTests: XCTestCase {    // MARK: - 1. No zero-distance drag    private static let zeroDistanceDrag = #"DragGesture\(\s*minimumDistance:\s*0\b"#    /// A zero-distance drag that is `.sequenced(before:` a hold is not a claim    /// on the finger: it starts only once the hold has succeeded.    private static let afterAHold = #"sequenced\(\s*before:\s*DragGesture\(\s*minimumDistance:\s*0\b"#    /// Files whose zero-distance drag is NOT sequenced after a hold, with the    /// sentence that earns each one.    private static let floatingOverThePage: [String: String] = [        "Surfaces/IndexRail.swift":            "the A–Z rail floats over an indexed list in an overlay, outside its scroll view; a finger on a letter means that letter from the first touch, as in Contacts",    ]    // MARK: - 2. No vertical scroll view inside another    // MARK: - 3. No precedence gesture on a scroll view    // MARK: - 4. The scroll column's width is the scroll view's    /// `Chrome/ScrollColumn.swift` and nothing else defines the pin, and it    /// defines it as a `Layout` — which is the half of this rule that had to be    /// learnt twice.    ///    /// **`containerRelativeFrame(.horizontal)` STATES THE SAME WIDTH BY ASKING    /// THE CONTAINER FOR IT, AND ON THE MAC THAT CLOSES A CYCLE.** The pin was    /// that modifier from #839 until 2026-09-21. A Mac column hosts its screen    /// so that the scroll view's reported width includes what is laid out in    /// it, so the column asked the scroll view, the scroll view answered with    /// the column plus the 15 points a legacy (space-taking) scroller occupies,    /// and SwiftUI — which resolves a cycle from the PREVIOUS pass — came back    /// 15 points wider every pass and never settled. Instrumented on the built    /// Mac app over `/todos` in an 1831pt window: `1590, 1590, 1590, 1590,    /// 1605, 1620, …` and still climbing past 9600 ten seconds later, 539    /// layout passes, 95 % of a core, the paper marching out of view on both    /// sides. `Boot/HomeSidebar.swift` had already refused the modifier for its    /// own column after it took the window down inside    /// `NSViewUpdateConstraints`; this is the same defect reaching every paper    /// screen at once.    ///    /// A `Layout` is handed the PROPOSAL — inside a vertical scroll view that    /// IS the scroll view's width — so it states the same number with nothing    /// to ask and no cycle to close.    ///    /// `Chrome/ScreenBar.swift` is the one place `containerRelativeFrame`    /// survives, and it is a different rule: it clamps the TITLE to a FRACTION    /// of the bar, narrowed by what the side controls leave    /// (`ScreenBarTitle.width`). Its answer is strictly narrower than its    /// container and it is laid out over the control row rather than inside a    /// scroller, so it cannot feed its own container. Named here so it stays    /// deliberate.    private static let thePin = "CottonUI/Chrome/ScrollColumn.swift"    private static let theBar = "CottonUI/Chrome/ScreenBar.swift"    private static func pinSource() throws -> String {        try SourceSweep.mustRead(            "apps/cotton/Sources/CottonUI/Chrome/ScrollColumn.swift",            ownedBy: "the scroll column's one spelling"        )    }    // MARK: - 5. A press lets go when the finger travels    private static let thePress = "CottonUI/Chrome/Pressable.swift"    private static let theRoot = "CottonUI/Boot/RootView.swift"    private static func pressSource() throws -> String {        try SourceSweep.mustRead(            "apps/cotton/Sources/CottonUI/Chrome/Pressable.swift",            ownedBy: "the phone's row press"        )    }    // MARK: - The matchers, and that they see the shape    /// A `ScrollView` that scrolls vertically: `ScrollView {`, `ScrollView(.vertical…`,    /// `ScrollView(showsIndicators:…`, `ScrollView([.vertical…`. Never `.horizontal`.    private static let verticalScrollView = try! NSRegularExpression(        pattern: #"\bScrollView\s*(\([^)]*\))?\s*\{"#    )    /// Each vertical scroll view's body, from its opening brace to the matching    /// closing one.    static func verticalScrollViewBodies(in code: String) -> [String] {        var bodies: [String] = []        let ns = code as NSString        for match in verticalScrollView.matches(in: code, range: NSRange(location: 0, length: ns.length)) {            let head = ns.substring(with: match.range)            if head.contains(".horizontal") { continue }            let open = match.range.location + match.range.length - 1            guard let close = closingBrace(in: code, openingAt: open) else { continue }            bodies.append(ns.substring(with: NSRange(location: open + 1, length: close - open - 1)))        }        return bodies    }    /// The modifiers chained straight after a vertical scroll view's closing    /// brace: the lines that follow it and begin with a dot.    static func modifierChainsAfterVerticalScrollViews(in code: String) -> [String] {        var chains: [String] = []        let ns = code as NSString        for match in verticalScrollView.matches(in: code, range: NSRange(location: 0, length: ns.length)) {            let head = ns.substring(with: match.range)            if head.contains(".horizontal") { continue }            let open = match.range.location + match.range.length - 1            guard let close = closingBrace(in: code, openingAt: open) else { continue }            let after = ns.substring(from: close + 1)            var chain = ""            for line in after.split(separator: "\n", omittingEmptySubsequences: false).dropFirst() {                let trimmed = line.trimmingCharacters(in: .whitespaces)                guard trimmed.hasPrefix(".") else { break }                chain += trimmed + "\n"            }            chains.append(chain)        }        return chains    }    /// The index of the brace that closes the one at `open`, or nil if the    /// source is unbalanced from there.    private static func closingBrace(in code: String, openingAt open: Int) -> Int? {        let scalars = Array(code.utf16)        var depth = 0        var i = open        while i < scalars.count {            let c = scalars[i]            if c == 0x7B { depth += 1 } else if c == 0x7D {                depth -= 1                if depth == 0 { return i }            }            i += 1        }        return nil    }}