Some Updates

This commit is contained in:
2025-10-02 12:12:52 -05:00
parent 998531f95d
commit 4b647d9d41
9 changed files with 150 additions and 24 deletions

View File

@@ -29,6 +29,7 @@ import (
"github.com/gdamore/tcell"
)
// TODO: Sub-Menus
type MenuItem struct {
id string
label string
@@ -44,6 +45,8 @@ type MenuItem struct {
items []*MenuItem
onPressed func() bool
hotKey rune
manualExpand bool
expanded bool
disabled bool
@@ -101,6 +104,20 @@ func (w *MenuItem) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
// If the user hits the hotkey of this active item, call 'Enter' on it
if wh.RuneEqualsNC(ev.Rune(), w.hotKey) {
w.HandleKey(tcell.NewEventKey(tcell.KeyEnter, 0, 0))
}
for i := range w.items {
if wh.RuneEqualsNC(ev.Rune(), w.items[i].GetHotKey()) {
if w.items[i].Active() {
return w.items[i].HandleKey(ev)
}
w.cursor = i
w.updateActive()
return true
}
}
// Look for a sub-item that's selected
return w.keyMap.Handle(ev)
}
@@ -116,17 +133,29 @@ func (w *MenuItem) Draw(screen tcell.Screen) {
return
}
st := w.style.Dim(w.disabled).Italic(w.disabled)
// st := w.style.Reverse(w.active).Dim(w.disabled).Italic(w.disabled)
x, y := w.x, w.y
// wd := w.w
fndHotKey := w.GetHotKey() != 0
if w.expanded {
if len(w.items) > 0 {
wh.DrawText(x, y, fmt.Sprintf("╭%s╮", w.label), st, screen)
wh.DrawText(x+1, y, fmt.Sprintf("%s", w.label), st.Reverse(true), screen)
y += 1
if len(w.items) > 0 {
wh.TitledBorderFilled(w.x-1, y, w.x+w.WantW(), y+w.WantH(), fmt.Sprintf("╯%s╰", strings.Repeat(" ", len(w.label))), wh.BRD_CSIMPLE, w.style, screen)
screen.SetContent(x, y, '╭', nil, st)
x += 1
for i := range w.label {
if fndHotKey && wh.RuneEqualsNC(rune(w.label[i]), w.hotKey) {
screen.SetContent(x, y, rune(w.label[i]), nil, st.Reverse(true).Underline(true))
fndHotKey = false
} else {
screen.SetContent(x, y, rune(w.label[i]), nil, st.Reverse(true))
}
x += 1
}
screen.SetContent(x, y, '╮', nil, st)
x = w.x
y += 1
if len(w.items) == 0 {
return
}
wh.BorderFilled(w.x-1, y, w.x+w.WantW(), y+w.WantH(), wh.BRD_CSIMPLE, w.style, screen)
wh.DrawText(w.x, y, fmt.Sprintf("╯%s╰", strings.Repeat(" ", len(w.label))), w.style, screen)
x += 1
y += 1
// pos := w.GetPos()
@@ -138,10 +167,32 @@ func (w *MenuItem) Draw(screen tcell.Screen) {
y++
}
} else {
wh.DrawText(x, y, fmt.Sprintf(" %s ", w.label), st.Reverse(true), screen)
screen.SetContent(x, y, ' ', nil, st)
x += 1
for i := range w.label {
if fndHotKey && wh.RuneEqualsNC(rune(w.label[i]), w.hotKey) {
screen.SetContent(x, y, rune(w.label[i]), nil, st.Reverse(true).Underline(true))
fndHotKey = false
} else {
screen.SetContent(x, y, rune(w.label[i]), nil, st.Reverse(true))
}
x += 1
}
screen.SetContent(x, y, ' ', nil, st)
}
} else {
wh.DrawText(x, y, fmt.Sprintf(" %s ", w.label), st, screen)
screen.SetContent(x, y, ' ', nil, st)
x += 1
for i := range w.label {
if fndHotKey && wh.RuneEqualsNC(rune(w.label[i]), w.hotKey) {
screen.SetContent(x, y, rune(w.label[i]), nil, st.Underline(true))
fndHotKey = false
} else {
screen.SetContent(x, y, rune(w.label[i]), nil, st)
}
x += 1
}
screen.SetContent(x, y, ' ', nil, st)
}
}
@@ -167,7 +218,7 @@ func (w *MenuItem) GetH() int { return w.y }
func (w *MenuItem) MinW() int { return w.w }
func (w *MenuItem) MinH() int { return w.y }
func (w *MenuItem) WantW() int {
ret := len(w.label) + 2
ret := len(w.label) + 4
if len(w.items) > 0 {
for i := range w.items {
if w.items[i].WantW() > ret {
@@ -263,3 +314,6 @@ func (w *MenuItem) FindItem(id string) *MenuItem {
}
return nil
}
func (w *MenuItem) SetHotKey(r rune) { w.hotKey = r }
func (w *MenuItem) GetHotKey() rune { return w.hotKey }