Add SetCurrentView(). Remove dupped field Gui.curview

This commit is contained in:
Roi Martin (@nibble_ds) 2014-01-06 01:47:22 +01:00
parent 425741d93e
commit efa370bfa8
2 changed files with 46 additions and 10 deletions

View File

@ -20,6 +20,39 @@ func layout(g *gocui.Gui) error {
return nil
}
func focusMain(g *gocui.Gui, v *gocui.View) error {
return g.SetCurrentView("main")
}
func focusSide(g *gocui.Gui, v *gocui.View) error {
return g.SetCurrentView("side")
}
func focusCmdLine(g *gocui.Gui, v *gocui.View) error {
return g.SetCurrentView("cmdline")
}
func keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlM, 0, focusMain); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyCtrlS, 0, focusSide); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyCtrlL, 0, focusCmdLine); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyCtrlC, 0, quit); err != nil {
return err
}
if err := g.SetKeybinding("main", 'q', 0, quit); err != nil {
return err
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrorQuit
}
@ -35,13 +68,7 @@ func main() {
g.Layout = layout
if err := g.SetKeybinding("", gocui.KeyCtrlC, 0, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", 'q', 0, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("main", 'x', 0, quit); err != nil {
if err := keybindings(g); err != nil {
log.Panicln(err)
}

15
gui.go
View File

@ -10,10 +10,9 @@ var ErrorQuit error = errors.New("quit")
type Gui struct {
events chan termbox.Event
curview *View
CurrentView *View
views []*View
keybindings []*Keybinding
currentView *View
Layout func(*Gui) error
maxX, maxY int
BgColor, FgColor termbox.Attribute
@ -95,6 +94,16 @@ func (g *Gui) DeleteView(name string) (err error) {
return errors.New("unknown view")
}
func (g *Gui) SetCurrentView(name string) (err error) {
for _, v := range g.views {
if v.Name == name {
g.CurrentView = v
return nil
}
}
return errors.New("unknown view")
}
func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, cb KeybindingCB) (err error) {
var kb *Keybinding
@ -325,7 +334,7 @@ func horizontalRune(ch rune) bool {
func (g *Gui) onKey(ev *termbox.Event) (err error) {
for _, kb := range g.keybindings {
if ev.Ch == kb.Ch && Key(ev.Key) == kb.Key && Modifier(ev.Mod) == kb.Mod &&
(kb.ViewName == "" || (g.curview != nil && kb.ViewName == g.curview.Name)) {
(kb.ViewName == "" || (g.CurrentView != nil && kb.ViewName == g.CurrentView.Name)) {
if err := kb.CB(g, nil); err != nil {
return err
}