Cotton code
206 lines · 9.7 KB · SwiftRaw
// Home's head (B8, architect with the owner, 7 Oct 2026): left-aligned as// every title is — the cap at the title's full height, 10 pt before the name;// "only you" / "with Owner" in the handwriting under the name; beneath, ONE// row of the small tags in their colours — and no row at all when no tag has// anything to say (owner feedback d721c070, 11 Oct 2026: an empty gap sat// between the name and the search; it was the row's height held for tags// that were not there). No sync words: the cap's rim turns while it syncs.import CottonKitimport CoreTextimport SwiftUIenum BoardHeadWords {    /// The personal database's people line.    static var onlyYou: String { ShellLines.line("board", "head.only_you") }    static let fallbackName = "Cotton"    static func asked(_ count: Int) -> String { ShellLines.line("board", "head.asked", ["n": "\(count)"]) }    static func done(_ count: Int) -> String { ShellLines.line("board", "head.done", ["n": "\(count)"]) }}struct BoardHead: View {    @Environment(\.theme) private var theme    /// The board's magnifier (`search` part, face `icon`).    @Environment(\.headSearch) private var headSearch    let name: String    let who: String    let asked: Int    let done: Int    /// Where the shown database's sync stands, as the page's ONE sync slot    /// says it ("Syncing · 62% · 41,000 changes to go", "Copying your    /// database · 400 MB to go", "Not synced since 10:46 · mac-c not    /// reachable"); `nil` when it is current and the slot is empty.    var syncLine: String?    /// The share done the thin bar under the line draws, 0 to 1; `nil` draws    /// no bar.    var syncFraction: Double?    /// The line is the refusal no retry changes ("update app"), in red.    var syncRefuses = false    /// Opens This phone, where sync is said in full.    var onSync: () -> Void = {}    /// The cap and the name are ONE button opening the caps sheet (owner,    /// 6 Oct); nil where there is no sheet to open.    let onChange: (() -> Void)?    let onAsked: () -> Void    let onDone: () -> Void    /// Opens Members: the line says who is in the database, and Members is    /// where someone is admitted or removed.    let onWho: () -> Void    /// Home's stack button (Spec 10.2, 10.4): "This database" and "This phone", the two    /// pages that hold what Settings held. Empty draws no circle.    var menu: [PopoverMenuItem] = []    /// The board's head draws the cap (`head.cap`); without it the name alone    /// is the page's title — no cap, no people line, no stack button.    var showsCap = true    /// The tags the board's head lists (`head.tags`): "asked", "done". None    /// draws no tags row at all.    var tags: [String] = ["asked", "done"]    /// The title's size, and the cap sized to it: the title's full height (a    /// 34 pt Fraunces line is ~42 pt, ascender to descender), centred on the    /// capitals, 10 pt before the name (B8).    /// The name in the head row beside its circles, one size down from a    /// screen's old large title (`head_title_size`, fan3 part 7).    private var titleSize: CGFloat { theme.scale[.headTitleSize] }    // The head draws no database symbol (defaults, night plan, 16 Oct    // 2026): the caps' arc under the fan says which database is shown, and    // its last cap opens the Databases screen.    var body: some View {        VStack(alignment: .leading, spacing: 0) {            HStack(alignment: .center, spacing: BoardHeadKnobs.boardHeadNameGap) {                Button { onChange?() } label: {                    HStack(alignment: .firstTextBaseline, spacing: 0) {                        Text(name)                            .font(CottonFonts.serif(titleSize))                            .foregroundStyle(CottonColors.ink)                            .lineLimit(1)                            .multilineTextAlignment(.leading)                            .fixedSize(horizontal: false, vertical: true)                            .frame(maxWidth: .infinity, alignment: .leading)                    }                    .contentShape(Rectangle())                }                .buttonStyle(MotionButtonStyle())                .layoutPriority(1)                .disabled(onChange == nil || !showsCap)                .accessibilityElement(children: .ignore)                .accessibilityLabel(name)                .accessibilityHint(onChange == nil || !showsCap ? "" : ShellLines.line("board", "head.changes_database"))                .accessibilityAddTraits(.isHeader)                .accessibilityIdentifier("board-name")                if let headSearch {                    HeadSearchCircle(action: headSearch)                        .accessibilityIdentifier("bar-search")                }            }            // Under the name, from the name's left edge — not the cap's.            // No line until the shown database says who is in it.            if showsCap, !who.isEmpty {                Button(action: onWho) {                    Text(who)                        .paperHand(theme.scale.handSize + 4)                        .lineLimit(1)                }                .buttonStyle(MotionButtonStyle())                .accessibilityIdentifier("board-who")            }            // THE ONE SYNC SLOT (sync programme, lane A): always one line            // high, so nothing under it shifts when sync has something to say            // and when it stops; empty when the database is current. The thin            // bar under the line is the only bar while the app is usable.            if showsCap {                Button(action: onSync) {                    VStack(alignment: .leading, spacing: BoardHeadKnobs.boardHeadSyncBarGap) {                        Text(syncLine ?? " ")                            .paperHand(theme.scale.handSize + 2, ink: syncRefuses ? .red : .pencil)                            .lineLimit(1)                        SyncBar(fraction: syncFraction)                    }                    .frame(maxWidth: .infinity, alignment: .leading)                    .contentShape(Rectangle())                }                .buttonStyle(MotionButtonStyle())                .disabled(syncLine == nil)                .accessibilityHidden(syncLine == nil)                .accessibilityLabel(syncLine ?? "")                .accessibilityHint(ShellLines.line("board", "head.opens_device"))                .accessibilityIdentifier("board-sync")            }            if Self.drawsTags(tags, asked: asked, done: done) {                tagsRow.padding(.top, BoardHeadKnobs.boardHeadTagsGap)            }        }        .frame(maxWidth: .infinity, alignment: .leading)    }    /// Whether the tags row is drawn: only when a tag the head lists has    /// something to say. With nothing asked and nothing done there is no row    /// and no room kept for one, so the search sits right under the name.    static func drawsTags(_ tags: [String], asked: Int, done: Int) -> Bool {        (tags.contains("asked") && asked > 0) || (tags.contains("done") && done > 0)    }    /// ONE left-aligned row, asked then done; a tag with nothing to say is    /// absent.    private var tagsRow: some View {        HStack(spacing: BoardHeadKnobs.boardHeadTagRowGap) {            if tags.contains("asked"), asked > 0 { slip(BoardHeadWords.asked(asked), dye: .sage, id: "board-asked", action: onAsked) }            if tags.contains("done"), done > 0 { slip(BoardHeadWords.done(done), dye: .buff, id: "board-done", action: onDone) }        }    }    private func slip(_ words: String, dye: PaperDye, id: String, action: @escaping () -> Void) -> some View {        Button(action: action) { face(words, dye: dye, id: id) }            .buttonStyle(MotionButtonStyle())            .accessibilityIdentifier(id)    }    private func face(_ words: String, dye: PaperDye, id: String) -> some View {        Paper(dye: dye, edge: .cut, seed: id, tilt: false, inset: 0) {            Text(words)                .paperBody(theme.scale.bodySize - 1)                .lineLimit(1)                .fixedSize()                .padding(.horizontal, BoardHeadKnobs.boardHeadTagHorizontalPadding)                .padding(.vertical, BoardHeadKnobs.boardHeadTagVerticalPadding)        }        .fixedSize()    }}/// The thin bar under the page's sync line: how far a catch-up or a file copy/// has come. It keeps its height with nothing to draw, so the slot above the/// page's content is the same size in every state.struct SyncBar: View {    /// The share done, 0 to 1; `nil` draws nothing.    let fraction: Double?    @Environment(\.theme) private var theme    var body: some View {        ZStack(alignment: .leading) {            Capsule().fill(theme.print(.pencil).opacity(BoardHeadKnobs.faceBoardHeadSyncFillOpacity))            Capsule()                .fill(theme.print(.pencil))                .frame(width: BoardHeadKnobs.boardHeadSyncBarWidth * min(max(fraction ?? 0, 0), 1))        }        .frame(width: BoardHeadKnobs.boardHeadSyncBarWidth, height: BoardHeadKnobs.boardHeadSyncBarHeight)        .opacity(fraction == nil ? 0 : 1)        .accessibilityHidden(true)    }}/// The sizes, counts and timings BoardHead draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum BoardHeadKnobs {    static let boardHeadNameGap: CGFloat = 8    static let boardHeadSyncBarGap: CGFloat = 2    static let boardHeadSyncBarHeight: CGFloat = 2    static let boardHeadSyncBarWidth: CGFloat = 160    static let boardHeadTagHorizontalPadding: CGFloat = 9    static let boardHeadTagRowGap: CGFloat = 8    static let boardHeadTagVerticalPadding: CGFloat = 5    static let boardHeadTagsGap: CGFloat = 10    static let boardHeadTitleSize: CGFloat = 34    static let faceBoardHeadSyncFillOpacity: CGFloat = 0.25}