Cotton code
239 lines · 10.2 KB · SwiftRaw
// A record's only cross-cutting membership, as a fan of caps.//// The tag layer was retired at schema v16; a project is the one thing a record// belongs TO.//// THE SHEET IS THE BOARD'S CAPS, LAID OUT. Every project offered is its own// `CapChip` in a wrapping run, in the enamel Home's board wears it in// (`CapColour.assign` over the ACTIVE roster, the todo board's rule), so a// project is found by its colour before its name is read. A cap this record is// in sits at full strength with a red pencil tick drawn over it; the rest are// dimmed a little, there to be picked. The words are the paper voices; the// sheet's material is `cottonSheet`'s.//// THE RECORD-FILING SHEET WRITES NO NOTE. It used to carry a typed line per// chosen cap — "what this record is to the project" — and it earned nothing:// a membership is the fact, and the sentence nobody wrote was a field// everybody had to read past. The store column and the MCP argument are// untouched, so an assistant's note survives; no screen that FILES a record// writes or shows one.//// ONE link note is still written by the app, and it is not prose: the brand// kit gives a document its slot by linking it with the role as the note// (`BrandScreen`, read back by `get_project`'s `brand_assets_of`). That is a// slot name, not a sentence about a membership — the one allowed site, named// in `ProjectsSheetCapTests`.//// The rules, all of which the test names:////   • ONLY ACTIVE projects are offered — an archived project is not a working//     context — BUT an archived one this record is still IN stays listed and//     ticked, with "archived" written under its cap. Hiding it would hide a//     membership that exists.//   • MULTI-SELECT, LIVE MIRROR: each tap links or unlinks at once. There is no//     save button, so there is no state that can be lost by closing the sheet.//   • THE RUN OF CAPS IS ITS OWN VIEW (`ProjectCapRun`), because a screen can//     mount it WITHOUT a sheet: the memory entry lays the same run on the page//     (the owner's feedback of 2026-09-20 — the caps ARE the selection there,//     so a tap files the entry rather than opening the project).//// A CHECKED cap carried an accessory once — the memory entry's per-project pin// — and the generic went with the pin (2026-09-20): pinning is done where the// arrangement is read, on the project's own memory list.import SwiftUIpublic enum ProjectStatus: String, Sendable, CaseIterable { case active, archived }public struct ProjectsSheetOption: Identifiable, Sendable, Equatable {    public let id: String    public let name: String    public let status: ProjectStatus    /// The slug the cap's enamel is assigned by. A caller that does not pass    /// one gets a colour assigned by the id instead — stable in this sheet, but    /// not the enamel Home's board wears, because the board assigns by slug.    public let slug: String?    public init(id: String, name: String, status: ProjectStatus, slug: String? = nil) {        self.id = id        self.name = name        self.status = status        self.slug = slug    }}public struct ProjectsSheetLink: Sendable, Equatable {    public let projectId: String    public init(projectId: String) {        self.projectId = projectId    }}/// Which projects the sheet draws. Pure, so the rule is testable without a/// sheet: active ones, plus any this record is already in.public func offeredProjects(    _ projects: [ProjectsSheetOption],    links: [ProjectsSheetLink]) -> [ProjectsSheetOption] {    let linked = Set(links.map(\.projectId))    return projects.filter { $0.status == .active || linked.contains($0.id) }}/// Each offered project's cap enamel, by option id — `CapColour.assign` over/// the ACTIVE options' slugs, exactly as Home's board and the todo board assign/// it, so the same roster is the same colours here. An archived project is/// outside that roster and gets no enamel (the sheet draws it cream).public func projectCapColours(_ options: [ProjectsSheetOption]) -> [String: CapColour] {    let key: (ProjectsSheetOption) -> String = { $0.slug ?? $0.id }    let assigned = CapColour.assign(options.filter { $0.status == .active }.map(key))    var out: [String: CapColour] = [:]    for option in options where option.status == .active {        out[option.id] = assigned[key(option)]    }    return out}/// What a cap SAYS, both ways round — one sentence, so every mount of the run/// reads the same and a screen-reader user is told the state rather than left/// to infer it from a tick they cannot see.func projectCapVoice(_ name: String, isOn: Bool) -> String {    isOn ? ShellLines.line("projects", "sheet.in_this_project", ["name": "\(name)"]) : ShellLines.line("projects", "sheet.not_in_this_project", ["name": "\(name)"])}/// The run of caps, and nothing around it: every offered project, the ones this/// record is in at full strength with the red pencil tick, the rest dimmed and/// there to be picked. A tap toggles the membership at once — there is no save.////// Its own view because it has two mounts: the sheet below, and the memory/// entry's page, where the run IS the control.struct ProjectCapRun: View {    @Environment(\.theme) private var theme    let projects: [ProjectsSheetOption]    let links: [ProjectsSheetLink]    var spacing: CGFloat = 14    var side: CGFloat = ProjectsSheetMetrics.capSide    let onLink: (String) -> Void    let onUnlink: (String) -> Void    private func isOn(_ id: String) -> Bool { links.contains { $0.projectId == id } }    public var body: some View {        let colours = projectCapColours(projects)        PaperWrap(spacing: spacing) {            ForEach(projects) { project in                cap(project, colour: colours[project.id] ?? .cream)            }        }    }    /// One project's cap: full strength and ticked in red when this record is    /// in it, dimmed a little when it is not.    private func cap(_ project: ProjectsSheetOption, colour: CapColour) -> some View {        let on = isOn(project.id)        return Button {            if on {                onUnlink(project.id)            } else {                onLink(project.id)            }        } label: {            VStack(spacing: 4) {                CapChip(name: project.name, colour: colour, side: side)                    .scaleEffect(on ? 1.04 : 1)                    .opacity(on ? 1 : ProjectsSheetMetrics.offOpacity)                    .overlay(alignment: .bottomTrailing) {                        if on {                            HandTick(theme: theme)                                .stroke(                                    theme.print(.red),                                    style: StrokeStyle(lineWidth: 2.6, lineCap: .round, lineJoin: .round))                                .frame(width: 26, height: 22)                                .offset(x: 6, y: 2)                        }                    }                if project.status == .archived {                    Text(CottonStrings.archived.lowercased()).paperHand(14)                }            }            .contentShape(Rectangle())        }        .buttonStyle(MotionButtonStyle())        .accessibilityElement(children: .ignore)        // A pick is a two-state control, so BOTH states are SAID. `.isSelected`        // alone tells a screen-reader user which caps are on and leaves the        // rest silent — indistinguishable from caps that cannot be picked.        .accessibilityLabel(projectCapVoice(project.name, isOn: on))        .accessibilityAddTraits(on ? [.isButton, .isSelected] : .isButton)        .accessibilityHint(ShellLines.line("projects", "sheet.toggles"))        .accessibilityIdentifier("project-cap-\(project.id)")    }}public struct ProjectsSheetContent: View {    @Environment(\.theme) private var theme    private let projects: [ProjectsSheetOption]    private let links: [ProjectsSheetLink]    private let caption: String    private let onLink: (String) -> Void    private let onUnlink: (String) -> Void    /// The sentence under the heading. It says a record may belong to several    /// projects, because a record may — except on the ONE surface where it may    /// not: a report is about one project or about none, so the report sheet    /// passes its own sentence rather than letting the caps tell a person they    /// can pick two and then taking only the last.    public static var membershipCaption: String {        ShellLines.line("projects", "sheet.a_project_is_a")    }    public init(        projects: [ProjectsSheetOption],        links: [ProjectsSheetLink],        caption: String = ProjectsSheetContent.membershipCaption,        onLink: @escaping (String) -> Void,        onUnlink: @escaping (String) -> Void    ) {        self.projects = projects        self.links = links        self.caption = caption        self.onLink = onLink        self.onUnlink = onUnlink    }    private var offered: [ProjectsSheetOption] { offeredProjects(projects, links: links) }    public var body: some View {        let offered = self.offered        VStack(alignment: .leading, spacing: 0) {            Text(CottonStrings.projects).paperTitle(theme.scale.titleSize + 7)            Text(caption)                .paperMeta(13)                .fixedSize(horizontal: false, vertical: true)                .padding(.top, 4)            if offered.isEmpty {                Text(ShellLines.line("projects", "sheet.no_projects_yet_make"))                    .paperBody(15, ink: .pencil)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.top, 16)            } else {                ProjectCapRun(                    projects: offered,                    links: links,                    onLink: onLink,                    onUnlink: onUnlink                )                .padding(.top, 18)            }        }        .frame(maxWidth: .infinity, alignment: .leading)    }}/// The fan's sizes, named once.enum ProjectsSheetMetrics {    /// A cap in the run — bigger than a row's chip, so its lettering reads.    static let capSide: CGFloat = 60    /// A cap this record is not in: there, and a step back.    static let offOpacity: Double = 0.62}