231 lines
4.9 KiB
Go
231 lines
4.9 KiB
Go
package layout
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/BeatGlow/beatglow/app"
|
|
"github.com/BeatGlow/beatglow/app/widget"
|
|
"github.com/BeatGlow/beatglow/config"
|
|
)
|
|
|
|
type settings struct {
|
|
base
|
|
settings *config.Settings
|
|
menu []settingsMenu
|
|
menuWidget *widget.Menu
|
|
index int
|
|
}
|
|
|
|
type settingsMenu struct {
|
|
Item interface{} // pointer to the item we're showing a menu for
|
|
Value reflect.Value // value of the item we're showing a menu for
|
|
Fields []reflect.Value // fields contained in this item
|
|
Items []widget.MenuItem // items shown in the menu
|
|
}
|
|
|
|
// Settings is the settings menu.
|
|
func Settings(ctx app.Context) app.Layout {
|
|
bounds := ctx.Display().Bounds()
|
|
styles := app.StylesFor(ctx)
|
|
layout := &settings{
|
|
base: base{name: "settings"},
|
|
menuWidget: widget.NewMenu(bounds.Size()),
|
|
settings: ctx.Settings(),
|
|
}
|
|
|
|
layout.menuWidget.SetStyles(*styles)
|
|
layout.widgets = []app.Widget{
|
|
layout.menuWidget,
|
|
}
|
|
layout.show(layout.settings)
|
|
|
|
return layout
|
|
}
|
|
|
|
func (l *settings) show(data interface{}) {
|
|
var (
|
|
log = l.log()
|
|
v = reflect.ValueOf(data)
|
|
pointer bool
|
|
pointerV = v
|
|
)
|
|
for {
|
|
if pointerV.Kind() == reflect.Interface {
|
|
pointerV = pointerV.Elem()
|
|
}
|
|
|
|
if pointerV.Kind() == reflect.Ptr {
|
|
pointer = true
|
|
v = reflect.Indirect(pointerV)
|
|
}
|
|
|
|
if pointer {
|
|
pointerV = v
|
|
}
|
|
pointer = false
|
|
|
|
// If we still have a pointer or interface we have to indirect another level.
|
|
switch pointerV.Kind() {
|
|
case reflect.Ptr, reflect.Interface:
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
|
|
switch v.Kind() {
|
|
case reflect.Struct:
|
|
log.Printf("setup for struct: %T", data)
|
|
l.showStruct(v, data)
|
|
|
|
default:
|
|
log.Printf("setup for what? %T", data)
|
|
}
|
|
|
|
// Update widget.
|
|
l.menuWidget.SetItems(l.menu[0].Items)
|
|
|
|
// Reset selected item.
|
|
l.index = 0
|
|
}
|
|
|
|
func (l *settings) showStruct(v reflect.Value, data interface{}) {
|
|
var (
|
|
log = l.log()
|
|
vt = v.Type()
|
|
fields = make([]reflect.Value, 0, vt.NumField())
|
|
menuItems = make([]widget.MenuItem, 0, vt.NumField())
|
|
)
|
|
|
|
foreachStructField(v, func(fv reflect.Value, sf reflect.StructField) bool {
|
|
icon, tags := parseSettingsTag(sf.Tag)
|
|
log.Printf("field %#+v: %#+v -> %s %v", sf, fv, icon, tags)
|
|
log.Printf("can addr? %t", fv.CanAddr())
|
|
|
|
if _, ignore := tags["ignore"]; ignore {
|
|
return false
|
|
}
|
|
|
|
fields = append(fields, fv)
|
|
menuItems = append(menuItems, widget.MenuItem{
|
|
Icon: icon,
|
|
Text: sf.Name,
|
|
Type: fv.Type(),
|
|
Value: fv,
|
|
})
|
|
|
|
return false
|
|
})
|
|
|
|
// Push current item forward on the stack
|
|
l.menu = append([]settingsMenu{{
|
|
Item: data,
|
|
Value: v,
|
|
Fields: fields,
|
|
Items: menuItems,
|
|
}}, l.menu...)
|
|
}
|
|
|
|
func (l *settings) handleSelect(index int) {
|
|
var (
|
|
active = l.menu[0].Fields[index]
|
|
value = active
|
|
kind = value.Type().Kind()
|
|
)
|
|
|
|
if kind == reflect.Ptr {
|
|
value = reflect.Indirect(value)
|
|
kind = value.Type().Kind()
|
|
}
|
|
|
|
log := l.log()
|
|
log.WithField("index", index).Debugf("jump to menu item %v (%v/%v)", value, value.Type(), kind)
|
|
|
|
switch kind {
|
|
case reflect.Struct:
|
|
l.show(active.Interface())
|
|
|
|
case reflect.Bool:
|
|
//newValue := reflect.New(active.Type())
|
|
//newValue.Elem().Set(reflect.ValueOf(!active.Bool()))
|
|
//active.Elem().Set(newValue)
|
|
active.SetBool(!active.Bool())
|
|
l.menuWidget.Redraw()
|
|
|
|
default:
|
|
log.WithField("type", kind).Debug("no handler for data type")
|
|
}
|
|
}
|
|
|
|
func parseSettingsTag(tag reflect.StructTag) (icon string, settings map[string]string) {
|
|
icon = tag.Get("icon")
|
|
if s, ok := tag.Lookup("settings"); ok {
|
|
settings = make(map[string]string)
|
|
for _, kv := range strings.Split(s, " ") {
|
|
if kv = strings.TrimSpace(kv); kv == "" {
|
|
continue
|
|
}
|
|
if i := strings.IndexByte(kv, ':'); i > -1 {
|
|
k, v := kv[:i], kv[i+1:]
|
|
settings[k] = v
|
|
} else {
|
|
settings[kv] = ""
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (l *settings) HandleRotary(ctx app.Context, delta int) {
|
|
if l.menuWidget.IsVisible() {
|
|
l.index = l.menuWidget.Change(delta)
|
|
l.log().WithField("item", l.index).Debug("menu item changed")
|
|
}
|
|
}
|
|
|
|
func (l *settings) HandleButtonPress(ctx app.Context) {
|
|
if l.menuWidget.IsVisible() {
|
|
log := l.log().WithField("press", l.index)
|
|
if l.index == 0 {
|
|
if depth := len(l.menu); depth > 1 {
|
|
log.Debug("go back in menu")
|
|
l.index = 0
|
|
l.menu = l.menu[1:]
|
|
l.menuWidget.SetItems(l.menu[0].Items)
|
|
} else {
|
|
// Go back pressed.
|
|
log.Debug("go back to previous layout")
|
|
ctx.Show(nil)
|
|
}
|
|
return
|
|
}
|
|
|
|
log.Debug("handle selected item")
|
|
l.handleSelect(l.index - 1)
|
|
}
|
|
}
|
|
|
|
func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
|
if l.menuWidget.IsVisible() {
|
|
l.menu = nil
|
|
l.show(l.settings)
|
|
ctx.Show(nil) // Close settings
|
|
}
|
|
}
|
|
|
|
func foreachStructField(v reflect.Value, f func(fv reflect.Value, sf reflect.StructField) (stop bool)) {
|
|
t := v.Type()
|
|
for i, l := 0, t.NumField(); i < l; i++ {
|
|
sf := t.Field(i)
|
|
fv := v.Field(i).Addr()
|
|
if f(fv, sf) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
var (
|
|
_ app.RotaryInputHandler = (*settings)(nil)
|
|
_ app.ButtonInputHandler = (*settings)(nil)
|
|
)
|