Menu/MenuItem work

This commit is contained in:
2025-08-01 15:00:03 -05:00
parent 9e88799391
commit 1474caffaa
2 changed files with 89 additions and 9 deletions

30
menu.go
View File

@@ -37,7 +37,8 @@ type Menu struct {
menuType MenuType
cursor int
items []Widget
items []*MenuItem
disabled []bool
onPressed func() bool
manualExpand bool
expanded bool
@@ -134,7 +135,6 @@ func (w *Menu) drawVMenu(screen tcell.Screen) {
h.TitledBorderFilled(x-1, y, x+w.WantW(), y+w.WantH(), w.label, h.BRD_CSIMPLE, w.style, screen)
}
h.DrawText(w.x, w.y, w.label, st, screen)
y++
if w.expanded || (w.active && !w.manualExpand) {
for i := range w.items {
w.items[i].SetActive(w.active && w.cursor == i)
@@ -190,8 +190,14 @@ func (w *Menu) SetType(tp MenuType) { w.menuType = tp }
func (w *Menu) SetLabel(lbl string) { w.label = lbl }
func (w *Menu) SetFocusable(f bool) {}
func (w *Menu) GetItem(idx int) *MenuItem {
if len(w.items) > idx {
return w.items[idx]
}
return nil
}
func (w *Menu) SetOnPressed(p func() bool) { w.onPressed = p }
func (w *Menu) AddItems(iL ...Widget) {
func (w *Menu) AddItems(iL ...*MenuItem) {
var maxW int
for i := range iL {
if iL[i].WantW() > maxW {
@@ -202,6 +208,24 @@ func (w *Menu) AddItems(iL ...Widget) {
w.SetW(maxW)
}
func (w *Menu) RemoveItems(iL ...*MenuItem) {
var wrk []*MenuItem
for i := range w.items {
var skip bool
for j := range iL {
if w.items[i] == iL[j] {
skip = true
break
}
}
if skip {
continue
}
wrk = append(wrk, w.items[i])
}
w.items = wrk
}
func (w *Menu) MoveRight(ev *tcell.EventKey) bool {
if w.menuType != MenuTypeH {
return false