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

@@ -52,10 +52,12 @@ type LinearLayout struct {
active bool
visible bool
tabbable bool
focusable bool
disableTab bool
insetBorder bool
keyMap KeyMap
logger func(string, ...any)
}
@@ -78,10 +80,23 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
w.id = id
w.style = s
w.visible = true
w.tabbable = true
w.focusable = true
w.defFlags = LayoutFlag(LFAlignHCenter | LFAlignVCenter)
w.layoutFlags = make(map[Widget]LayoutFlag)
w.layoutWeights = make(map[Widget]int)
w.keyMap = BlankKeyMap()
w.keyMap.Add(tcell.KeyTab, func(ev *tcell.EventKey) bool {
active := w.findActive()
if active == nil && len(w.widgets) > 0 {
// No widget is active
if w.widgets[0].Focusable() {
w.widgets[0].SetActive(true)
return true
}
return false
}
return w.activateNext()
})
}
func (w *LinearLayout) Id() string { return w.id }
@@ -90,6 +105,17 @@ func (w *LinearLayout) HandleResize(ev *tcell.EventResize) {
w.updateWidgetLayouts()
}
func (w *LinearLayout) SetKeyMap(km KeyMap) { w.keyMap = km }
func (w *LinearLayout) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
func (w *LinearLayout) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.keyMap.Remove(k)
}
for r := range km.Runes {
w.keyMap.RemoveRune(r)
}
}
func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
if !w.active || w.disableTab {
return false
@@ -100,19 +126,7 @@ func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
return true
}
}
if ev.Key() == tcell.KeyTab {
if active == nil && len(w.widgets) > 0 {
// No widget is active
if w.widgets[0].Tabbable() {
w.widgets[0].SetActive(true)
return true
}
return false
}
return w.activateNext()
}
return false
return w.keyMap.Handle(ev)
}
func (w *LinearLayout) GetActive() Widget { return w.findActive() }
@@ -146,23 +160,22 @@ func (w *LinearLayout) SetActive(a bool) {
}
}
}
func (w *LinearLayout) Visible() bool { return w.visible }
func (w *LinearLayout) SetVisible(a bool) { w.visible = a }
func (w *LinearLayout) Focusable() bool { return true }
func (w *LinearLayout) SetTabbable(b bool) { w.tabbable = b }
func (w *LinearLayout) Tabbable() bool { return w.tabbable }
func (w *LinearLayout) SetX(x int) { w.x = x }
func (w *LinearLayout) SetY(y int) { w.y = y }
func (w *LinearLayout) GetX() int { return w.x }
func (w *LinearLayout) GetY() int { return w.y }
func (w *LinearLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *LinearLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *LinearLayout) GetW() int { return w.w }
func (w *LinearLayout) GetH() int { return w.h }
func (w *LinearLayout) SetW(wd int) { w.w = wd }
func (w *LinearLayout) SetH(h int) { w.h = h }
func (w *LinearLayout) getSize() Coord { return Coord{X: w.w, Y: w.h} }
func (w *LinearLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *LinearLayout) Visible() bool { return w.visible }
func (w *LinearLayout) SetVisible(a bool) { w.visible = a }
func (w *LinearLayout) Focusable() bool { return w.focusable }
func (w *LinearLayout) SetFocusable(b bool) { w.focusable = b }
func (w *LinearLayout) SetX(x int) { w.x = x }
func (w *LinearLayout) SetY(y int) { w.y = y }
func (w *LinearLayout) GetX() int { return w.x }
func (w *LinearLayout) GetY() int { return w.y }
func (w *LinearLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *LinearLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *LinearLayout) GetW() int { return w.w }
func (w *LinearLayout) GetH() int { return w.h }
func (w *LinearLayout) SetW(wd int) { w.w = wd }
func (w *LinearLayout) SetH(h int) { w.h = h }
func (w *LinearLayout) getSize() Coord { return Coord{X: w.w, Y: w.h} }
func (w *LinearLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *LinearLayout) WantW() int {
var wantW int
for _, wd := range w.widgets {
@@ -245,7 +258,7 @@ func (w *LinearLayout) findActiveOrFirst() Widget {
func (w *LinearLayout) activateNext() bool {
var found bool
for i := range w.widgets {
if found && w.widgets[i].Tabbable() {
if found && w.widgets[i].Focusable() {
w.widgets[i].SetActive(true)
return true
} else if w.widgets[i].Active() {
@@ -443,7 +456,9 @@ func (w *LinearLayout) updateLLVWidgetPos(wd Widget) {
if w.widgets[i] == wd {
break
}
c.Y = w.widgets[i].GetY() + w.widgets[i].GetH()
if w.widgets[i].Visible() {
c.Y = w.widgets[i].GetY() + w.widgets[i].GetH()
}
}
// Do we have a layout flag for this widget?
@@ -484,7 +499,9 @@ func (w *LinearLayout) updateLLHWidgetPos(wd Widget) {
}
break
}
c.X = w.widgets[i].GetX() + w.widgets[i].GetW()
if w.widgets[i].Visible() {
c.X = w.widgets[i].GetX() + w.widgets[i].GetW()
}
}
// Do we have a layout flag for this widget?