Much Work

- Definable KeyMaps
- Change 'Tabbable' to just use 'Focusable'
This commit is contained in:
2025-09-04 11:09:34 -05:00
parent c1db729bb3
commit f571b13a31
26 changed files with 881 additions and 615 deletions

View File

@@ -39,8 +39,12 @@ type TopMenuLayout struct {
menu *Menu
widget Widget
active bool
visible bool
active bool
visible bool
focusable bool
layoutFlags LayoutFlag
keyMap KeyMap
logger func(string, ...any)
}
@@ -62,9 +66,19 @@ func (w *TopMenuLayout) Init(id string, s tcell.Style) {
w.menu = NewMenu(fmt.Sprintf("%s.mainmenu", id), tcell.StyleDefault)
w.menu.SetActive(false)
w.menu.SetType(MenuTypeH)
w.menu.SetTabbable(false)
w.widget = NewBlankWidget("blank")
w.keyMap = BlankKeyMap()
w.keyMap.Add(tcell.KeyEscape, func(ev *tcell.EventKey) bool {
if w.menu != nil {
w.menu.SetActive(!w.menu.Active())
if w.widget != nil {
w.widget.SetActive(!w.menu.Active())
}
return true
}
return false
})
}
func (w *TopMenuLayout) Id() string { return w.id }
@@ -76,8 +90,35 @@ func (w *TopMenuLayout) HandleResize(ev *tcell.EventResize) {
}
if w.widget != nil {
w.widget.SetPos(Coord{X: 0, Y: 1})
w.widget.HandleResize(tcell.NewEventResize(w.w, w.h-1))
w.Log("Widget Size: %d,%d", w.widget.GetW(), w.widget.GetH())
x, y := 0, 1
switch w.layoutFlags.AlignH() {
case LFAlignHRight:
x = w.w - w.widget.GetW()
case LFAlignHCenter:
x = (w.w / 2) - (w.widget.GetW() / 2)
}
switch w.layoutFlags.AlignV() {
case LFAlignVBottom:
y = w.h - w.widget.GetH()
case LFAlignVCenter:
y = (w.h / 2) - (w.widget.GetH() / 2)
}
w.widget.SetPos(Coord{X: x, Y: y})
}
}
func (w *TopMenuLayout) SetKeyMap(km KeyMap) { w.keyMap = km }
func (w *TopMenuLayout) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
func (w *TopMenuLayout) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.keyMap.Remove(k)
}
for r := range km.Runes {
w.keyMap.RemoveRune(r)
}
}
@@ -85,17 +126,13 @@ func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
if ev.Key() == tcell.KeyEscape && w.menu != nil {
w.menu.SetActive(!w.menu.Active())
if w.widget != nil {
w.widget.SetActive(!w.menu.Active())
}
if w.keyMap.Handle(ev) {
return true
}
if w.menu.Active() {
if w.menu != nil && w.menu.Active() {
return w.menu.HandleKey(ev)
}
// Pass the key through to the main widget
if w.widget != nil {
// If we're here, that means the menu isn't active, but we are. So the
@@ -132,22 +169,21 @@ func (w *TopMenuLayout) SetActive(a bool) {
w.widget.SetActive(a)
}
}
func (w *TopMenuLayout) Visible() bool { return w.visible }
func (w *TopMenuLayout) SetVisible(a bool) { w.visible = a }
func (w *TopMenuLayout) SetX(x int) { w.x = x }
func (w *TopMenuLayout) SetY(y int) { w.y = y }
func (w *TopMenuLayout) GetX() int { return w.x }
func (w *TopMenuLayout) GetY() int { return w.y }
func (w *TopMenuLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *TopMenuLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *TopMenuLayout) SetW(x int) { w.w = x }
func (w *TopMenuLayout) SetH(y int) { w.h = y }
func (w *TopMenuLayout) GetW() int { return w.w }
func (w *TopMenuLayout) GetH() int { return w.y }
func (w *TopMenuLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *TopMenuLayout) Focusable() bool { return true }
func (w *TopMenuLayout) SetTabbable(b bool) {}
func (w *TopMenuLayout) Tabbable() bool { return true }
func (w *TopMenuLayout) Visible() bool { return w.visible }
func (w *TopMenuLayout) SetVisible(a bool) { w.visible = a }
func (w *TopMenuLayout) SetX(x int) { w.x = x }
func (w *TopMenuLayout) SetY(y int) { w.y = y }
func (w *TopMenuLayout) GetX() int { return w.x }
func (w *TopMenuLayout) GetY() int { return w.y }
func (w *TopMenuLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *TopMenuLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *TopMenuLayout) SetW(x int) { w.w = x }
func (w *TopMenuLayout) SetH(y int) { w.h = y }
func (w *TopMenuLayout) GetW() int { return w.w }
func (w *TopMenuLayout) GetH() int { return w.y }
func (w *TopMenuLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *TopMenuLayout) Focusable() bool { return w.focusable }
func (w *TopMenuLayout) SetFocusable(b bool) { w.focusable = b }
func (w *TopMenuLayout) WantW() int { return wh.MaxInt }
func (w *TopMenuLayout) WantH() int { return wh.MaxInt }
@@ -159,6 +195,24 @@ func (w *TopMenuLayout) AddMenuItems(iL ...*MenuItem) { w.menu.AddItems(iL...
func (w *TopMenuLayout) RemoveMenuItems(iL ...*MenuItem) { w.menu.RemoveItems(iL...) }
func (w *TopMenuLayout) SetWidget(wd Widget) { w.widget = wd }
func (w *TopMenuLayout) AddLayoutFlag(f LayoutFlag) {
if f.IsAlignH() {
w.layoutFlags.ClearAlignH()
}
if f.IsAlignV() {
w.layoutFlags.ClearAlignV()
}
w.layoutFlags.Add(f)
}
func (w *TopMenuLayout) RemoveLayoutFlag(f LayoutFlag) {
if f.IsAlignH() {
w.layoutFlags.ClearAlignH()
}
if f.IsAlignV() {
w.layoutFlags.ClearAlignV()
}
}
func (w *TopMenuLayout) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *TopMenuLayout) Log(txt string, args ...any) {
@@ -179,11 +233,9 @@ func (w *TopMenuLayout) CreateMenuItem(id, lbl string, do func() bool, subItems
func (w *TopMenuLayout) SetItemVisible(id string, v bool) bool {
if mi := w.FindItem(id); mi != nil {
w.Log("%s.SetItemVisible: Found Item: %s (Set Visible: %t)", w.Id(), id, v)
mi.SetVisible(v)
return true
}
w.Log("%s.SetItemVisible: Didn't find item with id: %s", w.Id(), id)
return false
}