1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/view/scrollable_widget.go
Chris Cummer fd794707cd
☢️ WTF-1031 Support multiple simultaneous configurations (#1032)
* WTF-1031 Rename WtfApp.app to WtfApp.tviewApp

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 Add scaffolding for main to support multiple WtfApp instances

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 WIP

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove common functionality from KeyboardWidget and into Base

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Augment with some descriptive comments

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Add full support for multiple app instances via the AppManager.

Still to do:

* Config support for multiple apps/multiple config files
* The ability to switch between apps

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Move SetTerminal out of main and into its own file

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-12-21 03:25:41 -08:00

91 lines
1.9 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(tviewApp *tview.Application, pages *tview.Pages, commonSettings *cfg.Common) ScrollableWidget {
widget := ScrollableWidget{
TextWidget: NewTextWidget(tviewApp, pages, commonSettings),
}
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
if items == 0 {
widget.Selected = -1
}
}
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
}
if widget.maxItems == 0 {
widget.Selected = -1
}
widget.RenderFunction()
}
func (widget *ScrollableWidget) Prev() {
widget.Selected--
if widget.Selected < 0 {
widget.Selected = widget.maxItems - 1
}
if widget.maxItems == 0 {
widget.Selected = -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.tviewApp.QueueUpdateDraw(func() {
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
})
}