Fix keymaps. Backtab
This commit is contained in:
17
keymap.go
17
keymap.go
@@ -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,11 +53,17 @@ 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.Modifiers() == ev.Modifiers()
|
||||
}
|
||||
|
||||
func (k *Key) Handle(ev *tcell.EventKey) bool {
|
||||
if !k.Matches(ev) {
|
||||
@@ -68,11 +78,13 @@ func (k *Key) Handle(ev *tcell.EventKey) bool {
|
||||
|
||||
type KeyMap struct {
|
||||
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) {
|
||||
|
||||
@@ -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)
|
||||
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 }
|
||||
|
||||
@@ -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 {
|
||||
NewKey(tcell.NewEventKey(tcell.KeyRune, ' ', tcell.ModAlt), func(ev *tcell.EventKey) bool {
|
||||
return w.ToggleMenu()
|
||||
}
|
||||
return false
|
||||
}),
|
||||
)
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user