enforce that keybindings can only be handled by a single handler, prioritising view handlers over global handlers

This commit is contained in:
Jesse Duffield 2018-08-21 22:46:08 +10:00
parent 432b7f6215
commit 76a959bb4b
2 changed files with 21 additions and 10 deletions

28
gui.go
View File

@ -653,17 +653,31 @@ func (g *Gui) onKey(ev *termbox.Event) error {
// execKeybindings executes the keybinding handlers that match the passed view // 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. // 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) { func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err error) {
matched = false var globalKb *keybinding
for _, kb := range g.keybindings { for _, kb := range g.keybindings {
if kb.handler == nil { if kb.handler == nil {
continue continue
} }
if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(v) { if !kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) {
if err := kb.handler(g, v); err != nil { continue
return false, err }
} if kb.matchView(v) {
matched = true 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
} }

View File

@ -38,9 +38,6 @@ func (kb *keybinding) matchView(v *View) bool {
if v.Editable == true && kb.ch != 0 { if v.Editable == true && kb.ch != 0 {
return false return false
} }
if kb.viewName == "" {
return true
}
return v != nil && kb.viewName == v.name return v != nil && kb.viewName == v.name
} }