2018-02-18 01:42:29 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2018-06-01 09:58:00 +02:00
|
|
|
"github.com/gdamore/tcell"
|
2019-05-14 22:18:21 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2018-02-18 01:42:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Tabs struct {
|
|
|
|
Tabs []*Tab
|
|
|
|
TabStrip *TabStrip
|
|
|
|
TabContent *TabContent
|
|
|
|
Selected int
|
2019-06-02 20:13:18 +02:00
|
|
|
history []int
|
2018-02-18 01:42:29 +01:00
|
|
|
|
|
|
|
onInvalidateStrip func(d Drawable)
|
|
|
|
onInvalidateContent func(d Drawable)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tab struct {
|
|
|
|
Content Drawable
|
|
|
|
Name string
|
|
|
|
invalid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type TabStrip Tabs
|
|
|
|
type TabContent Tabs
|
|
|
|
|
|
|
|
func NewTabs() *Tabs {
|
|
|
|
tabs := &Tabs{}
|
|
|
|
tabs.TabStrip = (*TabStrip)(tabs)
|
|
|
|
tabs.TabContent = (*TabContent)(tabs)
|
2019-06-02 20:13:18 +02:00
|
|
|
tabs.history = []int{0}
|
2018-02-18 01:42:29 +01:00
|
|
|
return tabs
|
|
|
|
}
|
|
|
|
|
2019-03-17 21:19:15 +01:00
|
|
|
func (tabs *Tabs) Add(content Drawable, name string) *Tab {
|
|
|
|
tab := &Tab{
|
2018-02-18 01:42:29 +01:00
|
|
|
Content: content,
|
|
|
|
Name: name,
|
2019-03-17 21:19:15 +01:00
|
|
|
}
|
|
|
|
tabs.Tabs = append(tabs.Tabs, tab)
|
2018-02-18 01:42:29 +01:00
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
content.OnInvalidate(tabs.invalidateChild)
|
2019-03-17 21:19:15 +01:00
|
|
|
return tab
|
2018-02-18 01:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) invalidateChild(d Drawable) {
|
2018-06-13 07:00:57 +02:00
|
|
|
if tabs.Selected >= len(tabs.Tabs) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tabs.Tabs[tabs.Selected].Content == d {
|
|
|
|
if tabs.onInvalidateContent != nil {
|
|
|
|
tabs.onInvalidateContent(tabs.TabContent)
|
2018-02-18 01:42:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) Remove(content Drawable) {
|
|
|
|
for i, tab := range tabs.Tabs {
|
|
|
|
if tab.Content == content {
|
|
|
|
tabs.Tabs = append(tabs.Tabs[:i], tabs.Tabs[i+1:]...)
|
2019-06-02 20:13:18 +02:00
|
|
|
tabs.removeHistory(i)
|
2018-02-18 01:42:29 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 20:13:18 +02:00
|
|
|
tabs.Select(tabs.popHistory())
|
2018-02-18 01:42:29 +01:00
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) Select(index int) {
|
2019-03-17 22:23:53 +01:00
|
|
|
if index >= len(tabs.Tabs) {
|
2018-06-13 07:00:57 +02:00
|
|
|
panic("Tried to set tab index to a non-existing element")
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
if tabs.Selected != index {
|
|
|
|
tabs.Selected = index
|
2019-06-02 20:13:18 +02:00
|
|
|
tabs.pushHistory(index)
|
2018-02-18 01:42:29 +01:00
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
tabs.TabContent.Invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 20:13:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
// TODO: Color repository
|
|
|
|
func (strip *TabStrip) Draw(ctx *Context) {
|
|
|
|
x := 0
|
|
|
|
for i, tab := range strip.Tabs {
|
2019-03-30 17:59:18 +01:00
|
|
|
style := tcell.StyleDefault.Reverse(true)
|
2018-02-18 01:42:29 +01:00
|
|
|
if strip.Selected == i {
|
2019-03-30 17:59:18 +01:00
|
|
|
style = tcell.StyleDefault
|
2018-02-18 01:42:29 +01:00
|
|
|
}
|
2019-05-14 22:18:21 +02:00
|
|
|
trunc := runewidth.Truncate(tab.Name, 32, "…")
|
|
|
|
x += ctx.Printf(x, 0, style, " %s ", trunc)
|
2018-02-18 01:42:29 +01:00
|
|
|
}
|
2019-03-30 17:59:18 +01:00
|
|
|
style := tcell.StyleDefault.Reverse(true)
|
2018-06-01 09:58:00 +02:00
|
|
|
ctx.Fill(x, 0, ctx.Width()-x, 1, ' ', style)
|
2018-02-18 01:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (strip *TabStrip) Invalidate() {
|
|
|
|
if strip.onInvalidateStrip != nil {
|
|
|
|
strip.onInvalidateStrip(strip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) {
|
|
|
|
strip.onInvalidateStrip = onInvalidate
|
|
|
|
}
|
|
|
|
|
2019-01-20 21:06:44 +01:00
|
|
|
func (content *TabContent) Children() []Drawable {
|
|
|
|
children := make([]Drawable, len(content.Tabs))
|
|
|
|
for i, tab := range content.Tabs {
|
|
|
|
children[i] = tab.Content
|
|
|
|
}
|
|
|
|
return children
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
func (content *TabContent) Draw(ctx *Context) {
|
2018-06-13 07:00:57 +02:00
|
|
|
if content.Selected >= len(content.Tabs) {
|
|
|
|
width := ctx.Width()
|
|
|
|
height := ctx.Height()
|
|
|
|
ctx.Fill(0, 0, width, height, ' ', tcell.StyleDefault)
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
tab := content.Tabs[content.Selected]
|
|
|
|
tab.Content.Draw(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (content *TabContent) Invalidate() {
|
|
|
|
if content.onInvalidateContent != nil {
|
|
|
|
content.onInvalidateContent(content)
|
|
|
|
}
|
2018-06-12 02:13:02 +02:00
|
|
|
tab := content.Tabs[content.Selected]
|
|
|
|
tab.Content.Invalidate()
|
2018-02-18 01:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (content *TabContent) OnInvalidate(onInvalidate func(d Drawable)) {
|
|
|
|
content.onInvalidateContent = onInvalidate
|
|
|
|
}
|