forked from fyne-io/fyne
/
tabcontainer.go
208 lines (168 loc) · 5.41 KB
/
tabcontainer.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package widget
import (
"image/color"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/theme"
)
// TabItem represents a single view in a TabContainer.
// The Text is used for the tab button and the Content is shown when the corresponding tab is active.
type TabItem struct {
Text string
Content fyne.CanvasObject
}
// NewTabItem creates a new item for a tabbed widget - each item specifies the content an a label for it's tab.
func NewTabItem(text string, content fyne.CanvasObject) *TabItem {
return &TabItem{Text: text, Content: content}
}
// TabContainer widget allows switching visible content from a list of TabItems.
// Each item is represented by a button at the top of the widget.
type TabContainer struct {
baseWidget
Items []*TabItem
current int
}
// Resize sets a new size for a widget.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (t *TabContainer) Resize(size fyne.Size) {
t.resize(size, t)
}
// Move the widget to a new position, relative to it's parent.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (t *TabContainer) Move(pos fyne.Position) {
t.move(pos, t)
}
// MinSize returns the smallest size this widget can shrink to
func (t *TabContainer) MinSize() fyne.Size {
return t.minSize(t)
}
// Show this widget, if it was previously hidden
func (t *TabContainer) Show() {
t.show(t)
}
// Hide this widget, if it was previously visible
func (t *TabContainer) Hide() {
t.hide(t)
}
// SelectTab sets the specified TabItem to be selected and it's content visible.
func (t *TabContainer) SelectTab(item *TabItem) {
for i, child := range t.Items {
if child == item {
t.SelectTabIndex(i)
return
}
}
}
// CurrentTab returns the currently selected TabItem.
func (t *TabContainer) CurrentTab() *TabItem {
return t.Items[t.current]
}
// SelectTabIndex sets the TabItem at the specifie index to be selected and it's content visible.
func (t *TabContainer) SelectTabIndex(index int) {
if index < 0 || index >= len(t.Items) {
return
}
t.current = index
Refresh(t)
}
// CurrentTabIndex returns the index of the currently selected TabItem.
func (t *TabContainer) CurrentTabIndex() int {
return t.current
}
func (t *TabContainer) makeButton(item *TabItem) *Button {
it := item
return NewButton(item.Text, func() { t.SelectTab(it) })
}
/*
// Prepend inserts a new CanvasObject at the top of the group
func (t *TabContainer) Prepend(object fyne.CanvasObject) {
t.Items = append(t.Items, item)
t.tabBar.Append(t.makeButton(item))
t.children = append(t.children, item.Content)
t.CreateRenderer().Refresh()}
}
// Append adds a new CanvasObject to the end of the group
func (t *TabContainer) Append(item TabItem) {
t.Items = append(t.Items, item)
// t.tabBar.Append(t.makeButton(item))
// t.children = append(t.children, item.Content)
t.CreateRenderer().Refresh()
}
*/
// CreateRenderer is a private method to Fyne which links this widget to it's renderer
func (t *TabContainer) CreateRenderer() fyne.WidgetRenderer {
var contents []fyne.CanvasObject
buttons := NewHBox()
for i, item := range t.Items {
button := t.makeButton(item)
if i == t.current {
button.Style = PrimaryButton
} else {
item.Content.Hide()
}
buttons.Append(button)
contents = append(contents, item.Content)
}
line := canvas.NewRectangle(theme.ButtonColor())
objects := append(contents, line, buttons)
return &tabContainerRenderer{tabBar: buttons, line: line, objects: objects, container: t}
}
// NewTabContainer creates a new tab bar widget that allows the user to choose between different visible containers
func NewTabContainer(items ...*TabItem) *TabContainer {
tabs := &TabContainer{baseWidget{}, items, 0}
Renderer(tabs).Layout(tabs.MinSize())
return tabs
}
type tabContainerRenderer struct {
tabBar *Box
line *canvas.Rectangle
objects []fyne.CanvasObject
container *TabContainer
}
func (t *tabContainerRenderer) MinSize() fyne.Size {
buttonsMin := t.tabBar.MinSize()
childMin := fyne.NewSize(0, 0)
for _, child := range t.container.Items {
childMin = childMin.Union(child.Content.MinSize())
}
return fyne.NewSize(fyne.Max(buttonsMin.Width, childMin.Width),
buttonsMin.Height+childMin.Height+theme.Padding())
}
func (t *tabContainerRenderer) Layout(size fyne.Size) {
buttonHeight := t.tabBar.MinSize().Height
t.tabBar.Resize(fyne.NewSize(size.Width, buttonHeight))
t.line.Move(fyne.NewPos(0, buttonHeight))
t.line.Resize(fyne.NewSize(size.Width, theme.Padding()))
child := t.container.Items[t.container.current].Content
child.Move(fyne.NewPos(0, buttonHeight+theme.Padding()))
child.Resize(fyne.NewSize(size.Width, size.Height-buttonHeight-theme.Padding()))
}
func (t *tabContainerRenderer) ApplyTheme() {
t.line.StrokeColor = theme.ButtonColor()
}
func (t *tabContainerRenderer) BackgroundColor() color.Color {
return theme.BackgroundColor()
}
func (t *tabContainerRenderer) Objects() []fyne.CanvasObject {
return t.objects
}
func (t *tabContainerRenderer) Refresh() {
Renderer(t.tabBar).Refresh()
t.Layout(t.container.Size())
for i, child := range t.container.Items {
if i == t.container.current {
child.Content.Show()
} else {
child.Content.Hide()
}
}
canvas.Refresh(t.container)
for i, button := range t.tabBar.Children {
if i == t.container.current {
button.(*Button).Style = PrimaryButton
} else {
button.(*Button).Style = DefaultButton
}
Renderer(button.(*Button)).Refresh()
}
}