Repository Public MIT
1# GlassPromptBar23| | |4|---|---|5| Identifier | `glass-prompt-bar` |6| Module | `SPGlassAI` |7| Kind | `custom-glass` — one untinted `glassSurface` holding a system `TextField`, an accessory slot and filled round buttons |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/AI/GlassPromptBarExample.swift` |1011UI only: the bar reports what the user wants through closures. It sends nothing, opens no file and contacts no model.1213## Minimal usage1415```swift16@State private var draft = ""1718GlassPromptBar(text: $draft) { prompt in send(prompt) }19```2021## Variants2223| Variant | Use |24|---|---|25| Default | The editor, and under it a row that ends with the send button. |26| `isResponding: true` | The stop button replaces the send button; `onStop` is called by the button and by Escape. |27| `onAttach:` | Adds the attachment button ("+"). The host presents its own importer or picker. |28| `attachments:` + `onRemoveAttachment:` | Chips above the editor, each with a remove button. |29| `lineLimit:` | Visible lines before the editor scrolls (default 1…6). |30| `accessory:` | A trailing view builder: small controls placed in the bottom row between the attach and send buttons, such as a `GlassModelPicker` or a context chip. |3132## Parameters3334| Parameter | Type | Default | Meaning |35|---|---|---|---|36| `text` | `Binding<String>` | — | The draft. |37| `placeholder` | `LocalizedStringKey?` | "Message" | Prompt shown while the draft is empty. |38| `isResponding` | `Bool` | `false` | A response is in progress. |39| `attachments` | `[GlassPromptAttachment]` | `[]` | Items already attached (`id`, `name`, `systemImage`). |40| `lineLimit` | `ClosedRange<Int>` | `1...6` | Smallest and largest number of visible lines. |41| `onSend` | `@MainActor (String) -> Void` | — | Receives the trimmed text; the bar then clears the draft. |42| `onStop` | `@MainActor () -> Void` | `{}` | Stop the response. |43| `onAttach` | `(@MainActor () -> Void)?` | `nil` | The user asked to attach something. |44| `onRemoveAttachment` | `(@MainActor (GlassPromptAttachment) -> Void)?` | `nil` | The user removed a chip. |45| `accessory` | `@ViewBuilder` | none | Controls shown in the bottom row. `GlassPromptBar` is generic over this view; without it the type is `GlassPromptBar<EmptyView>`. |4647## States4849- **Empty or blank draft**: the send button is disabled; Return does nothing.50- **Ready**: the send button takes the theme's `primary` colour.51- **Responding**: stop button; the editor stays editable so that the next prompt can be prepared, but nothing can be sent.52- **Disabled**: the standard `.disabled(_:)` modifier.5354## Keyboard5556What the code does, verified against the SDK's API surface (`onKeyPress(_:phases:action:)`, `KeyPress.modifiers`, `onSubmit`, `submitLabel`, `TextField(_:text:selection:prompt:axis:)`):5758| Key | Behaviour | Mechanism |59|---|---|---|60| 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` |61| 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 |62| Return on the software keyboard | Sends. The key is labelled "Send". | `TextField(axis: .vertical)` submits on Return; `onSubmit` + `submitLabel(.send)` |63| Escape, while responding | Stops. | `keyboardShortcut(.cancelAction)` on the stop button |6465The 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.6667## Advanced example6869```swift70GlassPromptBar(71 text: $model.draft,72 placeholder: "Ask about this document",73 isResponding: model.isResponding,74 attachments: model.attachments,75 lineLimit: 1...1076) { prompt in77 model.send(prompt)78} onStop: {79 model.cancel()80} onAttach: {81 isImporterPresented = true82} onRemoveAttachment: { attachment in83 model.remove(attachment.id)84}85.fileImporter(isPresented: $isImporterPresented, allowedContentTypes: [.pdf, .image]) { result in86 model.attach(result) // the host reads the file, not the bar87}88.safeAreaPadding(.horizontal)89```9091## Accessibility9293- The editor is labelled "Message"; the buttons are labelled "Attach", "Send" and "Stop" with tooltips on macOS and 44-point targets on iOS.94- Chips sit in a container labelled "Attachments"; each remove button reads "Remove <name>".95- The text follows Dynamic Type and the bar grows with the number of lines up to the limit.96- With Reduce Transparency the bar is an opaque themed surface.9798## Limits99100- On a software keyboard a line break cannot be typed with Return (it sends); pasting multi-line text works.101- 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.102- The bar does not expose the editor's focus. Wrap it and use `defaultFocus` or place it where the system focuses it.103- The bar has no knowledge of the content of attachments, their size or their type: validation belongs to the host.104- Attaching by drag and drop or paste is not handled.105