Cotton code
84 lines · 4.1 KB · SwiftRaw
// A scroll column asks its content ONE question a pass — the column's// width and the height it was itself proposed — and places it at that same// proposal.//// Why: the column used to measure its content three ways (width with nothing// proposed, height at the column's width, then size at the width AND the height// just answered). A lazy stack answers each from an estimate built at that// proposal, so the three disagreed, every pass reported a new height, and the// scroll view never settled: the app hung at 100 % of a core on Home at first// launch and after quick flings in Calendar on the large seed (ws-mini-walk2,// 5 Oct 2026; the samples and the measurements are in// `Chrome/ScrollColumn.swift`). The probe below records every proposal the// content is handed during a real layout pass.import SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class ScrollColumnProposalTests: XCTestCase {    private static let width: CGFloat = 300    /// RULE: A scroll column measures and places its content only at a proposal the column itself was given — never an unspecified width, never the height it just answered — so a lazy list inside a scroll view settles instead of hanging the app.    func testTheContentIsMeasuredAndPlacedAtOneProposal() {        for alignment in [Alignment.leading, .center, .trailing] {            let column = ProposalLog()            let content = ProposalLog()            let renderer = ImageRenderer(                content: ProposalProbe(log: column) {                    ProposalProbe(log: content) { Color.clear }.cottonScrollColumn(alignment: alignment)                })            // What a vertical scroll view proposes its content: its width, and            // no height. The renderer adds its own probes (a zero size, then            // the size it settled on); whatever the column is asked, its            // content may be asked that and nothing else.            renderer.proposedSize = ProposedViewSize(width: Self.width, height: nil)            _ = renderer.cgImage            XCTAssertFalse(content.proposals.isEmpty, "the content was never laid out")            let asked = Set(column.proposals.map(Self.words))            let heard = Set(content.proposals.map(Self.words))            XCTAssertTrue(                heard.isSubset(of: asked),                "\(alignment): the column was asked \(asked.sorted()) and asked its content \(heard.sorted()) — a lazy stack answers each new question from a different estimate and the layout never settles")            XCTAssertTrue(asked.contains("\(Self.width)×nil"), "the renderer did not propose what a scroll view proposes")        }    }    private static func words(_ proposal: ProposedViewSize) -> String {        "\(proposal.width.map { "\($0)" } ?? "nil")×\(proposal.height.map { "\($0)" } ?? "nil")"    }    func testTheContentWidthIsAskedOnlyWhenNothingIsProposed() {        var asked = 0        XCTAssertEqual(ScrollColumnLayout.stated(proposal: 320, contentWidth: { asked += 1; return 999 }), 320)        XCTAssertEqual(asked, 0)        XCTAssertEqual(ScrollColumnLayout.stated(proposal: nil, contentWidth: { asked += 1; return 210 }), 210)        XCTAssertEqual(ScrollColumnLayout.stated(proposal: .infinity, contentWidth: { asked += 1; return 210 }), 210)        XCTAssertEqual(asked, 2)    }}private final class ProposalLog {    var proposals: [ProposedViewSize] = []}/// Writes down every proposal it is measured or placed at, and hands its one/// subview (if any) exactly that proposal. With no subview it is a leaf 40pt/// tall.private struct ProposalProbe: Layout {    let log: ProposalLog    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {        log.proposals.append(proposal)        if let only = subviews.first { return only.sizeThatFits(proposal) }        return CGSize(width: proposal.width ?? 120, height: 40)    }    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {        log.proposals.append(proposal)        subviews.first?.place(at: bounds.origin, anchor: .topLeading, proposal: proposal)    }}