tooltip

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: 7 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 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

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 New

func New(text string, target hittest.Rect) Model

New returns a closed tooltip with the given text for the thing drawn at target.

func (*Model) Hide

func (m *Model) Hide()

Hide closes the tooltip.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

Linearize renders the tooltip as plain text for accessible output (see tui.Linearizer): "Hint: " and its text, or "" while it is closed.

func (Model) Open

func (m Model) Open() bool

Open reports whether the tooltip is shown.

func (Model) Rect

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

func (m Model) Render(base string) string

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

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 tooltip.

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.ComponentTooltip, ...) and then by WithTokens.

func (Model) Update

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

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.

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