scrollview

package
v1.95.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package scrollview provides a composable scrollable view that pairs content with a fixed-position scrollbar.

Simple path: call Model.Update + Model.View. Advanced path (custom scroll management): use Model.UpdateMouse, Model.SetScrollOffset, and Model.ViewWithLines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is a composable scrollable view that owns a scrollbar and ensures fixed-width rendering.

func New

func New(opts ...Option) *Model

New creates a new scrollview with the given options.

func (*Model) ContentWidth

func (m *Model) ContentWidth() int

ContentWidth returns the width available for content text.

func (*Model) EnsureLineVisible

func (m *Model) EnsureLineVisible(line int)

EnsureLineVisible scrolls minimally to bring a line into the viewport. Works before [SetContent] — only needs [SetSize].

func (*Model) EnsureRangeVisible

func (m *Model) EnsureRangeVisible(startLine, endLine int)

EnsureRangeVisible scrolls minimally to bring lines startLine..endLine into the view. If the range is taller than the view, the start is prioritized.

func (*Model) IsDragging

func (m *Model) IsDragging() bool

IsDragging returns whether the scrollbar thumb is being dragged.

func (*Model) LineDown

func (m *Model) LineDown()

func (*Model) LineUp

func (m *Model) LineUp()

func (*Model) NeedsScrollbar

func (m *Model) NeedsScrollbar() bool

NeedsScrollbar returns true if content is taller than the viewport.

func (*Model) PageDown

func (m *Model) PageDown()

func (*Model) PageUp

func (m *Model) PageUp()

func (*Model) ReservedCols

func (m *Model) ReservedCols() int

ReservedCols returns columns reserved for gap + scrollbar.

func (*Model) ScrollBy

func (m *Model) ScrollBy(delta int)

ScrollBy adjusts the scroll offset by delta lines.

func (*Model) ScrollOffset

func (m *Model) ScrollOffset() int

ScrollOffset returns the current scroll offset.

func (*Model) ScrollToBottom

func (m *Model) ScrollToBottom()

func (*Model) ScrollToTop

func (m *Model) ScrollToTop()

func (*Model) ScrollbarX

func (m *Model) ScrollbarX() int

ScrollbarX returns the absolute screen X of the scrollbar column.

func (*Model) SetContent

func (m *Model) SetContent(lines []string, totalHeight int)

SetContent provides the full content buffer and total height. totalHeight may be >= len(lines) for virtual blank lines (e.g. bottomSlack). The lines slice must not be mutated in place after being passed here; callers rebuild a fresh slice when content changes.

func (*Model) SetPosition

func (m *Model) SetPosition(x, y int)

SetPosition sets the absolute screen position (for mouse hit-testing).

func (*Model) SetScrollOffset

func (m *Model) SetScrollOffset(offset int)

SetScrollOffset sets the scroll offset, clamped when content dimensions are known.

func (*Model) SetSize

func (m *Model) SetSize(width, height int)

SetSize sets the total width and height of the scrollable region.

func (*Model) Update

func (m *Model) Update(msg tea.Msg) (handled bool, cmd tea.Cmd)

Update handles mouse (scrollbar click/drag/wheel) and keyboard scroll events. Returns handled=true when the event was consumed.

func (*Model) UpdateMouse

func (m *Model) UpdateMouse(msg tea.Msg) (handled bool, cmd tea.Cmd)

UpdateMouse delegates mouse events to the scrollbar. Low-level alternative to [Update].

func (*Model) View

func (m *Model) View() string

View renders the scrollable region with automatic content slicing.

func (*Model) ViewWithLines

func (m *Model) ViewWithLines(visibleLines []string) string

ViewWithLines renders pre-sliced visible lines with the scrollbar.

func (*Model) ViewWithRestyledLines added in v1.95.0

func (m *Model) ViewWithRestyledLines(visibleLines []string) string

ViewWithRestyledLines is like Model.ViewWithLines for callers whose visibleLines are sliced from the content set via Model.SetContent at the current scroll offset (possibly restyled, e.g. selection or hover highlights). Unchanged lines reuse memoized width lookups in compose; restyled lines are re-measured.

func (*Model) VisibleHeight

func (m *Model) VisibleHeight() int

VisibleHeight returns the viewport height in lines.

type Option

type Option func(*Model)

func WithKeyMap

func WithKeyMap(km *ScrollKeyMap) Option

WithKeyMap sets keyboard bindings for scroll actions. Pass nil to disable.

func WithReserveScrollbarSpace

func WithReserveScrollbarSpace(v bool) Option

WithReserveScrollbarSpace always reserves gap+scrollbar columns, preventing layout shifts.

func WithWheelStep

func WithWheelStep(n int) Option

WithWheelStep sets lines scrolled per wheel tick (default 2).

type ScrollKeyMap

type ScrollKeyMap struct {
	Up       key.Binding // optional — leave unset for list dialogs that use up/down for selection
	Down     key.Binding
	PageUp   key.Binding
	PageDown key.Binding
	Top      key.Binding // home
	Bottom   key.Binding // end
}

ScrollKeyMap defines which keys trigger scroll actions.

func DefaultScrollKeyMap

func DefaultScrollKeyMap() *ScrollKeyMap

DefaultScrollKeyMap returns a key map with page-up/down and home/end. Up/Down are intentionally unbound so list dialogs can use them for selection.

func ReadOnlyScrollKeyMap

func ReadOnlyScrollKeyMap() *ScrollKeyMap

ReadOnlyScrollKeyMap returns a key map where up/down/j/k also scroll.

Jump to

Keyboard shortcuts

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