widget

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package widget collects the small leaf views that live inside a Window or Dialog: static text, labels, buttons, input lines, clusters, scrollbars, and so on.

v0.2 phase 3 ships the first batch (StaticText, Label, Button, InputLine). Lists, scrollbars, and clusters arrive in phase 4.

Index

Constants

View Source
const (
	SlotDialogText     byte = 14
	SlotDialogShortcut byte = 17
	SlotButtonNormal   byte = 18
	SlotButtonFocused  byte = 19
	SlotButtonDisabled byte = 20
	SlotButtonShortcut byte = 21
	SlotInputNormal    byte = 22
	SlotInputFocused   byte = 23
	SlotInputSelection byte = 24
	SlotClusterNormal  byte = 25
	SlotClusterFocused byte = 26
	SlotClusterSelect  byte = 27
	SlotClusterHotKey  byte = 28
	SlotScrollPage     byte = 29
	SlotScrollArrow    byte = 30
	SlotListNormal     byte = 31
	SlotListFocused    byte = 32
	SlotListSelected   byte = 33
	SlotListDisabled   byte = 34
)

Palette slot constants used by widgets. They line up with the slots added to vio.BorlandClassic for dialog content. Widgets default to these but callers may override PaletteIndex if a host theme adds extra slots.

View Source
const HistoryLimit = 16

HistoryLimit caps the number of strings kept per ID. Older entries fall off the back when the ring fills up.

Variables

This section is empty.

Functions

func HistoryAdd

func HistoryAdd(id HistoryID, s string)

HistoryAdd pushes s onto the front of the ring for id. If s already exists in the ring it is moved to the front (no duplicates). The ring is capped at HistoryLimit; the oldest entry is discarded when it overflows. Empty strings and id == 0 are ignored.

func HistoryCount

func HistoryCount(id HistoryID) int

HistoryCount returns the number of stored strings for id.

func HistoryReset

func HistoryReset()

HistoryReset clears all rings. Tests use this to start clean.

func HistoryStr

func HistoryStr(id HistoryID, n int) string

HistoryStr returns the n-th string in the ring for id, with 0 the most recent. Out-of-range indices return "".

Types

type Button

type Button struct {
	*view.View

	Title   string
	Command event.CommandID
	Flags   ButtonFlags
	// contains filtered or unexported fields
}

Button is a clickable control that emits a command. Mouse-down inside the bounds, Space or Enter while focused, or Alt-<mnemonic> all fire the same command.

func NewButton

func NewButton(bounds vio.Rect, title string, cmd event.CommandID, flags ButtonFlags) *Button

NewButton returns a Button sized to bounds. The title may include a '~x~' marker to advertise an Alt-x hot-key.

func (*Button) Draw

func (b *Button) Draw(s *vio.Surface)

Draw paints the button. The title sits inside square brackets when the button is the default; otherwise plain. The focused button uses the focused palette slot.

func (*Button) HandleEvent

func (b *Button) HandleEvent(e *event.Event)

HandleEvent fires the command on Space, Enter, mouse-down inside the bounds, or Alt-<mnemonic>.

func (*Button) IsDefault

func (b *Button) IsDefault() bool

IsDefault reports whether the button fires on Enter without focus.

func (*Button) Mnemonic

func (b *Button) Mnemonic() rune

Mnemonic returns the parsed hot-key rune (always lowercase), or 0.

func (*Button) Press

func (b *Button) Press()

Press programmatically fires the button. Useful for Dialog Enter dispatch and for tests.

type ButtonFlags

type ButtonFlags uint8

ButtonFlags is a bitmask of behavior flags for a Button. The names match Turbo Vision's bfXxx constants.

const (
	BfNormal    ButtonFlags = 0
	BfDefault   ButtonFlags = 1 << iota // fired on Enter regardless of focus
	BfLeftJust                          // left-justify the title (default is centered)
	BfBroadcast                         // emit ClassBroadcast instead of ClassCommand
)

