Cotton code
152 lines · 6.5 KB · SwiftRaw
// A list's row of links to other lists (`ListLink`), each its name and how// many it holds, and the few sizes the field lines beside it are drawn with.import SwiftUI/// A row of links to other lists, each its name and how many it holds/// ("Scenes 12  Lines 2"): a list's own list links (`ListLink`), on one line/// that scrolls sideways when they do not fit. Nothing for no link.struct SideLinksRow: View {    @Environment(\.theme) private var theme    let links: [PageSideLink]    /// Each link is identified "<identifier>-<link id>".    let identifier: String    /// The links are still being counted: with none to draw yet, the row    /// keeps one link's height, so what stands under it does not move when    /// they land.    var holding = false    /// Which edges links lie past, read off the scroll view's own geometry.    @State private var edges = SideLinksEdges()    var body: some View {        if !links.isEmpty {            // ONE row: links that do not fit the width scroll sideways on            // their line, never wrap to a second (walk 1, shot 408 —            // "Statements 179" fell under the others).            ScrollViewReader { proxy in                ScrollView(.horizontal, showsIndicators: false) {                    HStack(spacing: FieldLineKnobs.wrapGap) {                        ForEach(links) { link in                            PaperTypedButton(link.words, size: FieldLineKnobs.linkButtonSize, action: link.open)                                .accessibilityIdentifier("\(identifier)-\(link.id)")                                .id(link.id)                        }                    }                }                // The scroll view's own geometry — its offset, its content's                // width and its own — says which edge links lie past; a frame                // measured inside the content by a preference never reached                // the row (the fade did not show, walk 5 Oct).                .modifier(SideLinksEdgesReading(edges: $edges))                .scrollBounceBehavior(.basedOnSize, axes: .horizontal)                // A row with more links than fit SAYS so: the edge they run                // off fades out, so "Drafts 2" past the card's edge is not a                // link nobody knows is there (walk 5 Oct).                .mask(SideLinksFade(more: (edges.before, edges.after)))                // Back on the screen, the row starts at its first link, never                // where it was left scrolled (a stray count at the edge).                .onAppear { if let first = links.first?.id { proxy.scrollTo(first, anchor: .leading) } }            }        } else if holding {            PaperTypedButton(" ", size: FieldLineKnobs.linkButtonSize) {}                .hidden()                .accessibilityHidden(true)        }    }    /// Which edges of the row have links lying past them: `links` is the    /// links' frame in the row's own space (it starts at 0 and moves left as    /// the row is scrolled), `row` the width the row has.    static func more(links: CGRect, row: CGFloat) -> (before: Bool, after: Bool) {        guard row > 0, links.width > row + 1 else { return (false, false) }        return (links.minX < -1, links.maxX > row + 1)    }}/// Which edges of a row of links have links lying past them.struct SideLinksEdges: Equatable {    var before = false    var after = false}/// Reads `SideLinksEdges` off the row's scroll geometry: the links' frame/// starts at minus the offset and is as wide as the content.private struct SideLinksEdgesReading: ViewModifier {    @Binding var edges: SideLinksEdges    func body(content: Content) -> some View {        if #available(iOS 18, macOS 15, *) {            content.onScrollGeometryChange(for: SideLinksEdges.self) { geometry in                let more = SideLinksRow.more(                    links: CGRect(x: -geometry.contentOffset.x, y: 0, width: geometry.contentSize.width, height: 0),                    row: geometry.containerSize.width)                return SideLinksEdges(before: more.before, after: more.after)            } action: { _, now in                edges = now            }        } else {            content        }    }}/// The row's mask: opaque across it, fading to clear over the last stretch/// of an edge that links run past.private struct SideLinksFade: View {    @Environment(\.theme) private var theme    let more: (before: Bool, after: Bool)    private var reach: CGFloat { FieldLineKnobs.sideLinksFadeReach }    var body: some View {        HStack(spacing: 0) {            LinearGradient(colors: [.black.opacity(more.before ? 0 : 1), .black], startPoint: .leading, endPoint: .trailing)                .frame(width: reach)            theme.colors.shadow.color            LinearGradient(colors: [.black, .black.opacity(more.after ? 0 : 1)], startPoint: .leading, endPoint: .trailing)                .frame(width: reach)        }        .animation(.easeOut(duration: 0.15), value: more.before)        .animation(.easeOut(duration: 0.15), value: more.after)    }}/// One link of a row of links: a list by its label and how many records it/// holds ("Scenes 12").struct PageSideLink: Identifiable {    let id: String    let label: String    /// How many records the list holds; nil before it is read. None is not    /// counted: the link reads as its name alone.    var count: Int?    /// The definition's words for the count ("1 recording did not work"),    /// when it has them; else the count stands beside the label.    var units: ColumnUnits? = nil    let open: () -> Void    @MainActor    var words: String {        guard let count, count > 0 else { return label }        return counted ?? CountLines.shared.line("lists", "link.side", n: count, fills: ["label": label]) ?? label    }    /// The count in the definition's own words, when it has them, as the    /// core says it.    @MainActor    var counted: String? {        guard let count, count > 0, let units else { return nil }        return CountLines.shared.units(units, n: count)    }}/// The sizes a field's line (a link's value, a web address), a slip's check/// and a row of links are drawn with: Swift's own, beside their readers — a/// theme sets only its tokens (`scale.tokens`).enum FieldLineKnobs {    static let fieldLabelWidth: CGFloat = 96    static let inlineGap: CGFloat = 12    static let linkButtonSize: CGFloat = 15    static let sideLinksFadeReach: CGFloat = 36    static let wrapGap: CGFloat = 16    static let slipCheckSide: CGFloat = 20}