scrollbar

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 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

Overview

Package scrollbar is the scroll logic of the backends that keep scroll offsets of their own: where a scroll view's thumb is, whether a point is on its bar, and where dragging the thumb scrolls to; the offsets its content inset allows; and where a snapping view settles after a wheel or a thumb drag. The backends show a bar while its scroll view moves, while the pointer is over it, or while its thumb is dragged. macOS takes the motion too (motion.go): the rubber band, eased snaps, the indicator's geometry and fade, and trackpad events shown in step with the display.

Index

Constants

View Source
const (
	// Animation is how long animated scrolls and springs back take.
	Animation = 300 * time.Millisecond
	// IndicatorHold is how long the indicator stays after the last
	// scroll; it then fades out in IndicatorFade.
	IndicatorHold = 700 * time.Millisecond
	IndicatorFade = 300 * time.Millisecond
	// IndicatorMargin keeps the indicator off the ends of the viewport.
	IndicatorMargin driver.Points = 2
)
View Source
const (
	// Thickness is the thumb's, Inset its distance from the edge.
	Thickness driver.Points = 6
	Inset     driver.Points = 3
	// MinLength is the shortest a thumb gets on long content.
	MinLength driver.Points = 24
	// Grab is the width of the strip along the edge where a press takes
	// the bar.
	Grab driver.Points = 12
)
View Source
const TouchSlop driver.Points = 10

TouchSlop is how far a finger moves before it drags a scroll view.

View Source
const WheelIdle = 150 * time.Millisecond

WheelIdle is how long after its last turn a mouse wheel has settled.

View Source
const WheelSnap = 0.6

WheelSnap is the speed (points per millisecond) a settled wheel snaps with: fast enough to move on to the next snap point that way, as NSH's desktops do.

Variables

This section is empty.

Functions

func Along

Along returns p's coordinate along the view's axis.

func Bounce

func Bounce(velocity float32, t time.Duration) (driver.Points, bool)

Bounce returns how far past an edge content that hit it at velocity is t later, as macOS's and iOS's glides bounce: a critically damped spring that starts at that velocity and comes back to the edge. It reports whether the content is back.

func Clamp

func Clamp(c *driver.ScrollConfig, frame, content driver.Size, offset driver.Point) driver.Point

Clamp returns offset within the offsets a scroll view of size frame showing content takes: from minus its leading content inset to the end of the content and its trailing inset.

func ClampRefreshing added in v0.2.0

func ClampRefreshing(c *driver.ScrollConfig, frame, content driver.Size, offset driver.Point, refreshing bool) driver.Point

ClampRefreshing clamps a resting offset after a configuration or layout change, retaining the refresh room. Explicit scroll commands and reports to the engine use Clamp's ordinary content bounds instead.

func ClampZoomOffset

func ClampZoomOffset(c *driver.ScrollConfig, frame, content driver.Size, p driver.Point, z float64) driver.Point

ClampZoomOffset clamps offset p to both axes' limits at zoom z.

func ContentAt

func ContentAt(offset, focus driver.Point, z float64) driver.Point

ContentAt returns the unzoomed content point at viewport point focus, given offset and zoom z.

func Dragged

func Dragged(raw, lo, hi, dim driver.Points, bounces bool) driver.Points

Dragged returns where a gesture that took the offset to raw puts it, with lo and hi the offsets it rests within, in a viewport dim long: past them it rubber-bands when bounces, and stops otherwise.

func EaseOut

func EaseOut(t float64) float64

EaseOut returns the cubic ease out at t, from 0 to 1.

func Indicator

func Indicator(viewport, total, offset driver.Points) (start, length driver.Points, ok bool)

Indicator returns where the indicator starts along a viewport of that length showing total points from offset (0 at the start of total) and how long it is, or false when everything fits. Pulled past an edge, it shrinks, as on iOS.

func IndicatorAlpha

func IndicatorAlpha(since time.Duration) float32

