mirror of https://github.com/jroimartin/gocui.git
Refactoring
This commit is contained in:
parent
2a0623774f
commit
30f7d65597
41
gui.go
41
gui.go
|
@ -221,34 +221,20 @@ func (g *Gui) CurrentView() *View {
|
||||||
func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, h KeybindingHandler) error {
|
func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, h KeybindingHandler) error {
|
||||||
var kb *keybinding
|
var kb *keybinding
|
||||||
|
|
||||||
switch k := key.(type) {
|
k, ch, err := getKey(key)
|
||||||
case Key:
|
if err != nil {
|
||||||
kb = newKeybinding(viewname, k, 0, mod, h)
|
return err
|
||||||
case rune:
|
|
||||||
kb = newKeybinding(viewname, 0, k, mod, h)
|
|
||||||
default:
|
|
||||||
return errors.New("unknown type")
|
|
||||||
}
|
}
|
||||||
|
kb = newKeybinding(viewname, k, ch, mod, h)
|
||||||
g.keybindings = append(g.keybindings, kb)
|
g.keybindings = append(g.keybindings, kb)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteKeybinding deletes a keybinding.
|
// DeleteKeybinding deletes a keybinding.
|
||||||
func (g *Gui) DeleteKeybinding(viewname string, key interface{}, mod Modifier) error {
|
func (g *Gui) DeleteKeybinding(viewname string, key interface{}, mod Modifier) error {
|
||||||
var (
|
k, ch, err := getKey(key)
|
||||||
k Key
|
if err != nil {
|
||||||
ch rune
|
return err
|
||||||
)
|
|
||||||
|
|
||||||
switch t := key.(type) {
|
|
||||||
case Key:
|
|
||||||
k = t
|
|
||||||
ch = 0
|
|
||||||
case rune:
|
|
||||||
k = 0
|
|
||||||
ch = t
|
|
||||||
default:
|
|
||||||
return errors.New("unknown type")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, kb := range g.keybindings {
|
for i, kb := range g.keybindings {
|
||||||
|
@ -260,6 +246,19 @@ func (g *Gui) DeleteKeybinding(viewname string, key interface{}, mod Modifier) e
|
||||||
return errors.New("keybinding not found")
|
return errors.New("keybinding not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getKey takes an empty interface with a key and returns the corresponding
|
||||||
|
// typed Key or rune.
|
||||||
|
func getKey(key interface{}) (Key, rune, error) {
|
||||||
|
switch t := key.(type) {
|
||||||
|
case Key:
|
||||||
|
return t, 0, nil
|
||||||
|
case rune:
|
||||||
|
return 0, t, nil
|
||||||
|
default:
|
||||||
|
return 0, 0, errors.New("unknown type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Execute executes the given handler. This function can be called safely from
|
// Execute executes the given handler. This function can be called safely from
|
||||||
// a goroutine in order to update the GUI. It is important to note that it
|
// a goroutine in order to update the GUI. It is important to note that it
|
||||||
// won't be executed immediately, instead it will be added to the user events
|
// won't be executed immediately, instead it will be added to the user events
|
||||||
|
|
Loading…
Reference in New Issue