Files
tcell-widgets/wdgt_menu_item.go
2025-10-10 16:46:29 -05:00

329 lines
8.6 KiB
Go

/*
Copyright © Brian Buller <brian@bullercodeworks.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package widgets
import (
"fmt"
"strings"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
// TODO: Sub-Menus
type MenuItem struct {
id string
label string
style tcell.Style
active bool
visible bool
focusable bool
x, y int
w, h int
menuType MenuType // TODO: Handle Horizontal
cursor int
items []*MenuItem
onPressed func() bool
hotKey rune
manualExpand bool
expanded bool
disabled bool
keyMap, customKeyMap KeyMap
}
var _ Widget = (*MenuItem)(nil)
func NewMenuItem(id string, style tcell.Style) *MenuItem {
ret := &MenuItem{}
ret.Init(id, style)
return ret
}
func (w *MenuItem) Init(id string, style tcell.Style) {
w.id = id
w.style = style
w.visible = true
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
tcell.KeyUp: w.MoveUp,
tcell.KeyDown: w.MoveDown,
tcell.KeyEnter: func(ev *tcell.EventKey) bool {
if w.onPressed != nil {
return w.onPressed()
} else if len(w.items) > w.cursor && w.items[w.cursor].HandleKey(ev) {
return true
}
return false
},
})
w.customKeyMap = BlankKeyMap()
for i := range w.items {
w.items[i].SetActive(i == w.cursor)
}
w.focusable = true
}
func (w *MenuItem) Id() string { return w.id }
func (w *MenuItem) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
// TODO: Trickle-down HandleResize
}
func (w *MenuItem) SetKeyMap(km KeyMap, def bool) {
if def {
w.keyMap = km
} else {
w.customKeyMap = km
}
}
func (w *MenuItem) AddToKeyMap(km KeyMap) { w.customKeyMap.Merge(km) }
func (w *MenuItem) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.customKeyMap.Remove(k)
}
for r := range km.Runes {
w.customKeyMap.RemoveRune(r)
}
}
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
b1, b2 := w.keyMap.Handle(ev), w.customKeyMap.Handle(ev)
return b1 || b2
}
func (w *MenuItem) HandleTime(ev *tcell.EventTime) {
for i := range w.items {
w.items[i].HandleTime(ev)
}
}
func (w *MenuItem) Draw(screen tcell.Screen) {
if !w.visible {
return
}
st := w.style.Dim(w.disabled).Italic(w.disabled)
x, y := w.x, w.y
fndHotKey := w.GetHotKey() != 0
if w.expanded {
if len(w.items) > 0 {
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()
for i := range w.items {
// TODO: Use DrawOffset
// pos.DrawOffset(w.items[i], screen)
w.items[i].SetPos(Coord{X: x, Y: y})
w.items[i].Draw(screen)
y++
}
} else {
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 {
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)
}
}
func (w *MenuItem) Active() bool { return w.active }
func (w *MenuItem) SetActive(a bool) {
w.active = a
if !w.manualExpand {
w.expanded = a
}
}
func (w *MenuItem) Visible() bool { return w.visible }
func (w *MenuItem) SetVisible(a bool) { w.visible = a }
func (w *MenuItem) SetX(x int) { w.x = x }
func (w *MenuItem) SetY(y int) { w.y = y }
func (w *MenuItem) GetX() int { return w.x }
func (w *MenuItem) GetY() int { return w.y }
func (w *MenuItem) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *MenuItem) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *MenuItem) SetW(x int) { w.w = x }
func (w *MenuItem) SetH(y int) { w.h = y }
func (w *MenuItem) GetW() int { return w.w }
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) + 4
if len(w.items) > 0 {
for i := range w.items {
if w.items[i].WantW() > ret {
ret = w.items[i].WantW() + 2
// TODO: Figure offset of subitems
}
}
}
return ret
}
func (w *MenuItem) WantH() int {
ret := 1
if len(w.items) > 0 {
for i := range len(w.items) {
ret += w.items[i].WantH()
}
}
return ret
}
func (w *MenuItem) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *MenuItem) Focusable() bool { return w.focusable && !w.disabled }
func (w *MenuItem) SetFocusable(b bool) { w.focusable = b }
// How much width this item wants
func (w *MenuItem) Expand(e bool) {
if w.manualExpand {
w.expanded = e
}
}
func (w *MenuItem) Expanded() bool { return w.expanded }
func (w *MenuItem) updateActive() {
for i := range w.items {
w.items[i].SetActive(i == w.cursor)
}
}
func (w *MenuItem) MoveUp(ev *tcell.EventKey) bool {
if len(w.items) == 0 {
return false
}
// Look for a previous enabled item
st := w.cursor
i := (w.cursor - 1 + len(w.items)) % len(w.items)
for ; i != st; i = (i - 1 + len(w.items)) % len(w.items) {
if !w.items[i].IsDisabled() && w.items[i].Visible() {
break
}
}
w.cursor = i
w.updateActive()
return true
}
func (w *MenuItem) MoveDown(ev *tcell.EventKey) bool {
if len(w.items) == 0 {
return false
}
// Look for next enabled item
st := w.cursor
i := (st + 1) % len(w.items)
for ; i != st; i = (i + 1) % len(w.items) {
if !w.items[i].IsDisabled() && w.items[i].Visible() {
break
}
}
w.cursor = i
w.updateActive()
return true
}
func (w *MenuItem) Label() string { return w.label }
func (w *MenuItem) SetLabel(lbl string) { w.label = lbl }
func (w *MenuItem) SetDisabled(d bool) { w.disabled = d }
func (w *MenuItem) IsDisabled() bool { return w.disabled }
func (w *MenuItem) GetItems() []*MenuItem { return w.items }
func (w *MenuItem) AddItems(iL ...*MenuItem) {
w.items = append(w.items, iL...)
for i := range w.items {
w.items[i].SetActive(i == w.cursor)
}
}
func (w *MenuItem) SetOnPressed(p func() bool) { w.onPressed = p }
func (w *MenuItem) FindItem(id string) *MenuItem {
for _, itm := range w.items {
if itm.Id() == id {
return itm
} else if wrk := itm.FindItem(id); wrk != nil {
return wrk
}
}
return nil
}
func (w *MenuItem) SetHotKey(r rune) { w.hotKey = r }
func (w *MenuItem) GetHotKey() rune { return w.hotKey }