Add scroll bar to TextView

Resolves #34.
This commit is contained in:
Trevor Slocum 2020-09-22 18:43:19 -07:00
parent 0974659c81
commit 441992fd77
3 changed files with 61 additions and 8 deletions

View File

@ -1,3 +1,6 @@
v1.5.0 (WIP)
- Add scroll bar to TextView
v1.4.9 (2020-09-08)
- Add InputField.GetCursorPosition and InputField.SetCursorPosition
- Add Table.Sort, Table.SetSortFunc and Table.SetSortClicked

7
doc.go
View File

@ -169,6 +169,13 @@ When primitives are instantiated, they are initialized with colors taken from
the global Styles variable. You may change this variable to adapt the look and
feel of the primitives to your preferred style.
Scroll Bars
Scroll bars are supported by the following widgets: List, Table, TextView and
TreeView. Each widget will display scroll bars automatically when there are
additional items off-screen by default, except TextView. See
Widget.SetScrollBarColor and Widget.SetScrollBarVisibility.
Hello World
The following is an example application which shows a box titled "Greetings"

View File

@ -155,6 +155,12 @@ type TextView struct {
// navigated when the text is longer than what fits into the box.
scrollable bool
// Visibility of the scroll bar.
scrollBarVisibility ScrollBarVisibility
// The scroll bar color.
scrollBarColor tcell.Color
// If set to true, lines that are longer than the available width are wrapped
// onto the next line. If set to false, any characters beyond the available
// width are discarded.
@ -200,14 +206,15 @@ type TextView struct {
// NewTextView returns a new text view.
func NewTextView() *TextView {
return &TextView{
Box: NewBox(),
highlights: make(map[string]struct{}),
lineOffset: -1,
reindex: true,
scrollable: true,
align: AlignLeft,
wrap: true,
textColor: Styles.PrimaryTextColor,
Box: NewBox(),
highlights: make(map[string]struct{}),
lineOffset: -1,
reindex: true,
scrollable: true,
align: AlignLeft,
wrap: true,
textColor: Styles.PrimaryTextColor,
scrollBarColor: Styles.ScrollBarColor,
}
}
@ -225,6 +232,24 @@ func (t *TextView) SetScrollable(scrollable bool) *TextView {
return t
}
// SetScrollBarVisibility specifies the display of the scroll bar.
func (t *TextView) SetScrollBarVisibility(visibility ScrollBarVisibility) *TextView {
t.Lock()
defer t.Unlock()
t.scrollBarVisibility = visibility
return t
}
// SetScrollBarColor sets the color of the scroll bar.
func (t *TextView) SetScrollBarColor(color tcell.Color) *TextView {
t.Lock()
defer t.Unlock()
t.scrollBarColor = color
return t
}
// SetWrap sets the flag that, if true, leads to lines that are longer than the
// available width being wrapped onto the next line. If false, any characters
// beyond the available width are not displayed.
@ -968,10 +993,20 @@ func (t *TextView) Draw(screen tcell.Screen) {
x, y, width, height := t.GetInnerRect()
t.pageSize = height
t.reindexBuffer(width)
showVerticalScrollBar := t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && len(t.index) >= height)
if showVerticalScrollBar {
width-- // Subtract space for scroll bar.
if t.wrap {
t.index = nil
}
}
// If the width has changed, we need to reindex.
if width != t.lastWidth && t.wrap {
t.index = nil
}
t.lastWidth = width
// Re-index.
@ -1186,6 +1221,14 @@ func (t *TextView) Draw(screen tcell.Screen) {
}
}
// Draw scroll bar.
if showVerticalScrollBar {
cursor := int(float64(len(t.index)) * (float64(t.lineOffset) / float64(len(t.index)-height)))
for printed := 0; printed < height; printed++ {
RenderScrollBar(screen, t.scrollBarVisibility, x+width, y+printed, height, len(t.index), cursor, printed, t.hasFocus, t.scrollBarColor)
}
}
// If this view is not scrollable, we'll purge the buffer of lines that have
// scrolled out of view.
if !t.scrollable && t.lineOffset > 0 {