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

@@ -51,7 +51,7 @@ type Chat struct {
history []ChatMsg
historyPosition int
keyMap, customKeyMap KeyMap
keyMap *KeyMap
}
var _ Widget = (*Chat)(nil)
@@ -74,26 +74,8 @@ func (w *Chat) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
}
func (w *Chat) SetKeyMap(km KeyMap, def bool) {
if def {
w.keyMap = km
} else {
w.customKeyMap = km
}
}
func (w *Chat) AddToKeyMap(km KeyMap) {
w.customKeyMap.Merge(km)
}
func (w *Chat) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.customKeyMap.Remove(k)
}
for r := range km.Runes {
w.customKeyMap.RemoveRune(r)
}
}
func (w *Chat) GetKeyMap() *KeyMap { return w.keyMap }
func (w *Chat) SetKeyMap(km *KeyMap) { w.keyMap = km }
func (w *Chat) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
@@ -111,9 +93,7 @@ func (w *Chat) HandleKey(ev *tcell.EventKey) bool {
w.value = w.value[:w.cursor] + w.value[w.cursor+1:]
}
}
b1 := w.keyMap.Handle(ev)
b2 := w.customKeyMap.Handle(ev)
if b1 || b2 {
if w.keyMap.Handle(ev) {
return true
}
@@ -201,12 +181,12 @@ func (w *Chat) MinW() int { return 2 + 20 }
func (w *Chat) MinH() int { return 6 }
func (w *Chat) initKeyMap() {
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
tcell.KeyEsc: func(ev *tcell.EventKey) bool {
w.keyMap = NewKeyMap(
NewKey(BuildEK(tcell.KeyEsc), func(ev *tcell.EventKey) bool {
w.SetActive(false)
return true
},
tcell.KeyUp: func(ev *tcell.EventKey) bool {
}),
NewKey(BuildEK(tcell.KeyUp), func(ev *tcell.EventKey) bool {
if w.historyPosition < len(w.history)-1 {
w.historyPosition++
w.value = w.history[w.historyPosition].Message
@@ -214,8 +194,8 @@ func (w *Chat) initKeyMap() {
return true
}
return false
},
tcell.KeyDown: func(ev *tcell.EventKey) bool {
}),
NewKey(BuildEK(tcell.KeyDown), func(ev *tcell.EventKey) bool {
if w.historyPosition > -1 {
w.historyPosition--
if w.historyPosition == -1 {
@@ -227,37 +207,36 @@ func (w *Chat) initKeyMap() {
return true
}
return false
},
tcell.KeyLeft: func(ev *tcell.EventKey) bool {
}),
NewKey(BuildEK(tcell.KeyLeft), func(ev *tcell.EventKey) bool {
if w.cursor > 0 {
w.cursor--
return true
}
return false
},
tcell.KeyRight: func(ev *tcell.EventKey) bool {
}),
NewKey(BuildEK(tcell.KeyRight), func(ev *tcell.EventKey) bool {
if w.cursor < len(w.value) {
w.cursor++
return true
}
return false
},
tcell.KeyCtrlU: func(ev *tcell.EventKey) bool {
}),
NewKey(BuildEK(tcell.KeyCtrlU), func(ev *tcell.EventKey) bool {
w.value = w.value[w.cursor:]
w.cursor = 0
return true
},
tcell.KeyTab: func(ev *tcell.EventKey) bool {
}),
NewKey(BuildEK(tcell.KeyTab), func(ev *tcell.EventKey) bool {
// Auto-complete
// TODO: Find best guess for current word
return false
},
tcell.KeyEnter: func(ev *tcell.EventKey) bool {
}),
NewKey(BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool {
// TODO: Submit the message
return false
},
})
w.customKeyMap = BlankKeyMap()
}),
)
}
func (w *Chat) SetTitle(ttl string) { w.title = ttl }
func (w *Chat) Title() string { return w.title }