aerc/lib/ui/tab.go

227 lines
4.6 KiB
Go
Raw Normal View History

2018-02-18 00:42:29 +00:00
package ui
import (
"github.com/gdamore/tcell"
"github.com/mattn/go-runewidth"
2018-02-18 00:42:29 +00:00
)
type Tabs struct {
Tabs []*Tab
TabStrip *TabStrip
TabContent *TabContent
Selected int
history []int
2018-02-18 00:42:29 +00: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)
tabs.history = []int{}
2018-02-18 00:42:29 +00:00
return tabs
}
func (tabs *Tabs) Add(content Drawable, name string) *Tab {
tab := &Tab{
2018-02-18 00:42:29 +00:00
Content: content,
Name: name,
}
tabs.Tabs = append(tabs.Tabs, tab)
2018-02-18 00:42:29 +00:00
tabs.TabStrip.Invalidate()
content.OnInvalidate(tabs.invalidateChild)
return tab
2018-02-18 00:42:29 +00:00
}
func (tabs *Tabs) invalidateChild(d Drawable) {
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 00:42:29 +00: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:]...)
tabs.removeHistory(i)
2018-02-18 00:42:29 +00:00
break
}
}
index, ok := tabs.popHistory()
if ok {
tabs.Select(index)
}
2018-02-18 00:42:29 +00:00
tabs.TabStrip.Invalidate()
}
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 00:42:29 +00:00
func (tabs *Tabs) Select(index int) {
2019-03-17 21:23:53 +00:00
if index >= len(tabs.Tabs) {
return
}
2018-02-18 00:42:29 +00:00
if tabs.Selected != index {
tabs.pushHistory(tabs.Selected)
2018-02-18 00:42:29 +00:00
tabs.Selected = index
tabs.TabStrip.Invalidate()
tabs.TabContent.Invalidate()
}
}
func (tabs *Tabs) SelectPrevious() bool {
index, ok := tabs.popHistory()
if !ok {
return false
}
tabs.Select(index)
return true
}
func (tabs *Tabs) pushHistory(index int) {
tabs.history = append(tabs.history, index)
}
func (tabs *Tabs) popHistory() (int, bool) {
lastIdx := len(tabs.history) - 1
if lastIdx < 0 {
return 0, false
}
item := tabs.history[lastIdx]
tabs.history = tabs.history[:lastIdx]
return item, true
}
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 11:56:08 +00:00
if i > 0 && len(newHist) > 0 && item == newHist[len(newHist)-1] {
continue
}
newHist = append(newHist, item)
}
tabs.history = newHist
}
func (tabs *Tabs) MouseEvent(event tcell.Event) {
switch event := event.(type) {
case *tcell.EventMouse:
if event.Buttons()&tcell.Button1 != 0 {
x, y := event.Position()
selectedTab, ok := tabs.TabStrip.Clicked(x, y)
if ok {
tabs.Select(selectedTab)
}
}
}
}
2018-02-18 00:42:29 +00:00
// TODO: Color repository
func (strip *TabStrip) Draw(ctx *Context) {
x := 0
for i, tab := range strip.Tabs {
style := tcell.StyleDefault.Reverse(true)
2018-02-18 00:42:29 +00:00
if strip.Selected == i {
style = tcell.StyleDefault
2018-02-18 00:42:29 +00:00
}
trunc := runewidth.Truncate(tab.Name, 32, "…")
x += ctx.Printf(x, 0, style, " %s ", trunc)
2018-02-18 00:42:29 +00:00
}
style := tcell.StyleDefault.Reverse(true)
ctx.Fill(x, 0, ctx.Width()-x, 1, ' ', style)
2018-02-18 00:42:29 +00:00
}
func (strip *TabStrip) Invalidate() {
if strip.onInvalidateStrip != nil {
strip.onInvalidateStrip(strip)
}
}
func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) {
strip.onInvalidateStrip = onInvalidate
}
func (strip *TabStrip) Clicked(mouseX int, mouseY int) (int, bool) {
x := 0
if mouseY == 0 {
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
}
x += length
}
}
return 0, false
}
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 00:42:29 +00:00
func (content *TabContent) Draw(ctx *Context) {
if content.Selected >= len(content.Tabs) {
width := ctx.Width()
height := ctx.Height()
ctx.Fill(0, 0, width, height, ' ', tcell.StyleDefault)
}
2018-02-18 00:42:29 +00:00
tab := content.Tabs[content.Selected]
tab.Content.Draw(ctx)
}
func (content *TabContent) Invalidate() {
if content.onInvalidateContent != nil {
content.onInvalidateContent(content)
}
2018-06-12 00:13:02 +00:00
tab := content.Tabs[content.Selected]
tab.Content.Invalidate()
2018-02-18 00:42:29 +00:00
}
func (content *TabContent) OnInvalidate(onInvalidate func(d Drawable)) {
content.onInvalidateContent = onInvalidate
}