Cotton code
123 lines · 5.3 KB · SwiftRaw
// THE SHORT-TEXT INPUT (owner, 28 Oct 2026): the one way the shell writes a// text column — a title, a line — of every kind, and a new record's title.// One line of text rising with the keyboard over the dimmed deck, drawn as// the shell's one-line input is (`SearchField`: the paper-coloured capsule,// its hairline inking while it has the keyboard, the typed words in ink). No// sheet, and the card under it is not drawn again. Return commits — the// column written, or the record made; a tap on the dim dismisses, and// nothing is written.import CottonKitimport CottonPaperimport CottonVoiceimport SwiftUIenum DeckLineInputKnobs {    /// Its gap over the keyboard and from the screen's sides.    static let rise: CGFloat = 12    static let side: CGFloat = 16}struct DeckLineInput: View {    let writing: DeckWriting    let commit: (String) -> Void    let dismiss: () -> Void    @Environment(\.theme) private var theme    @Environment(\.cottonCore) private var core    @State private var draft = ""    @State private var selection: TextSelection?    @State private var dictation: VoiceField?    @FocusState private var focused: Bool    var body: some View {        ZStack(alignment: .bottom) {            theme.colors.shadow.color.opacity(Double(theme.scale[.pageDim]))                .ignoresSafeArea()                .contentShape(Rectangle())                .onTapGesture { dismiss() }                .accessibilityLabel(Text(CottonStrings.cancel))                .accessibilityAddTraits(.isButton)                .accessibilityIdentifier("deck-line-dim")            VStack(spacing: 6) {                // The words as they stand while dictating, the tentative ones                // lighter, over the capsule.                if let dictation, dictation.isActive || !dictation.session.offers.isEmpty || dictation.session.isFailed {                    VoiceStrip(session: dictation.session, showsWords: true)                        .padding(.vertical, 8)                        .background(Capsule().fill(SearchGrammar.fill))                        .padding(.horizontal, DeckLineInputKnobs.side)                }                HStack(spacing: 0) {                    TextField("", text: $draft, selection: $selection, prompt: writing.hint.map { Text($0).foregroundStyle(CottonColors.inkTertiary) })                        .font(CottonFonts.type(theme.scale.titleSize))                        .foregroundStyle(CottonColors.ink)                        .tint(theme.print(.red))                        .submitLabel(.done)                        .phoneKeyboard(writing.keyboard == "phone", figures: writing.editor == .amount)                        .focused($focused)                        .onSubmit { submit() }                        .cottonPaperField()                        .accessibilityIdentifier("deck-line-input")                        .onChange(of: draft) { _, now in dictation?.fieldChanged(now) }                    if let dictation {                        // The mic at the trailing end of the capsule on the keyboard.                        VoiceFieldBar(                            field: dictation, identifier: "deck-line-mic", showsStrip: false,                            request: { writing.voicePlan(surface: "deck") },                            text: { draft }, selection: { draft.utf16Range(selection) }, write: { draft = $0 })                            .fixedSize()                    }                }                .padding(.horizontal, DeckLineInputKnobs.side)                .frame(height: SearchPillKnobs.chromeFooterSearchHeight)                .frame(maxWidth: .infinity)                .background(Capsule().fill(SearchGrammar.fill))                .cottonInkFocusRing(focused, in: Capsule())                .softShadow()                .padding(.horizontal, DeckLineInputKnobs.side)                .padding(.bottom, DeckLineInputKnobs.rise)            }        }        .onAppear {            draft = writing.words            focused = true            if dictation == nil, let core { dictation = VoiceField(session: VoiceSessions.make(core: core), inlineTentative: false) }        }        .onDisappear { dictation?.session.cancel() }    }    /// Return while dictating keeps what was heard, then commits.    private func submit() {        guard let dictation, dictation.isActive else { return commit(draft) }        Task {            draft = await dictation.stop()            commit(draft)        }    }}private extension View {    /// A number's keyboard (Send's Add a person): digits, + and a return    /// key to commit on, which the phone pad lacks; the system offers    /// numbers it knows.    /// An amount's figures take the same keyboard, offered no numbers.    @ViewBuilder func phoneKeyboard(_ phone: Bool, figures: Bool = false) -> some View {        #if os(iOS)            if figures {                keyboardType(.numbersAndPunctuation)                    .autocorrectionDisabled()                    .textInputAutocapitalization(.never)            } else if phone {                keyboardType(.numbersAndPunctuation)                    .textContentType(.telephoneNumber)                    .autocorrectionDisabled()                    .textInputAutocapitalization(.never)            } else {                self            }        #else            self        #endif    }}