Refactored the menu system
This commit is contained in:
12 files changed
+474
-344
No files matched your search
+58
-164
@@ -2,9 +2,9 @@ package layout
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
"github.com/BeatGlow/beatglow/app/menu"
|
||||
"github.com/BeatGlow/beatglow/app/widget"
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
)
|
||||
@@ -12,204 +12,98 @@ import (
|
||||
type settings struct {
|
||||
base
|
||||
settings *config.Settings
|
||||
menu []settingsMenu
|
||||
menu *menu.Menu
|
||||
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)
|
||||
styles := app.StylesFor(ctx.Display().ColorModel())
|
||||
layout := &settings{
|
||||
base: base{name: "settings"},
|
||||
menuWidget: widget.NewMenu(bounds.Size()),
|
||||
menuWidget: widget.NewMenu(bounds.Size(), nil),
|
||||
settings: ctx.Settings(),
|
||||
}
|
||||
|
||||
layout.menuWidget.SetStyles(*styles)
|
||||
var err error
|
||||
if layout.menu, err = menu.New(layout.settings, 4, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
layout.menuWidget.
|
||||
Update(layout.menu).
|
||||
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")
|
||||
log := l.log()
|
||||
log.WithField("item", l.index).Debug("menu item changed")
|
||||
l.menu.Change(delta)
|
||||
if l.menu.IsEditing() {
|
||||
// React on changed value, if it's an immediate change.
|
||||
switch item := l.menu.Items[l.menu.Position]; item.Label {
|
||||
case "Display contrast":
|
||||
if err := ctx.Display().SetContrast(*(item.Value.(*uint8))); err != nil {
|
||||
log.WithError(err).Error("error updating contrast")
|
||||
}
|
||||
default:
|
||||
log.WithField("label", item.Label).Debug("no immediate change")
|
||||
}
|
||||
}
|
||||
l.menuWidget.Redraw()
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
log := l.log()
|
||||
if l.menu.IsEditing() {
|
||||
log.Debug("exit edit mode")
|
||||
l.menu.ToggleEdit()
|
||||
} else if l.menu.IsSubMenu() {
|
||||
log.Debug("enter sub menu")
|
||||
l.menu = l.menu.EnterSubMenu()
|
||||
} else if item := l.menu.ActiveItem(); item.Value == menu.Back {
|
||||
log.Debug("exit sub menu")
|
||||
menu := l.menu.ExitSubMenu()
|
||||
if menu == nil {
|
||||
// Close settings layout, exiting root.
|
||||
ctx.Show(nil)
|
||||
return
|
||||
}
|
||||
return
|
||||
l.menu = menu
|
||||
} else {
|
||||
log.Debug("enter edit mode")
|
||||
l.menu.ToggleEdit()
|
||||
}
|
||||
|
||||
log.Debug("handle selected item")
|
||||
l.handleSelect(l.index - 1)
|
||||
// Trigger re-render.
|
||||
l.menuWidget.Update(l.menu)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
||||
if l.menuWidget.IsVisible() {
|
||||
l.menu = nil
|
||||
l.show(l.settings)
|
||||
ctx.Show(nil) // Close settings
|
||||
// Rewind to root menu
|
||||
for {
|
||||
parent := l.menu.Parent
|
||||
if parent == nil {
|
||||
break
|
||||
}
|
||||
l.menu = parent
|
||||
}
|
||||
|
||||
// Close settings layout.
|
||||
ctx.Show(nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user