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

@@ -47,7 +47,7 @@ type Field struct {
filter func(tcell.EventKey) bool
onChange func(prev, curr string)
keyMap, customKeyMap KeyMap
keyMap *KeyMap
}
var _ Widget = (*Field)(nil)
@@ -66,36 +66,21 @@ func (w *Field) Init(id string, style tcell.Style) {
return wh.IsBS(ev) ||
wh.KeyIsDisplayable(ev)
}
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
tcell.KeyLeft: w.handleLeft,
tcell.KeyRight: w.handleRight,
tcell.KeyHome: w.handleHome,
tcell.KeyEnd: w.handleEnd,
tcell.KeyCtrlU: w.clearValueBeforeCursor,
})
w.customKeyMap = BlankKeyMap()
w.keyMap = NewKeyMap(
NewKey(BuildEK(tcell.KeyLeft), w.handleLeft),
NewKey(BuildEK(tcell.KeyRight), w.handleRight),
NewKey(BuildEK(tcell.KeyHome), w.handleHome),
NewKey(BuildEK(tcell.KeyEnd), w.handleEnd),
NewKey(BuildEK(tcell.KeyCtrlU), w.clearValueBeforeCursor),
)
w.focusable = true
}
func (w *Field) Id() string { return w.id }
func (w *Field) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() }
func (w *Field) SetKeyMap(km KeyMap, def bool) {
if def {
w.keyMap = km
} else {
w.customKeyMap = km
}
}
func (w *Field) AddToKeyMap(km KeyMap) { w.customKeyMap.Merge(km) }
func (w *Field) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.customKeyMap.Remove(k)
}
for r := range km.Runes {
w.customKeyMap.RemoveRune(r)
}
}
func (w *Field) GetKeyMap() *KeyMap { return w.keyMap }
func (w *Field) SetKeyMap(km *KeyMap) { w.keyMap = km }
func (w *Field) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
@@ -114,9 +99,7 @@ func (w *Field) HandleKey(ev *tcell.EventKey) bool {
return w.handleBackspace(ev)
}
}
b1 := w.keyMap.Handle(ev)
b2 := w.customKeyMap.Handle(ev)
if b1 || b2 {
if w.keyMap.Handle(ev) {
return true
}
if w.filter != nil && !w.filter(*ev) {