Reworking Keymaps

This commit is contained in:
2025-10-26 08:47:07 -05:00
parent cf47b5a4e4
commit d63e3a414a
30 changed files with 384 additions and 715 deletions

View File

@@ -56,7 +56,7 @@ type LinearLayout struct {
disableTab bool
insetBorder bool
keyMap, customKeyMap KeyMap
keyMap *KeyMap
logger func(string, ...any)
}
@@ -84,8 +84,7 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
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 {
w.keyMap = NewKeyMap(NewKey(BuildEK(tcell.KeyTab), func(ev *tcell.EventKey) bool {
active := w.findActive()
if active == nil && len(w.widgets) > 0 {
// No widget is active, but we do have some
@@ -97,8 +96,7 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
return false
}
return w.ActivateNext()
})
w.customKeyMap = BlankKeyMap()
}))
}
func (w *LinearLayout) Id() string { return w.id }
@@ -107,22 +105,8 @@ func (w *LinearLayout) HandleResize(ev *tcell.EventResize) {
w.updateWidgetLayouts()
}
func (w *LinearLayout) SetKeyMap(km KeyMap, def bool) {
if def {
w.keyMap = km
} else {
w.customKeyMap = km
}
}
func (w *LinearLayout) AddToKeyMap(km KeyMap) { w.customKeyMap.Merge(km) }
func (w *LinearLayout) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.customKeyMap.Remove(k)
}
for r := range km.Runes {
w.customKeyMap.RemoveRune(r)
}
}
func (w *LinearLayout) GetKeyMap() *KeyMap { return w.keyMap }
func (w *LinearLayout) SetKeyMap(km *KeyMap) { w.keyMap = km }
func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
if !w.active || w.disableTab {
@@ -134,8 +118,7 @@ func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
return true
}
}
b1, b2 := w.keyMap.Handle(ev), w.customKeyMap.Handle(ev)
return b1 || b2
return w.keyMap.Handle(ev)
}
func (w *LinearLayout) GetActiveWidgetIdx() int { return w.findActiveIdx() }
@@ -335,6 +318,7 @@ func (w *LinearLayout) FindById(id string) Widget {
}
return nil
}
func (w *LinearLayout) Contains(n Widget) bool {
return w.IndexOf(n) >= 0
}