Button flag bits.

type CheckBoxes

type CheckBoxes struct {
	*Cluster
}

CheckBoxes is a Cluster where each item is independently toggled. Value is a bitmask: bit i is set when item i is checked.

func NewCheckBoxes

func NewCheckBoxes(bounds vio.Rect, items []string) *CheckBoxes

NewCheckBoxes returns a check-box cluster sized to bounds. Each item title may include a '~x~' mnemonic marker for Alt-x dispatch.

func (*CheckBoxes) Draw

func (c *CheckBoxes) Draw(s *vio.Surface)

Draw paints the check-box list.

func (*CheckBoxes) HandleEvent

func (c *CheckBoxes) HandleEvent(e *event.Event)

HandleEvent moves focus, toggles on Space/Enter, and dispatches Alt-mnemonic.

func (*CheckBoxes) Mark

func (c *CheckBoxes) Mark(i int) bool

Mark reports whether item i is checked.

func (*CheckBoxes) Press

func (c *CheckBoxes) Press(i int)

Press toggles item i.

type Cluster

type Cluster struct {
	*view.View

	Items []string
	Value uint32
	// contains filtered or unexported fields
}

Cluster is the shared backing for CheckBoxes and RadioButtons. It holds the option titles, the parsed mnemonic letters, the focused item index, and a Value field whose meaning is owned by the concrete type (bitmask for check boxes, index for radio buttons).

func (*Cluster) Sel

func (c *Cluster) Sel() int

Sel returns the focused item index.

func (*Cluster) SetSel

func (c *Cluster) SetSel(i int)

SetSel moves the focus to item i, clamped to a valid index.

type GetTextFunc

type GetTextFunc func(item, maxLen int) string

GetTextFunc returns the rendered string for item index when at most maxLen runes are wanted. It is the abstract entry point ListViewer uses to fetch row text; ListBox installs one that reads from a string slice. Callers can install any function (e.g. backed by a log buffer) without subtyping.

type History

type History struct {
	*view.View

	ID   HistoryID
	Open bool
}

History is the small dropdown-arrow button that sits next to an InputLine. A click or a focused Enter sets Open to true; the host dialog watches Open and pushes a HistoryWindow when it flips. Phase 4 keeps the trigger simple; richer integration with ExecView lands alongside the menu/cmd work in phase 5.

func NewHistory

func NewHistory(bounds vio.Rect, id HistoryID) *History

NewHistory returns a History trigger attached to id.

func (*History) Draw

func (h *History) Draw(s *vio.Surface)

Draw paints the trigger as a "[▼]" cap.

func (*History) HandleEvent

func (h *History) HandleEvent(e *event.Event)

HandleEvent flips Open on click or Enter. Hosts reset Open after they spawn the dropdown window.

type HistoryID

type HistoryID uint16

HistoryID groups history entries by purpose: each numeric ID owns its own ring of recent strings. Two views that share an ID share their history. Zero means "no history" and HistoryAdd ignores it.

type HistoryWindow

type HistoryWindow struct {
	*view.Group

	ID       HistoryID
	List     *ListBox
	Selected string
	// contains filtered or unexported fields
}

HistoryWindow is the dropdown list that appears under a History trigger. It hosts a ListBox seeded from the ring for ID. Enter on the focused row stores Selected and sets Result to CmdOk; Esc sets Result to CmdCancel. Hosts insert a HistoryWindow into a Group and poll Result like any other modal view.

func NewHistoryWindow

func NewHistoryWindow(bounds vio.Rect, id HistoryID) *HistoryWindow

NewHistoryWindow returns a dropdown sized to bounds, populated from the ring for id. The internal ListBox is inserted as the first child.

func (*HistoryWindow) HandleEvent

func (w *HistoryWindow) HandleEvent(e *event.Event)

HandleEvent intercepts Enter and Esc, then defers everything else to the embedded Group (which feeds the ListBox).

func (*HistoryWindow) Result

