Files
tcell-widgets/menu.go
2025-06-18 07:13:07 -05:00

228 lines
5.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 (
h "git.bullercodeworks.com/brian/dhcli/helpers"
"github.com/gdamore/tcell"
)
type Menu struct {
id string
label string
style tcell.Style
active bool
visible bool
x, y int
w, h int
menuType MenuType
cursor int
items []Widget
onPressed func() bool
manualExpand bool
expanded bool
}
type MenuType int
const (
MenuTypeH = MenuType(iota)
MenuTypeV
)
func NewMenu(id string, style tcell.Style) *Menu {
ret := &Menu{style: style}
ret.Init(id)
return ret
}
func (w *Menu) Id() string { return w.id }
func (w *Menu) Init(id string) {
w.id = id
w.visible = true
}
func (w *Menu) HandleResize(ev *tcell.EventResize) {}
func (w *Menu) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
// See if the active menuitem consumes this event
if w.items[w.cursor].HandleKey(ev) {
return true
}
// Otherwise see if we handle it
if h.HandleKeys(*ev, map[tcell.Key]func() bool{
tcell.KeyRight: w.MoveRight,
tcell.KeyLeft: w.MoveLeft,
tcell.KeyUp: w.MoveUp,
tcell.KeyDown: w.MoveDown,
tcell.KeyEnter: func() bool {
if w.onPressed != nil {
return w.onPressed()
}
return false
},
}) {
return true
}
// See if we can find an item that matches the key pressed
return false
}
func (w *Menu) HandleTime(ev *tcell.EventTime) {}
func (w *Menu) Draw(screen tcell.Screen) {
switch w.menuType {
case MenuTypeH:
w.drawHMenu(screen)
case MenuTypeV:
w.drawVMenu(screen)
}
}
func (w *Menu) drawHMenu(screen tcell.Screen) {
st := w.style
if w.active {
st = w.style.Reverse(true)
}
x, y := w.x, w.y
if len(w.label) > 0 {
h.DrawText(x, y, w.label, st, screen)
x = x + len(w.label) + 1
}
h.DrawText(x, y, "-", w.style, screen)
x += 2
for i := range w.items {
w.items[i].SetActive(w.active && w.cursor == i)
w.items[i].SetPos(Coord{X: x, Y: y})
w.items[i].Draw(screen)
x += w.items[i].WantW() + 1
}
}
func (w *Menu) drawVMenu(screen tcell.Screen) {
if !w.visible {
return
}
x, y := w.x, w.y
wW, wH := w.WantW(), w.WantH()
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)
}
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)
w.items[i].SetSize(Coord{X: wW, Y: wH})
w.items[i].SetPos(Coord{X: x, Y: y})
w.items[i].Draw(screen)
y++
}
y++
}
}
func (w *Menu) Active() bool { return w.active }
func (w *Menu) SetActive(a bool) { w.active = a }
func (w *Menu) Visible() bool { return w.visible }
func (w *Menu) SetVisible(a bool) { w.visible = a }
func (w *Menu) SetX(x int) { w.x = x }
func (w *Menu) SetY(y int) { w.y = y }
func (w *Menu) GetX() int { return w.x }
func (w *Menu) GetY() int { return w.y }
func (w *Menu) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *Menu) SetW(x int) { w.w = x }
func (w *Menu) SetH(y int) { w.h = y }
func (w *Menu) GetW() int { return w.w }
func (w *Menu) GetH() int { return w.y }
func (w *Menu) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *Menu) Focusable() bool { return true }
func (w *Menu) WantW() int {
var maxW int
for i := range w.items {
if w.items[i].WantW() > maxW {
maxW = w.items[i].WantW()
}
}
return maxW
}
func (w *Menu) WantH() int {
if w.menuType == MenuTypeH {
return 1
} else {
var ret int
if len(w.label) > 0 {
ret = 1
}
return ret + len(w.items)
}
}
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) SetOnPressed(p func() bool) { w.onPressed = p }
func (w *Menu) AddItems(iL ...Widget) {
var maxW int
for i := range iL {
if iL[i].WantW() > maxW {
maxW = iL[i].WantW()
}
w.items = append(w.items, iL[i])
}
w.SetW(maxW)
}
func (w *Menu) MoveRight() bool {
if w.menuType != MenuTypeH {
return false
}
w.cursor = (w.cursor + 1) % len(w.items)
return true
}
func (w *Menu) MoveLeft() bool {
if w.menuType != MenuTypeH {
return false
}
w.cursor = (w.cursor - 1 + len(w.items)) % len(w.items)
return true
}
func (w *Menu) MoveUp() bool {
if w.menuType != MenuTypeV {
return false
}
w.cursor = (w.cursor - 1 + len(w.items)) % len(w.items)
return true
}
func (w *Menu) MoveDown() bool {
if w.menuType != MenuTypeV {
return false
}
w.cursor = (w.cursor + 1) % len(w.items)
return true
}