IndicatorAlpha returns the indicator's opacity since it last showed.

func KeepStart

func KeepStart(old, inset driver.Edges, offset driver.Point) driver.Point

KeepStart returns where a view at offset goes when its content inset changes from old to inset: one resting at its start stays at the start, showing the inset, as on NSH's other backends.

func LimitZoom

func LimitZoom(c *driver.ScrollConfig, scale float64, rubber bool) float64

LimitZoom keeps scale within c's zoom limits, rubber-banding past them while rubber is true unless NoBouncesZoom is set.

func Limits

func Limits(c *driver.ScrollConfig, frame, content driver.Size, refreshing bool) (lo, hi driver.Points)

Limits returns the offsets along its axis a view of size frame showing content rests within: from its leading content inset to the end of the content and its trailing inset, with the refresh room above the content while it refreshes.

func OffsetFor

func OffsetFor(content, focus driver.Point, z float64) driver.Point

OffsetFor returns the offset that places unzoomed content point c at viewport point focus at zoom z.

func Pulled

func Pulled(c *driver.ScrollConfig, offset driver.Point) driver.Points

Pulled returns how far offset is pulled past the top of a vertical view: what values bound to its pull follow.

func RebaseOffset

func RebaseOffset(config *driver.ScrollConfig, offset, delta driver.Point, moving *OffsetAnimation, drag *Drag) driver.Point

RebaseOffset carries a scroll or thumb drag along with an unclamped ItemScrollBy, so a change above the viewport preserves the content in view.

func Refreshes

func Refreshes(c *driver.ScrollConfig, offset driver.Point, refreshing bool) bool

Refreshes reports whether a view let go at offset refreshes: it is refreshable, not refreshing yet, and pulled past its threshold.

func Report

func Report(sink EventSink, event driver.Event, frameWanted bool)

Report shares the next display frame's engine pass when one is pending.

func Rest

func Rest(animator *driver.Animator, id driver.NodeID, report, startClock func())

Rest reports a scroll's end before starting the snapping diff clamps that follow it. The clock runs only while their animator has work.

func RestInset added in v0.2.0

func RestInset(c *driver.ScrollConfig, refreshing bool) driver.Edges

RestInset is the leading inset used to keep a resting view at its start across configuration changes, including the room held by a local refresh that the engine may not have confirmed yet.

func RevealDelta

func RevealDelta(start, end, viewport driver.Points) driver.Points

RevealDelta returns how far a scroll view moves along its axis so that the span from start to end, in its viewport, shows within the viewport with a margin; the start wins when the span does not fit.

func Rubber

func Rubber(over, dim driver.Points) driver.Points

Rubber returns how far content dragged over points past an edge of a viewport dim long moves: iOS's rubber band.

func SetAlong

func SetAlong(c *driver.ScrollConfig, p *driver.Point, v driver.Points)

SetAlong sets p's coordinate along the view's axis to v.

func Settle

func Settle(c *driver.ScrollConfig, frame, content driver.Size, offset driver.Point, velocity float32) (driver.Point, bool)

Settle returns where a snapping or paging scroll view at offset comes to rest when the user lets it go at velocity (points per millisecond along its axis), and false when it does not snap.

func SnapDuration

func SnapDuration(distance driver.Points, velocity float32) time.Duration

SnapDuration returns how long an eased move by distance takes when it starts at velocity: ease out starts at three times its mean speed, which then matches the release, within 180 to 600 ms; a move against the release takes Animation. Nothing to move takes no time.

func WheelZoom

func WheelZoom(current, deltaY float64) float64

