Add Tabs history
Fixes #77: When closing a tab, bring you back to the one you last had focused
This commit is contained in:
parent
3a5b4c229e
commit
92b10fcef5
|
@ -10,6 +10,7 @@ type Tabs struct {
|
||||||
TabStrip *TabStrip
|
TabStrip *TabStrip
|
||||||
TabContent *TabContent
|
TabContent *TabContent
|
||||||
Selected int
|
Selected int
|
||||||
|
history []int
|
||||||
|
|
||||||
onInvalidateStrip func(d Drawable)
|
onInvalidateStrip func(d Drawable)
|
||||||
onInvalidateContent func(d Drawable)
|
onInvalidateContent func(d Drawable)
|
||||||
|
@ -28,6 +29,7 @@ func NewTabs() *Tabs {
|
||||||
tabs := &Tabs{}
|
tabs := &Tabs{}
|
||||||
tabs.TabStrip = (*TabStrip)(tabs)
|
tabs.TabStrip = (*TabStrip)(tabs)
|
||||||
tabs.TabContent = (*TabContent)(tabs)
|
tabs.TabContent = (*TabContent)(tabs)
|
||||||
|
tabs.history = []int{0}
|
||||||
return tabs
|
return tabs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,13 +60,11 @@ func (tabs *Tabs) Remove(content Drawable) {
|
||||||
for i, tab := range tabs.Tabs {
|
for i, tab := range tabs.Tabs {
|
||||||
if tab.Content == content {
|
if tab.Content == content {
|
||||||
tabs.Tabs = append(tabs.Tabs[:i], tabs.Tabs[i+1:]...)
|
tabs.Tabs = append(tabs.Tabs[:i], tabs.Tabs[i+1:]...)
|
||||||
|
tabs.removeHistory(i)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Force the selected index into the existing range */
|
tabs.Select(tabs.popHistory())
|
||||||
if tabs.Selected >= len(tabs.Tabs) {
|
|
||||||
tabs.Select(tabs.Selected - 1)
|
|
||||||
}
|
|
||||||
tabs.TabStrip.Invalidate()
|
tabs.TabStrip.Invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,11 +75,41 @@ func (tabs *Tabs) Select(index int) {
|
||||||
|
|
||||||
if tabs.Selected != index {
|
if tabs.Selected != index {
|
||||||
tabs.Selected = index
|
tabs.Selected = index
|
||||||
|
tabs.pushHistory(index)
|
||||||
tabs.TabStrip.Invalidate()
|
tabs.TabStrip.Invalidate()
|
||||||
tabs.TabContent.Invalidate()
|
tabs.TabContent.Invalidate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tabs *Tabs) pushHistory(index int) {
|
||||||
|
tabs.history = append(tabs.history, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tabs *Tabs) popHistory() int {
|
||||||
|
lastIdx := len(tabs.history) - 1
|
||||||
|
item := tabs.history[lastIdx]
|
||||||
|
tabs.history = tabs.history[:lastIdx]
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tabs *Tabs) removeHistory(index int) {
|
||||||
|
newHist := make([]int, 0, len(tabs.history))
|
||||||
|
for i, item := range tabs.history {
|
||||||
|
if item == index {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if item > index {
|
||||||
|
item = item - 1
|
||||||
|
}
|
||||||
|
// dedup
|
||||||
|
if i > 0 && item == newHist[len(newHist)-1] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newHist = append(newHist, item)
|
||||||
|
}
|
||||||
|
tabs.history = newHist
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Color repository
|
// TODO: Color repository
|
||||||
func (strip *TabStrip) Draw(ctx *Context) {
|
func (strip *TabStrip) Draw(ctx *Context) {
|
||||||
x := 0
|
x := 0
|
||||||
|
|
Loading…
Reference in New Issue