summary refs log tree commit diff stats
path: root/ui/tab.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-02-17 19:42:29 -0500
committerDrew DeVault <sir@cmpwn.com>2018-02-17 19:42:29 -0500
commit5b2e3a0ca0b549c569ff6c01549c2dc425b0ba40 (patch)
tree7ac92fe8cb13e662aeeb851c3a9a945bf837f600 /ui/tab.go
parent60b351b78c930110716b0c9db2227e13704f826d (diff)
downloadaerc-5b2e3a0ca0b549c569ff6c01549c2dc425b0ba40.tar.gz
Implement tab container
Diffstat (limited to 'ui/tab.go')
-rw-r--r--ui/tab.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/ui/tab.go b/ui/tab.go
new file mode 100644
index 0000000..e6a8aa5
--- /dev/null
+++ b/ui/tab.go
@@ -0,0 +1,115 @@
+package ui
+
+import (
+	tb "github.com/nsf/termbox-go"
+)
+
+type Tabs struct {
+	Tabs       []*Tab
+	TabStrip   *TabStrip
+	TabContent *TabContent
+	Selected   int
+
+	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)
+	return tabs
+}
+
+func (tabs *Tabs) Add(content Drawable, name string) {
+	tabs.Tabs = append(tabs.Tabs, &Tab{
+		Content: content,
+		Name:    name,
+	})
+	tabs.TabStrip.Invalidate()
+	content.OnInvalidate(tabs.invalidateChild)
+}
+
+func (tabs *Tabs) invalidateChild(d Drawable) {
+	for i, tab := range tabs.Tabs {
+		if tab.Content == d {
+			if i == tabs.Selected {
+				tabs.TabContent.Invalidate()
+			}
+			return
+		}
+	}
+}
+
+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:]...)
+			break
+		}
+	}
+	tabs.TabStrip.Invalidate()
+}
+
+func (tabs *Tabs) Select(index int) {
+	if tabs.Selected != index {
+		tabs.Selected = index
+		tabs.TabStrip.Invalidate()
+		tabs.TabContent.Invalidate()
+	}
+}
+
+// TODO: Color repository
+func (strip *TabStrip) Draw(ctx *Context) {
+	x := 0
+	for i, tab := range strip.Tabs {
+		cell := tb.Cell{
+			Fg: tb.ColorBlack,
+			Bg: tb.ColorWhite,
+		}
+		if strip.Selected == i {
+			cell.Fg = tb.ColorDefault
+			cell.Bg = tb.ColorDefault
+		}
+		x += ctx.Printf(x, 0, cell, " %s ", tab.Name)
+	}
+	cell := tb.Cell{
+		Fg: tb.ColorBlack,
+		Bg: tb.ColorWhite,
+	}
+	ctx.Fill(x, 0, ctx.Width()-x, 1, cell)
+}
+
+func (strip *TabStrip) Invalidate() {
+	if strip.onInvalidateStrip != nil {
+		strip.onInvalidateStrip(strip)
+	}
+}
+
+func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) {
+	strip.onInvalidateStrip = onInvalidate
+}
+
+func (content *TabContent) Draw(ctx *Context) {
+	tab := content.Tabs[content.Selected]
+	tab.Content.Draw(ctx)
+}
+
+func (content *TabContent) Invalidate() {
+	if content.onInvalidateContent != nil {
+		content.onInvalidateContent(content)
+	}
+}
+
+func (content *TabContent) OnInvalidate(onInvalidate func(d Drawable)) {
+	content.onInvalidateContent = onInvalidate
+}
0f851e48 ^

6fc975eb ^
4a943d4e ^




83c67014 ^
4a943d4e ^


83c67014 ^
4a943d4e ^









6fc975eb ^




c442a5ad ^
6fc975eb ^
861418f0 ^

c442a5ad ^
861418f0 ^

c442a5ad ^
6fc975eb ^

861418f0 ^
4a943d4e ^




83c67014 ^
4a943d4e ^











2eb174d6 ^





4a943d4e ^



83c67014 ^
4a943d4e ^


83c67014 ^
4a943d4e ^








2eb174d6 ^




c442a5ad ^
3f097f3f ^
2eb174d6 ^







c442a5ad ^
2eb174d6 ^

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176