1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/wtf/multisource_widget.go
Chris Cummer ed8d357881 Simplifies the use of MultiSourceWidget
Widgets that embed MultiSourceWidget no longer have to explicitly call
loadSources(). This is done as part of the set-up for MultiSourceWidget.
2019-04-28 20:43:39 -07:00

99 lines
2.4 KiB
Go

package wtf
import (
"github.com/wtfutil/wtf/cfg"
)
type MultiSourceWidget struct {
moduleConfig *cfg.Common
singular string
plural string
DisplayFunction func()
Idx int
Sources []string
}
// NewMultiSourceWidget creates and returns an instance of MultiSourceWidget
func NewMultiSourceWidget(moduleConfig *cfg.Common, singular, plural string) MultiSourceWidget {
widget := MultiSourceWidget{
moduleConfig: moduleConfig,
singular: singular,
plural: plural,
}
widget.loadSources()
return widget
}
/* -------------------- Exported Functions -------------------- */
// CurrentSource returns the string representations of the currently-displayed source
func (widget *MultiSourceWidget) CurrentSource() string {
if widget.Idx >= len(widget.Sources) {
return ""
}
return widget.Sources[widget.Idx]
}
// Next displays the next source in the source list. If the current source is the last
// source it wraps around to the first source
func (widget *MultiSourceWidget) Next() {
widget.Idx = widget.Idx + 1
if widget.Idx == len(widget.Sources) {
widget.Idx = 0
}
if widget.DisplayFunction != nil {
widget.DisplayFunction()
}
}
// Prev displays the previous source in the source list. If the current source is the first
// source, it wraps around to the last source
func (widget *MultiSourceWidget) Prev() {
widget.Idx = widget.Idx - 1
if widget.Idx < 0 {
widget.Idx = len(widget.Sources) - 1
}
if widget.DisplayFunction != nil {
widget.DisplayFunction()
}
}
// SetDisplayFunction stores the function that should be called when the source is
// changed. This is typically called from within the initializer for the struct that
// embeds MultiSourceWidget
//
// Example:
//
// widget := Widget{
// MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "person", "people")
// }
//
// widget.SetDisplayFunction(widget.display)
//
func (widget *MultiSourceWidget) SetDisplayFunction(displayFunc func()) {
widget.DisplayFunction = displayFunc
}
/* -------------------- Unexported Functions -------------------- */
func (widget *MultiSourceWidget) loadSources() {
var empty []interface{}
single := widget.moduleConfig.Config.UString(widget.singular, "")
multiple := widget.moduleConfig.Config.UList(widget.plural, empty)
asStrs := ToStrs(multiple)
if single != "" {
asStrs = append(asStrs, single)
}
widget.Sources = asStrs
}