Refactored the menu system

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:54 +01:00
1 parent 4ae7655cd8
commit 86aad51e37
12 files changed
+474 -344

No files matched your search

+121 -41
View File
@@ -6,6 +6,8 @@ import (
"reflect"
"strconv"
"strings"
"github.com/sirupsen/logrus"
)
type Menu struct {
@@ -14,14 +16,14 @@ type Menu struct {
// Current selected position.
Position int
// Number of items visible per page.
PageSize int
// Parent menu for this submenu.
Parent *Menu
// IsEditing indicates edit mode for an item.
IsEditing bool
// Editing indicates edit mode for an item.
Editing bool
// Number of items visible per page.
pageSize int
}
func New(data any, pageSize int, parent *Menu) (*Menu, error) {
@@ -41,8 +43,13 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
}
menu := &Menu{
PageSize: pageSize,
Parent: parent,
pageSize: pageSize,
Items: []Item{{
Label: "Go back",
Icon: "md_history",
Value: Back,
}},
}
typ := ref.Type()
@@ -50,7 +57,7 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
var (
field = typ.Field(i)
fieldRef = ref.Field(i)
value = ref.Field(i).Interface()
value = ref.Field(i).Addr().Interface()
icon = field.Tag.Get("icon")
label = field.Tag.Get("menu")
options = strings.Split(field.Tag.Get("options"), ",")
@@ -81,6 +88,13 @@ 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))
}
if !fieldRef.CanSet() {
panic(fmt.Sprintf("fieldref %T for %T is not settable", fieldRef, value))
}
switch field.Type.Kind() {
case reflect.Struct:
subMenu, err := New(value, pageSize, menu)
@@ -165,62 +179,56 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
// Change is used for editing and navigating, delta is either positive or negative input.
func (m *Menu) Change(delta int) {
if m.IsEditing {
if m.Editing {
m.edit(delta)
} else {
m.move(delta)
}
}
// ToggleEdit switches between navigation and editing modes.
func (m *Menu) ToggleEdit() {
m.IsEditing = !m.IsEditing
}
// EnterSubmenu enters the selected submenu
func (m *Menu) EnterSubmenu() *Menu {
if m.Position < len(m.Items) && m.Items[m.Position].SubMenu != nil {
return m.Items[m.Position].SubMenu
}
return nil
}
// ExitSubmenu returns to the parent menu
func (m *Menu) ExitSubmenu() *Menu {
if m.Parent != nil {
return m.Parent
}
return m // If no parent, return itself (root menu)
}
func (m *Menu) edit(delta int) {
// Adjust the value if editing
item := &m.Items[m.Position]
logrus.WithFields(logrus.Fields{
"action": "edit",
"delta": delta,
"type": fmt.Sprintf("%T", item.Value),
}).Debug("menu change")
// Adjust the value if editing
switch v := item.Value.(type) {
case int:
newValue := v + delta
case *uint8:
newValue := *v + uint8(delta*item.Step)
if newValue >= uint8(item.Min) && newValue <= uint8(item.Max) {
*v = newValue
}
case *int:
newValue := *v + (delta * item.Step)
if newValue >= item.Min && newValue <= item.Max {
item.Value = newValue
item.Field.SetInt(int64(newValue)) // Update the original struct field
*v = newValue
}
case bool:
case *bool:
if delta != 0 {
item.Value = !v
item.Field.SetBool(!v) // Update the original struct field
*v = !*v
}
case string:
case *string:
if len(item.Options) > 0 {
currentIndex := indexOf(v, item.Options)
currentIndex := indexOf(*v, item.Options)
if currentIndex != -1 {
newIndex := (currentIndex + delta + len(item.Options)) % len(item.Options)
item.Value = item.Options[newIndex]
item.Field.SetString(item.Options[newIndex]) // Update the original struct field
*v = item.Options[newIndex]
}
}
}
}
func (m *Menu) move(delta int) {
logrus.WithFields(logrus.Fields{
"action": "move",
"delta": delta,
"position": m.Position,
}).Debug("menu change")
// Move the cursor if not editing
m.Position += delta
if m.Position < 0 {
@@ -228,6 +236,69 @@ func (m *Menu) move(delta int) {
} else if m.Position >= len(m.Items) {
m.Position = len(m.Items) - 1
}
logrus.WithFields(logrus.Fields{
"action": "move",
"delta": delta,
"position": m.Position,
}).Debug("menu change done")
}
// ActiveItem is the current item that is active.
func (m *Menu) ActiveItem() Item {
return m.Items[m.Position]
}
// PageSize returns the current page size.
func (m *Menu) PageSize() int {
return m.pageSize
}
// SetPageSize updates the page size.
func (m *Menu) SetPageSize(pageSize int) {
m.pageSize = pageSize
for _, item := range m.Items {
if item.SubMenu != nil {
item.SubMenu.SetPageSize(pageSize)
}
}
}
// IsEditing checks if we're in editing mode.
func (m *Menu) IsEditing() bool {
return m.Editing
}
// ToggleEdit switches between navigation and editing modes.
func (m *Menu) ToggleEdit() {
m.Editing = !m.Editing
}
func (m *Menu) IsSubMenu() bool {
return m.Position < len(m.Items) && m.Items[m.Position].SubMenu != nil
}
// EnterSubmenu enters the selected sub menu.
func (m *Menu) EnterSubMenu() *Menu {
if m.Position < len(m.Items) && m.Items[m.Position].SubMenu != nil {
logrus.WithFields(logrus.Fields{
"action": "enter subment",
"item": m.Items[m.Position].Label,
}).Debug("menu enter sub menu")
return m.Items[m.Position].SubMenu
}
logrus.Debug("menu enter sub menu failed; no sub menu item")
return nil
}
// ExitSubmenu returns to the parent menu.
func (m *Menu) ExitSubMenu() *Menu {
if m.Parent != nil {
logrus.Debug("menu exit sub menu")
return m.Parent
}
logrus.Debug("menu exit sub menu failed; no parent")
return nil // If no parent, return nil.
}
type Item struct {
@@ -262,3 +333,12 @@ func indexOf(needle string, haystack []string) int {
}
return -1
}
// Magic actions.
type menuAction struct{}
// Actions.
var (
Back menuAction
SubMenu menuAction
)
+131
View File
@@ -0,0 +1,131 @@
package menu
import (
"fmt"
"image"
"image/color"
"math"
"github.com/sirupsen/logrus"
"github.com/BeatGlow/display/draw"
"github.com/BeatGlow/display/pixel"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/beatglow/app/font"
)
func (item *Item) Render(dst draw.Image, isActive, isEditing bool, style *app.TextStyle) {
var (
bounds = dst.Bounds()
edge = image.Rectangle{Min: bounds.Min, Max: bounds.Max.Sub(image.Pt(1, 1))}
)
logrus.WithFields(logrus.Fields{
"bounds": bounds,
"size": bounds.Size(),
}).Debug("render menu item")
if style == nil {
style = &app.StylesFor(dst.ColorModel()).Text
}
// Clear area.
app.ImageClear(dst)
if isEditing && isActive {
draw.Box(dst, bounds, pixel.Gray4{Y: 0x4})
draw.Rectangle(dst, edge, pixel.Gray4{Y: 0xf})
} else if isActive {
draw.Box(dst, bounds, pixel.Gray4{Y: 0x2})
draw.Rectangle(dst, edge, pixel.Gray4{Y: 0xb})
} else {
draw.Rectangle(dst, edge, pixel.Gray4{Y: 0x2})
}
// Render icon.
if item.Icon != "" {
font.Icon(dst, bounds.Min.Add(image.Pt(3, 2)), color.White, item.Icon)
}
// Render label.
style.Font.Draw(dst, bounds.Min.Add(image.Pt(16, 3)), style.Foreground(), item.Label)
// Render icon and widgets.
switch value := item.Value.(type) {
case *bool:
item.renderBool(dst, *value, isEditing)
case *uint8:
item.renderUint8(dst, *value, isEditing, style)
case *int:
item.renderInt(dst, *value, isEditing, style)
case *string:
item.renderString(dst, *value, isEditing, style)
case menuAction:
// no further renditions needed
case nil:
// sub menu item has no value
item.renderSubMenu(dst, style)
default:
logrus.WithFields(logrus.Fields{
"label": item.Label,
"type": fmt.Sprintf("%T", value),
}).Warn("no menu renderer for type")
}
}
func (item *Item) renderSubMenu(dst draw.Image, style *app.TextStyle) {
bounds := dst.Bounds()
font.Icon(dst, image.Pt(bounds.Max.X-14, bounds.Min.Y+3), color.White, "md_folder_open_outline")
}
func (item *Item) renderBool(dst draw.Image, value bool, isEditing bool) {
var (
bounds = dst.Bounds()
icon = "md_checkbox_blank_outline"
)
if value {
icon = "md_checkbox_marked_outline"
}
font.Icon(dst, image.Pt(bounds.Max.X-14, bounds.Min.Y+3), color.White, icon)
}
func (item *Item) renderUint8(dst draw.Image, value uint8, isEditing bool, style *app.TextStyle) {
var (
bounds = dst.Bounds()
text = fmt.Sprintf("%d", value)
width, _ = style.Font.MeasureString(text)
point = image.Pt(bounds.Max.X-3-width, bounds.Min.Y+3)
)
style.Font.Draw(dst, point, style.Foreground(), text)
}
func (item *Item) renderInt(dst draw.Image, value int, isEditing bool, style *app.TextStyle) {
var (
bounds = dst.Bounds()
text = fmt.Sprintf("%d", value)
width, _ = style.Font.MeasureString(text)
point = image.Pt(bounds.Max.X-3-width, bounds.Min.Y+3)
)
style.Font.Draw(dst, point, style.Foreground(), text)
}
func (item *Item) renderString(dst draw.Image, value string, isEditing bool, style *app.TextStyle) {
var (
bounds = dst.Bounds()
width, _ = style.Font.MeasureString(value)
point = image.Pt(bounds.Max.X-3-width, bounds.Min.Y+3)
)
style.Font.Draw(dst, point, style.Foreground(), value)
}
func numberOfDigits(n int) int {
if n == 0 {
return 1
}
sign := 0
if n < 0 {
sign++
n = -n
}
return int(math.Log10(float64(n))) + sign + 1
}