viewport

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package viewport provides item-aware scrolling over a list of variable-height items separated by gaps.

It is pure math — no rendering, no external dependencies. The consumer provides item heights and gap sizes as int slices, and the viewport handles scroll position, focus tracking, hit testing, and bottom detection.

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 an item-aware viewport that tracks scroll position, focus, and layout over a list of variable-height items.

func New

func New() Model

New returns a viewport with no items.

func (*Model) AtBottom

func (m *Model) AtBottom() bool

AtBottom returns whether the viewport is showing the bottom.

func (*Model) FocusIdx

func (m *Model) FocusIdx() int

FocusIdx returns the focused item index (-1 if none).

func (*Model) FocusNext

func (m *Model) FocusNext()

FocusNext moves focus to the next item and scrolls to keep it visible.

func (*Model) FocusPrev

func (m *Model) FocusPrev()

FocusPrev moves focus to the previous item and scrolls to keep it visible.

func (*Model) ItemAtY

func (m *Model) ItemAtY(y int) (idx, localY int)

ItemAtY maps a viewport-relative Y coordinate to an item index and the Y offset within that item. Returns (-1, -1) if the position is in a gap, above, or below all items.

func (*Model) Len

func (m *Model) Len() int

Len returns the number of items.

func (*Model) Offset

func (m *Model) Offset() (idx, line int)

Offset returns the current scroll position: the index of the first visible item and the number of lines scrolled into it.

func (*Model) ScrollBy

func (m *Model) ScrollBy(lines int)

ScrollBy scrolls by the given number of lines (positive = down, negative = up).

func (*Model) ScrollToBottom

func (m *Model) ScrollToBottom()

ScrollToBottom scrolls so the last item is at the bottom of the viewport.

func (*Model) ScrollToTop

func (m *Model) ScrollToTop()

ScrollToTop scrolls to the first item.

func (*Model) SetFocusIdx

func (m *Model) SetFocusIdx(idx int)

SetFocusIdx sets the focused item index.

func (*Model) SetHeight

func (m *Model) SetHeight(h int)

SetHeight sets the viewport height in lines.

func (*Model) SetItems

func (m *Model) SetItems(heights, gaps []int)

SetItems replaces the item heights and gaps. gaps[i] is the number of lines between item i-1 and item i. gaps[0] must be 0 (no gap before the first item). Both slices must have the same length. Invalidates the bottom cache and clamps scroll/focus state.

func (*Model) SetTrailingHeight

func (m *Model) SetTrailingHeight(h int)

SetTrailingHeight sets extra content height below the last item (e.g. a divider or status line). The viewport accounts for this when computing the bottom scroll position.

func (*Model) UpdateFocusFromScroll

func (m *Model) UpdateFocusFromScroll()

UpdateFocusFromScroll sets focus to the last fully visible item.

Jump to

Keyboard shortcuts

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