1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Create abtract scrollable widget

This cleans up a bunch of boilerplate for scrollable items and standardizes their usage
This commit is contained in:
Sean Smith
2019-05-09 22:43:01 -04:00
parent 0b59a3b62f
commit 210723cd74
17 changed files with 232 additions and 470 deletions

79
wtf/scrollable.go Normal file
View File

@@ -0,0 +1,79 @@
package wtf
import (
"strconv"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
)
type ScrollableWidget struct {
TextWidget
selected int
maxItems int
RenderFunction func()
}
func NewScrollableWidget(app *tview.Application, commonSettings *cfg.Common, focusable bool) ScrollableWidget {
widget := ScrollableWidget{
TextWidget: NewTextWidget(app, commonSettings, focusable),
}
widget.Unselect()
widget.View.SetScrollable(true)
widget.View.SetRegions(true)
return widget
}
func (widget *ScrollableWidget) SetRenderFunction(displayFunc func()) {
widget.RenderFunction = displayFunc
}
func (widget *ScrollableWidget) SetItemCount(items int) {
widget.maxItems = items
}
func (widget *ScrollableWidget) GetSelected() int {
return widget.selected
}
func (widget *ScrollableWidget) RowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
widget.CommonSettings.DefaultFocussedRowColor()
}
return widget.CommonSettings.RowColor(idx)
}
func (widget *ScrollableWidget) Next() {
widget.selected++
if widget.selected >= widget.maxItems {
widget.selected = 0
}
widget.RenderFunction()
}
func (widget *ScrollableWidget) Prev() {
widget.selected--
if widget.selected < 0 {
widget.selected = widget.maxItems - 1
}
widget.RenderFunction()
}
func (widget *ScrollableWidget) Unselect() {
widget.selected = -1
if widget.RenderFunction != nil {
widget.RenderFunction()
}
}
func (widget *ScrollableWidget) Redraw(title, content string, wrap bool) {
widget.TextWidget.Redraw(title, content, wrap)
widget.app.QueueUpdateDraw(func() {
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
})
}