func (w *HistoryWindow) Result() event.CommandID

Result returns the modal command, or CmdNone while still open.

type InputLine

type InputLine struct {
	*view.View

	Data   []rune
	MaxLen int
	// contains filtered or unexported fields
}

InputLine is a single-line text editor. It exposes Data for the caller to read or seed; the cursor and selection are kept in private fields and updated by HandleEvent.

Phase 3 supports printable insertion, Backspace, Delete, arrow-key navigation, Home/End, Ctrl-Y (clear), and shift-extend selection. Word-wrap, undo, and the History dropdown ride along in phase 4.

func NewInputLine

func NewInputLine(bounds vio.Rect, maxLen int) *InputLine

NewInputLine returns an InputLine with the given bounds and length cap. maxLen <= 0 disables the cap.

func (*InputLine) CursorIndex

func (i *InputLine) CursorIndex() int

CursorIndex returns the insertion point as a rune index. It does not shadow the View.Cursor field, which holds the on-screen cell offset for the rendered caret.

func (*InputLine) Draw

func (i *InputLine) Draw(s *vio.Surface)

Draw paints the field. The selection is highlighted in the selection slot; otherwise the line uses normal/focused color.

func (*InputLine) HandleEvent

func (i *InputLine) HandleEvent(e *event.Event)

HandleEvent processes one keystroke. Non-key events fall through.

func (*InputLine) Selection

func (i *InputLine) Selection() (int, int)

Selection returns the selected range [start, end) as rune indices. start == end when nothing is selected.

func (*InputLine) SetString

func (i *InputLine) SetString(s string)

SetString replaces the data. The cursor moves to the end and any selection clears.

func (*InputLine) String

func (i *InputLine) String() string

String returns the current edit value.

type Label

type Label struct {
	*view.View

	Title string
	Peer  view.Viewer
	// contains filtered or unexported fields
}

Label is a non-focusable text element bound to a peer view. Pressing Alt-<mnemonic> on the dialog moves focus to the peer. The mnemonic is the rune surrounded by '~' in the title (e.g. "~N~ame:" advertises Alt-N and underlines the N).

func NewLabel

func NewLabel(bounds vio.Rect, title string, peer view.Viewer) *Label

NewLabel returns a Label sized to bounds. peer may be nil for a caption that just announces text without claiming a hot-key.

func (*Label) Draw

func (l *Label) Draw(s *vio.Surface)

Draw paints the label. The mnemonic rune is drawn in the dialog shortcut color so it stands out.

func (*Label) HandleEvent

func (l *Label) HandleEvent(e *event.Event)

HandleEvent moves focus to the peer when the user presses Alt-<mnemonic>. Without a peer, or without a mnemonic, the label is inert.

func (*Label) Mnemonic

func (l *Label) Mnemonic() rune

Mnemonic returns the parsed hot-key rune (always lowercase), or 0 if the title had no '~x~' marker.

type ListBox

type ListBox struct {
	*ListViewer
	// contains filtered or unexported fields
}

ListBox is a ListViewer backed by a string slice. Items can be updated at any time with SetItems; the list resizes accordingly.

func NewListBox

func NewListBox(bounds vio.Rect, items []string, vScroll *ScrollBar) *ListBox

NewListBox returns a list box sized to bounds with the given items and an optional vertical scrollbar. Pass a nil scrollbar to omit it.

func (*ListBox) Items

func (l *ListBox) Items() []string

Items returns the current item slice. The slice is shared with the list box; callers must not modify it concurrently with redraws.

func (*ListBox) Selected

func (l *ListBox) Selected() string

Selected returns the focused item's string, or "" when the list is empty.

func (*ListBox) SetItems

func (l *ListBox) SetItems(items []string)

SetItems replaces the items and resets focus to the first row.

type ListViewer

type ListViewer struct {
	*view.View

	Range   int
	Focused int
	HScroll *ScrollBar
	VScroll *ScrollBar

	GetText GetTextFunc
	// contains filtered or unexported fields
}

