Documentation
¶
Overview ¶
Package scrollbar is a scrollbar widget: a track with a thumb whose size and position show how much of some content is visible and where. It does not own the content. The app gives it the content's Total size, how much is Visible and the current Offset (the numbers viewport, logview and virtuallist already keep), draws it beside the content, and applies the ScrolledMsg it sends back when the user scrolls it by key, wheel, click or drag.
The bar is vertical by default (one column wide) or horizontal (one row tall). It has a LayoutNode, draws straight into a cell grid (layout.CellNode), and reads its colours from a theme.Theme.
Example ¶
A scrollbar for 100 lines with 10 showing; the down arrow scrolls one line.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/scrollbar"
)
func main() {
m := scrollbar.New(100, 10)
m, _ = m.Update(tui.Key{Type: tui.KeyDown})
fmt.Println(m.Offset)
fmt.Println(m.Linearize())
}
Output: 1 Scrollbar, vertical, 1% scrolled, showing 10 of 100
Index ¶
- Constants
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Dragging() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) MaxOffset() int
- func (m Model) Percent() float64
- func (m Model) Scrollable() bool
- func (m *Model) SetContent(total, visible int)
- func (m *Model) SetOffset(o int)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Thumb(n int) (start, size int)
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
- type Orientation
- type ScrolledMsg
Examples ¶
Constants ¶
const WheelStep = 3
WheelStep is how many content units one wheel notch scrolls when the Model's Wheel field is 0.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyMap ¶
type KeyMap struct {
Up keymap.Binding
Down keymap.Binding
PageUp keymap.Binding
PageDown keymap.Binding
Start keymap.Binding
End keymap.Binding
}
KeyMap is the set of keys Update reacts to.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the default bindings: up/down (k/j) scroll one unit, pgup/pgdown a page, home/end jump to the ends. Left and right also scroll a Horizontal bar.
type Model ¶
type Model struct {
// Total is the size of the whole content, Visible how much of it shows
// at once, and Offset where the visible window starts (0 to
// Total-Visible).
Total, Visible, Offset int
// Orientation is Vertical (the zero value) or Horizontal.
Orientation Orientation
// Length is the track length in cells. When Bounds is not empty the
// track is as long as Bounds on the bar's axis and Length is ignored.
Length int
// Wheel is the content units per wheel notch; 0 means WheelStep.
Wheel int
Theme theme.Theme
// KeyMap holds the key bindings Update obeys. New fills it with
// DefaultKeyMap; a Model built as a literal with no bindings set behaves
// as if it held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left press on
// the thumb starts a drag, a press on the track pages toward it, and the
// wheel over Bounds scrolls. Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the bar; mouse
// events outside it are ignored (a drag in progress keeps following the
// pointer wherever it goes).
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is a scrollbar. Total, Visible and Offset are in the content's own units (lines or columns); the thumb is Visible/Total of the track long and sits at Offset/(Total-Visible) of the way along.
func New ¶
New builds a vertical scrollbar for content of total units with visible shown, at offset 0.
func (Model) LayoutNode ¶
LayoutNode adapts the scrollbar to a layout.Node (also a layout.CellNode). Measure reports one cell across and Length along the bar's axis; Render and DrawCells fit the allotted Size, so the track is as long as the space the layout gives it (Length and Bounds are ignored there). The Model is not changed.
func (Model) Linearize ¶
Linearize describes the bar as plain text for accessible output (see tui.Linearizer), e.g. "Scrollbar, vertical, 25% scrolled, showing 10 of 40" or "Scrollbar, horizontal, all content visible".
func (Model) MaxOffset ¶
MaxOffset is the largest Offset: Total-Visible, or 0 when everything is visible.
func (Model) Percent ¶
Percent is the scroll position from 0 (top) to 1 (end); it is 0 when nothing can scroll.
func (Model) Scrollable ¶
Scrollable reports whether there is content beyond the visible window.
func (*Model) SetContent ¶
SetContent sets Total and Visible together and re-clamps the offset.
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) Thumb ¶
Thumb returns the thumb's start and size in cells along a track of length n: the size is n*Visible/Total (at least 1, at most n) and the start moves from 0 to n-size as Offset goes from 0 to MaxOffset. When nothing can scroll the thumb fills the track.
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentScrollbar, ...) and then by WithTokens.
func (Model) Update ¶
Update scrolls on the KeyMap bindings and, with Mouse on, on mouse events (see Model.Mouse). It returns a Cmd delivering ScrolledMsg when the offset changed. Any other Msg is a no-op.
type Orientation ¶
type Orientation int
Orientation is the axis a scrollbar runs along.
const ( // Vertical is a one-column bar along the right edge of the content. It // is the zero value. Vertical Orientation = iota // Horizontal is a one-row bar along the bottom edge of the content. Horizontal )
type ScrolledMsg ¶
type ScrolledMsg struct{ Offset int }
ScrolledMsg is delivered (via the Cmd Update returns) when the user moves the offset. The app applies Offset to its content.