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)
|
2019-09-06 00:32:36 +02:00
|
|
|
|
|
|
|
parent *Tabs
|
|
|
|
CloseTab func(index int)
|
2018-02-18 01:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Tab struct {
|
|
|
|
Content Drawable
|
|
|
|
Name string
|
|
|
|
invalid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type TabStrip Tabs
|
|
|
|
type TabContent Tabs
|
|
|
|
|
|
|
|
func NewTabs() *Tabs {
|
|
|
|
tabs := &Tabs{}
|
|
|
|
tabs.TabStrip = (*TabStrip)(tabs)
|
2019-09-06 00:32:36 +02:00
|
|
|
tabs.TabStrip.parent = tabs
|
2018-02-18 01:42:29 +01:00
|
|
|
tabs.TabContent = (*TabContent)(tabs)
|
2019-09-06 00:32:36 +02:00
|
|
|
tabs.TabContent.parent = tabs
|
2019-07-19 19:12:57 +02:00
|
|
|
tabs.history = []int{}
|
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) {
|
2019-10-10 14:24:42 +02:00
|
|
|
indexToRemove := -1
|
2018-02-18 01:42:29 +01:00
|
|
|
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)
|
2019-10-10 14:24:42 +02:00
|
|
|
indexToRemove = i
|
2018-02-18 01:42:29 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 14:24:42 +02:00
|
|
|
if indexToRemove < 0 {
|
2019-07-23 20:41:15 +02:00
|
|
|
return
|
|
|
|
}
|
2019-10-10 14:24:42 +02:00
|
|
|
// only pop the tab history if the closing tab is selected
|
|
|
|
if indexToRemove == tabs.Selected {
|
|
|
|
index, ok := tabs.popHistory()
|
2019-09-16 18:39:03 +02:00
|
|
|
if ok {
|
2019-10-10 14:24:42 +02:00
|
|
|
tabs.Select(index)
|
|
|
|
interactive, ok := tabs.Tabs[tabs.Selected].Content.(Interactive)
|
|
|
|
if ok {
|
|
|
|
interactive.Focus(true)
|
|
|
|
}
|
2019-09-16 18:39:03 +02:00
|
|
|
}
|
2019-10-10 14:24:42 +02:00
|
|
|
} else if indexToRemove < tabs.Selected {
|
|
|
|
// selected tab is now one to the left of where it was
|
|
|
|
tabs.Selected--
|
2019-07-19 19:12:57 +02:00
|
|
|
}
|
2018-02-18 01:42:29 +01:00
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-06-11 07:05:56 +02:00
|
|
|
func (tabs *Tabs) Replace(contentSrc Drawable, contentTarget Drawable, name string) {
|
|
|
|
replaceTab := &Tab{
|
|
|
|
Content: contentTarget,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
for i, tab := range tabs.Tabs {
|
|
|
|
if tab.Content == contentSrc {
|
|
|
|
tabs.Tabs[i] = replaceTab
|
|
|
|
tabs.Select(i)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
contentTarget.OnInvalidate(tabs.invalidateChild)
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
func (tabs *Tabs) Select(index int) {
|
2019-03-17 22:23:53 +01:00
|
|
|
if index >= len(tabs.Tabs) {
|
2019-07-26 20:37:45 +02:00
|
|
|
index = len(tabs.Tabs) - 1
|
2018-06-13 07:00:57 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
if tabs.Selected != index {
|
2019-10-10 14:09:45 +02:00
|
|
|
// only push valid tabs onto the history
|
|
|
|
if tabs.Selected < len(tabs.Tabs) {
|
|
|
|
tabs.pushHistory(tabs.Selected)
|
|
|
|
}
|
2018-02-18 01:42:29 +01:00
|
|
|
tabs.Selected = index
|
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
tabs.TabContent.Invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 19:12:57 +02:00
|
|
|
func (tabs *Tabs) SelectPrevious() bool {
|
|
|
|
index, ok := tabs.popHistory()
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tabs.Select(index)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:54:42 +01:00
|
|
|
func (tabs *Tabs) MoveTab(to int) {
|
|
|
|
from := tabs.Selected
|
|
|
|
|
|
|
|
if to < 0 {
|
|
|
|
to = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if to >= len(tabs.Tabs) {
|
|
|
|
to = len(tabs.Tabs) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
tab := tabs.Tabs[from]
|
|
|
|
if to > from {
|
|
|
|
copy(tabs.Tabs[from:to], tabs.Tabs[from+1:to+1])
|
|
|
|
for i, h := range tabs.history {
|
|
|
|
if h == from {
|
|
|
|
tabs.history[i] = to
|
|
|
|
}
|
|
|
|
if h > from && h <= to {
|
|
|
|
tabs.history[i] -= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if from > to {
|
|
|
|
copy(tabs.Tabs[to+1:from+1], tabs.Tabs[to:from])
|
|
|
|
for i, h := range tabs.history {
|
|
|
|
if h == from {
|
|
|
|
tabs.history[i] = to
|
|
|
|
}
|
|
|
|
if h >= to && h < from {
|
|
|
|
tabs.history[i] += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tabs.Tabs[to] = tab
|
|
|
|
tabs.Selected = to
|
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
func (tabs *Tabs) NextTab() {
|
|
|
|
next := tabs.Selected + 1
|
|
|
|
if next >= len(tabs.Tabs) {
|
|
|
|
next = 0
|
|
|
|
}
|
|
|
|
tabs.Select(next)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) PrevTab() {
|
|
|
|
next := tabs.Selected - 1
|
|
|
|
if next < 0 {
|
|
|
|
next = len(tabs.Tabs) - 1
|
|
|
|
}
|
|
|
|
tabs.Select(next)
|
|
|
|
}
|
|
|
|
|
2019-06-02 20:13:18 +02:00
|
|
|
func (tabs *Tabs) pushHistory(index int) {
|
|
|
|
tabs.history = append(tabs.history, index)
|
|
|
|
}
|
|
|
|
|
2019-07-19 19:12:57 +02:00
|
|
|
func (tabs *Tabs) popHistory() (int, bool) {
|
2019-06-02 20:13:18 +02:00
|
|
|
lastIdx := len(tabs.history) - 1
|
2019-07-19 19:12:57 +02:00
|
|
|
if lastIdx < 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2019-06-02 20:13:18 +02:00
|
|
|
item := tabs.history[lastIdx]
|
|
|
|
tabs.history = tabs.history[:lastIdx]
|
2019-07-19 19:12:57 +02:00
|
|
|
return item, true
|
2019-06-02 20:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-06-03 13:56:08 +02:00
|
|
|
if i > 0 && len(newHist) > 0 && item == newHist[len(newHist)-1] {
|
2019-06-02 20:13:18 +02:00
|
|
|
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-07-26 10:46:15 +02:00
|
|
|
tabWidth := 32
|
|
|
|
if ctx.Width()-x < tabWidth {
|
|
|
|
tabWidth = ctx.Width() - x - 2
|
|
|
|
}
|
|
|
|
trunc := runewidth.Truncate(tab.Name, tabWidth, "…")
|
2019-05-14 22:18:21 +02:00
|
|
|
x += ctx.Printf(x, 0, style, " %s ", trunc)
|
2019-07-26 10:46:15 +02:00
|
|
|
if x >= ctx.Width() {
|
|
|
|
break
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) {
|
|
|
|
changeFocus := func(focus bool) {
|
|
|
|
interactive, ok := strip.parent.Tabs[strip.parent.Selected].Content.(Interactive)
|
|
|
|
if ok {
|
|
|
|
interactive.Focus(focus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unfocus := func() { changeFocus(false) }
|
|
|
|
refocus := func() { changeFocus(true) }
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventMouse:
|
|
|
|
switch event.Buttons() {
|
|
|
|
case tcell.Button1:
|
|
|
|
selectedTab, ok := strip.Clicked(localX, localY)
|
|
|
|
if !ok || selectedTab == strip.parent.Selected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
unfocus()
|
|
|
|
strip.parent.Select(selectedTab)
|
|
|
|
refocus()
|
|
|
|
case tcell.WheelDown:
|
|
|
|
unfocus()
|
|
|
|
strip.parent.NextTab()
|
|
|
|
refocus()
|
|
|
|
case tcell.WheelUp:
|
|
|
|
unfocus()
|
|
|
|
strip.parent.PrevTab()
|
|
|
|
refocus()
|
|
|
|
case tcell.Button3:
|
|
|
|
selectedTab, ok := strip.Clicked(localX, localY)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
unfocus()
|
|
|
|
if selectedTab == strip.parent.Selected {
|
|
|
|
strip.parent.CloseTab(selectedTab)
|
|
|
|
} else {
|
|
|
|
current := strip.parent.Selected
|
|
|
|
strip.parent.CloseTab(selectedTab)
|
|
|
|
strip.parent.Select(current)
|
|
|
|
}
|
|
|
|
refocus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) {
|
|
|
|
strip.onInvalidateStrip = onInvalidate
|
|
|
|
}
|
|
|
|
|
2019-07-12 00:15:15 +02:00
|
|
|
func (strip *TabStrip) Clicked(mouseX int, mouseY int) (int, bool) {
|
|
|
|
x := 0
|
2019-09-06 00:32:36 +02:00
|
|
|
for i, tab := range strip.Tabs {
|
|
|
|
trunc := runewidth.Truncate(tab.Name, 32, "…")
|
|
|
|
length := len(trunc) + 2
|
|
|
|
if x <= mouseX && mouseX < x+length {
|
|
|
|
return i, true
|
2019-07-12 00:15:15 +02:00
|
|
|
}
|
2019-09-06 00:32:36 +02:00
|
|
|
x += length
|
2019-07-12 00:15:15 +02:00
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
func (content *TabContent) MouseEvent(localX int, localY int, event tcell.Event) {
|
|
|
|
tab := content.Tabs[content.Selected]
|
|
|
|
switch tabContent := tab.Content.(type) {
|
|
|
|
case Mouseable:
|
|
|
|
tabContent.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:42:29 +01:00
|
|
|
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
|
|
|
|
}
|