Browse Source

Add option to always show scroll bar

merge-requests/1/merge
Trevor Slocum 2 years ago
parent
commit
c6f9bcda29
  1. 1
      CHANGELOG
  2. 2
      go.mod
  3. 5
      go.sum
  4. 17
      list.go
  5. 53
      table.go
  6. 23
      treeview.go
  7. 14
      util.go

1
CHANGELOG

@ -1,5 +1,6 @@
v1.4.3 (WIP)
- Add SetFocusedFunc to TreeNode
- Add option to always show scroll bar
- Do not wrap around list by default
v1.4.2 (2020-02-02)

2
go.mod

@ -7,6 +7,6 @@ require (
github.com/lucasb-eyer/go-colorful v1.0.3
github.com/mattn/go-runewidth v0.0.8
github.com/rivo/uniseg v0.1.0
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect
golang.org/x/text v0.3.2 // indirect
)

5
go.sum

@ -16,10 +16,11 @@ github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 h1:sfkvUWPNGwSV+8/fNqctR5lS2AqCSqYwXdrjCxp/dXo=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

17
list.go

@ -42,8 +42,8 @@ type List struct {
// The text color for selected items.
selectedTextColor tcell.Color
// Whether or not to render a scroll bar.
showScrollBar bool
// Visibility of the scroll bar.
scrollBarVisibility ScrollBarVisibility
// The scroll bar color.
scrollBarColor tcell.Color
@ -80,7 +80,7 @@ func NewList() *List {
return &List{
Box: NewBox(),
showSecondaryText: true,
showScrollBar: true,
scrollBarVisibility: ScrollBarAuto,
mainTextColor: Styles.PrimaryTextColor,
secondaryTextColor: Styles.TertiaryTextColor,
shortcutColor: Styles.SecondaryTextColor,
@ -223,10 +223,9 @@ func (l *List) ShowSecondaryText(show bool) *List {
return l
}
// ShowScrollBar determines whether or not to render a scroll bar when there
// are additional items offscreen.
func (l *List) ShowScrollBar(show bool) *List {
l.showScrollBar = show
// SetScrollBarVisibility specifies the display of the scroll bar.
func (l *List) SetScrollBarVisibility(visibility ScrollBarVisibility) *List {
l.scrollBarVisibility = visibility
return l
}
@ -489,7 +488,7 @@ func (l *List) Draw(screen tcell.Screen) {
}
}
if l.showScrollBar {
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)
}
@ -503,7 +502,7 @@ func (l *List) Draw(screen tcell.Screen) {
if l.showSecondaryText {
Print(screen, item.SecondaryText, x, y, width, AlignLeft, l.secondaryTextColor)
if l.showScrollBar {
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)
}

53
table.go

@ -251,8 +251,8 @@ type Table struct {
// The number of visible rows the last time the table was drawn.
visibleRows int
// Whether or not to render a scroll bar.
showScrollBar bool
// Visibility of the scroll bar.
scrollBarVisibility ScrollBarVisibility
// The scroll bar color.
scrollBarColor tcell.Color
@ -279,12 +279,12 @@ type Table struct {
// NewTable returns a new table.
func NewTable() *Table {
return &Table{
Box: NewBox(),
showScrollBar: true,
scrollBarColor: Styles.ScrollBarColor,
bordersColor: Styles.GraphicsColor,
separator: ' ',
lastColumn: -1,
Box: NewBox(),
scrollBarVisibility: ScrollBarAuto,
scrollBarColor: Styles.ScrollBarColor,
bordersColor: Styles.GraphicsColor,
separator: ' ',
lastColumn: -1,
}
}
@ -308,10 +308,9 @@ func (t *Table) SetBordersColor(color tcell.Color) *Table {
return t
}
// ShowScrollBar determines whether or not to render a scroll bar when there
// are additional rows and/or columns offscreen.
func (t *Table) ShowScrollBar(show bool) *Table {
t.showScrollBar = show
// SetScrollBarVisibility specifies the display of the scroll bar.
func (t *Table) SetScrollBarVisibility(visibility ScrollBarVisibility) *Table {
t.scrollBarVisibility = visibility
return t
}
@ -591,7 +590,7 @@ func (t *Table) Draw(screen tcell.Screen) {
t.visibleRows = height
}
showVerticalScrollBar := t.showScrollBar && len(t.cells) > height
showVerticalScrollBar := t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && len(t.cells) > t.visibleRows-t.fixedRows)
if showVerticalScrollBar {
width-- // Subtract space for scroll bar.
}
@ -792,6 +791,7 @@ ColumnLoop:
toDistribute -= expWidth
expansionTotal -= expansion
}
tableWidth = width - toDistribute
}
// Helper function which draws border runes.
@ -888,13 +888,32 @@ ColumnLoop:
}
if showVerticalScrollBar {
// Calculate scroll bar position.
// Calculate scroll bar position and dimensions.
rows := len(t.cells)
cursor := int(float64(rows-t.fixedRows) * (float64(t.rowOffset) / float64(((rows-t.fixedRows)-t.visibleRows)+1)))
scrollBarItems := rows - t.fixedRows
scrollBarHeight := t.visibleRows - t.fixedRows
scrollBarX := x + width
scrollBarY := y + t.fixedRows
if scrollBarX > x+tableWidth {
scrollBarX = x + tableWidth
}
padTotalOffset := 1
if t.borders {
padTotalOffset = 2
scrollBarItems *= 2
scrollBarHeight = (scrollBarHeight * 2) - 1
scrollBarY += t.fixedRows + 1
}
// Draw scroll bar.
for printed := 0; printed < (t.visibleRows - t.fixedRows); printed++ {
RenderScrollBar(screen, x+width, y+t.fixedRows+printed, t.visibleRows-t.fixedRows, rows-t.fixedRows, cursor, printed, t.hasFocus, t.scrollBarColor)
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)
}
}

23
treeview.go

@ -282,8 +282,8 @@ type TreeView struct {
// The color of the lines.
graphicsColor tcell.Color
// Whether or not to render a scroll bar.
showScrollBar bool
// Visibility of the scroll bar.
scrollBarVisibility ScrollBarVisibility
// The scroll bar color.
scrollBarColor tcell.Color
@ -304,11 +304,11 @@ type TreeView struct {
// NewTreeView returns a new tree view.
func NewTreeView() *TreeView {
return &TreeView{
Box: NewBox(),
showScrollBar: true,
graphics: true,
graphicsColor: Styles.GraphicsColor,
scrollBarColor: Styles.ScrollBarColor,
Box: NewBox(),
scrollBarVisibility: ScrollBarAuto,
graphics: true,
graphicsColor: Styles.GraphicsColor,
scrollBarColor: Styles.ScrollBarColor,
}
}
@ -386,10 +386,9 @@ func (t *TreeView) SetGraphicsColor(color tcell.Color) *TreeView {
return t
}
// ShowScrollBar determines whether or not to render a scroll bar when there
// are additional nodes offscreen.
func (t *TreeView) ShowScrollBar(show bool) *TreeView {
t.showScrollBar = show
// SetScrollBarVisibility specifies the display of the scroll bar.
func (t *TreeView) SetScrollBarVisibility(visibility ScrollBarVisibility) *TreeView {
t.scrollBarVisibility = visibility
return t
}
@ -710,7 +709,7 @@ func (t *TreeView) Draw(screen tcell.Screen) {
}
// Draw scroll bar.
if t.showScrollBar {
if t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && rows > height) {
RenderScrollBar(screen, x+(width-1), posY, height, rows, cursor, posY-y, t.hasFocus, tcell.ColorWhite)
}

14
util.go

@ -631,6 +631,20 @@ func iterateStringReverse(text string, callback func(main rune, comb []rune, tex
return false
}
// ScrollBarVisibility specifies the display of a scroll bar.
type ScrollBarVisibility int
const (
// ScrollBarNever never shows a scroll bar.
ScrollBarNever ScrollBarVisibility = iota
// ScrollBarAuto shows a scroll bar when there are items offscreen.
ScrollBarAuto
// ScrollBarAlways always shows a scroll bar.
ScrollBarAlways
)
// 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.

Loading…
Cancel
Save