Skip to content

Commit

Permalink
support tabs and parent views for bubbling up key presses
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Jun 29, 2019
1 parent 3219759 commit 117e4fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
37 changes: 28 additions & 9 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func (g *Gui) SetManager(managers ...Manager) {
g.currentView = nil
g.views = nil
g.keybindings = nil
g.tabClickBindings = nil

go func() { g.tbEvents <- termbox.Event{Type: termbox.EventResize} }()
}
Expand Down Expand Up @@ -509,14 +510,7 @@ func (g *Gui) flush() error {
continue
}
if v.Frame {
var fgColor, bgColor Attribute
if g.Highlight && v == g.currentView {
fgColor = g.SelFgColor
bgColor = g.SelBgColor
} else {
fgColor = g.FgColor
bgColor = g.BgColor
}
fgColor, bgColor := g.viewColors(v)

if err := g.drawFrameEdges(v, fgColor, bgColor); err != nil {
return err
Expand All @@ -543,6 +537,13 @@ func (g *Gui) flush() error {
return nil
}

func (g *Gui) viewColors(v *View) (Attribute, Attribute) {
if g.Highlight && v == g.currentView {
return g.SelFgColor, g.SelBgColor
}
return g.FgColor, g.BgColor
}

// drawFrameEdges draws the horizontal and vertical edges of a view.
func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error {
runeH, runeV := '─', '│'
Expand Down Expand Up @@ -668,10 +669,20 @@ func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error {
} else if x > v.x1-2 || x >= g.maxX {
break
}

currentFgColor := fgColor
currentBgColor := bgColor
// if you are the current view and you have multiple tabs, de-highlight the non-selected tabs
if v == g.currentView && len(v.Tabs) > 0 {
currentFgColor = v.FgColor
currentBgColor = v.BgColor
}

if i >= currentTabStart && i <= currentTabEnd {
currentFgColor = v.SelFgColor - AttrBold
currentFgColor = v.SelFgColor
if v != g.currentView {
currentFgColor -= AttrBold
}
currentBgColor = v.SelBgColor
}
if err := g.SetRune(x, v.y0, ch, currentFgColor, currentBgColor); err != nil {
Expand Down Expand Up @@ -787,6 +798,8 @@ func (g *Gui) onKey(ev *termbox.Event) error {
// and event. The value of matched is true if there is a match and no errors.
func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err error) {
var globalKb *keybinding
var matchingParentViewKb *keybinding

for _, kb := range g.keybindings {
if kb.handler == nil {
continue
Expand All @@ -797,10 +810,16 @@ func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err err
if kb.matchView(v) {
return g.execKeybinding(v, kb)
}
if kb.matchView(v.ParentView) {
matchingParentViewKb = kb
}
if kb.viewName == "" && ((v != nil && !v.Editable) || (kb.ch == 0 && kb.key != KeyCtrlU && kb.key != KeyCtrlA && kb.key != KeyCtrlE)) {
globalKb = kb
}
}
if matchingParentViewKb != nil {
return g.execKeybinding(v.ParentView, matchingParentViewKb)
}
if globalKb != nil {
return g.execKeybinding(v, globalKb)
}
Expand Down
3 changes: 3 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type View struct {

// IgnoreCarriageReturns tells us whether to ignore '\r' characters
IgnoreCarriageReturns bool

// ParentView is the view which catches events bubbled up from the given view if there's no matching handler
ParentView *View
}

type viewLine struct {
Expand Down

0 comments on commit 117e4fb

Please sign in to comment.