Cotton code
163 lines · 8.2 KB · SwiftRaw
// THE LETTERS READ, SHOWN (owner, 3 Nov 2026: "what is being captured …// Even for the user"; the design session's spec the same day): the search// disc on the fan's top row grows along the row toward the centre into a// pill holding the letters the ink has read, lower case as read, in a// card's detail face, letter-spaced a little. Each fades// in over the ink's own fade, so the stroke seems to become the letter, and// the pill shrinks back to the disc over the same when they clear. It grows// no wider than about half the row (`widest`); past that the earliest// letters slide off its far end. The magnifier stands only in the empty// disc, exactly as before; a tap opens the typed search as it always did.//// A letter the model doubted (the core's `doubtful`, owner, 3 Nov 2026: "the// 3rd letter was not recognized") stands in the pill all the same, in a// lighter ink (`doubtful_ink`), and VoiceOver says it is doubtful: the writer// sees it was read, and a tap takes it back if it is wrong. The letter in// progress stands after them from the lift of its every stroke, lighter// still (`provisional_ink`) and narrowing nothing, until its group is read// (the core's `provisional`, `ink::stream`):// then it is the letter itself, in place (owner, 3 Nov 2026: "a bit of a// lag between writing a letter and that getting filled").//// The letters are in the disc's own ink, the magnifier's, not the ground's// as the design asked: they sit on the disc's cream paper, and on the walk// of 3 Nov 2026 the ground's ink there was cream on cream, nothing to read.//// The pill holding letters is the letters' own control (owner, 3 Nov 2026):// a tap takes the last one back, a hold (`DeckEditableKnobs.hold`) clears// them all; VoiceOver says both as the pill's actions. The plain disc's tap// opens the typed search as it always did.//// One view, placed from one place (`DeckSurface.floatingLine`): the design// moves or restyles it here, the mechanics untouched.import SwiftUIstruct InkReadout: View {    let letters: InkLetters    /// The letter in progress as it reads so far, after the letters.    var provisional: [String] = []    /// The widest the pill grows.    let widest: CGFloat    let search: () -> Void    /// A tap or a hold on the pill holding letters: the last taken back, or    /// all cleared.    let edit: (InkEdit) -> Void    @Environment(\.theme) private var theme    private var knobs: InkKnobs.Values? { InkKnobs.shared.values }    /// The letters' own width, before the pill clips them.    @State private var natural: CGFloat = 0    /// A hold just cleared the letters: the button's own release that ends    /// it does nothing.    @State private var held = false    /// A letter as the pill sets it: the identity it fades in and out by    /// (its place and itself, so a letter taken back and another written in    /// its place fades in rather than standing on as it), and its ink, a    /// share of the disc's: a doubted letter's is lighter.    struct Shown: Equatable {        let id: String        let letter: String        let ink: Double    }    /// The letters as the pill sets them, as read, and after them the    /// letter in progress, provisionally: its identity the one it will have    /// once read, so it turns final in place.    /// Letters come only once the ink's knobs are read (`InkKnobs`): with    /// none there is nothing to set.    static func shown(_ letters: InkLetters, provisional: [String] = [], knobs: InkKnobs.Values?) -> [Shown] {        guard let knobs else { return [] }        let read = letters.letters.enumerated().map {            Shown(id: "\($0.offset)\($0.element)", letter: String($0.element), ink: letters.doubts($0.offset) ? knobs.doubtfulInk : 1)        }        let pending = provisional.joined().enumerated().map {            Shown(id: "\(letters.letters.count + $0.offset)\($0.element)", letter: String($0.element), ink: knobs.provisionalInk)        }        return read + pending    }    /// What VoiceOver says: the search with no letters, else what they    /// narrow by, and which of them the model doubted.    static func label(_ letters: InkLetters) -> String {        guard !letters.isEmpty else { return ShellLines.line("cards", "deck.search") }        let doubted = letters.letters.enumerated().filter { letters.doubts($0.offset) }.map { String($0.element) }        return doubted.isEmpty ? ShellLines.line("cards", "deck.narrowed", ["letters": letters.letters])            : ShellLines.line("cards", "deck.narrowed_doubtful", ["letters": letters.letters, "doubtful": doubted.joined(separator: " ")])    }    /// What a press on the readout does to `letters`: nil, the plain disc's    /// own (the typed search); else a tap takes the last back and a hold    /// clears them.    static func edit(_ letters: String, held: Bool) -> InkEdit? {        guard !letters.isEmpty else { return nil }        return held ? .clear : .backspace    }    var body: some View {        // One button whatever it holds, so the pill grows from the disc and        // shrinks back to it; a hold that cleared swallows its own release.        Button {            if held { held = false; return }            if let edit = Self.edit(letters.letters, held: false) { self.edit(edit) } else { search() }        } label: { pill }        .buttonStyle(MotionButtonStyle())        .simultaneousGesture(LongPressGesture(minimumDuration: DeckEditableKnobs.hold).onEnded { _ in            guard let edit = Self.edit(letters.letters, held: true) else { return }            held = true            self.edit(edit)        })        .accessibilityLabel(Self.label(letters))        // The letter in progress, as letters alone: what the walk reads.        .accessibilityValue(provisional.joined())        .accessibilityActions {            if !letters.isEmpty {                Button(ShellLines.line("cards", "deck.take_back")) { edit(.backspace) }                Button(ShellLines.line("cards", "deck.clear_letters")) { edit(.clear) }            }        }    }    private var pill: some View {            ZStack {                if letters.isEmpty && provisional.isEmpty {                    Image(systemName: "magnifyingglass")                        .font(.system(size: NavCircleKnobs.chromeNavGlyphSize, weight: .regular))                        .foregroundStyle(NavChrome.glyphInk)                        .transition(.opacity)                } else {                    HStack(spacing: 0) {                        ForEach(Self.shown(letters, provisional: provisional, knobs: knobs), id: \.id) { shown in                            Text(shown.letter)                                .font(theme.type.meta(InkReadoutKnobs.letterSize))                                .tracking(InkReadoutKnobs.letterSize * InkReadoutKnobs.tracking)                                .foregroundStyle(NavChrome.glyphInk)                                .opacity(shown.ink)                                .transition(.opacity)                        }                    }                    .fixedSize()                    .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { natural = $0 }                    .frame(width: min(natural, max(0, widest - 2 * InkReadoutKnobs.side)), alignment: .trailing)                    .clipped()                    .padding(.horizontal, InkReadoutKnobs.side)                    .transition(.opacity)                }            }            .frame(minWidth: NavCircleKnobs.chromeNavCircleDiameter)            .frame(height: NavCircleKnobs.chromeNavCircleDiameter)            .background { Capsule().fill(NavChrome.circleFill).softShadow() }            .frame(minWidth: ScreenBarKnobs.chromeNavCircleTarget, minHeight: ScreenBarKnobs.chromeNavCircleTarget)            .contentShape(Rectangle())            .animation(.easeOut(duration: knobs?.inkFadeSeconds ?? 0), value: Self.shown(letters, provisional: provisional, knobs: knobs))    }}/// The sizes the readout draws with.enum InkReadoutKnobs {    static let letterSize: CGFloat = 17    /// The letters' spacing, in ems.    static let tracking: CGFloat = 0.08    /// The room either side inside the pill, once it holds letters.    static let side: CGFloat = 14}