mirror of
https://github.com/jroimartin/gocui.git
synced 2025-05-08 19:29:28 +08:00
enforce that keybindings can only be handled by a single handler, prioritising view handlers over global handlers
This commit is contained in:
parent
432b7f6215
commit
76a959bb4b
28
gui.go
28
gui.go
@ -653,17 +653,31 @@ func (g *Gui) onKey(ev *termbox.Event) error {
|
||||
// execKeybindings executes the keybinding handlers that match the passed view
|
||||
// and event. The value of matched is true if there is a match and no errors.
|
||||
func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err error) {
|
||||
matched = false
|
||||
var globalKb *keybinding
|
||||
for _, kb := range g.keybindings {
|
||||
if kb.handler == nil {
|
||||
continue
|
||||
}
|
||||
if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(v) {
|
||||
if err := kb.handler(g, v); err != nil {
|
||||
return false, err
|
||||
}
|
||||
matched = true
|
||||
if !kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) {
|
||||
continue
|
||||
}
|
||||
if kb.matchView(v) {
|
||||
return g.execKeybinding(v, kb)
|
||||
}
|
||||
if kb.viewName == "" {
|
||||
globalKb = kb
|
||||
}
|
||||
}
|
||||
return matched, nil
|
||||
if globalKb != nil {
|
||||
return g.execKeybinding(v, globalKb)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// execKeybinding executes a given keybinding
|
||||
func (g *Gui) execKeybinding(v *View, kb *keybinding) (bool, error) {
|
||||
if err := kb.handler(g, v); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
@ -38,9 +38,6 @@ func (kb *keybinding) matchView(v *View) bool {
|
||||
if v.Editable == true && kb.ch != 0 {
|
||||
return false
|
||||
}
|
||||
if kb.viewName == "" {
|
||||
return true
|
||||
}
|
||||
return v != nil && kb.viewName == v.name
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user