WheelZoom scales current by a Ctrl+wheel delta in points (positive delta zooms out, negative delta zooms in, matching web's wheel zoom).

func ZoomAxisLimits

func ZoomAxisLimits(c *driver.ScrollConfig, frame, content driver.Size, z float64, horizontal bool) (lo, hi driver.Points)

ZoomAxisLimits returns the offset limits along one axis for a zooming scroll view of viewport frame and unzoomed content size at zoom z. When CenterContent is set and the zoomed content plus insets fits inside the viewport, lo and hi both equal the offset that centers it.

Types

type Bar

type Bar struct {
	Horizontal bool
	// RTL places a vertical scroll bar on the left edge instead of the right.
	RTL bool
	// Frame is the scroll view's size, Content its content's and Offset
	// how far the content is scrolled.
	Frame, Content driver.Size
	Offset         driver.Point
	// Inset is the content inset, which scrolls as part of the content.
	Inset driver.Edges
}

Bar is a scroll view's scroll bar.

func (Bar) Contains

func (b Bar) Contains(p driver.Point) bool

Contains reports whether p, in the scroll view, is on the bar: in the strip along its edge, while there is something to scroll.

func (Bar) Indicator

func (b Bar) Indicator() (rect driver.Bounds, travel driver.Points, ok bool)

Indicator returns the overlay indicator's rectangle in the scroll view, how far it travels along its track, and false when the content fits. It is macOS's: IndicatorMargin at each end, no shorter than indicatorMin, and shrinking while the content is pulled past an edge.

func (Bar) Press

func (b Bar) Press(p driver.Point) Drag

Press starts dragging the bar from p, in the scroll view. Off the thumb, the thumb first jumps to center under p.

func (Bar) PressIndicator

func (b Bar) PressIndicator(p driver.Point) Drag

PressIndicator starts dragging the overlay indicator of Indicator from p, in the scroll view. Off the thumb, the thumb first jumps to center under p.

func (Bar) Thumb

func (b Bar) Thumb() (driver.Bounds, bool)

Thumb returns the thumb's rectangle in the scroll view, or false when the content fits and there is nothing to scroll.

type Drag

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

Drag follows the pointer pressing a bar.

func (Drag) Offset

func (d Drag) Offset(p driver.Point) driver.Point

Offset returns where the content scrolls to with the pointer at p, in the scroll view, clamped to what it can show.

func (*Drag) Shift

func (d *Drag) Shift(by driver.Points)

Shift moves the drag along with the core moving the offset by d along the axis (ItemScrollBy), so it continues from the content it was on.

type EventSink

type EventSink interface {
	Dispatch(driver.Event)
	DispatchBeforeFrame(driver.Event)
}

EventSink queues scroll reports either on their own or with a frame already requested by the app.

type InterruptHost

type InterruptHost struct {
	Momentum       bool
	ClearAnimation func()
	Gliding        bool
	ClearGlide     func()
	Event          func(driver.EventKind)
	AfterGlide     func()
}

InterruptHost ends scroll animation and glide state before a new input. Native wheel ownership and refresh indicators stay in the callbacks.

func (InterruptHost) Interrupt

func (host InterruptHost) Interrupt()

Interrupt preserves the order of momentum reports and native cleanup.

type Motion

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

Motion is where a released view goes along its axis: an eased move to a point, or a glide that slows by the view's deceleration.

func Release

func Release(c *driver.ScrollConfig, frame, content driver.Size, offset driver.Point, velocity float32, lo, hi driver.Points, now time.Time) Motion

Release returns how a view of size frame showing content, let go at offset with velocity, comes to rest: back to its edge when past one, to its snap point, or gliding. lo and hi are Limits'.

func (*Motion) At

func (m *Motion) At(now time.Time) (driver.Points, bool)

At returns where the view is at now, and whether it came to rest. A glide bounces at the edge it reaches, or stops there when the view does not bounce.

func (*Motion) Moves

func (m *Motion) Moves() bool

Moves reports whether the motion goes anywhere.

type OffsetAnimation

type OffsetAnimation struct {
	From, To driver.Point
	Start    time.Time
	Momentum bool
}

OffsetAnimation is a scroll toward an offset. A user snap ends momentum when it finishes. The toolkit chooses the timing curve.

type Resampler

type Resampler struct {

	// Interval is the time between events, smoothed.
	Interval time.Duration
	// contains filtered or unexported fields
}

Resampler shows where trackpad events put an offset in step with the display. Events come at about the display's rate but not in step with it: applied as they come, some frames move twice and some not at all, and a steady scroll judders. Each event records the offset at its time; each frame shows the path one event interval back, interpolated, so every frame moves by what that interval did.

func (*Resampler) Active

func (r *Resampler) Active() bool

Active reports whether events wait to be shown.

func (*Resampler) Goal

func (r *Resampler) Goal() (driver.Points, bool)

Goal returns where the newest event put the offset.

func (*Resampler) Push

func (r *Resampler) Push(at time.Duration, pos, shown driver.Points)

Push records that an event at at put the offset at pos; shown is the offset showing, where the motion starts an interval before the first event.

func (*Resampler) Reset

func (r *Resampler) Reset()

Reset forgets the events.

func (*Resampler) Sample

func (r *Resampler) Sample(t time.Duration) (driver.Points, bool)

Sample returns the offset at t, dropping the events before it, and whether t is past the newest event.

func (*Resampler) Shift

func (r *Resampler) Shift(d driver.Points)

Shift moves the recorded offsets by d, as the core moves the content.

type SettleHost

type SettleHost struct {
	Scroll  func(driver.Point)
	Animate func(driver.Point)
	Event   func(driver.EventKind)
	Rest    func()
}

SettleHost applies a snap through the backend's scrolling and animation paths. The callbacks run synchronously in the order the scroll reports.

func (SettleHost) Settle

func (h SettleHost) Settle(c *driver.ScrollConfig, frame, content driver.Size, offset driver.Point, velocity float32, reduceMotion bool)

Settle snaps a released view or reports its rest. The backend owns the animation curve and reports momentum's end when that animation finishes.

type StickyBindings

type StickyBindings[V comparable] map[V]driver.NodeID

StickyBindings tracks scroll views with sticky cells whose insets are animated. Tracking the view as well as its ID detects replacement views.

func (StickyBindings[V]) Repin

func (bound StickyBindings[V]) Repin(lookup func(driver.NodeID) V, pin func(V))

Repin replaces each tracked view's current shifts and drops views that no longer occupy their IDs.

func (*StickyBindings[V]) Replace

func (bound *StickyBindings[V]) Replace(v V, id driver.NodeID, current *StickyCells, cells StickyCells, place func(driver.NodeID, driver.Point), pin func(V))

Replace restores the previous cells, tracks the new bindings and pins the new cells.

type StickyCells

type StickyCells []driver.StickyCell

StickyCells holds the cells pinned by one scroll view.

func (StickyCells) Pin

func (cells StickyCells) Pin(offset driver.Point, horizontal bool, animator *driver.Animator, place func(driver.NodeID, driver.Point))

Pin places the cells along the scroll view's main axis at offset.

type TouchDrag

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

TouchDrag follows a finger on a scroll view from where it went down.

func NewTouchDrag

func NewTouchDrag(at driver.Point, from driver.Points, now time.Time) *TouchDrag

NewTouchDrag starts following a finger down at at on a view whose offset along its axis is from.

func (*TouchDrag) Claimed

func (d *TouchDrag) Claimed() bool

Claimed reports whether the drag took the finger.

func (*TouchDrag) Claims

func (d *TouchDrag) Claims(c *driver.ScrollConfig, at driver.Point) bool

Claims reports whether a finger now at at drags the view: moved past TouchSlop along its axis, and more along it than across. Once it does, the drag starts from where the finger is, so the content does not jump.

func (*TouchDrag) Move

Move records the finger at at and returns the offset it asks along the axis, before any rubber band.

func (*TouchDrag) Refreshes added in v0.2.0

func (d *TouchDrag) Refreshes(c *driver.ScrollConfig, refreshing bool) bool

Refreshes reports whether releasing the finger refreshes, using the pull before rubber banding so the threshold does not depend on the viewport's size or whether the content bounces.

func (*TouchDrag) Velocity

func (d *TouchDrag) Velocity(c *driver.ScrollConfig) float32

Velocity returns the offset's speed along the axis as the finger lifts, in points per millisecond: the finger's, reversed.

type TouchHost

type TouchHost[V comparable] struct {
	State     func(V) TouchView
	Interrupt func(V)
	Begin     func(V, bool)
	Move      func(V, driver.Point)
	Show      func(V)
	Blocked   bool
}

TouchHost keeps native press cancellation, scrolling and indicators in the backend while TouchSession owns which scroll view takes a finger.

type TouchSession

type TouchSession[V comparable] struct {
	Taken  V
	Drag   *TouchDrag
	Braked bool
	// contains filtered or unexported fields
}

TouchSession follows the scroll views under one finger, innermost first.

func NewTouchSession

func NewTouchSession[V comparable](under []V, at driver.Point, now time.Time, host TouchHost[V]) *TouchSession[V]

NewTouchSession brakes ongoing motion and skips views that cannot scroll. under is the outermost-first hit-test result.

func (*TouchSession[V]) Move

func (session *TouchSession[V]) Move(at driver.Point, now time.Time, host TouchHost[V]) (handled, keep bool)

Move returns whether the finger was handled and whether to keep its session. Braking makes the finger the scroll view's immediately; otherwise a pan may win before the drag crosses its slop.

type TouchView

type TouchView struct {
	Config          *driver.ScrollConfig
	Offset          driver.Point
	Frame, Content  driver.Size
	Deleted, Moving bool
	Refreshing      bool
}

TouchView is the state needed to arbitrate and move a desktop touch drag.

type Velocity

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

Velocity follows a pointer's positions for its speed over the last 100 ms.

func (*Velocity) Add

func (v *Velocity) Add(at time.Duration, p driver.Point)

Add records that the pointer is at p at time at.

func (*Velocity) Reset

func (v *Velocity) Reset()

Reset forgets the positions.

func (*Velocity) Speed

func (v *Velocity) Speed() (x, y float32)

Speed returns the pointer's velocity in points per millisecond, zero after a pause.

type Wheel

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

Wheel follows the wheel turns on a snapping scroll view until they settle.

func (*Wheel) Settled

func (w *Wheel) Settled(now time.Time) (velocity float32, ok bool)

Settled reports whether the turns stopped by now; it then forgets them, and returns the speed to snap with in the wheel's direction.

func (*Wheel) Turn

func (w *Wheel) Turn(now time.Time, d driver.Points)

Turn records a turn moving the offset by d along the view's axis.

func (*Wheel) Turning

func (w *Wheel) Turning() bool

Turning reports whether turns came within WheelIdle of now.

type ZoomEase

type ZoomEase struct {
	From, To, Focus driver.Point
	Scale, Target   float64
	Clamped         bool
}

ZoomEase interpolates a scroll view's zoom from Scale to Target while keeping unzoomed content point moving from From to To at viewport Focus.

func NewZoomEase

func NewZoomEase(c *driver.ScrollConfig, frame, content driver.Size, offset driver.Point, zoom float64, targetContent, focus driver.Point, targetZoom float64) (ZoomEase, driver.Point, bool)

NewZoomEase builds a ZoomEase from current (offset, zoom) to targetZoom with content point c placed at viewport focus. It returns ok=false when the view is already within half a point and 1e-3 scale of the target.

func (ZoomEase) Step

func (e ZoomEase) Step(c *driver.ScrollConfig, frame, content driver.Size, k float64) (driver.Point, float64)

Step returns the offset and zoom at eased progress k in [0, 1].

Jump to

Keyboard shortcuts

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