From efa370bfa8e04a71806015e49444db9102851ab3 Mon Sep 17 00:00:00 2001 From: "Roi Martin (@nibble_ds)" Date: Mon, 6 Jan 2014 01:47:22 +0100 Subject: [PATCH] Add SetCurrentView(). Remove dupped field Gui.curview --- _demos/demo2.go | 41 ++++++++++++++++++++++++++++++++++------- gui.go | 15 ++++++++++++--- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/_demos/demo2.go b/_demos/demo2.go index 8b6c84f..3979f4f 100644 --- a/_demos/demo2.go +++ b/_demos/demo2.go @@ -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) } diff --git a/gui.go b/gui.go index f0dc851..98dea91 100644 --- a/gui.go +++ b/gui.go @@ -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 }