Documentation
¶
Overview ¶
Package tooltip is a short hint shown next to the thing it describes while the pointer is over that thing. It is an overlay: Render draws it on top of a finished frame, under its target, or above when there is no room below.
The pointer shows it only when the program reports motion with no button held, tui.WithMouse(tui.MouseAllMotion). Without a mouse, Show it when its target takes keyboard focus and Hide it when focus leaves.
Stability: experimental. Its API may change in any minor release.
Example ¶
The pointer moving onto the target shows the hint under it.
package main
import (
"fmt"
"strings"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/hittest"
"github.com/ows4444/tui/tooltip"
)
func main() {
view := "[ Save ] \n \n \n "
tip := tooltip.New("Ctrl+S", hittest.Rect{X: 0, Y: 0, W: 8, H: 1})
tip.Mouse, tip.Bounds = true, hittest.Rect{W: 12, H: 4}
tip, _ = tip.Update(tui.MouseEvent{X: 3, Y: 0, Action: tui.MouseActionMotion})
// Rows are trimmed of the view's blank cells for the comparison.
for _, row := range strings.Split(ansi.StripANSI(tip.Render(view)), "\n") {
fmt.Println(strings.TrimRight(row, " "))
}
}
Output: [ Save ] ┌────────┐ │ Ctrl+S │ └────────┘
Index ¶
- 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()
- func (m Model) Tokens() theme.Tokens
- 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 Model ¶
type Model struct {
// Text is the hint. It may have several lines.
Text string
// Width, when above 0, wraps Text to that many cells. Zero or less
// keeps each line of Text as it is.
Width int
// Target is the screen rectangle of the thing the tooltip describes.
// The tooltip is drawn under it and lined up with its left edge.
Target hittest.Rect
Theme theme.Theme
// Mouse, when true, makes Update handle tui.MouseEvent: the pointer
// moving onto Target shows the tooltip, and the pointer leaving it, or
// any button pressed, hides it. Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle of the base view the tooltip is
// rendered over.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one tooltip. The zero value is closed and has no text; build one with New.
func (Model) LayoutNode ¶
LayoutNode returns the tooltip's box as an ordinary layout.Node, for an app that places it itself. While the tooltip is closed it measures as nothing. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the tooltip as plain text for accessible output (see tui.Linearizer): "Hint: " and its text, or "" while it is closed.
func (Model) Rect ¶
Rect is the screen rectangle the tooltip takes when rendered over a base view filling Bounds. It is the zero Rect while the tooltip is closed.
func (Model) Render ¶
Render draws the tooltip over base: a bordered box in the theme's Info colour, its top-left corner under Target's bottom-left. A box that would pass base's right edge is moved left, and one that would pass its last row is drawn above Target. A closed tooltip 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.ComponentTooltip, ...) and then by WithTokens.
func (Model) Update ¶
Update shows and hides the tooltip as the pointer moves onto and off Target, when Mouse is on. A button pressed anywhere hides it, and so does any key, since either means the user has gone on to something else. It never returns a Cmd and never keeps a message from the widgets behind it: forward the same message to them.