Fix keymaps. Backtab

This commit is contained in:
2025-10-26 11:33:46 -05:00
parent d63e3a414a
commit 8f6e52df41
3 changed files with 39 additions and 14 deletions

View File

@@ -21,7 +21,9 @@ THE SOFTWARE.
*/
package widgets
import "github.com/gdamore/tcell"
import (
"github.com/gdamore/tcell"
)
type Key struct {
ev *tcell.EventKey
@@ -39,6 +41,8 @@ func NewKey(ev *tcell.EventKey, do func(*tcell.EventKey) bool) *Key {
}
}
func (k *Key) EventKey() *tcell.EventKey { return k.ev }
func (k *Key) AddBinding(do func(*tcell.EventKey) bool) *Key {
k.do = append(k.do, do)
return k
@@ -49,9 +53,15 @@ func (k *Key) SetDescription(d string) *Key {
return k
}
func (k *Key) Description() string { return k.desc }
func (k *Key) Matches(ev *tcell.EventKey) bool {
if k.ev.Key() == tcell.KeyRune {
return k.ev.Key() == ev.Key() &&
k.ev.Rune() == ev.Rune() &&
k.ev.Modifiers() == ev.Modifiers()
}
return k.ev.Key() == ev.Key() &&
k.ev.Rune() == ev.Rune() &&
k.ev.Modifiers() == ev.Modifiers()
}
@@ -67,12 +77,14 @@ func (k *Key) Handle(ev *tcell.EventKey) bool {
}
type KeyMap struct {
Keys []*Key
Keys []*Key
logger func(string, ...any)
}
func BlankKeyMap() *KeyMap { return &KeyMap{} }
func NewKeyMap(k *Key, rest ...*Key) *KeyMap {
ret := &KeyMap{}
ret.Add(k, rest...)
return ret
}
@@ -109,6 +121,9 @@ func (m *KeyMap) Add(k *Key, rest ...*Key) {
} else {
m.Keys = append(m.Keys, k)
}
for i := range rest {
m.Add(rest[i])
}
}
func (m *KeyMap) Remove(k *Key, rest ...*Key) {

View File

@@ -88,15 +88,30 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
active := w.findActive()
if active == nil && len(w.widgets) > 0 {
// No widget is active, but we do have some
if w.widgets[0].Focusable() {
w.widgets[0].SetActive(true)
return true
for i := range w.widgets {
if w.widgets[i].Focusable() {
w.widgets[i].SetActive(true)
return true
}
}
return false
}
return w.ActivateNext()
}))
w.keyMap.Add(NewKey(BuildEK(tcell.KeyBacktab), func(ev *tcell.EventKey) bool {
active := w.findActive()
if active == nil && len(w.widgets) > 0 {
// No widget is active, but we do have some
for i := len(w.widgets) - 1; i >= 0; i-- {
if w.widgets[i].Focusable() {
w.widgets[i].SetActive(true)
return true
}
}
return false
}
return w.ActivatePrev()
}))
}
func (w *LinearLayout) Id() string { return w.id }

View File

@@ -72,11 +72,8 @@ func (w *TopMenuLayout) Init(id string, s tcell.Style) {
NewKey(BuildEK(tcell.KeyEscape), func(ev *tcell.EventKey) bool {
return w.ToggleMenu()
}),
NewKey(BuildEKr(' '), func(ev *tcell.EventKey) bool {
if ev.Modifiers()&tcell.ModAlt != 0 {
return w.ToggleMenu()
}
return false
NewKey(tcell.NewEventKey(tcell.KeyRune, ' ', tcell.ModAlt), func(ev *tcell.EventKey) bool {
return w.ToggleMenu()
}),
)
}
@@ -136,8 +133,6 @@ func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
}
// Pass the key through to the main widget
if w.widget != nil {
// If we're here, that means the menu isn't active, but we are. So the
// widget should be.
w.widget.SetActive(true)
ret := w.widget.HandleKey(ev)
w.widget.SetActive(true)