API 文档由 Go 源码生成,内容为英文。
type MessageScrollState struct {
// AutoScroll keeps the transcript at its end while the reader is at the end:
// the official autoScroll. Off, the offset only moves when told to.
AutoScroll bool
// PreviousPeek is how much of the item before an anchored turn stays visible
// above it; MessageScrollPreviousPeek is the official 64.
PreviousPeek float32
// content height including the room an anchored turn grows into
}
MessageScrollState is the scroll policy of a chat transcript, the part of the
official MessageScroller provider that is not a DOM observer: it follows the live
edge while the reader is there, anchors a new turn near the top of the viewport,
and keeps the reader's place when older rows are prepended. It owns one number, the
offset, and moves it only from what the caller tells it, so it needs no timer and
schedules no frame; the caller paints the transcript at Offset and asks for a
frame itself when its content changes.
The caller feeds it three kinds of input:
the reader's scrolling (ScrollBy, ScrollTo), which releases the live edge when
it moves away from it and takes hold of it again on reaching it;
commands (ScrollToEnd, ScrollToStart, AnchorTurn, Prepended);
the sizes of the viewport and the content, once per paint, in Resolve, which
returns the offset to paint at.
The zero value sits at the start, follows nothing and keeps no peek.
state := components.MessageScrollState{AutoScroll: true}
state.ScrollToEnd() // open at the newest message
offset := state.Resolve(viewport.Height, content) // every paint
state.ScrollBy(wheelDelta) // on a wheel event
方法
AnchorTurn#
func (state *MessageScrollState) AnchorTurn(top float32)
AnchorTurn puts the item whose top is at top (in content space) near the top of the
viewport, PreviousPeek below it, and holds it there while its reply streams in
below. With AutoScroll the transcript follows the end once the reply has grown to
fill the viewport; without it the turn stays where it is until the reader moves.
AtEnd#
func (state *MessageScrollState) AtEnd() bool
AtEnd reports whether the viewport is within MessageScrollerEdgeThreshold of the
end of the content as of the last Resolve.
Extent#
func (state *MessageScrollState) Extent() float32
Extent is the content height Resolve last settled on: the caller's, or more while a
turn is anchored, so that there is room below the anchor for the reply to grow into.
Give it to BeginMessageScroller as the content extent.
Following#
func (state *MessageScrollState) Following() bool
Following reports whether the transcript is pinned to its end and follows new content.
Offset#
func (state *MessageScrollState) Offset() float32
Offset is the offset resolved by the last Resolve, after the reader's scrolling since.
Prepended#
func (state *MessageScrollState) Prepended(height float32)
Prepended tells the state that height of content was inserted above what it was
showing, older history loaded while the reader looks at the transcript. The offset
moves by the same amount at the next Resolve so the rows on screen stay where they
are. A transcript that follows its end needs no correction.
Resolve#
func (state *MessageScrollState) Resolve(viewport, extent float32) float32
Resolve settles the offset for a viewport and content of the given heights, applying
the commands given since the last call, and returns it. Call it once per paint.
ScrollBy#
func (state *MessageScrollState) ScrollBy(delta float32)
ScrollBy moves the offset by delta, positive toward the end, as the reader's wheel,
keys or scrollbar do. Moving toward the start releases the live edge; reaching the
end takes hold of it again. Any reader scrolling releases an anchored turn.
ScrollTo#
func (state *MessageScrollState) ScrollTo(offset float32)
ScrollTo moves the offset to an absolute position the reader chose, such as a
dragged scrollbar thumb. It follows the same rules as ScrollBy.
ScrollToEnd#
func (state *MessageScrollState) ScrollToEnd()
ScrollToEnd jumps to the end at the next Resolve and, with AutoScroll, follows the
content from there: the jump button, a sent message, opening at the newest message.
ScrollToStart#
func (state *MessageScrollState) ScrollToStart()
ScrollToStart jumps to the start and releases the live edge.