-
Notifications
You must be signed in to change notification settings - Fork 5
/
tabs.go
109 lines (93 loc) · 2.06 KB
/
tabs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package widgets
import (
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/views"
)
var (
styleTabActive = tcell.StyleDefault.Background(tcell.ColorSilver).Foreground(tcell.ColorWhite).Bold(true)
styleTabInactive = tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)
styleTabBackground = tcell.StyleDefault.Background(tcell.ColorWhite)
)
type tabsItem struct {
name string
text *views.Text
}
// Tabs is a View with multiple items in single line.
type Tabs struct {
items []tabsItem
selected int
views.BoxLayout
views.WidgetWatchers
}
// NewTabs returns a new Tabs.
func NewTabs() *Tabs {
w := &Tabs{
selected: -1,
}
w.SetStyle(styleTabBackground)
w.SetOrientation(views.Horizontal)
return w
}
// AddTab adds a new item with the name.
func (w *Tabs) AddTab(name string) {
text := &views.Text{}
text.SetText(" " + name + " ")
text.SetStyle(styleTabInactive)
w.AddWidget(text, 0)
w.items = append(w.items, tabsItem{
name: name,
text: text,
})
}
// TabCount returns the count of the tabs.
func (w *Tabs) TabCount() int {
return len(w.items)
}
// SelectNext selects next tab of the current
func (w *Tabs) SelectNext() {
index := w.selected + 1
if index >= len(w.items) {
index = 0
}
w.SelectAt(index)
}
// SelectPrev selects previous tab of the current
func (w *Tabs) SelectPrev() {
index := w.selected + 1
if index >= len(w.items) {
index = 0
}
w.SelectAt(index)
}
// Clear clears current tabs.
func (w *Tabs) Clear() {
for _, item := range w.items {
w.RemoveWidget(item.text)
}
w.items = nil
w.selected = -1
}
// SelectAt selects the tab of the index in items.
func (w *Tabs) SelectAt(index int) {
if index == w.selected {
return
}
if w.selected >= 0 {
item := w.items[w.selected]
item.text.SetStyle(styleTabInactive)
}
if index < 0 || index >= len(w.items) {
return
}
w.selected = index
item := w.items[w.selected]
item.text.SetStyle(styleTabActive)
w.PostEventWidgetContent(w)
ev := &EventItemSelected{
Name: item.name,
Index: index,
widget: w,
}
ev.SetEventNow()
w.PostEvent(ev)
}