Repository Public MIT
1# GlassPromptBar23| | |4|---|---|5| Identifier | `glass-prompt-bar` |6| Module | `SPGlassAI` |7| Kind | `custom-glass` — one `glassSurface` holding a system `TextField` and plain symbol 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 | Editor and 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). |3031## Parameters3233| Parameter | Type | Default | Meaning |34|---|---|---|---|35| `text` | `Binding<String>` | — | The draft. |36| `placeholder` | `LocalizedStringKey?` | "Message" | Prompt shown while the draft is empty. |37| `isResponding` | `Bool` | `false` | A response is in progress. |38| `attachments` | `[GlassPromptAttachment]` | `[]` | Items already attached (`id`, `name`, `systemImage`). |39| `lineLimit` | `ClosedRange<Int>` | `1...6` | Smallest and largest number of visible lines. |40| `onSend` | `@MainActor (String) -> Void` | — | Receives the trimmed text; the bar then clears the draft. |41| `onStop` | `@MainActor () -> Void` | `{}` | Stop the response. |42| `onAttach` | `(@MainActor () -> Void)?` | `nil` | The user asked to attach something. |43| `onRemoveAttachment` | `(@MainActor (GlassPromptAttachment) -> Void)?` | `nil` | The user removed a chip. |4445## States4647- **Empty or blank draft**: the send button is disabled; Return does nothing.48- **Ready**: the send button takes the theme's `primary` colour.49- **Responding**: stop button; the editor stays editable so that the next prompt can be prepared, but nothing can be sent.50- **Disabled**: the standard `.disabled(_:)` modifier.5152## Keyboard5354What the code does, verified against the SDK's API surface (`onKeyPress(_:phases:action:)`, `KeyPress.modifiers`, `onSubmit`, `submitLabel`, `TextField(_:text:selection:prompt:axis:)`):5556| Key | Behaviour | Mechanism |57|---|---|---|58| 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` |59| 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 |60| Return on the software keyboard | Sends. The key is labelled "Send". | `TextField(axis: .vertical)` submits on Return; `onSubmit` + `submitLabel(.send)` |61| Escape, while responding | Stops. | `keyboardShortcut(.cancelAction)` on the stop button |6263The 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.6465## Advanced example6667```swift68GlassPromptBar(69 text: $model.draft,70 placeholder: "Ask about this document",71 isResponding: model.isResponding,72 attachments: model.attachments,73 lineLimit: 1...1074) { prompt in75 model.send(prompt)76} onStop: {77 model.cancel()78} onAttach: {79 isImporterPresented = true80} onRemoveAttachment: { attachment in81 model.remove(attachment.id)82}83.fileImporter(isPresented: $isImporterPresented, allowedContentTypes: [.pdf, .image]) { result in84 model.attach(result) // the host reads the file, not the bar85}86.safeAreaPadding(.horizontal)87```8889## Accessibility9091- The editor is labelled "Message"; the buttons are labelled "Attach", "Send" and "Stop" with tooltips on macOS and 44-point targets on iOS.92- Chips sit in a container labelled "Attachments"; each remove button reads "Remove <name>".93- The text follows Dynamic Type and the bar grows with the number of lines up to the limit.94- With Reduce Transparency the bar is an opaque themed surface.9596## Limits9798- On a software keyboard a line break cannot be typed with Return (it sends); pasting multi-line text works.99- 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.100- The bar does not expose the editor's focus. Wrap it and use `defaultFocus` or place it where the system focuses it.101- The bar has no knowledge of the content of attachments, their size or their type: validation belongs to the host.102- Attaching by drag and drop or paste is not handled.103