2018-02-18 00:42:29 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2020-04-16 17:29:36 +00:00
|
|
|
"io"
|
2022-07-14 20:10:23 +00:00
|
|
|
"sync"
|
2020-04-16 17:29:36 +00:00
|
|
|
|
2020-11-30 22:07:03 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2019-05-14 20:18:21 +00:00
|
|
|
"github.com/mattn/go-runewidth"
|
2020-03-07 16:42:41 +00:00
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
2018-02-18 00:42:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Tabs struct {
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs []*Tab
|
2018-02-18 00:42:29 +00:00
|
|
|
TabStrip *TabStrip
|
|
|
|
TabContent *TabContent
|
2022-07-14 16:29:56 +00:00
|
|
|
curIndex int
|
2019-06-02 18:13:18 +00:00
|
|
|
history []int
|
2022-07-14 20:10:23 +00:00
|
|
|
m sync.Mutex
|
2018-02-18 00:42:29 +00:00
|
|
|
|
2020-03-07 16:42:41 +00:00
|
|
|
uiConfig *config.UIConfig
|
|
|
|
|
2018-02-18 00:42:29 +00:00
|
|
|
onInvalidateStrip func(d Drawable)
|
|
|
|
onInvalidateContent func(d Drawable)
|
2019-09-05 22:32:36 +00:00
|
|
|
|
|
|
|
parent *Tabs
|
|
|
|
CloseTab func(index int)
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Tab struct {
|
2020-03-07 16:42:41 +00:00
|
|
|
Content Drawable
|
|
|
|
Name string
|
|
|
|
invalid bool
|
|
|
|
pinned bool
|
|
|
|
indexBeforePin int
|
2022-04-16 23:03:49 +00:00
|
|
|
uiConf *config.UIConfig
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:16:40 +00:00
|
|
|
type (
|
|
|
|
TabStrip Tabs
|
|
|
|
TabContent Tabs
|
|
|
|
)
|
2018-02-18 00:42:29 +00:00
|
|
|
|
2020-03-07 16:42:41 +00:00
|
|
|
func NewTabs(uiConf *config.UIConfig) *Tabs {
|
2018-02-18 00:42:29 +00:00
|
|
|
tabs := &Tabs{}
|
2020-03-07 16:42:41 +00:00
|
|
|
tabs.uiConfig = uiConf
|
2018-02-18 00:42:29 +00:00
|
|
|
tabs.TabStrip = (*TabStrip)(tabs)
|
2019-09-05 22:32:36 +00:00
|
|
|
tabs.TabStrip.parent = tabs
|
2018-02-18 00:42:29 +00:00
|
|
|
tabs.TabContent = (*TabContent)(tabs)
|
2019-09-05 22:32:36 +00:00
|
|
|
tabs.TabContent.parent = tabs
|
2019-07-19 17:12:57 +00:00
|
|
|
tabs.history = []int{}
|
2018-02-18 00:42:29 +00:00
|
|
|
return tabs
|
|
|
|
}
|
|
|
|
|
2022-04-16 23:03:49 +00:00
|
|
|
func (tabs *Tabs) Add(content Drawable, name string, uiConf *config.UIConfig) *Tab {
|
2019-03-17 20:19:15 +00:00
|
|
|
tab := &Tab{
|
2018-02-18 00:42:29 +00:00
|
|
|
Content: content,
|
|
|
|
Name: name,
|
2022-04-16 23:03:49 +00:00
|
|
|
uiConf: uiConf,
|
2019-03-17 20:19:15 +00:00
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.tabs = append(tabs.tabs, tab)
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.selectPriv(len(tabs.tabs) - 1)
|
2018-02-18 00:42:29 +00:00
|
|
|
content.OnInvalidate(tabs.invalidateChild)
|
2019-03-17 20:19:15 +00:00
|
|
|
return tab
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
func (tabs *Tabs) Names() []string {
|
|
|
|
var names []string
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
2022-07-14 16:29:56 +00:00
|
|
|
for _, tab := range tabs.tabs {
|
|
|
|
names = append(names, tab.Name)
|
|
|
|
}
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Unlock()
|
2022-07-14 16:29:56 +00:00
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:42:29 +00:00
|
|
|
func (tabs *Tabs) invalidateChild(d Drawable) {
|
2022-07-14 16:29:56 +00:00
|
|
|
if tabs.curIndex >= len(tabs.tabs) {
|
2018-06-13 05:00:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
if tabs.tabs[tabs.curIndex].Content == d {
|
2018-06-13 05:00:57 +00:00
|
|
|
if tabs.onInvalidateContent != nil {
|
|
|
|
tabs.onInvalidateContent(tabs.TabContent)
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) Remove(content Drawable) {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2019-10-10 12:24:42 +00:00
|
|
|
indexToRemove := -1
|
2022-07-14 16:29:56 +00:00
|
|
|
for i, tab := range tabs.tabs {
|
2018-02-18 00:42:29 +00:00
|
|
|
if tab.Content == content {
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.tabs = append(tabs.tabs[:i], tabs.tabs[i+1:]...)
|
2022-07-18 09:00:05 +00:00
|
|
|
tabs.removeHistory(i)
|
2019-10-10 12:24:42 +00:00
|
|
|
indexToRemove = i
|
2018-02-18 00:42:29 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 12:24:42 +00:00
|
|
|
if indexToRemove < 0 {
|
2019-07-23 18:41:15 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-10 12:24:42 +00:00
|
|
|
// only pop the tab history if the closing tab is selected
|
2022-07-14 16:29:56 +00:00
|
|
|
if indexToRemove == tabs.curIndex {
|
2019-10-10 12:24:42 +00:00
|
|
|
index, ok := tabs.popHistory()
|
2019-09-16 16:39:03 +00:00
|
|
|
if ok {
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.selectPriv(index)
|
2019-09-16 16:39:03 +00:00
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
} else if indexToRemove < tabs.curIndex {
|
2019-10-10 12:24:42 +00:00
|
|
|
// selected tab is now one to the left of where it was
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.selectPriv(tabs.curIndex - 1)
|
2022-07-14 16:29:56 +00:00
|
|
|
}
|
|
|
|
interactive, ok := tabs.tabs[tabs.curIndex].Content.(Interactive)
|
|
|
|
if ok {
|
|
|
|
interactive.Focus(true)
|
2019-07-19 17:12:57 +00:00
|
|
|
}
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 05:05:56 +00:00
|
|
|
func (tabs *Tabs) Replace(contentSrc Drawable, contentTarget Drawable, name string) {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2019-06-11 05:05:56 +00:00
|
|
|
replaceTab := &Tab{
|
|
|
|
Content: contentTarget,
|
|
|
|
Name: name,
|
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
for i, tab := range tabs.tabs {
|
2019-06-11 05:05:56 +00:00
|
|
|
if tab.Content == contentSrc {
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.tabs[i] = replaceTab
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.selectPriv(i)
|
2020-04-16 17:29:36 +00:00
|
|
|
if c, ok := contentSrc.(io.Closer); ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
2019-06-11 05:05:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
contentTarget.OnInvalidate(tabs.invalidateChild)
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
func (tabs *Tabs) Get(index int) *Tab {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2022-07-14 16:29:56 +00:00
|
|
|
if index < 0 || index >= len(tabs.tabs) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return tabs.tabs[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) Selected() *Tab {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2022-07-14 16:29:56 +00:00
|
|
|
if tabs.curIndex < 0 || tabs.curIndex >= len(tabs.tabs) {
|
|
|
|
return nil
|
2018-06-13 05:00:57 +00:00
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
return tabs.tabs[tabs.curIndex]
|
|
|
|
}
|
2018-06-13 05:00:57 +00:00
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
func (tabs *Tabs) Select(index int) bool {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
|
|
|
return tabs.selectPriv(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) selectPriv(index int) bool {
|
2022-07-14 16:29:56 +00:00
|
|
|
if index < 0 || index >= len(tabs.tabs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if tabs.curIndex != index {
|
2019-10-10 12:09:45 +00:00
|
|
|
// only push valid tabs onto the history
|
2022-07-14 16:29:56 +00:00
|
|
|
if tabs.curIndex < len(tabs.tabs) {
|
|
|
|
tabs.pushHistory(tabs.curIndex)
|
2019-10-10 12:09:45 +00:00
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.curIndex = index
|
2018-02-18 00:42:29 +00:00
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
tabs.TabContent.Invalidate()
|
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) SelectName(name string) bool {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2022-07-14 16:29:56 +00:00
|
|
|
for i, tab := range tabs.tabs {
|
|
|
|
if tab.Name == name {
|
2022-07-14 20:10:23 +00:00
|
|
|
return tabs.selectPriv(i)
|
2022-07-14 16:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 17:12:57 +00:00
|
|
|
func (tabs *Tabs) SelectPrevious() bool {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2019-07-19 17:12:57 +00:00
|
|
|
index, ok := tabs.popHistory()
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-14 20:10:23 +00:00
|
|
|
return tabs.selectPriv(index)
|
2019-07-19 17:12:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
func (tabs *Tabs) MoveTab(to int, relative bool) {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
tabs.moveTabPriv(to, relative)
|
|
|
|
tabs.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) moveTabPriv(to int, relative bool) {
|
2022-07-14 16:29:56 +00:00
|
|
|
from := tabs.curIndex
|
2020-03-02 19:54:42 +00:00
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
if relative {
|
|
|
|
to = from + to
|
|
|
|
}
|
2020-03-02 19:54:42 +00:00
|
|
|
if to < 0 {
|
|
|
|
to = 0
|
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
if to >= len(tabs.tabs) {
|
|
|
|
to = len(tabs.tabs) - 1
|
2020-03-02 19:54:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
tab := tabs.tabs[from]
|
2020-03-02 19:54:42 +00:00
|
|
|
if to > from {
|
2022-07-14 16:29:56 +00:00
|
|
|
copy(tabs.tabs[from:to], tabs.tabs[from+1:to+1])
|
2020-03-02 19:54:42 +00:00
|
|
|
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 {
|
2022-07-14 16:29:56 +00:00
|
|
|
copy(tabs.tabs[to+1:from+1], tabs.tabs[to:from])
|
2020-03-02 19:54:42 +00:00
|
|
|
for i, h := range tabs.history {
|
|
|
|
if h == from {
|
|
|
|
tabs.history[i] = to
|
|
|
|
}
|
|
|
|
if h >= to && h < from {
|
|
|
|
tabs.history[i] += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.tabs[to] = tab
|
|
|
|
tabs.curIndex = to
|
2020-03-02 19:54:42 +00:00
|
|
|
tabs.TabStrip.Invalidate()
|
|
|
|
}
|
|
|
|
|
2020-03-07 16:42:41 +00:00
|
|
|
func (tabs *Tabs) PinTab() {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2022-07-14 16:29:56 +00:00
|
|
|
if tabs.tabs[tabs.curIndex].pinned {
|
2020-03-07 16:42:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
pinEnd := len(tabs.tabs)
|
|
|
|
for i, t := range tabs.tabs {
|
2020-03-07 16:42:41 +00:00
|
|
|
if !t.pinned {
|
|
|
|
pinEnd = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
for _, t := range tabs.tabs {
|
|
|
|
if t.pinned && t.indexBeforePin > tabs.curIndex-pinEnd {
|
2020-03-07 16:42:41 +00:00
|
|
|
t.indexBeforePin -= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.tabs[tabs.curIndex].pinned = true
|
|
|
|
tabs.tabs[tabs.curIndex].indexBeforePin = tabs.curIndex - pinEnd
|
2020-03-07 16:42:41 +00:00
|
|
|
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.moveTabPriv(pinEnd, false)
|
2020-03-07 16:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) UnpinTab() {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
|
|
|
defer tabs.m.Unlock()
|
2022-07-14 16:29:56 +00:00
|
|
|
if !tabs.tabs[tabs.curIndex].pinned {
|
2020-03-07 16:42:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
pinEnd := len(tabs.tabs)
|
|
|
|
for i, t := range tabs.tabs {
|
|
|
|
if i != tabs.curIndex && t.pinned && t.indexBeforePin > tabs.tabs[tabs.curIndex].indexBeforePin {
|
2020-03-07 16:42:41 +00:00
|
|
|
t.indexBeforePin += 1
|
|
|
|
}
|
|
|
|
if !t.pinned {
|
|
|
|
pinEnd = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
tabs.tabs[tabs.curIndex].pinned = false
|
2020-03-07 16:42:41 +00:00
|
|
|
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.moveTabPriv(tabs.tabs[tabs.curIndex].indexBeforePin+pinEnd-1, false)
|
2020-03-07 16:42:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 22:32:36 +00:00
|
|
|
func (tabs *Tabs) NextTab() {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
2022-07-14 16:29:56 +00:00
|
|
|
next := tabs.curIndex + 1
|
|
|
|
if next >= len(tabs.tabs) {
|
2019-09-05 22:32:36 +00:00
|
|
|
next = 0
|
|
|
|
}
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.selectPriv(next)
|
|
|
|
tabs.m.Unlock()
|
2019-09-05 22:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tabs *Tabs) PrevTab() {
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.m.Lock()
|
2022-07-14 16:29:56 +00:00
|
|
|
next := tabs.curIndex - 1
|
2019-09-05 22:32:36 +00:00
|
|
|
if next < 0 {
|
2022-07-14 16:29:56 +00:00
|
|
|
next = len(tabs.tabs) - 1
|
2019-09-05 22:32:36 +00:00
|
|
|
}
|
2022-07-14 20:10:23 +00:00
|
|
|
tabs.selectPriv(next)
|
|
|
|
tabs.m.Unlock()
|
2019-09-05 22:32:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 18:13:18 +00:00
|
|
|
func (tabs *Tabs) pushHistory(index int) {
|
|
|
|
tabs.history = append(tabs.history, index)
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:12:57 +00:00
|
|
|
func (tabs *Tabs) popHistory() (int, bool) {
|
2019-06-02 18:13:18 +00:00
|
|
|
lastIdx := len(tabs.history) - 1
|
2019-07-19 17:12:57 +00:00
|
|
|
if lastIdx < 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2019-06-02 18:13:18 +00:00
|
|
|
item := tabs.history[lastIdx]
|
|
|
|
tabs.history = tabs.history[:lastIdx]
|
2019-07-19 17:12:57 +00:00
|
|
|
return item, true
|
2019-06-02 18:13:18 +00: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 11:56:08 +00:00
|
|
|
if i > 0 && len(newHist) > 0 && item == newHist[len(newHist)-1] {
|
2019-06-02 18:13:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
newHist = append(newHist, item)
|
|
|
|
}
|
|
|
|
tabs.history = newHist
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:42:29 +00:00
|
|
|
// TODO: Color repository
|
|
|
|
func (strip *TabStrip) Draw(ctx *Context) {
|
|
|
|
x := 0
|
2022-07-14 20:10:23 +00:00
|
|
|
strip.parent.m.Lock()
|
2022-07-14 16:29:56 +00:00
|
|
|
for i, tab := range strip.tabs {
|
2022-04-16 23:03:49 +00:00
|
|
|
uiConfig := strip.uiConfig
|
|
|
|
if tab.uiConf != nil {
|
|
|
|
uiConfig = tab.uiConf
|
|
|
|
}
|
|
|
|
style := uiConfig.GetStyle(config.STYLE_TAB)
|
2022-07-14 16:29:56 +00:00
|
|
|
if strip.curIndex == i {
|
2022-04-16 23:03:49 +00:00
|
|
|
style = uiConfig.GetStyleSelected(config.STYLE_TAB)
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
2019-07-26 08:46:15 +00:00
|
|
|
tabWidth := 32
|
|
|
|
if ctx.Width()-x < tabWidth {
|
|
|
|
tabWidth = ctx.Width() - x - 2
|
|
|
|
}
|
2020-03-07 16:42:41 +00:00
|
|
|
name := tab.Name
|
|
|
|
if tab.pinned {
|
2022-04-16 23:03:49 +00:00
|
|
|
name = uiConfig.PinnedTabMarker + name
|
2020-03-07 16:42:41 +00:00
|
|
|
}
|
|
|
|
trunc := runewidth.Truncate(name, tabWidth, "…")
|
2019-05-14 20:18:21 +00:00
|
|
|
x += ctx.Printf(x, 0, style, " %s ", trunc)
|
2019-07-26 08:46:15 +00:00
|
|
|
if x >= ctx.Width() {
|
|
|
|
break
|
|
|
|
}
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
2022-07-14 20:10:23 +00:00
|
|
|
strip.parent.m.Unlock()
|
2020-07-27 08:03:55 +00:00
|
|
|
ctx.Fill(x, 0, ctx.Width()-x, 1, ' ',
|
|
|
|
strip.uiConfig.GetStyle(config.STYLE_TAB))
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (strip *TabStrip) Invalidate() {
|
|
|
|
if strip.onInvalidateStrip != nil {
|
|
|
|
strip.onInvalidateStrip(strip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:32:36 +00:00
|
|
|
func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) {
|
2022-07-14 20:10:23 +00:00
|
|
|
strip.parent.m.Lock()
|
|
|
|
defer strip.parent.m.Unlock()
|
2019-09-05 22:32:36 +00:00
|
|
|
changeFocus := func(focus bool) {
|
2022-07-14 16:29:56 +00:00
|
|
|
interactive, ok := strip.parent.tabs[strip.parent.curIndex].Content.(Interactive)
|
2019-09-05 22:32:36 +00:00
|
|
|
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:
|
2022-07-14 16:29:56 +00:00
|
|
|
selectedTab, ok := strip.clicked(localX, localY)
|
|
|
|
if !ok || selectedTab == strip.parent.curIndex {
|
2019-09-05 22:32:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
unfocus()
|
2022-07-14 20:10:23 +00:00
|
|
|
strip.parent.selectPriv(selectedTab)
|
2019-09-05 22:32:36 +00:00
|
|
|
refocus()
|
|
|
|
case tcell.WheelDown:
|
|
|
|
unfocus()
|
2022-07-14 20:10:23 +00:00
|
|
|
index := strip.parent.curIndex + 1
|
|
|
|
if index >= len(strip.parent.tabs) {
|
|
|
|
index = 0
|
|
|
|
}
|
|
|
|
strip.parent.selectPriv(index)
|
2019-09-05 22:32:36 +00:00
|
|
|
refocus()
|
|
|
|
case tcell.WheelUp:
|
|
|
|
unfocus()
|
2022-07-14 20:10:23 +00:00
|
|
|
index := strip.parent.curIndex - 1
|
|
|
|
if index < 0 {
|
|
|
|
index = len(strip.parent.tabs) - 1
|
|
|
|
}
|
|
|
|
strip.parent.selectPriv(index)
|
2019-09-05 22:32:36 +00:00
|
|
|
refocus()
|
|
|
|
case tcell.Button3:
|
2022-07-14 16:29:56 +00:00
|
|
|
selectedTab, ok := strip.clicked(localX, localY)
|
2019-09-05 22:32:36 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
unfocus()
|
2022-07-14 20:10:23 +00:00
|
|
|
strip.parent.m.Unlock()
|
2022-07-14 16:29:56 +00:00
|
|
|
strip.parent.CloseTab(selectedTab)
|
2022-07-14 20:10:23 +00:00
|
|
|
strip.parent.m.Lock()
|
2019-09-05 22:32:36 +00:00
|
|
|
refocus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:42:29 +00:00
|
|
|
func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) {
|
|
|
|
strip.onInvalidateStrip = onInvalidate
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:29:56 +00:00
|
|
|
func (strip *TabStrip) clicked(mouseX int, mouseY int) (int, bool) {
|
2019-07-11 22:15:15 +00:00
|
|
|
x := 0
|
2022-07-14 16:29:56 +00:00
|
|
|
for i, tab := range strip.tabs {
|
2019-09-05 22:32:36 +00:00
|
|
|
trunc := runewidth.Truncate(tab.Name, 32, "…")
|
|
|
|
length := len(trunc) + 2
|
|
|
|
if x <= mouseX && mouseX < x+length {
|
|
|
|
return i, true
|
2019-07-11 22:15:15 +00:00
|
|
|
}
|
2019-09-05 22:32:36 +00:00
|
|
|
x += length
|
2019-07-11 22:15:15 +00:00
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2019-01-20 20:06:44 +00:00
|
|
|
func (content *TabContent) Children() []Drawable {
|
2022-07-14 20:10:23 +00:00
|
|
|
content.parent.m.Lock()
|
2022-07-14 16:29:56 +00:00
|
|
|
children := make([]Drawable, len(content.tabs))
|
|
|
|
for i, tab := range content.tabs {
|
2019-01-20 20:06:44 +00:00
|
|
|
children[i] = tab.Content
|
|
|
|
}
|
2022-07-14 20:10:23 +00:00
|
|
|
content.parent.m.Unlock()
|
2019-01-20 20:06:44 +00:00
|
|
|
return children
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:42:29 +00:00
|
|
|
func (content *TabContent) Draw(ctx *Context) {
|
2022-07-14 20:10:23 +00:00
|
|
|
content.parent.m.Lock()
|
2022-07-14 16:29:56 +00:00
|
|
|
if content.curIndex >= len(content.tabs) {
|
2018-06-13 05:00:57 +00:00
|
|
|
width := ctx.Width()
|
|
|
|
height := ctx.Height()
|
2020-07-27 08:03:55 +00:00
|
|
|
ctx.Fill(0, 0, width, height, ' ',
|
|
|
|
content.uiConfig.GetStyle(config.STYLE_TAB))
|
2018-06-13 05:00:57 +00:00
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
tab := content.tabs[content.curIndex]
|
2022-07-14 20:10:23 +00:00
|
|
|
content.parent.m.Unlock()
|
2018-02-18 00:42:29 +00:00
|
|
|
tab.Content.Draw(ctx)
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:32:36 +00:00
|
|
|
func (content *TabContent) MouseEvent(localX int, localY int, event tcell.Event) {
|
2022-07-14 20:10:23 +00:00
|
|
|
content.parent.m.Lock()
|
2022-07-14 16:29:56 +00:00
|
|
|
tab := content.tabs[content.curIndex]
|
2022-07-14 20:10:23 +00:00
|
|
|
content.parent.m.Unlock()
|
2019-09-05 22:32:36 +00:00
|
|
|
switch tabContent := tab.Content.(type) {
|
|
|
|
case Mouseable:
|
|
|
|
tabContent.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:42:29 +00:00
|
|
|
func (content *TabContent) Invalidate() {
|
|
|
|
if content.onInvalidateContent != nil {
|
|
|
|
content.onInvalidateContent(content)
|
|
|
|
}
|
2022-07-14 16:29:56 +00:00
|
|
|
tab := content.tabs[content.curIndex]
|
2018-06-12 00:13:02 +00:00
|
|
|
tab.Content.Invalidate()
|
2018-02-18 00:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (content *TabContent) OnInvalidate(onInvalidate func(d Drawable)) {
|
|
|
|
content.onInvalidateContent = onInvalidate
|
|
|
|
}
|