Fix menu navigation, boolean editing, and unexpected exits #3
No files matched your search
@@ -78,7 +78,7 @@ func (l *settings) HandleButtonPress(ctx app.Context) {
|
||||
return
|
||||
}
|
||||
l.menu = menu
|
||||
} else {
|
||||
} else if item.IsEditable() {
|
||||
log.Debug("enter edit mode")
|
||||
l.menu.ToggleEdit()
|
||||
}
|
||||
@@ -90,12 +90,14 @@ func (l *settings) HandleButtonPress(ctx app.Context) {
|
||||
func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
||||
// Rewind to root menu
|
||||
for {
|
||||
l.menu.Editing = false
|
||||
parent := l.menu.Parent
|
||||
if parent == nil {
|
||||
break
|
||||
}
|
||||
l.menu = parent
|
||||
}
|
||||
l.menuWidget.Update(l.menu)
|
||||
|
||||
// Close settings layout.
|
||||
ctx.Show(nil)
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"image"
|
||||
"testing"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
"github.com/BeatGlow/beatglow/app/menu"
|
||||
"github.com/BeatGlow/beatglow/app/widget"
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
)
|
||||
|
||||
type settingsTestContext struct {
|
||||
app.Context
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (c *settingsTestContext) Show(layout app.Layout) { c.closed = layout == nil }
|
||||
|
||||
func TestSettingsBackAndInformation(t *testing.T) {
|
||||
data := config.DefaultSettings
|
||||
root, err := menu.New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l := &settings{menu: root, menuWidget: widget.NewMenu(image.Pt(256, 64), root)}
|
||||
ctx := &settingsTestContext{}
|
||||
root.Position = 4 // System
|
||||
l.HandleButtonPress(ctx)
|
||||
if l.menu.Parent != root {
|
||||
t.Fatal("did not enter System submenu")
|
||||
}
|
||||
for _, position := range []int{1, 2} {
|
||||
l.menu.Position = position
|
||||
l.HandleButtonPress(ctx)
|
||||
if l.menu.IsEditing() {
|
||||
t.Fatal("system information entered edit mode")
|
||||
}
|
||||
}
|
||||
l.HandleRotary(ctx, -2)
|
||||
l.HandleButtonPress(ctx)
|
||||
if l.menu != root || l.menuWidget.Root != root {
|
||||
t.Fatal("Back did not restore the root menu")
|
||||
}
|
||||
if root.Position != 4 {
|
||||
t.Fatal("parent selection was lost")
|
||||
}
|
||||
root.Position = 0
|
||||
l.HandleButtonPress(ctx)
|
||||
if !ctx.closed {
|
||||
t.Fatal("root Back did not close settings")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsLongPressSynchronizesMenu(t *testing.T) {
|
||||
data := config.DefaultSettings
|
||||
root, err := menu.New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root.Position = 1 // Display
|
||||
child := root.EnterSubMenu()
|
||||
child.Position = 1
|
||||
child.ToggleEdit()
|
||||
l := &settings{menu: child, menuWidget: widget.NewMenu(image.Pt(256, 64), child)}
|
||||
ctx := &settingsTestContext{}
|
||||
l.HandleButtonLongPress(ctx)
|
||||
if !ctx.closed || l.menu != root || l.menuWidget.Root != root {
|
||||
t.Fatal("long press left the rendered menu out of sync")
|
||||
}
|
||||
if child.IsEditing() || root.IsEditing() {
|
||||
t.Fatal("long press left an item in edit mode")
|
||||
}
|
||||
l.HandleButtonPress(ctx)
|
||||
if l.menu != child || l.menu.IsEditing() {
|
||||
t.Fatal("reopening submenu did not restore navigation mode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBooleanSelectionSurvivesConfirmation(t *testing.T) {
|
||||
data := config.DefaultSettings
|
||||
data.Player.ShowArt = true
|
||||
root, err := menu.New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root.Position = 2 // Player
|
||||
l := &settings{settings: &data, menu: root, menuWidget: widget.NewMenu(image.Pt(256, 64), root)}
|
||||
ctx := &settingsTestContext{}
|
||||
l.HandleButtonPress(ctx)
|
||||
l.HandleRotary(ctx, 2) // Show album art
|
||||
l.HandleButtonPress(ctx)
|
||||
for _, delta := range []int{-1, -1, -3} {
|
||||
l.HandleRotary(ctx, delta)
|
||||
}
|
||||
if data.Player.ShowArt {
|
||||
t.Fatal("left did not keep checkbox unchecked")
|
||||
}
|
||||
l.HandleButtonPress(ctx)
|
||||
if l.menu.IsEditing() || data.Player.ShowArt {
|
||||
t.Fatal("press did not confirm unchecked value")
|
||||
}
|
||||
l.HandleRotary(ctx, -2)
|
||||
l.HandleButtonPress(ctx) // Back to root
|
||||
l.HandleButtonPress(ctx) // Reopen Player
|
||||
l.HandleRotary(ctx, 2) // Show album art
|
||||
l.HandleButtonPress(ctx) // Edit selected Show album art
|
||||
if data.Player.ShowArt {
|
||||
t.Fatal("reopening checkbox reset its value")
|
||||
}
|
||||
for _, delta := range []int{1, 1, 3} {
|
||||
l.HandleRotary(ctx, delta)
|
||||
}
|
||||
l.HandleButtonPress(ctx)
|
||||
if l.menu.IsEditing() || !data.Player.ShowArt {
|
||||
t.Fatal("press did not confirm checked value")
|
||||
}
|
||||
}
|
||||
+46
-14
@@ -26,6 +26,9 @@ type Menu struct {
|
||||
pageSize int
|
||||
}
|
||||
|
||||
// New builds a menu over the exported fields of a struct pointer. It returns an
|
||||
// error for non-struct or unaddressable data, unsupported fields, or invalid tags.
|
||||
// Values remain owned by the caller; menu edits and reads must be serialized.
|
||||
func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
ref := reflect.ValueOf(data)
|
||||
|
||||
@@ -41,6 +44,9 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
if ref.Kind() != reflect.Struct {
|
||||
return nil, fmt.Errorf("menu: %T is not a struct", data)
|
||||
}
|
||||
if !ref.CanAddr() {
|
||||
return nil, fmt.Errorf("menu: %T must be a pointer to a struct", data)
|
||||
}
|
||||
|
||||
menu := &Menu{
|
||||
Parent: parent,
|
||||
@@ -51,16 +57,20 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
Value: Back,
|
||||
}},
|
||||
}
|
||||
menu.SetPageSize(pageSize)
|
||||
|
||||
typ := ref.Type()
|
||||
for i, n := 0, ref.NumField(); i < n; i++ {
|
||||
field := typ.Field(i)
|
||||
if field.Tag.Get("menu") == "-" || !field.IsExported() {
|
||||
continue
|
||||
}
|
||||
var (
|
||||
field = typ.Field(i)
|
||||
fieldRef = ref.Field(i)
|
||||
value = ref.Field(i).Addr().Interface()
|
||||
icon = field.Tag.Get("icon")
|
||||
label = field.Tag.Get("menu")
|
||||
options = strings.Split(field.Tag.Get("options"), ",")
|
||||
options []string
|
||||
step = 1
|
||||
min, max int
|
||||
err error
|
||||
@@ -68,8 +78,9 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
|
||||
if len(label) == 0 {
|
||||
label = field.Name
|
||||
} else if label == "-" {
|
||||
continue
|
||||
}
|
||||
if tag := field.Tag.Get("options"); tag != "" {
|
||||
options = strings.Split(tag, ",")
|
||||
}
|
||||
|
||||
if stepTag := field.Tag.Get("step"); stepTag != "" {
|
||||
@@ -89,10 +100,10 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||
}
|
||||
|
||||
if !fieldRef.CanAddr() {
|
||||
panic(fmt.Sprintf("fieldref %T for %T is not addressable", fieldRef, value))
|
||||
return nil, fmt.Errorf("menu: field %s is not addressable", field.Name)
|
||||
}
|
||||
if !fieldRef.CanSet() {
|
||||
panic(fmt.Sprintf("fieldref %T for %T is not settable", fieldRef, value))
|
||||
return nil, fmt.Errorf("menu: field %s is not settable", field.Name)
|
||||
}
|
||||
|
||||
switch field.Type.Kind() {
|
||||
@@ -198,9 +209,9 @@ func (m *Menu) edit(delta int) {
|
||||
// Adjust the value if editing
|
||||
switch v := item.Value.(type) {
|
||||
case *uint8:
|
||||
newValue := *v + uint8(delta*item.Step)
|
||||
if newValue >= uint8(item.Min) && newValue <= uint8(item.Max) {
|
||||
*v = newValue
|
||||
newValue := int(*v) + delta*item.Step
|
||||
if newValue >= item.Min && newValue <= item.Max {
|
||||
*v = uint8(newValue)
|
||||
}
|
||||
case *int:
|
||||
newValue := *v + (delta * item.Step)
|
||||
@@ -209,13 +220,13 @@ func (m *Menu) edit(delta int) {
|
||||
}
|
||||
case *bool:
|
||||
if delta != 0 {
|
||||
*v = !*v
|
||||
*v = delta > 0
|
||||
}
|
||||
case *string:
|
||||
if len(item.Options) > 0 {
|
||||
currentIndex := indexOf(*v, item.Options)
|
||||
if currentIndex != -1 {
|
||||
newIndex := (currentIndex + delta + len(item.Options)) % len(item.Options)
|
||||
newIndex := (currentIndex + delta%len(item.Options) + len(item.Options)) % len(item.Options)
|
||||
*v = item.Options[newIndex]
|
||||
}
|
||||
}
|
||||
@@ -254,8 +265,11 @@ func (m *Menu) PageSize() int {
|
||||
return m.pageSize
|
||||
}
|
||||
|
||||
// SetPageSize updates the page size.
|
||||
// SetPageSize updates this menu and its submenus, with a minimum of one item.
|
||||
func (m *Menu) SetPageSize(pageSize int) {
|
||||
if pageSize < 1 {
|
||||
pageSize = 1
|
||||
}
|
||||
m.pageSize = pageSize
|
||||
for _, item := range m.Items {
|
||||
if item.SubMenu != nil {
|
||||
@@ -269,9 +283,14 @@ func (m *Menu) IsEditing() bool {
|
||||
return m.Editing
|
||||
}
|
||||
|
||||
// ToggleEdit switches between navigation and editing modes.
|
||||
// ToggleEdit exits editing or starts editing an editable item. Informational
|
||||
// items, submenu entries and Back remain in navigation mode.
|
||||
func (m *Menu) ToggleEdit() {
|
||||
m.Editing = !m.Editing
|
||||
if m.Editing {
|
||||
m.Editing = false
|
||||
} else if m.ActiveItem().IsEditable() {
|
||||
m.Editing = true
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Menu) IsSubMenu() bool {
|
||||
@@ -324,6 +343,19 @@ type Item struct {
|
||||
Options []string
|
||||
}
|
||||
|
||||
// IsEditable reports whether the item accepts rotary input. Strings without
|
||||
// enumerated options are informational and remain read-only.
|
||||
func (item Item) IsEditable() bool {
|
||||
switch item.Value.(type) {
|
||||
case *bool, *uint8, *int:
|
||||
return true
|
||||
case *string:
|
||||
return len(item.Options) > 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// indexOf returns the index of a value in a slice, or -1 if not found
|
||||
func indexOf(needle string, haystack []string) int {
|
||||
for i, opt := range haystack {
|
||||
|
||||
+146
-4
@@ -49,10 +49,152 @@ func TestMenuNil(t *testing.T) {
|
||||
|
||||
func TestMenuNotPointer(t *testing.T) {
|
||||
var settings testSettings
|
||||
menu, err := New(settings, 4, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
_, err := New(settings, 4, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for an unaddressable struct")
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("menu: %#+v", menu)
|
||||
func TestInformationalItemsRemainNavigable(t *testing.T) {
|
||||
data := struct {
|
||||
LoadAverage string
|
||||
LocalTime string
|
||||
}{LoadAverage: "0.12 0.10 0.08", LocalTime: "12:00"}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, position := range []int{1, 2} {
|
||||
m.Position = position
|
||||
m.ToggleEdit()
|
||||
if m.IsEditing() {
|
||||
t.Fatal("informational item entered edit mode")
|
||||
}
|
||||
m.Change(-position)
|
||||
if m.ActiveItem().Value != Back {
|
||||
t.Fatal("cannot navigate back after selecting information")
|
||||
}
|
||||
}
|
||||
if data.LoadAverage != "0.12 0.10 0.08" || data.LocalTime != "12:00" {
|
||||
t.Fatal("informational values changed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubmenuBackPreservesParentPosition(t *testing.T) {
|
||||
var data testSettings
|
||||
root, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root.Position = 2
|
||||
child := root.EnterSubMenu()
|
||||
if child == nil {
|
||||
t.Fatal("missing submenu")
|
||||
}
|
||||
if child.ActiveItem().Value != Back {
|
||||
t.Fatal("submenu should initially select Back")
|
||||
}
|
||||
if got := child.ExitSubMenu(); got != root || got.Position != 2 {
|
||||
t.Fatal("Back did not restore parent selection")
|
||||
}
|
||||
root.Position = 0
|
||||
root.ToggleEdit()
|
||||
if root.IsEditing() {
|
||||
t.Fatal("Back entered edit mode")
|
||||
}
|
||||
if root.ExitSubMenu() != nil {
|
||||
t.Fatal("root should exit settings")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringOptionsLargeNegativeDelta(t *testing.T) {
|
||||
data := struct {
|
||||
Sleep string `options:"5min,15min,hour,day"`
|
||||
}{Sleep: "hour"}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Position = 1
|
||||
m.ToggleEdit()
|
||||
if !m.IsEditing() {
|
||||
t.Fatal("option item should be editable")
|
||||
}
|
||||
m.Change(-11)
|
||||
if data.Sleep != "day" {
|
||||
t.Fatalf("got %q, want day", data.Sleep)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint8DoesNotWrap(t *testing.T) {
|
||||
data := struct{ Contrast uint8 }{Contrast: 255}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Position = 1
|
||||
m.ToggleEdit()
|
||||
m.Change(1)
|
||||
if data.Contrast != 255 {
|
||||
t.Fatal("contrast wrapped above maximum")
|
||||
}
|
||||
data.Contrast = 0
|
||||
m.Change(-1)
|
||||
if data.Contrast != 0 {
|
||||
t.Fatal("contrast wrapped below minimum")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinimumPageSize(t *testing.T) {
|
||||
var data testSettings
|
||||
m, err := New(&data, 0, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.PageSize() != 1 {
|
||||
t.Fatal("page size must be positive")
|
||||
}
|
||||
m.SetPageSize(-1)
|
||||
if m.Items[1].SubMenu.PageSize() != 1 {
|
||||
t.Fatal("submenu page size must be positive")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBooleanEditingUsesDirection(t *testing.T) {
|
||||
for _, initial := range []bool{false, true} {
|
||||
data := struct{ Enabled bool }{Enabled: initial}
|
||||
m, err := New(&data, 4, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Position = 1
|
||||
m.ToggleEdit()
|
||||
for _, delta := range []int{1, 1, 3, 0, -1, -1, -4, 0, 1} {
|
||||
before := data.Enabled
|
||||
m.Change(delta)
|
||||
want := delta > 0
|
||||
if delta == 0 {
|
||||
want = before
|
||||
}
|
||||
if data.Enabled != want {
|
||||
t.Fatalf("initial=%v delta=%d: got %v, want %v", initial, delta, data.Enabled, want)
|
||||
}
|
||||
if m.Position != 1 || !m.IsEditing() {
|
||||
t.Fatal("rotating left boolean edit mode")
|
||||
}
|
||||
}
|
||||
m.ToggleEdit()
|
||||
if m.IsEditing() || !data.Enabled {
|
||||
t.Fatal("confirming did not preserve the selected value")
|
||||
}
|
||||
m.Change(-1)
|
||||
if m.Position != 0 || !data.Enabled {
|
||||
t.Fatal("navigation changed the confirmed value")
|
||||
}
|
||||
m.Change(1)
|
||||
m.ToggleEdit()
|
||||
if !data.Enabled {
|
||||
t.Fatal("reopening edit mode reset the value")
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -22,6 +22,7 @@ func NewMenu(size image.Point, root *menu.Menu) *Menu {
|
||||
w := new(Menu)
|
||||
w.Extend(w)
|
||||
w.Resize(size)
|
||||
w.Update(root)
|
||||
return w
|
||||
}
|
||||
|
||||
@@ -44,7 +45,7 @@ func (w *Menu) Redraw() {
|
||||
}
|
||||
|
||||
func (w *Menu) Refresh(ctx app.Context) bool {
|
||||
if !w.IsVisible() {
|
||||
if !w.IsVisible() || w.Root == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -41,8 +41,8 @@ type SpectrumSettings struct {
|
||||
}
|
||||
|
||||
type SystemSettings struct {
|
||||
LoadAverage string `label:"Load average"`
|
||||
LocalTime string `label:"Local time"`
|
||||
LoadAverage string `menu:"Load average"`
|
||||
LocalTime string `menu:"Local time"`
|
||||
}
|
||||
|
||||
var DefaultSettings = Settings{
|
||||
|
||||
+1
-2
@@ -115,8 +115,7 @@ func (c *controllerContext) Show(layout app.Layout) {
|
||||
|
||||
for _, other := range c.layouts {
|
||||
if other == layout {
|
||||
if layout != c.settingsLayout {
|
||||
// Only store a "previous" screen if the active screen isn't the settings.
|
||||
if layout != c.active {
|
||||
c.previous, c.active = c.active, layout
|
||||
}
|
||||
log.WithField("layout", layout).Debug("show layout")
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package beatglow
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
)
|
||||
|
||||
type navigationTestLayout struct {
|
||||
app.Layout
|
||||
name string
|
||||
visible bool
|
||||
}
|
||||
|
||||
func (l *navigationTestLayout) String() string { return l.name }
|
||||
func (l *navigationTestLayout) Show(app.Context) { l.visible = true }
|
||||
func (l *navigationTestLayout) Hide(app.Context) { l.visible = false }
|
||||
func (l *navigationTestLayout) IsVisible() bool { return l.visible }
|
||||
|
||||
func TestSettingsReturnsToActiveLayout(t *testing.T) {
|
||||
settings := &navigationTestLayout{name: "settings"}
|
||||
playing := &navigationTestLayout{name: "playing"}
|
||||
analyzer := &navigationTestLayout{name: "analyzer"}
|
||||
c := &controllerContext{settingsLayout: settings, layouts: []app.Layout{settings, playing, analyzer}}
|
||||
c.Show(playing)
|
||||
c.Show(analyzer)
|
||||
for i := 0; i < 2; i++ {
|
||||
c.Show(settings)
|
||||
if c.active != settings || c.previous != analyzer {
|
||||
t.Fatal("settings did not remember the active screen")
|
||||
}
|
||||
c.Show(nil)
|
||||
if c.active != analyzer || !analyzer.visible || settings.visible {
|
||||
t.Fatal("exiting settings did not restore analyzer")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user