Cotton code
91 lines · 3.7 KB · SwiftRaw
// The typed-sigil completion strip — the vocabulary answering inline, while the// thought is still being typed.//// The grammar it renders is `SigilTokenizer.swift`'s; this file draws it and// nothing more.//// Spec 2026-08-29 §D4: `#` completes PROJECT names, `@` completes people. The// strip does not care which — it renders what the dock hands it, wearing the// sigil that asked.//// It sits ABOVE the pill, where the plan chips sit, for the same reason they do:// what the capture is about to become belongs beside the words that made it,// not in a sheet that covers them.//// Every suggestion is the app's ONE `Chip` — `#name` / `@name` in ink, never an// emoji. **The LEFTMOST is the top match: the one a space or an enter commits// without a tap, so reading order and keyboard order are the same order.**//// NOTHING AT ALL when there is nothing to offer. Not an empty strip, not a// reserved height: a bare `#` is not a question, and a strip that held its space// would make the composer jump every time somebody typed one.//// `keyboardShouldPersistTaps="always"` becomes `.scrollDismissesKeyboard(.never)`// — the same requirement said in each toolkit's own words. A tap on a chip must// land on the CHIP; a keyboard that closes first eats the first tap and the// writer has to aim twice.import SwiftUI/// Which chip form a sigil wears. A total switch rather than a raw-value hop:/// the two enums are declared in different files for different reasons, and a/// third sigil would be a compile error here rather than a chip with no prefix.func chipPrefix(for sigil: Sigil) -> Chip.Prefix {    switch sigil {    case .hash: return .project    case .at: return .person    }}public struct SigilSuggestions: View {    private let sigil: Sigil    private let matches: [SigilCandidate]    private let onPick: (SigilCandidate) -> Void    /// The accessibility-identifier stem, so the strip's identifiers are the    /// dock's own rather than a second spelling of them — the dock's stem with    /// `-sigil` appended, which for the one host that exists is this default.    private let identifier: String    @Environment(\.cottonHaptics) private var haptics    /// - Parameters:    ///   - sigil: which grammar these completions answer — the chip wears it.    ///   - matches: already matched and ordered by `matchCandidates`. This view    ///     does no filtering of its own: the order IS the meaning (leftmost is    ///     what space commits), so a second opinion here could disagree with the    ///     keyboard.    public init(        sigil: Sigil,        matches: [SigilCandidate],        identifier: String = "dock-sigil",        onPick: @escaping (SigilCandidate) -> Void    ) {        self.sigil = sigil        self.matches = matches        self.identifier = identifier        self.onPick = onPick    }    public var body: some View {        // Nothing at all when empty — see the header.        if !matches.isEmpty {            ScrollView(.horizontal, showsIndicators: false) {                HStack(spacing: 8) {                    ForEach(matches, id: \.id) { candidate in                        Chip(                            label: candidate.name,                            prefix: chipPrefix(for: sigil)                        ) {                            haptics.select()                            onPick(candidate)                        }                        .accessibilityIdentifier("\(identifier)-\(candidate.id)")                    }                }                .padding(.bottom, 8)            }            .scrollDismissesKeyboard(.never)            .accessibilityIdentifier(identifier)        }    }}