SPB Git

Repository Public MIT

doc-glass-prompt-bar.md 4.9 KB

# GlassPromptBar

Identifier glass-prompt-bar
Module SPGlassAI
Kind custom-glass — one glassSurface holding a system TextField and plain symbol buttons
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/AI/GlassPromptBarExample.swift

UI only: the bar reports what the user wants through closures. It sends nothing, opens no file and contacts no model.

# Minimal usage

swift
@State private var draft = ""

GlassPromptBar(text: $draft) { prompt in send(prompt) }

# Variants

Variant Use
Default Editor and send button.
isResponding: true The stop button replaces the send button; onStop is called by the button and by Escape.
onAttach: Adds the attachment button ("+"). The host presents its own importer or picker.
attachments: + onRemoveAttachment: Chips above the editor, each with a remove button.
lineLimit: Visible lines before the editor scrolls (default 1…6).

# Parameters

Parameter Type Default Meaning
text Binding<String> — The draft.
placeholder LocalizedStringKey? "Message" Prompt shown while the draft is empty.
isResponding Bool false A response is in progress.
attachments [GlassPromptAttachment] [] Items already attached (id, name, systemImage).
lineLimit ClosedRange<Int> 1...6 Smallest and largest number of visible lines.
onSend @MainActor (String) -> Void — Receives the trimmed text; the bar then clears the draft.
onStop @MainActor () -> Void {} Stop the response.
onAttach (@MainActor () -> Void)? nil The user asked to attach something.
onRemoveAttachment (@MainActor (GlassPromptAttachment) -> Void)? nil The user removed a chip.

# States

  • Empty or blank draft: the send button is disabled; Return does nothing.
  • Ready: the send button takes the theme's primary colour.
  • Responding: stop button; the editor stays editable so that the next prompt can be prepared, but nothing can be sent.
  • Disabled: the standard .disabled(_:) modifier.

# Keyboard

What the code does, verified against the SDK's API surface (onKeyPress(_:phases:action:), KeyPress.modifiers, onSubmit, submitLabel, TextField(_:text:selection:prompt:axis:)):

Key Behaviour Mechanism
Return (hardware keyboard, macOS and iPad) Sends when the draft is not blank and no response is in progress; otherwise consumed without effect. onKeyPress(.return, phases: .down) returns .handled
Shift-Return or Option-Return Inserts a line break at the insertion point (replacing the selection). Same handler; the text and the TextSelection binding are updated
Return on the software keyboard Sends. The key is labelled "Send". TextField(axis: .vertical) submits on Return; onSubmit + submitLabel(.send)
Escape, while responding Stops. keyboardShortcut(.cancelAction) on the stop button

The decisions are pure functions (GlassPromptInput.canSend, returnKeyAction, insertingNewline) covered by tests. The key handling itself was compiled for macOS and iOS but not exercised in a running application during verification.

# Advanced example

swift
GlassPromptBar(
    text: $model.draft,
    placeholder: "Ask about this document",
    isResponding: model.isResponding,
    attachments: model.attachments,
    lineLimit: 1...10
) { prompt in
    model.send(prompt)
} onStop: {
    model.cancel()
} onAttach: {
    isImporterPresented = true
} onRemoveAttachment: { attachment in
    model.remove(attachment.id)
}
.fileImporter(isPresented: $isImporterPresented, allowedContentTypes: [.pdf, .image]) { result in
    model.attach(result)                                   // the host reads the file, not the bar
}
.safeAreaPadding(.horizontal)

# Accessibility

  • The editor is labelled "Message"; the buttons are labelled "Attach", "Send" and "Stop" with tooltips on macOS and 44-point targets on iOS.
  • Chips sit in a container labelled "Attachments"; each remove button reads "Remove ".
  • The text follows Dynamic Type and the bar grows with the number of lines up to the limit.
  • With Reduce Transparency the bar is an opaque themed surface.

# Limits

  • On a software keyboard a line break cannot be typed with Return (it sends); pasting multi-line text works.
  • Return is handled on key-down: with an input method that confirms a composition with Return (for example Japanese or Chinese input), the confirmation key may be taken as "send". Hosts with such users should test this path; it could not be verified here.
  • The bar does not expose the editor's focus. Wrap it and use defaultFocus or place it where the system focuses it.
  • The bar has no knowledge of the content of attachments, their size or their type: validation belongs to the host.
  • Attaching by drag and drop or paste is not handled.
Files main
Menu

Theme