Fix keymaps. Backtab
This commit is contained in:
17
keymap.go
17
keymap.go
@@ -21,7 +21,9 @@ THE SOFTWARE.
|
|||||||
*/
|
*/
|
||||||
package widgets
|
package widgets
|
||||||
|
|
||||||
import "github.com/gdamore/tcell"
|
import (
|
||||||
|
"github.com/gdamore/tcell"
|
||||||
|
)
|
||||||
|
|
||||||
type Key struct {
|
type Key struct {
|
||||||
ev *tcell.EventKey
|
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 {
|
func (k *Key) AddBinding(do func(*tcell.EventKey) bool) *Key {
|
||||||
k.do = append(k.do, do)
|
k.do = append(k.do, do)
|
||||||
return k
|
return k
|
||||||
@@ -49,10 +53,16 @@ func (k *Key) SetDescription(d string) *Key {
|
|||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k *Key) Description() string { return k.desc }
|
||||||
|
|
||||||
func (k *Key) Matches(ev *tcell.EventKey) bool {
|
func (k *Key) Matches(ev *tcell.EventKey) bool {
|
||||||
|
if k.ev.Key() == tcell.KeyRune {
|
||||||
return k.ev.Key() == ev.Key() &&
|
return k.ev.Key() == ev.Key() &&
|
||||||
k.ev.Rune() == ev.Rune() &&
|
k.ev.Rune() == ev.Rune() &&
|
||||||
k.ev.Modifiers() == ev.Modifiers()
|
k.ev.Modifiers() == ev.Modifiers()
|
||||||
|
}
|
||||||
|
return k.ev.Key() == ev.Key() &&
|
||||||
|
k.ev.Modifiers() == ev.Modifiers()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Key) Handle(ev *tcell.EventKey) bool {
|
func (k *Key) Handle(ev *tcell.EventKey) bool {
|
||||||
@@ -68,11 +78,13 @@ func (k *Key) Handle(ev *tcell.EventKey) bool {
|
|||||||
|
|
||||||
type KeyMap struct {
|
type KeyMap struct {
|
||||||
Keys []*Key
|
Keys []*Key
|
||||||
|
logger func(string, ...any)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BlankKeyMap() *KeyMap { return &KeyMap{} }
|
func BlankKeyMap() *KeyMap { return &KeyMap{} }
|
||||||
func NewKeyMap(k *Key, rest ...*Key) *KeyMap {
|
func NewKeyMap(k *Key, rest ...*Key) *KeyMap {
|
||||||
ret := &KeyMap{}
|
ret := &KeyMap{}
|
||||||
|
ret.Add(k, rest...)
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +121,9 @@ func (m *KeyMap) Add(k *Key, rest ...*Key) {
|
|||||||
} else {
|
} else {
|
||||||
m.Keys = append(m.Keys, k)
|
m.Keys = append(m.Keys, k)
|
||||||
}
|
}
|
||||||
|
for i := range rest {
|
||||||
|
m.Add(rest[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *KeyMap) Remove(k *Key, rest ...*Key) {
|
func (m *KeyMap) Remove(k *Key, rest ...*Key) {
|
||||||
|
|||||||
@@ -88,15 +88,30 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
|
|||||||
active := w.findActive()
|
active := w.findActive()
|
||||||
if active == nil && len(w.widgets) > 0 {
|
if active == nil && len(w.widgets) > 0 {
|
||||||
// No widget is active, but we do have some
|
// No widget is active, but we do have some
|
||||||
|
for i := range w.widgets {
|
||||||
if w.widgets[0].Focusable() {
|
if w.widgets[i].Focusable() {
|
||||||
w.widgets[0].SetActive(true)
|
w.widgets[i].SetActive(true)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return w.ActivateNext()
|
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 }
|
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 {
|
NewKey(BuildEK(tcell.KeyEscape), func(ev *tcell.EventKey) bool {
|
||||||
return w.ToggleMenu()
|
return w.ToggleMenu()
|
||||||
}),
|
}),
|
||||||
NewKey(BuildEKr(' '), func(ev *tcell.EventKey) bool {
|
NewKey(tcell.NewEventKey(tcell.KeyRune, ' ', tcell.ModAlt), func(ev *tcell.EventKey) bool {
|
||||||
if ev.Modifiers()&tcell.ModAlt != 0 {
|
|
||||||
return w.ToggleMenu()
|
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
|
// Pass the key through to the main widget
|
||||||
if w.widget != nil {
|
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)
|
w.widget.SetActive(true)
|
||||||
ret := w.widget.HandleKey(ev)
|
ret := w.widget.HandleKey(ev)
|
||||||
w.widget.SetActive(true)
|
w.widget.SetActive(true)
|
||||||
|
|||||||
Reference in New Issue
Block a user