Fix applying ScrollBarAlways to List

This commit is contained in:
Trevor Slocum 2020-02-24 07:54:34 -08:00
parent afcd7dcdab
commit 74844d6d3f
5 changed files with 18 additions and 13 deletions

View File

@ -11,6 +11,7 @@ func main() {
app := cview.NewApplication()
grid := cview.NewGrid().SetColumns(-1, 6, 4, 30, -1).SetRows(-1, 12, 4, 4, -1)
grid.SetBackgroundColor(cview.Styles.PrimitiveBackgroundColor)
verticalProgressBar := cview.NewProgressBar()
verticalProgressBar.SetBorder(true)

14
list.go
View File

@ -488,9 +488,7 @@ func (l *List) Draw(screen tcell.Screen) {
}
}
if l.scrollBarVisibility == ScrollBarAlways || (l.scrollBarVisibility == ScrollBarAuto && len(l.items) > scrollBarHeight) {
RenderScrollBar(screen, scrollBarX, y, scrollBarHeight, len(l.items), l.currentItem, index-l.offset, l.hasFocus, l.scrollBarColor)
}
RenderScrollBar(screen, l.scrollBarVisibility, scrollBarX, y, scrollBarHeight, len(l.items), l.currentItem, index-l.offset, l.hasFocus, l.scrollBarColor)
y++
@ -502,13 +500,17 @@ func (l *List) Draw(screen tcell.Screen) {
if l.showSecondaryText {
Print(screen, item.SecondaryText, x, y, width, AlignLeft, l.secondaryTextColor)
if l.scrollBarVisibility == ScrollBarAlways || (l.scrollBarVisibility == ScrollBarAuto && len(l.items) > scrollBarHeight) {
RenderScrollBar(screen, scrollBarX, y, scrollBarHeight, len(l.items), l.currentItem, index-l.offset, l.hasFocus, l.scrollBarColor)
}
RenderScrollBar(screen, l.scrollBarVisibility, scrollBarX, y, scrollBarHeight, len(l.items), l.currentItem, index-l.offset, l.hasFocus, l.scrollBarColor)
y++
}
}
// Overdraw scroll bar when necessary.
for y < bottomLimit {
RenderScrollBar(screen, l.scrollBarVisibility, scrollBarX, y, scrollBarHeight, len(l.items), l.currentItem, bottomLimit-y, l.hasFocus, l.scrollBarColor)
y++
}
}
// InputHandler returns the handler for this primitive.

View File

@ -913,7 +913,7 @@ ColumnLoop:
// Draw scroll bar.
cursor := int(float64(scrollBarItems) * (float64(t.rowOffset) / float64(((rows-t.fixedRows)-t.visibleRows)+padTotalOffset)))
for printed := 0; printed < scrollBarHeight; printed++ {
RenderScrollBar(screen, scrollBarX, scrollBarY+printed, scrollBarHeight, scrollBarItems, cursor, printed, t.hasFocus, t.scrollBarColor)
RenderScrollBar(screen, t.scrollBarVisibility, scrollBarX, scrollBarY+printed, scrollBarHeight, scrollBarItems, cursor, printed, t.hasFocus, t.scrollBarColor)
}
}

View File

@ -709,9 +709,7 @@ func (t *TreeView) Draw(screen tcell.Screen) {
}
// Draw scroll bar.
if t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && rows > height) {
RenderScrollBar(screen, x+(width-1), posY, height, rows, cursor, posY-y, t.hasFocus, tcell.ColorWhite)
}
RenderScrollBar(screen, t.scrollBarVisibility, x+(width-1), posY, height, rows, cursor, posY-y, t.hasFocus, tcell.ColorWhite)
// Advance.
posY++

10
util.go
View File

@ -646,12 +646,16 @@ const (
)
// RenderScrollBar renders a scroll bar character at the specified position.
func RenderScrollBar(screen tcell.Screen, x int, y int, height int, items int, cursor int, printed int, focused bool, color tcell.Color) {
// Do not render a scroll bar when all items are visible.
if items <= height {
func RenderScrollBar(screen tcell.Screen, visibility ScrollBarVisibility, x int, y int, height int, items int, cursor int, printed int, focused bool, color tcell.Color) {
if visibility == ScrollBarNever || (visibility == ScrollBarAuto && items <= height) {
return
}
// Place cursor at top when there are no items offscreen.
if items <= height {
cursor = 0
}
// Handle negative cursor.
if cursor < 0 {
cursor = 0