Documentation
¶
Overview ¶
Package hovercard is a card of details shown next to the thing it describes while the pointer is over that thing: a preview of a link, a profile behind a name. It is an overlay: Render draws it on top of a finished frame, under its target, or above when there is no room below.
It differs from tooltip in what it holds and how long it stays. It has a title and a body the caller has drawn, and it stays open while the pointer is on the card itself, so the pointer can move from the target onto it.
The pointer opens it only when the program reports motion with no button held, tui.WithMouse(tui.MouseAllMotion). Without a mouse, Show it from a key of the app's own and let Esc close it.
Stability: experimental. Its API may change in any minor release.
Example ¶
The pointer moving onto the name opens its card; Esc closes it.
package main
import (
"fmt"
"strings"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/hittest"
"github.com/ows4444/tui/hovercard"
)
func main() {
view := "by @ada" + strings.Repeat("\n"+strings.Repeat(" ", 24), 6)
card := hovercard.New("Ada Lovelace", "Wrote the first\nprogram", hittest.Rect{X: 3, Y: 0, W: 4, H: 1})
card.Mouse, card.Bounds = true, hittest.Rect{W: 24, H: 7}
card, _ = card.Update(tui.MouseEvent{X: 4, Y: 0, Action: tui.MouseActionMotion})
// Rows are trimmed of the view's blank cells for the comparison.
for _, row := range strings.Split(ansi.StripANSI(card.Render(view)), "\n") {
fmt.Println(strings.TrimRight(row, " "))
}
card, _ = card.Update(tui.Key{Type: tui.KeyEsc})
fmt.Println(card.Open())
}
Output: by @ada ┌─────────────────┐ │ Ada Lovelace │ │ │ │ Wrote the first │ │ program │ └─────────────────┘ false
Index ¶
- type ClosedMsg
- 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) 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 ClosedMsg ¶
type ClosedMsg struct {
// ID is the card's ID.
ID string
}
ClosedMsg is delivered (via the Cmd Update returns) when the card closes by the pointer or by Esc. Hide delivers nothing.
type Model ¶
type Model struct {
// ID is carried by ClosedMsg, to tell one card's closing from another's.
ID string
// Title heads the card, in bold. Empty leaves the heading out.
Title string
// Content is the body: text the caller has already drawn, of any number
// of lines. Its colours and attributes are kept and every other escape
// sequence is dropped.
Content string
// Width, when above 0, wraps Content to that many cells. Zero or less
// keeps each line as it is.
Width int
// Target is the screen rectangle of the thing the card describes. The
// card is drawn under it and lined up with its left edge.
Target hittest.Rect
Theme theme.Theme
// KeyMap holds the keys that close the card. 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: the pointer
// moving onto Target opens the card; moving off both Target and the
// card, or a press outside both, closes it. Off (the default) ignores
// the mouse.
Mouse bool
// Bounds is the screen rectangle of the base view the card is rendered
// over.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one hover card. The zero value is closed and empty; build one with New.
func New ¶
New returns a closed card with the given title and content for the thing drawn at target.
func (Model) Bindings ¶
Bindings returns the actions the card honours now, with descriptions, for help text. A closed card honours none.
func (Model) LayoutNode ¶
LayoutNode returns the card as an ordinary layout.Node, for an app that places it itself. While the card is closed it measures as nothing. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the card as plain text for accessible output (see tui.Linearizer): its title and its content without styling, or "" while it is closed.
func (Model) Rect ¶
Rect is the screen rectangle the card takes when rendered over a base view filling Bounds. It is the zero Rect while the card is closed.
func (Model) Render ¶
Render draws the card over base: a bordered box, its top-left corner under Target's bottom-left, so the pointer can move from one onto the other without crossing a gap. A card that would pass base's right edge is moved left, and one that would pass its last row is drawn above Target. A closed card 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.ComponentHoverCard, ...) and then by WithTokens.
func (Model) Update ¶
Update opens the card when the pointer moves onto Target and closes it when the pointer moves off both Target and the card, or a button is pressed outside both, when Mouse is on. Esc closes an open card. It does not keep a message from the widgets behind it: forward the same message to them.