Documentation
¶
Overview ¶
Package dialog is a modal overlay — InkUI's "Dialog": a bordered box with a title and message, composited on top of the rest of the screen via layout.Overlay rather than replacing it, dismissed with Enter/Esc.
Example ¶
A new dialog is open. Enter dismisses it and reports that with a DismissedMsg; Show opens it again.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/dialog"
)
func main() {
d := dialog.New("Saved", "Your changes were saved.")
fmt.Println(d.Open())
d, cmd := d.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(d.Open())
fmt.Printf("%T\n", cmd())
d.Show()
fmt.Println(d.Open())
}
Output: true false dialog.DismissedMsg true
Index ¶
- type DismissedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Hide()
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) Open() bool
- func (m Model) Rect() hittest.Rect
- func (m *Model) Release(r focus.Ring) focus.Ring
- func (m Model) Render(base string) string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m *Model) Show()
- func (m Model) Tokens() theme.Tokens
- func (m *Model) Trap(r focus.Ring, items int) focus.Ring
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) WithTokens(tok theme.Tokens) Model
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DismissedMsg ¶
type DismissedMsg struct{}
DismissedMsg is delivered (via the Cmd Update returns) when the dialog is dismissed with Enter or Esc.
type KeyMap ¶
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the keys a Model used before KeyMap existed.
type Model ¶
type Model struct {
Title string
Message string
Theme theme.Theme
// Raw, when true, draws Title and Message unchanged. By default it is sanitised
// (ansi.Sanitize) so untrusted text cannot carry terminal escape
// sequences.
Raw bool
// Width constrains Message to word-wrap within it (Title is never
// wrapped — dialog titles are expected to be short). 0 means no
// wrapping: Message renders as-is, honoring only its own embedded
// newlines. A Message wider than the base it's rendered over will
// make the dialog box itself wider than its base, since Overlay
// deliberately never clips an overlay to fit — leaving Width unset
// for anything but a short, controlled message is asking for that.
Width int
// KeyMap holds the keys that dismiss the dialog. New fills it with
// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
// behaves as if it held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent while the dialog is
// open: a left click outside the dialog box dismisses it (DismissedMsg, as
// Esc does), and every mouse event is swallowed so nothing reaches the
// widgets behind a modal. Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle of the base view the dialog is rendered
// over; the box's own rectangle is worked out from it as Render does.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is a centered modal dialog. Render composites it over a base view when open; when closed, Render returns base unchanged, so a parent can unconditionally call it every frame without checking Open itself.
func New ¶
New returns an already-open Model — the common case is showing a dialog immediately in response to some event, not constructing one ahead of time and opening it later (though Show/Hide support that too).
func (Model) Bindings ¶
Bindings returns the actions the dialog currently honours, with descriptions, for help text. A closed dialog honours none.
func (*Model) Hide ¶
func (m *Model) Hide()
Hide closes the dialog without dismissing it via Update (no DismissedMsg is sent).
func (Model) LayoutNode ¶
LayoutNode returns the dialog's bordered box as an ordinary layout.Node, for placing in a layout instead of compositing over a base: the title, and the message word-wrapped to the width the box is given. While the dialog is closed it measures as nothing and renders blank. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the dialog as plain text for accessible output (see tui.Linearizer): "Dialog: <title>" and then the message, or "" while it is closed. An overlay has no base to composite over in a linear transcript, so an app that uses accessible mode shows this in addition to its own text.
func (Model) Rect ¶
Rect is the screen rectangle the dialog box occupies when rendered over a base view filling Bounds. It is the zero Rect while the dialog is closed.
func (*Model) Release ¶
Release hides the dialog and pops the scope Trap pushed, returning the Ring with focus back on the widget that had it before the dialog opened. Call it when DismissedMsg arrives, then Sync the Ring so that widget is told it has focus again. On a Ring with no open scope it only hides the dialog.
func (Model) Render ¶
Render composites the dialog, centered, over base. If the dialog is closed, it returns base unchanged.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentDialog, ...) and then by WithTokens.
func (*Model) Trap ¶
Trap shows the dialog and traps focus in it: it pushes a focus scope of items focusable items (at least 1) over r and returns the new Ring. While the scope is open Tab and Shift+Tab cycle among those items only, never reaching the widgets behind the dialog. Call Release with the Ring when the dialog closes.