scroll

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Anchor

type Anchor int

Anchor selects where a Scroll keeps its content when it grows.

const (
	// AnchorTop keeps the offset when content is added (the default).
	AnchorTop Anchor = iota
	// AnchorBottom keeps a scroll that's at the bottom there as its
	// content grows - a chat following a reply as it streams in - and
	// stops following once the user scrolls up, until they scroll
	// back to the bottom. It starts at the bottom.
	AnchorBottom
)

type Axis

type Axis int

Axis selects the directions a Scroll scrolls in.

const (
	// Vertical scrolls up and down (the default).
	Vertical Axis = 1 << iota
	// Horizontal scrolls sideways, for wide code or tables.
	Horizontal
)

type Scroll

type Scroll struct {
	// Contextual gives it a context menu (Context).
	elements.Contextual
	// contains filtered or unexported fields
}

Scroll is a stateful vertical scroll container. It implements elements.Element, so it can be placed directly in the tree like any other element. Create it once with New and lay it out every frame; content is rebuilt every frame while visible and may be taller than the viewport.

The scroll offset reacts to the mouse wheel and touch drags, and the scrollbar thumb can be dragged with the mouse.

func New

func New(content func() elements.Element, st ...styles.Styles) *Scroll

New returns a Scroll around content, rebuilt fresh every frame. The styles argument is optional: Background is the container fill and ScrollBar customizes the scrollbar's width, colors, corner radius and minimum thumb length.

func (*Scroll) Anchor

func (s *Scroll) Anchor(a Anchor) *Scroll

Anchor sets where the content stays as it grows; see AnchorBottom. It returns s for chaining.

func (*Scroll) AtBottom

func (s *Scroll) AtBottom() bool

AtBottom reports whether the scroll is at (or past) its maximum offset - also true when there's nothing to scroll at all (the content already fits the viewport), the same way a page with no scrollbar is trivially "at the bottom": there's nowhere else to go, so a load-more handler driven by AtBottom/OnReachBottom fires immediately, prompting an initial fetch when the first page of content doesn't even fill the viewport.

func (*Scroll) Axis

func (s *Scroll) Axis(a Axis) *Scroll

Axis sets the directions the content scrolls in: Vertical (the default), Horizontal, or Vertical|Horizontal. Along a scrolling axis the content gets unlimited room; along the other it gets the viewport's size. It returns s for chaining.

func (*Scroll) ContentHeight

func (s *Scroll) ContentHeight() int

ContentHeight returns the total height of the scrolled content, as measured on the last frame.

func (*Scroll) ContentWidth

func (s *Scroll) ContentWidth() int

ContentWidth returns the total width of the scrolled content, as measured on the last frame. With Axis(Horizontal) (or both axes) the content may be wider than the viewport; otherwise it's laid out at the viewport's width.

func (*Scroll) FitContent

func (s *Scroll) FitContent(on bool) *Scroll

FitContent makes the viewport only as tall (or, scrolling horizontally, as wide) as the content, up to the available space, instead of always filling it: a sheet with three rows stays three rows tall and scrolls once the content outgrows the screen. It returns s for chaining.

func (*Scroll) Layout

func (s *Scroll) Layout(gtx layout.Context) layout.Dimensions

Layout lays out the scrollable content.

func (*Scroll) MaxOffsetX

func (s *Scroll) MaxOffsetX() int

MaxOffsetX returns how far it can scroll sideways, in pixels: 0 when the content fits.

func (*Scroll) Offset

func (s *Scroll) Offset() float32

Offset returns the current vertical offset.

func (*Scroll) OffsetDp

func (s *Scroll) OffsetDp() float32

OffsetDp returns the vertical offset in dp.

func (*Scroll) OffsetX

func (s *Scroll) OffsetX() float32

OffsetX returns the horizontal offset in pixels, from the start edge: the left, or the right in a right-to-left locale, where a horizontal Scroll starts at its right end as in a browser.

func (*Scroll) OnReachBottom

func (s *Scroll) OnReachBottom(fn func()) *Scroll

OnReachBottom sets the handler invoked once when the scroll reaches its maximum offset - edge-triggered: it fires again only after scrolling away from the bottom and back, not on every frame spent there. It returns s for chaining.

func (*Scroll) OnScroll

func (s *Scroll) OnScroll(fn func(offset, max float32)) *Scroll

OnScroll sets a handler called after the offset changes - by the user, ScrollTo or an anchor - with the vertical offset and its maximum in pixels (the horizontal ones for a Horizontal-only scroll). It returns s for chaining.

func (*Scroll) Progress

func (s *Scroll) Progress() float32

Progress returns how far scrolled, from 0 (top) to 1 (bottom); 0 when there's nothing to scroll (the content fits the viewport).

func (*Scroll) ScrollBy

func (s *Scroll) ScrollBy(dx, dy float32)

ScrollBy moves the content by dx, dy pixels, as a wheel or drag would: positive dy scrolls down. The offsets are clamped to the content on the next layout. It's how automation (the MCP server's ui_scroll) scrolls.

func (*Scroll) ScrollOffset

func (s *Scroll) ScrollOffset() (x, y float32)

ScrollOffset returns the horizontal and vertical offsets in pixels.

func (*Scroll) ScrollTo

func (s *Scroll) ScrollTo(v float32)

ScrollTo sets the vertical offset in pixels.

func (*Scroll) ScrollToBottom

func (s *Scroll) ScrollToBottom()

ScrollToBottom scrolls to the end of the content, and resumes following it with AnchorBottom.

func (*Scroll) ScrollToDp

func (s *Scroll) ScrollToDp(v float32)

ScrollToDp sets the vertical offset in dp.

func (*Scroll) ScrollToX

func (s *Scroll) ScrollToX(v float32)

ScrollToX sets the horizontal offset in pixels, from the start edge (see OffsetX).

func (*Scroll) Styles

func (s *Scroll) Styles(st styles.Styles) *Scroll

Styles replaces the styles, from the next frame. It returns s for chaining.

func (*Scroll) ViewportHeight

func (s *Scroll) ViewportHeight() int

ViewportHeight returns the height of the visible viewport, as measured on the last frame.

func (*Scroll) ViewportWidth

func (s *Scroll) ViewportWidth() int

ViewportWidth returns the width of the visible viewport, as measured on the last frame: for keeping a caret in view sideways, together with OffsetX and ScrollToX.

func (*Scroll) Visible

func (s *Scroll) Visible(top, height int) bool

Visible reports whether the vertical range [top, top+height) - in the same coordinate space as Content's own output, e.g. a running offset tracked while building a long list - is at least partially within the current viewport. Useful for lazy-loading or skipping expensive work for an item that isn't currently on screen.

func (*Scroll) VisibleDp

func (s *Scroll) VisibleDp(top, height float32) bool

VisibleDp is Visible with top and height in dp, the units Styles use, converted with the display scale of the last frame.

Jump to

Keyboard shortcuts

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