-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTab.lua
223 lines (187 loc) · 5.2 KB
/
Tab.lua
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
--- @type StdUi
local StdUi = LibStub and LibStub('StdUi', true);
if not StdUi then
return
end
local module, version = 'Tab', 4;
if not StdUi:UpgradeNeeded(module, version) then
return
end
----------------------------------------------------
--- TabPanel
----------------------------------------------------
local TabPanelMethods = {
--- Runs callback thru all tabs, if callback returns truthy value, enumeration stops and function returns result
EnumerateTabs = function(self, callback, ...)
local result;
for i = 1, #self.tabs do
local tab = self.tabs[i];
result = callback(tab, self, i, ...);
if result then
break
end
end
return result;
end,
HideAllFrames = function(self)
for _, tab in pairs(self.tabs) do
if tab.frame then
tab.frame:Hide();
end
end
end,
DrawButtons = function(self)
local prevBtn;
for _, tab in pairs(self.tabs) do
if tab.button then
tab.button:Hide();
end
local btn = tab.button;
local btnContainer = self.buttonContainer;
if not btn then
btn = self.stdUi:Button(btnContainer, nil, self.buttonHeight);
tab.button = btn;
btn.tabFrame = self;
btn:SetScript('OnClick', function(bt)
bt.tabFrame:SelectTab(bt.tab.name);
end);
end
btn.tab = tab;
btn:SetText(tab.title);
btn:ClearAllPoints();
if self.vertical then
btn:SetWidth(self.buttonWidth);
else
self.stdUi:ButtonAutoWidth(btn);
end
if self.vertical then
if not prevBtn then
self.stdUi:GlueTop(btn, btnContainer, 0, 0, 'CENTER');
else
self.stdUi:GlueBelow(btn, prevBtn, 0, -1);
end
else
if not prevBtn then
self.stdUi:GlueTop(btn, btnContainer, 0, 0, 'LEFT');
else
self.stdUi:GlueRight(btn, prevBtn, 5, 0);
end
end
btn:Show();
prevBtn = btn;
end
end,
DrawFrames = function(self)
for _, tab in pairs(self.tabs) do
if not tab.frame then
tab.frame = self.stdUi:Frame(self.container);
end
tab.frame:ClearAllPoints();
tab.frame:SetAllPoints();
if tab.layout then
self.stdUi:BuildWindow(tab.frame, tab.layout);
self.stdUi:EasyLayout(tab.frame, { padding = { top = 10 } });
tab.frame:SetScript('OnShow', function(of)
of:DoLayout();
end);
end
if tab.onHide then
tab.frame:SetScript('OnHide', tab.onHide);
end
end
end,
Update = function(self, newTabs)
if newTabs then
self.tabs = newTabs;
end
self:DrawButtons();
self:DrawFrames();
end,
GetTabByName = function(self, name)
for _, tab in pairs(self.tabs) do
if tab.name == name then
return tab;
end
end
end,
SelectTab = function(self, name)
self.selected = name;
if self.selectedTab then
self.selectedTab.button:Enable();
end
self:HideAllFrames();
local foundTab = self:GetTabByName(name);
if foundTab.name == name and foundTab.frame then
foundTab.button:Disable();
foundTab.frame:Show();
self.selectedTab = foundTab;
return true;
end
end,
GetSelectedTab = function(self)
return self.selectedTab;
end,
DoLayout = function(self)
-- redoing layout as container
local tab = self:GetSelectedTab();
if tab then
if tab.frame and tab.frame.DoLayout then
tab.frame:DoLayout();
end
end
end
};
---
---local t = {
--- {
--- name = 'firstTab',
--- title = 'First',
--- },
--- {
--- name = 'secondTab',
--- title = 'Second',
--- },
--- {
--- name = 'thirdTab',
--- title = 'Third'
--- }
---}
function StdUi:TabPanel(parent, width, height, tabs, vertical, buttonWidth, buttonHeight)
vertical = vertical or false;
buttonWidth = buttonWidth or 160;
buttonHeight = buttonHeight or 20;
local tabFrame = self:Frame(parent, width, height);
tabFrame.stdUi = self;
tabFrame.tabs = tabs;
tabFrame.vertical = vertical;
tabFrame.buttonWidth = buttonWidth;
tabFrame.buttonHeight = buttonHeight;
tabFrame.buttonContainer = self:Frame(tabFrame);
tabFrame.container = self:Panel(tabFrame);
if vertical then
tabFrame.buttonContainer:SetPoint('TOPLEFT', tabFrame, 'TOPLEFT', 0, 0);
tabFrame.buttonContainer:SetPoint('BOTTOMLEFT', tabFrame, 'BOTTOMLEFT', 0, 0);
tabFrame.buttonContainer:SetWidth(buttonWidth);
tabFrame.container:SetPoint('TOPLEFT', tabFrame.buttonContainer, 'TOPRIGHT', 5, 0);
tabFrame.container:SetPoint('BOTTOMLEFT', tabFrame.buttonContainer, 'BOTTOMRIGHT', 5, 0);
tabFrame.container:SetPoint('TOPRIGHT', tabFrame, 'TOPRIGHT', 0, 0);
tabFrame.container:SetPoint('BOTTOMRIGHT', tabFrame, 'BOTTOMRIGHT', 0, 0);
else
tabFrame.buttonContainer:SetPoint('TOPLEFT', tabFrame, 'TOPLEFT', 0, 0);
tabFrame.buttonContainer:SetPoint('TOPRIGHT', tabFrame, 'TOPRIGHT', 0, 0);
tabFrame.buttonContainer:SetHeight(buttonHeight);
tabFrame.container:SetPoint('TOPLEFT', tabFrame.buttonContainer, 'BOTTOMLEFT', 0, -5);
tabFrame.container:SetPoint('TOPRIGHT', tabFrame.buttonContainer, 'BOTTOMRIGHT', 0, -5);
tabFrame.container:SetPoint('BOTTOMLEFT', tabFrame, 'BOTTOMLEFT', 0, 0);
tabFrame.container:SetPoint('BOTTOMRIGHT', tabFrame, 'BOTTOMRIGHT', 0, 0);
end
for k, v in pairs(TabPanelMethods) do
tabFrame[k] = v;
end
tabFrame:Update();
if #tabFrame.tabs > 0 then
tabFrame:SelectTab(tabFrame.tabs[1].name);
end
return tabFrame;
end
StdUi:RegisterModule(module, version);