ListViewer is the scrolling-list base. It owns the focused item, the top-visible row, optional scrollbars, and a GetText function that yields the row text. The view paints rows top-down, highlights the focused row when StateFocused is set, and broadcasts no commands; concrete lists wire commands themselves.

func NewListViewer

func NewListViewer(bounds vio.Rect, vScroll, hScroll *ScrollBar) *ListViewer

NewListViewer returns a list viewer sized to bounds. The caller must set GetText (and Range) before the first Draw, otherwise rows render blank.

func (*ListViewer) Draw

func (l *ListViewer) Draw(s *vio.Surface)

Draw paints all visible rows. Rows beyond Range are blanked. The focused row is highlighted when the view holds StateFocused.

func (*ListViewer) FocusItem

func (l *ListViewer) FocusItem(i int)

FocusItem moves the focused row to i (clamped) and pages the view if i would be off-screen.

func (*ListViewer) HandleEvent

func (l *ListViewer) HandleEvent(e *event.Event)

HandleEvent moves Focused with the arrow keys, pages with PgUp/Dn, jumps with Home/End, and selects on mouse-down inside the bounds. It also listens for CmdScrollBarChanged broadcasts from its own vertical scrollbar and snaps Focused to the new value.

func (*ListViewer) SetRange

func (l *ListViewer) SetRange(n int)

SetRange updates Range and clamps Focused and topItem to fit. It also reconfigures the vertical scrollbar if one is attached.

func (*ListViewer) TopItem

func (l *ListViewer) TopItem() int

TopItem returns the first visible row index.

type Memo

type Memo struct {
	*view.View

	TabWidth int
	// contains filtered or unexported fields
}

Memo is a multi-line text editor. Lines are stored as a slice of rune slices; the cursor is a (line, col) pair. v0.2 phase 4 covers printable insertion, Backspace/Delete, Enter inserts a newline, Tab inserts spaces, arrow/Home/End/PgUp/PgDn navigation, and viewport scrolling. Undo, clipboard, word-wrap, and selection ride along in later phases.

func NewMemo

func NewMemo(bounds vio.Rect) *Memo

NewMemo returns a Memo sized to bounds with one empty line. TabWidth defaults to 4; callers can change it before the first edit.

func (*Memo) CursorPos

func (m *Memo) CursorPos() (int, int)

CursorPos returns the (line, col) cursor position.

func (*Memo) Draw

func (m *Memo) Draw(s *vio.Surface)

Draw paints visible lines and positions the on-screen caret.

func (*Memo) HandleEvent

func (m *Memo) HandleEvent(e *event.Event)

HandleEvent dispatches one event. Non-key events fall through.

func (*Memo) Lines

func (m *Memo) Lines() [][]rune

Lines returns the current line slice. The slice and its rune slices are shared with the memo; callers must not mutate concurrently.

func (*Memo) SetString

func (m *Memo) SetString(s string)

SetString replaces the buffer with s, splitting on newlines, and puts the cursor at the end of the last line.

func (*Memo) String

func (m *Memo) String() string

String returns the memo content joined with newlines.

func (*Memo) TopLine

func (m *Memo) TopLine() int

TopLine returns the first visible line index.

type Orientation

type Orientation uint8

Orientation distinguishes a horizontal scrollbar (W > H) from a vertical one (H >= W). Callers don't construct this directly; the ScrollBar constructor picks the orientation from the bounds.

const (
	Horizontal Orientation = iota
	Vertical
)

Orientation values.

type ParamText

type ParamText struct {
	*view.View

	Format string
	Params []any
}

ParamText is a read-only text view whose payload is rendered through fmt.Sprintf. Use it for status fields with live values: "Line %d, Col %d" and so on. The format string is fixed at construction; SetParams updates the slice of values, redraw paints the formatted result.

func NewParamText

func NewParamText(bounds vio.Rect, format string) *ParamText

