dialog

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

type KeyMap struct {
	Dismiss keymap.Binding // close the dialog and deliver DismissedMsg
}

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

func New(title, message string) Model

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

func (m Model) Bindings() []keymap.Binding

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

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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) Open

func (m Model) Open() bool

Open reports whether the dialog is currently shown.

func (Model) Rect

func (m Model) Rect() hittest.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

func (m *Model) Release(r focus.Ring) focus.Ring

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

func (m Model) Render(base string) string

Render composites the dialog, centered, over base. If the dialog is closed, it returns base unchanged.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

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) Show

func (m *Model) Show()

Show opens the dialog.

func (Model) Tokens

func (m Model) Tokens() theme.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

func (m *Model) Trap(r focus.Ring, items int) focus.Ring

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.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update dismisses the dialog on Enter or Esc while open; it's a no-op (including for Enter/Esc) once closed, so a parent doesn't need to guard forwarding to it on Model.Open().

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL