mirror of
https://github.com/jroimartin/gocui.git
synced 2025-05-01 22:17:55 +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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cv string
|
||||||
|
if g.currentView != nil {
|
||||||
|
cv = g.currentView.name
|
||||||
|
|
||||||
|
}
|
||||||
for _, kb := range g.keybindings {
|
for _, kb := range g.keybindings {
|
||||||
if kb.h != nil && ev.Ch == kb.ch && Key(ev.Key) == kb.key && Modifier(ev.Mod) == kb.mod &&
|
if kb.h == nil {
|
||||||
(kb.viewName == "" || (g.currentView != nil && kb.viewName == g.currentView.name)) {
|
continue
|
||||||
return kb.h(g, g.currentView)
|
}
|
||||||
|
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
|
return nil
|
||||||
|
@ -120,3 +120,13 @@ func newKeybinding(viewname string, key Key, ch rune, mod Modifier, h Keybinding
|
|||||||
}
|
}
|
||||||
return kb
|
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