Documentation
¶
Overview ¶
Package toast is a transient auto-dismissing notification — InkUI's "Toast": a small bordered box in a screen corner, composited via layout.Overlay the same way dialog is, that closes itself after Duration via motion.After rather than waiting for a keypress.
Example ¶
Show opens the toast and returns the Cmd that dismisses it after Duration.
package main
import (
"fmt"
"github.com/ows4444/tui/toast"
)
func main() {
t := toast.New("Copied")
fmt.Println(t.Open())
cmd := t.Show()
fmt.Println(t.Open(), cmd != nil)
}
Output: false true true
Index ¶
- type DismissedMsg
- type Model
- 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) Render(base string) string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m *Model) Show() tui.Cmd
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) WithTokens(tok theme.Tokens) Model
- type Position
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 toast auto-dismisses after Duration.
type Model ¶
type Model struct {
Message string
// Raw, when true, draws Message unchanged. By default it is sanitised
// (ansi.Sanitize) so untrusted text cannot carry terminal escape
// sequences.
Raw bool
Variant widgets.Variant
Theme theme.Theme
Duration time.Duration
Position Position
// Margin insets the toast from base's edge, in cells. Defaults to 1
// via New so a corner toast doesn't sit exactly flush against (and
// visually merge into) whatever border base itself might have right
// at that edge — set to 0 for a flush-to-the-corner placement.
Margin int
// Mouse, when true, makes Update handle tui.MouseEvent: a left click on
// the toast dismisses it at once and delivers DismissedMsg. Off (the
// default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle of the base view the toast is rendered
// over (its origin and size, as passed to Render); the toast's own
// rectangle is worked out from it, Position and Margin. Events outside
// the toast are ignored.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is a corner notification. Like dialog.Model, Render composites it over a base view when open and returns base unchanged when closed, so a parent can call it unconditionally every frame.
func New ¶
New returns a Model that isn't shown yet — call Show (and return its Cmd) to actually display it.
func (*Model) Hide ¶
func (m *Model) Hide()
Hide closes the toast immediately, without waiting for Duration.
func (Model) LayoutNode ¶
LayoutNode returns the toast's bordered box as an ordinary layout.Node, its message word-wrapped to the width it is given and coloured by Variant. While the toast is closed it measures as nothing and renders blank. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the toast as plain text for accessible output (see tui.Linearizer): "Notification, <kind>: <message>", or "" while it is closed. The kind is the Variant in words, so meaning does not rely on colour.
func (Model) Rect ¶
Rect is the screen rectangle the toast occupies when rendered over a base view filling Bounds: where a click lands on it. It is the zero Rect while the toast is closed.
func (Model) Render ¶
Render composites the toast over base in its configured Position. If 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) Show ¶
Show opens the toast and returns a Cmd that auto-dismisses it after Duration — return this from your own Update (or Init) so it actually fires.
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentToast, ...) and then by WithTokens.
type Position ¶
type Position int
Position selects which screen corner a Toast appears in.
const ( // BottomRight places the toast in the bottom-right corner, the zero // value and so the default. BottomRight Position = iota // BottomLeft places the toast in the bottom-left corner. BottomLeft // TopRight places the toast in the top-right corner. TopRight // TopLeft places the toast in the top-left corner. TopLeft )