From 74844d6d3f062d055aa18cdaf0ef253da43dd2cd Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Mon, 24 Feb 2020 07:54:34 -0800 Subject: [PATCH] Fix applying ScrollBarAlways to List --- demos/progressbar/main.go | 1 + list.go | 14 ++++++++------ table.go | 2 +- treeview.go | 4 +--- util.go | 10 +++++++--- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/demos/progressbar/main.go b/demos/progressbar/main.go index 0a9d4a6..2e15f93 100644 --- a/demos/progressbar/main.go +++ b/demos/progressbar/main.go @@ -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) diff --git a/list.go b/list.go index b77e24b..6b5f42e 100644 --- a/list.go +++ b/list.go @@ -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. diff --git a/table.go b/table.go index a7c5ca0..cefe6ef 100644 --- a/table.go +++ b/table.go @@ -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) } } diff --git a/treeview.go b/treeview.go index 709f993..2fe5eba 100644 --- a/treeview.go +++ b/treeview.go @@ -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++ diff --git a/util.go b/util.go index 702a4fe..e1d704e 100644 --- a/util.go +++ b/util.go @@ -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