From 405bb85ed00344b4be653897ce88d5a7b8b92702 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 29 Jun 2019 11:27:21 +1000 Subject: [PATCH] support clicking on tabs --- Gopkg.lock | 4 +- pkg/config/app_config.go | 2 +- pkg/gui/keybindings.go | 5 ++ pkg/gui/main_panel.go | 26 ++++++++++ vendor/github.com/jesseduffield/gocui/gui.go | 47 +++++++++++++++---- vendor/github.com/jesseduffield/gocui/view.go | 17 +++++++ 6 files changed, 89 insertions(+), 12 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index a6cab8cf..5d2b3e83 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -131,11 +131,11 @@ [[projects]] branch = "master" - digest = "1:44d8be68f77843be5c779fcaf4793693a40ec96b83b5080a18bfd80123a4b2b2" + digest = "1:697738bf0065442016622def39cf22041c6f40df99d5315d845b0d528f96377f" name = "github.com/jesseduffield/gocui" packages = ["."] pruneopts = "NUT" - revision = "d99ac1a5a23de099e34cb8723e51114bb2b44d23" + revision = "32197598edc52af42d76b8cba78ed788074b4647" [[projects]] branch = "master" diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 530aaab5..0ac3c186 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -194,7 +194,7 @@ func GetDefaultConfig() UserConfig { Gui: GuiConfig{ ScrollHeight: 2, ScrollPastBottom: false, - MouseEvents: false, + MouseEvents: true, Theme: ThemeConfig{ ActiveBorderColor: []string{"green", "bold"}, InactiveBorderColor: []string{"white"}, diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index d75bfab4..6dbc3cad 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -414,5 +414,10 @@ func (gui *Gui) keybindings(g *gocui.Gui) error { return err } } + + if err := g.SetTabClickBinding("main", gui.onMainTabClick); err != nil { + return err + } + return nil } diff --git a/pkg/gui/main_panel.go b/pkg/gui/main_panel.go index 9ada6b2a..2037ff05 100644 --- a/pkg/gui/main_panel.go +++ b/pkg/gui/main_panel.go @@ -31,3 +31,29 @@ func (gui *Gui) autoScrollMain(g *gocui.Gui, v *gocui.View) error { gui.getMainView().Autoscroll = true return nil } + +func (gui *Gui) onMainTabClick(tabIndex int) error { + gui.Log.Warn(tabIndex) + + viewName := gui.currentViewName() + + switch viewName { + case "services": + gui.State.Panels.Services.ContextIndex = tabIndex + return gui.handleServiceSelect(gui.g, gui.getServicesView()) + case "containers": + gui.State.Panels.Containers.ContextIndex = tabIndex + return gui.handleContainerSelect(gui.g, gui.getContainersView()) + case "images": + gui.State.Panels.Images.ContextIndex = tabIndex + return gui.handleImageSelect(gui.g, gui.getImagesView()) + case "volumes": + gui.State.Panels.Volumes.ContextIndex = tabIndex + return gui.handleVolumeSelect(gui.g, gui.getVolumesView()) + case "status": + gui.State.Panels.Status.ContextIndex = tabIndex + return gui.handleStatusSelect(gui.g, gui.getStatusView()) + } + + return nil +} diff --git a/vendor/github.com/jesseduffield/gocui/gui.go b/vendor/github.com/jesseduffield/gocui/gui.go index f0864cf1..fe7dc403 100644 --- a/vendor/github.com/jesseduffield/gocui/gui.go +++ b/vendor/github.com/jesseduffield/gocui/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/vendor/github.com/jesseduffield/gocui/view.go b/vendor/github.com/jesseduffield/gocui/view.go index 043231af..6b4f0bb1 100644 --- a/vendor/github.com/jesseduffield/gocui/view.go +++ b/vendor/github.com/jesseduffield/gocui/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 +}