Questions about viewport #86
Any help is much appreciated |
Replies: 4 comments 7 replies
|
Hi! To help answer you we'll need a little more context. Can you link to the relevant parts in your source code? |
|
Okay, so for the viewport:
|
|
Viewport scrolling in Bubble Tea has a few subtleties worth knowing about: The import "github.com/charmbracelet/bubbles/viewport"
// In your model
type Model struct {
viewport viewport.Model
content string
}
// In Init
func (m Model) Init() tea.Cmd {
m.viewport = viewport.New(width, height)
m.viewport.SetContent(m.content)
return nil
}
// In Update - pass KeyMsg to viewport
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)
return m, cmd
}
return m, nil
}Common viewport issues: Size initialization timing: The viewport needs to know its size at init time, but the terminal size is typically received via a Content updates: After calling Scrollbar rendering: The viewport's Mouse scrolling: Enable via What specific scrolling behavior are you seeing issues with? |
|
The viewport model in Bubbletea has a few behaviors that trip people up. A few clarifications: Viewport height vs content height vp := viewport.New(80, 24) // display: 24 lines
vp.SetContent(longContent) // content: might be 1000 lines
vp.GotoBottom() // scroll to bottomUpdate loop integration func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - headerHeight
case tea.KeyMsg:
m.viewport, cmd = m.viewport.Update(msg)
}
return m, cmd
}Setting content dynamically AtTop/AtBottom helpers What's the specific behavior you're trying to achieve — a scrollable log view, a pageable document, or something else? |
Okay, so for the viewport:
viewport.SetContentmutates the viewport model, and thus it should only be used in anUpdate. In this case viewport is being altered updated in yourViewfunction and is almost certainly getting confused about its contents.