mirror of
https://github.com/jroimartin/gocui.git
synced 2025-04-24 13:48:51 +08:00
Refactoring of Gui.onKey()
This commit is contained in:
parent
311dedb655
commit
0193dee642
16
gui.go
16
gui.go
@ -461,10 +461,20 @@ func (g *Gui) onKey(ev *termbox.Event) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var cv string
|
||||
if g.currentView != nil {
|
||||
cv = g.currentView.name
|
||||
|
||||
}
|
||||
for _, kb := range g.keybindings {
|
||||
if kb.h != nil && ev.Ch == kb.ch && Key(ev.Key) == kb.key && Modifier(ev.Mod) == kb.mod &&
|
||||
(kb.viewName == "" || (g.currentView != nil && kb.viewName == g.currentView.name)) {
|
||||
return kb.h(g, g.currentView)
|
||||
if kb.h == nil {
|
||||
continue
|
||||
}
|
||||
if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(cv) {
|
||||
if err := kb.h(g, g.currentView); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -120,3 +120,13 @@ func newKeybinding(viewname string, key Key, ch rune, mod Modifier, h Keybinding
|
||||
}
|
||||
return kb
|
||||
}
|
||||
|
||||
// match returns if the keybinding matches the keypress
|
||||
func (kb *keybinding) matchKeypress(key Key, ch rune, mod Modifier) bool {
|
||||
return kb.key == key && kb.ch == ch && kb.mod == mod
|
||||
}
|
||||
|
||||
// match returns if the keybinding matches the current view
|
||||
func (kb *keybinding) matchView(viewname string) bool {
|
||||
return kb.viewName == "" || kb.viewName == viewname
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user