Scrolling
Scroll content that is taller or wider than its space with Scroll, move it with keys and the mouse wheel, and show scrollbars that can be dragged and clicked.
bloom has three ways to show more than fits:
Scrollshows any child from an offset. It lays out and draws all of the child on every frame, which is fine for an article, a help screen, or a form, but not for thousands of lines.- A viewport shows the lines of a text buffer, laying out only those in sight, for logs, files, and other long text.
- A list shows rows of components, building only those in view, for thousands of items.
All three work with the same scrollbars.
Scroll content
bloom.Scroll(child, offset) gives a child as much height as it wants, and shows it starting offset rows down,
clipped to the space the scroll has. The offset is model state, so you decide how keys and the mouse wheel move
it:
type model struct {
ui *bloom.Root
offset int // rows scrolled
// ...
}
func (m *model) layout() bloom.Child {
return bloom.Box(panel, bloom.Scroll(bloom.Text(m.article).Style(prose), m.offset))
}To move the offset with keys, and to clamp it so it doesn't scroll past the end, Update needs to know how tall
the scroll and its content are. Sizes and positions in Update shows how, with paging by
a screen at a time.
Scroll with the mouse wheel
Put an on.Scroll handler on the node the scroll is in,
so the wheel only scrolls what it's over. dy is 1 for each step down and -1 for each step up:
reader := bloom.Fill(bloom.Box(panel, bloom.Scroll(content, m.offset))).
ID(paneReader).
On(on.Scroll(func(_, dy int) tea.Cmd { m.offset += dy * 3; return nil }))The handler runs just before Update gets the wheel event, so clamp the offset in Update as you do for
keys.
Scroll sideways
X(offset) scrolls a Scroll sideways too: its child gets as much width as it wants, so long lines don't wrap,
and is shown from column offset. It's what you want for wide tables and code:
bloom.Scroll(bloom.Text(m.log).ID(paneLog), m.offset).X(m.logX)Without it, the child gets the scroll's width, as before.
Show a scrollbar
bloom.ScrollbarOf(id) is a scrollbar for the Scroll that the node with that ID is in, or
for the list or viewport with that ID. It follows the scroll
from the layout, so it needs no state of its own. Put it on the right side of the box's border with
Edge, and it takes no space. It's only drawn while the
content is taller than its view, so when everything fits, the plain border shows:
bloom.Box(panel, bloom.Scroll(bloom.Text(m.article).Style(prose).ID(paneArticle), m.offset)).
Edge(bloom.Right, bloom.Stretch, bloom.ScrollbarOf(paneArticle))╭──────────────╮
│there was a │
│border, and ┃
│the border was│
╰──────────────╯ScrollbarOf a list's own ID follows it in items, and its OnScroll gets the
first item to show, for Offset. ScrollbarOf a viewport's ID follows it in rows, and
without an OnScroll of its own, scrolls it with the viewport's.
For content that a component windows itself, such as the Bubbles viewport, give
bloom.Scrollbar(total, visible, offset) the numbers yourself:
bloom.Scrollbar(m.viewport.TotalLineCount(), m.viewport.VisibleLineCount(), m.viewport.YOffset())Horizontal() makes either one a row tall, for content scrolled sideways. Put it on the bottom of the border:
bloom.Box(panel, bloom.Scroll(bloom.Text(m.log).ID(paneLog), 0).X(m.logX)).
Edge(bloom.Bottom, bloom.Stretch, bloom.ScrollbarOf(paneLog).Horizontal())Either one can also go beside the content, in a Row, or under it in a Column when horizontal, where it takes a
column or a row. A scrollbar's natural length is zero, so on a border use Stretch.
Style a scrollbar
Style sets how a scrollbar looks, with a bloom.ScrollbarStyle: the glyph and style of its thumb, the part for
what's in view, and of its track, the rest. Leave out what you don't change. An empty thumb glyph is "┃", or "━"
when horizontal, and an empty track draws nothing, so a border beneath it shows through:
bloom.ScrollbarOf(paneArticle).Style(bloom.ScrollbarStyle{
ThumbStyle: lipgloss.NewStyle().Foreground(accent),
})Drag and click a scrollbar
OnScroll(fn) makes a scrollbar work with the mouse. fn gets the offset to scroll to, which you set in your
model as you do for keys:
bloom.ScrollbarOf(paneArticle).OnScroll(func(offset int) tea.Cmd {
m.offset = offset
return nil
})Pressing on the thumb and moving drags it, keeping the point you grabbed under the pointer. Pressing anywhere else on the scrollbar jumps so the thumb is centered there, and holding the button down keeps dragging. The scrollbar takes clicks only while it's drawn, so with nothing to scroll, clicks reach whatever is beneath it.
Change under the pointer
HoverStyle sets how a scrollbar looks while the pointer is over it or it's being dragged. Without one it looks the
same. The hover style replaces Style whole, so repeat anything you want kept. Setting the thumb glyph to "█" grows a vertical thumb, to show the scrollbar
works with the mouse:
bloom.ScrollbarOf(paneArticle).OnScroll(scroll).
Style(bloom.ScrollbarStyle{ThumbStyle: dim}).
HoverStyle(bloom.ScrollbarStyle{Thumb: "█", ThumbStyle: dim})A horizontal thumb has no glyph that grows as evenly from the middle of a cell, so light it up instead, which works for either direction:
bloom.ScrollbarOf(paneArticle).OnScroll(scroll).
Style(bloom.ScrollbarStyle{ThumbStyle: dim}).
HoverStyle(bloom.ScrollbarStyle{ThumbStyle: bright, Track: "│", TrackStyle: dim})The hover style is used whole, so repeat any glyph or style of Style you want kept. It needs no more than the
OnScroll (or the viewport's, for ScrollbarOf a viewport): the view from m.ui.View then reports mouse motion with
no button down, to follow the pointer.
A scrollbar with no OnScroll takes no mouse, so it doesn't change.
Select text in a scroll
A selectable node inside a Scroll takes the text scrolled out of sight too, and the selection stays on its text as
it scrolls. Dragging past the scroll's edge scrolls it, through the on.Scroll handler on it. See
Selecting and copying text.
Related
ScrollNode,ScrollbarNode, andScrollbarStyleAPI reference- Viewports, for long text
- Long lists, for rows of components
- Sizes and positions in Update, for paging and clamping
- Mouse events
Last updated on
FIGlet text
Show large text drawn from ordinary characters in FIGlet and TOIlet fonts, color it, step down to smaller fonts as space runs short, and animate it letter by letter.
Long lists
Show thousands of rows of components with List, building only those in view, keep a cursor in view, page by what fit, and scroll with the wheel and a scrollbar.