From 28e366bbce51860dab27f22ffd4acdc3ef3a11a3 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Wed, 21 Oct 2020 09:36:23 -0700 Subject: [PATCH] Display TextView scroll bar automatically by default --- CHANGELOG | 1 + demos/presentation/cover.go | 2 +- demos/presentation/textview.go | 4 ++-- demos/presentation/treeview.go | 2 +- doc.go | 3 +-- textview.go | 38 ++++++++++++++++++++-------------- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 32419be..79dcb7d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,6 +12,7 @@ v1.5.1 (WIP) - Allow modification of scroll bar render text - Allow scrolling List horizontally - Clarify that Table rows must each have the same number of columns +- Display TextView scroll bar automatically by default - Generalize tag stripping as StripTags - Make printWithStyle public and rename as PrintStyle - Optimize TextView (writing is 90% faster, drawing is 50% faster) diff --git a/demos/presentation/cover.go b/demos/presentation/cover.go index ecb39dc..952429d 100644 --- a/demos/presentation/cover.go +++ b/demos/presentation/cover.go @@ -11,7 +11,7 @@ import ( const logo = ` ======= === === === ======== === === === === === === === === === === === -=== === === === ====== === === === +=== === === === ====== === === === === ====== === === =========== ======= == === ======== ==== ==== ` diff --git a/demos/presentation/textview.go b/demos/presentation/textview.go index 601836b..dfdbd50 100644 --- a/demos/presentation/textview.go +++ b/demos/presentation/textview.go @@ -33,7 +33,6 @@ const textView1 = `[green]func[white] [yellow]main[white]() { func TextView1(nextSlide func()) (title string, content cview.Primitive) { textView := cview.NewTextView() textView.SetTextColor(tcell.ColorYellow.TrueColor()) - textView.SetScrollable(false) textView.SetDoneFunc(func(key tcell.Key) { nextSlide() }) @@ -52,7 +51,7 @@ func TextView1(nextSlide func()) (title string, content cview.Primitive) { } fmt.Fprintf(textView, "%d ", n) - time.Sleep(200 * time.Millisecond) + time.Sleep(75 * time.Millisecond) } }() textView.SetBorder(true) @@ -153,6 +152,7 @@ func TextView2(nextSlide func()) (title string, content cview.Primitive) { fmt.Fprint(textView, textView2) textView.SetBorder(true) textView.SetTitle("TextView output") + textView.SetScrollBarVisibility(cview.ScrollBarAuto) flex := cview.NewFlex() flex.AddItem(textView, 0, 1, true) diff --git a/demos/presentation/treeview.go b/demos/presentation/treeview.go index c998d89..0f71572 100644 --- a/demos/presentation/treeview.go +++ b/demos/presentation/treeview.go @@ -161,5 +161,5 @@ func TreeView(nextSlide func()) (title string, content cview.Primitive) { flex.AddItem(tree, 0, 1, true) flex.AddItem(treeCode, codeWidth, 1, false) - return "Tree", flex + return "TreeView", flex } diff --git a/doc.go b/doc.go index 62368aa..a4db83c 100644 --- a/doc.go +++ b/doc.go @@ -180,8 +180,7 @@ 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 offscreen, except TextView. See Widget.SetScrollBarColor and -Widget.SetScrollBarVisibility. +additional items offscreen. See SetScrollBarColor and SetScrollBarVisibility. Hello World diff --git a/textview.go b/textview.go index 98e2b03..2e62a58 100644 --- a/textview.go +++ b/textview.go @@ -206,15 +206,16 @@ 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, - scrollBarColor: Styles.ScrollBarColor, + Box: NewBox(), + highlights: make(map[string]struct{}), + lineOffset: -1, + reindex: true, + scrollable: true, + scrollBarVisibility: ScrollBarAuto, + scrollBarColor: Styles.ScrollBarColor, + align: AlignLeft, + wrap: true, + textColor: Styles.PrimaryTextColor, } } @@ -964,10 +965,13 @@ func (t *TextView) Draw(screen tcell.Screen) { // Get the available size. x, y, width, height := t.GetInnerRect() + if height == 0 { + return + } t.pageSize = height t.reindexBuffer(width) - showVerticalScrollBar := t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && len(t.index) >= height) + showVerticalScrollBar := t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && len(t.index) > height) if showVerticalScrollBar { width-- // Subtract space for scroll bar. if t.wrap { @@ -1285,12 +1289,16 @@ func (t *TextView) MouseHandler() func(action MouseAction, event *tcell.EventMou consumed = true setFocus(t) case MouseScrollUp: - t.trackEnd = false - t.lineOffset-- - consumed = true + if t.scrollable { + t.trackEnd = false + t.lineOffset-- + consumed = true + } case MouseScrollDown: - t.lineOffset++ - consumed = true + if t.scrollable { + t.lineOffset++ + consumed = true + } } return