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

+37 -87
View File
@@ -1,7 +1,6 @@
package widget
import (
"fmt"
"image"
"image/color"
"reflect"
@@ -9,17 +8,17 @@ import (
"github.com/sirupsen/logrus"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/beatglow/app/font"
"github.com/BeatGlow/beatglow/app/menu"
"github.com/BeatGlow/display/draw"
)
type Menu struct {
Base
items []MenuItem
index int
Root *menu.Menu
curr, next *menu.Menu // currently in view and next in view
}
func NewMenu(size image.Point) *Menu {
func NewMenu(size image.Point, root *menu.Menu) *Menu {
w := new(Menu)
w.Extend(w)
w.Resize(size)
@@ -30,32 +29,16 @@ func (w *Menu) String() string {
return "menu"
}
func (w *Menu) SetItems(items []MenuItem) *Menu {
func (w *Menu) Update(root *menu.Menu) *Menu {
w.mu.Lock()
w.items = append([]MenuItem{{
Text: "Go back",
Icon: "md_history",
Type: reflect.TypeOf(""),
}}, items...)
w.index = 0
w.Root = root
w.curr = nil
w.next = root
w.mu.Unlock()
w.changed.Store(true)
return w
}
func (w *Menu) Change(delta int) int {
w.mu.Lock()
if l := len(w.items); l == 0 {
w.index = 0
} else {
w.index = (w.index + delta + len(w.items)) % len(w.items)
}
index := w.index
w.mu.Unlock()
w.changed.Store(true)
return index
}
func (w *Menu) Redraw() {
w.changed.Store(true)
}
@@ -70,86 +53,53 @@ func (w *Menu) Refresh(ctx app.Context) bool {
size = bounds.Size()
)
if w.changed.CompareAndSwap(true, false) || w.buffer == nil {
if w.changed.CompareAndSwap(true, false) || w.buffer == nil || (w.curr != w.next && w.next != nil) {
if w.buffer == nil {
w.buffer = app.BufferFor(ctx, size)
w.Root.SetPageSize(size.Y / 16)
}
if w.curr != w.next && w.next != nil {
w.curr, w.next = w.next, nil
}
// Clear the buffer.
app.ImageClear(w.buffer)
// Calculate the start and end indices for the current page
var (
itemWidth = size.X - 8
itemHeight = 16
num = len(w.items)
max = size.Y / itemHeight
shift int
menu = w.curr
pageSize = menu.PageSize()
start = (menu.Position / pageSize) * pageSize
end = start + pageSize
)
if w.index >= max {
shift = -(w.index - max)
} else {
shift = -w.index
if max := len(menu.Items); end > max {
end = max
}
for y := 0; y < max && y < num; y++ {
// Draw visible menu items.
logrus.WithFields(logrus.Fields{
"start": start,
"end": end,
"per page": pageSize,
}).Debug("showing menu items")
for i, item := range menu.Items[start:end] {
var (
index = (w.index + shift + y + num) % num
item = w.items[index]
rect = image.Rect(0, y*itemHeight, itemWidth, (y+1)*itemHeight-2)
fg image.Image = image.White
border color.Color = color.Gray{Y: 0x1f}
y = i * 16 // 16 pixels per line
rect = image.Rect(0, 0, size.X-8, 16).Add(image.Pt(0, y))
view = app.ImageView(w.buffer, rect)
isActive = start+i == menu.Position
)
if index == w.index {
draw.Box(w.buffer, rect, color.Gray{Y: 0x1f})
border = color.Gray{Y: 0xbf}
}
draw.Rectangle(w.buffer, rect, border)
//font.SM4TIK.Draw(w.buffer, rect.Min.Add(image.Pt(1, -2)), fg, string(item.Icon))
w.styles.Text.Font.Draw(w.buffer, rect.Min.Add(image.Pt(16, 3)), fg, item.Text)
var (
value = item.Value
kind = item.Type.Kind()
)
if kind == reflect.Ptr {
value = reflect.Indirect(value)
kind = value.Type().Kind()
}
switch kind {
case reflect.String, reflect.Struct:
if item.Icon == "" {
font.Icon(w.buffer, rect.Min.Add(image.Pt(3, 2)), color.White, "md_star")
} else {
font.Icon(w.buffer, rect.Min.Add(image.Pt(3, 2)), color.White, item.Icon)
}
case reflect.Bool:
if value.Bool() {
font.Icon(w.buffer, rect.Min.Add(image.Pt(3, 2)), color.White, "fa_toggle_on")
} else {
font.Icon(w.buffer, rect.Min.Add(image.Pt(3, 2)), color.White, "fa_toggle_off")
}
case reflect.Uint8:
font.Icon(w.buffer, rect.Min.Add(image.Pt(3, 2)), color.White, "fa_gauge")
value := uint8(value.Uint())
w.styles.Text.Font.Draw(w.buffer, rect.Min.Add(image.Pt(16, itemWidth-64)), fg, fmt.Sprintf("%d", value))
default:
logrus.WithField("type", kind).Warn("no menu item renderer!")
}
item.Render(view, isActive, menu.IsEditing(), &w.styles.Text)
}
// Draw scroll bar.
for y := 0; y < size.Y; y += 2 {
w.buffer.Set(size.X-2, y, color.Gray{Y: 0x1f})
}
if len(w.items) > max {
y := size.Y / len(w.items) * w.index
draw.Box(w.buffer, image.Rect(size.X-3, y, size.X, y+size.Y/len(w.items)), color.Gray{Y: 0x7f})
if len(menu.Items) > pageSize {
y := size.Y / len(menu.Items) * menu.Position
draw.Box(w.buffer, image.Rect(size.X-3, y, size.X, y+size.Y/len(menu.Items)), color.Gray{Y: 0x7f})
}
}
+2 -10
View File
@@ -123,11 +123,7 @@ func (w *Spectrum) Refresh(ctx app.Context) bool {
if w.changed.CompareAndSwap(true, false) {
if w.buffer == nil {
w.buffer = app.BufferFor(ctx, bounds.Size())
if w.styles.Window.Background != nil {
app.ImageCopy(w.buffer, w.styles.Window.Background)
} else {
app.ImageFill(w.buffer, w.styles.Window.BackgroundColor)
}
app.ImageCopy(w.buffer, w.styles.Window.Background())
}
if w.waterfall {
@@ -143,11 +139,7 @@ func (w *Spectrum) Refresh(ctx app.Context) bool {
}
func (w *Spectrum) refreshAnalyzer(bounds image.Rectangle) {
if w.styles.Window.Background != nil {
app.ImageCopy(w.buffer, w.styles.Window.Background)
} else {
app.ImageFill(w.buffer, w.styles.Window.BackgroundColor)
}
app.ImageCopy(w.buffer, w.styles.Window.Background())
var (
width = bounds.Dx()
+2 -20
View File
@@ -124,29 +124,11 @@ func (w *Text) Refresh(ctx app.Context) bool {
}
// Fill background.
if w.styles.Text.Background != nil {
log.Trace("fill canvas with background")
app.ImageCopy(w.buffer, w.styles.Text.Background)
} else {
log.WithField("color", w.styles.Text.BackgroundColor).
Trace("fill canvas with background color")
app.ImageFill(w.buffer, w.styles.Text.BackgroundColor)
}
app.ImageCopy(w.buffer, w.styles.Text.Background())
// Fill text.
inner := w.styles.Text.Inner(w.buffer)
if w.styles.Text.Foreground != nil {
log.WithField("text", w.text).
WithField("inner", inner).
Debug("text canvas with foreground mask")
w.styles.Text.Font.Draw(inner, w.scroll, w.styles.Text.Foreground, w.text)
} else {
log.WithField("text", w.text).
WithField("inner", inner).
WithField("color", w.styles.Text.ForegroundColor).
Debug("text canvas with foreground color")
w.styles.Text.Font.Draw(inner, w.scroll, image.NewUniform(w.styles.Text.ForegroundColor), w.text)
}
w.styles.Text.Font.Draw(inner, w.scroll, w.styles.Text.Foreground(), w.text)
}
// Scroll text if required.