1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/view/scrollable_widget.go
Sean Smith 14e7619075 Renormalize the redraw function
Have all instances take a function
Update the remaining modules to take this into account
Numerous smaller refactors to make some widgets work more or less the same
2019-08-27 21:51:37 -04:00

81 lines
1.7 KiB
Go

package view
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
}
/* -------------------- Exported Functions -------------------- */
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) {
return widget.CommonSettings().DefaultFocusedRowColor()
}
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(data func() (string, string, bool)) {
widget.TextWidget.Redraw(data)
widget.app.QueueUpdateDraw(func() {
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
})
}