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

@@ -36,10 +36,10 @@ type Button struct {
x, y int
w, h int
active bool
visible bool
focusable bool
keyMap, customKeyMap KeyMap
active bool
visible bool
focusable bool
keyMap *KeyMap
onPressed func() bool
logger func(string, ...any)
@@ -57,10 +57,7 @@ func (w *Button) 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.KeyEnter: func(ev *tcell.EventKey) bool { return w.onPressed() },
})
w.customKeyMap = BlankKeyMap()
w.keyMap = NewKeyMap(NewKey(BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool { return w.onPressed() }))
w.onPressed = func() bool { return false }
w.focusable = true
}
@@ -69,30 +66,14 @@ func (w *Button) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
}
func (w *Button) SetKeyMap(km KeyMap, def bool) {
if def {
w.keyMap = km
} else {
w.customKeyMap = km
}
}
func (w *Button) AddToKeyMap(km KeyMap) { w.customKeyMap.Merge(km) }
func (w *Button) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.customKeyMap.Remove(k)
}
for r := range km.Runes {
w.customKeyMap.RemoveRune(r)
}
}
func (w *Button) GetKeyMap() *KeyMap { return w.keyMap }
func (w *Button) SetKeyMap(km *KeyMap) { w.keyMap = km }
func (w *Button) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
b1 := w.keyMap.Handle(ev)
b2 := w.customKeyMap.Handle(ev)
return b1 || b2
return w.keyMap.Handle(ev)
}
func (w *Button) HandleTime(ev *tcell.EventTime) {}