Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:54 +01:00
1 parent c5d6137fab
commit 4ae7655cd8
223 files changed
+14770 -18352

No files matched your search

+170
View File
@@ -0,0 +1,170 @@
package widget
import (
"fmt"
"image"
"image/color"
"reflect"
"github.com/sirupsen/logrus"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/beatglow/app/font"
"github.com/BeatGlow/display/draw"
)
type Menu struct {
Base
items []MenuItem
index int
}
func NewMenu(size image.Point) *Menu {
w := new(Menu)
w.Extend(w)
w.Resize(size)
return w
}
func (w *Menu) String() string {
return "menu"
}
func (w *Menu) SetItems(items []MenuItem) *Menu {
w.mu.Lock()
w.items = append([]MenuItem{{
Text: "Go back",
Icon: "md_history",
Type: reflect.TypeOf(""),
}}, items...)
w.index = 0
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)
}
func (w *Menu) Refresh(ctx app.Context) bool {
if !w.IsVisible() {
return false
}
var (
bounds = w.Bounds()
size = bounds.Size()
)
if w.changed.CompareAndSwap(true, false) || w.buffer == nil {
if w.buffer == nil {
w.buffer = app.BufferFor(ctx, size)
}
app.ImageClear(w.buffer)
var (
itemWidth = size.X - 8
itemHeight = 16
num = len(w.items)
max = size.Y / itemHeight
shift int
)
if w.index >= max {
shift = -(w.index - max)
} else {
shift = -w.index
}
for y := 0; y < max && y < num; y++ {
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}
)
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!")
}
}
// 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})
}
}
app.ImageCopyBounds(ctx.Display(), w.buffer, bounds.Add(w.position.Load()))
return true
}
type MenuItem struct {
Icon string
Text string
Type reflect.Type
Value reflect.Value
}
var (
_ app.Widget = (*Menu)(nil)
)