Skip to content

Commit

Permalink
support setting tab keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Jun 29, 2019
1 parent d99ac1a commit 3219759
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
47 changes: 38 additions & 9 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3219759

Please sign in to comment.