Really figuring some things out

This commit is contained in:
2025-08-07 11:18:03 -05:00
parent 7c96fbb187
commit b476c74683
23 changed files with 737 additions and 249 deletions

36
menu.go
View File

@@ -22,7 +22,9 @@ THE SOFTWARE.
package widgets
import (
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"fmt"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
@@ -55,6 +57,8 @@ const (
MenuTypeV
)
var _ Widget = (*Menu)(nil)
func NewMenu(id string, style tcell.Style) *Menu {
ret := &Menu{}
ret.Init(id, style)
@@ -79,8 +83,16 @@ func (w *Menu) Init(id string, style tcell.Style) {
})
w.tabbable = true
}
func (w *Menu) Id() string { return w.id }
func (w *Menu) HandleResize(ev *tcell.EventResize) {}
func (w *Menu) Id() string { return w.id }
func (w *Menu) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
w.w = wh.Min(w.w, w.WantW())
w.h = wh.Min(w.h, w.WantH())
// TODO: Trickle-down HandleResize
}
func (w *Menu) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
@@ -118,7 +130,7 @@ func (w *Menu) drawHMenu(screen tcell.Screen) {
}
x, y := w.x, w.y
if len(w.label) > 0 {
h.DrawText(x, y, w.label, st, screen)
wh.DrawText(x, y, w.label, st, screen)
x = x + len(w.label) + 2
}
x += 2
@@ -142,9 +154,9 @@ func (w *Menu) drawVMenu(screen tcell.Screen) {
st := w.style
if w.active {
st = w.style.Reverse(true)
h.TitledBorderFilled(x-1, y, x+w.WantW(), y+w.WantH(), w.label, h.BRD_CSIMPLE, w.style, screen)
wh.TitledBorderFilled(x-1, y, x+w.WantW(), y+w.WantH(), w.label, wh.BRD_CSIMPLE, w.style, screen)
}
h.DrawText(w.x, w.y, w.label, st, screen)
wh.DrawText(w.x, w.y, w.label, st, screen)
if w.expanded || (w.active && !w.manualExpand) {
// TODO: Use DrawOffset?
for i := range w.items {
@@ -214,7 +226,7 @@ func (w *Menu) MinW() int {
}
return wrk
case MenuTypeV:
return h.Longest(labels)
return wh.Longest(labels)
}
return 0
}
@@ -302,3 +314,13 @@ func (w *Menu) MoveDown(ev *tcell.EventKey) bool {
w.cursor = (w.cursor + 1) % len(w.items)
return true
}
func (w *Menu) CreateMenuItem(lbl string, do func() bool, subItems ...*MenuItem) *MenuItem {
d := NewMenuItem(fmt.Sprintf("menuitem-%s", lbl), tcell.StyleDefault)
d.SetLabel(lbl)
d.SetOnPressed(do)
if len(subItems) > 0 {
d.AddItems(subItems...)
}
return d
}