From 32197598edc52af42d76b8cba78ed788074b4647 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 29 Jun 2019 11:26:19 +1000 Subject: [PATCH] support setting tab keybindings --- gui.go | 47 ++++++++++++++++++++++++++++++++++++++--------- view.go | 17 +++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/gui.go b/gui.go index f0864cf1..fe7dc403 100644 --- a/gui.go +++ b/gui.go @@ -39,18 +39,26 @@ const ( Output216 = OutputMode(termbox.Output216) ) +type tabClickHandler func(int) error + +type tabClickBinding struct { + viewName string + handler tabClickHandler +} + // Gui represents the whole User Interface, including the views, layouts // and keybindings. type Gui struct { - tbEvents chan termbox.Event - userEvents chan userEvent - views []*View - currentView *View - managers []Manager - keybindings []*keybinding - maxX, maxY int - outputMode OutputMode - stop chan struct{} + tbEvents chan termbox.Event + userEvents chan userEvent + views []*View + currentView *View + managers []Manager + keybindings []*keybinding + tabClickBindings []*tabClickBinding + maxX, maxY int + outputMode OutputMode + stop chan struct{} // BgColor and FgColor allow to configure the background and foreground // colors of the GUI. @@ -325,6 +333,16 @@ func (g *Gui) DeleteKeybindings(viewname string) { g.keybindings = s } +// SetTabClickBinding sets a binding for a tab click event +func (g *Gui) SetTabClickBinding(viewName string, handler tabClickHandler) error { + g.tabClickBindings = append(g.tabClickBindings, &tabClickBinding{ + viewName: viewName, + handler: handler, + }) + + return nil +} + // getKey takes an empty interface with a key and returns the corresponding // typed Key or rune. func getKey(key interface{}) (Key, rune, error) { @@ -743,6 +761,17 @@ func (g *Gui) onKey(ev *termbox.Event) error { if err != nil { break } + if v.Frame && my == v.y0 { + if len(v.Tabs) > 0 { + tabIndex := v.GetClickedTabIndex(mx - v.x0) + + for _, binding := range g.tabClickBindings { + if binding.viewName == v.Name() { + return binding.handler(tabIndex) + } + } + } + } if err := v.SetCursor(mx-v.x0-1, my-v.y0-1); err != nil { return err } diff --git a/view.go b/view.go index 043231af..6b4f0bb1 100644 --- a/view.go +++ b/view.go @@ -627,3 +627,20 @@ func Loader() cell { func (v *View) IsTainted() bool { return v.tainted } + +// GetClickedTabIndex tells us which tab was clicked +func (v *View) GetClickedTabIndex(x int) int { + if len(v.Tabs) <= 1 { + return 0 + } + + charIndex := 0 + for i, tab := range v.Tabs { + charIndex += len(tab + " - ") + if x < charIndex { + return i + } + } + + return 0 +}