support clicking on tabs

This commit is contained in:
Jesse Duffield 2019-06-29 11:27:21 +10:00
parent 0e76f61359
commit 405bb85ed0
6 changed files with 89 additions and 12 deletions

4
Gopkg.lock generated
View file

@ -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"

View file

@ -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"},

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}