NewParamText returns a ParamText sized to bounds with the given format string. The initial parameter list is empty.

func (*ParamText) Draw

func (p *ParamText) Draw(s *vio.Surface)

Draw paints the formatted text. Lines longer than the bounds width are clipped; only the first row is used.

func (*ParamText) SetParams

func (p *ParamText) SetParams(params ...any)

SetParams replaces the parameter slice. The slice is shared with the view; callers must not mutate concurrently with redraws.

func (*ParamText) Text

func (p *ParamText) Text() string

Text returns the formatted string. It is the value Draw paints.

type RadioButtons

type RadioButtons struct {
	*Cluster
}

RadioButtons is a Cluster with single selection: Value is the index of the chosen item.

func NewRadioButtons

func NewRadioButtons(bounds vio.Rect, items []string) *RadioButtons

NewRadioButtons returns a radio-button cluster sized to bounds.

func (*RadioButtons) Draw

func (r *RadioButtons) Draw(s *vio.Surface)

Draw paints the radio-button list.

func (*RadioButtons) HandleEvent

func (r *RadioButtons) HandleEvent(e *event.Event)

HandleEvent moves focus, selects on Space/Enter, and dispatches Alt-mnemonic. Selecting an item also moves focus to that item.

func (*RadioButtons) Mark

func (r *RadioButtons) Mark(i int) bool

Mark reports whether item i is the chosen one.

func (*RadioButtons) Press

func (r *RadioButtons) Press(i int)

Press makes item i the chosen one.

type ScrollBar

type ScrollBar struct {
	*view.View

	Min, Max int
	Value    int
	PgStep   int
	ArStep   int
	// contains filtered or unexported fields
}

ScrollBar is the strip with arrow caps at each end and a thumb that represents the current value inside the [Min, Max] range. PgStep is the increment when the user clicks the page area; ArStep is the increment for the arrow caps. Whenever Value changes, ScrollBar broadcasts a CmdScrollBarChanged command via its owner.

func NewScrollBar

func NewScrollBar(bounds vio.Rect) *ScrollBar

NewScrollBar returns a scrollbar sized to bounds. Orientation is horizontal when bounds.W > bounds.H. Step defaults are 1 for the arrow caps and max(1, length-2) for the page region.

func (*ScrollBar) Draw

func (s *ScrollBar) Draw(surf *vio.Surface)

Draw paints the arrows, the page background, and the thumb.

func (*ScrollBar) HandleEvent

func (s *ScrollBar) HandleEvent(e *event.Event)

HandleEvent maps mouse-down on the arrow caps to ArStep increments and clicks on the page region to PgStep jumps. Releases and drag are not implemented in v0.2 phase 4; they ride along in a later polish pass.

func (*ScrollBar) Orientation

func (s *ScrollBar) Orientation() Orientation

Orientation returns whether the bar is horizontal or vertical.

func (*ScrollBar) SetParams

func (s *ScrollBar) SetParams(value, minVal, maxVal, pgStep, arStep int)

SetParams updates value, min, max, page step, and arrow step in one call and clamps Value to the new range. SetParams broadcasts a CmdScrollBarChanged if the clamped Value differs from the prior one.

func (*ScrollBar) SetValue

func (s *ScrollBar) SetValue(v int)

SetValue clamps v to [Min, Max] and stores it. If the clamped value differs from the previous one, it broadcasts CmdScrollBarChanged.

type StaticText

type StaticText struct {
	*view.View

	Text string
}

StaticText is a non-selectable text view that paints its Text into its bounds. Newlines start new rows; lines longer than the bounds width are clipped (no word-wrap in v0.2 phase 3).

func NewStaticText

func NewStaticText(bounds vio.Rect, text string) *StaticText

NewStaticText returns a StaticText sized to bounds with the given text.

func (*StaticText) Draw

func (s *StaticText) Draw(surf *vio.Surface)

Draw paints the static text. Empty text leaves the bounds blank.

Jump to

Keyboard shortcuts

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