uitabbar.lua

main 164 lines · 3.8 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1-- @docclass
2UITabBar = extends(UIWidget, "UITabBar")
3
4-- private functions
5local function onTabClick(tab)
6 tab.tabBar:selectTab(tab)
7end
8
9local function onTabMouseRelease(tab, mousePos, mouseButton)
10 if mouseButton == MouseRightButton and tab:containsPoint(mousePos) then
11 signalcall(tab.tabBar.onTabLeftClick, tab.tabBar, tab)
12 end
13end
14
15-- public functions
16function UITabBar.create()
17 local tabbar = UITabBar.internalCreate()
18 tabbar:setFocusable(false)
19 tabbar.tabs = {}
20 return tabbar
21end
22
23function UITabBar:onSetup()
24 self.buttonsPanel = self:getChildById('buttonsPanel')
25end
26
27function UITabBar:setContentWidget(widget)
28 self.contentWidget = widget
29 if #self.tabs > 0 then
30 self.contentWidget:addChild(self.tabs[1].tabPanel)
31 end
32end
33
34function UITabBar:addTab(text, panel, icon)
35 if panel == nil then
36 panel = g_ui.createWidget(self:getStyleName() .. 'Panel')
37 panel:setId('tabPanel')
38 end
39
40 local tab = g_ui.createWidget(self:getStyleName() .. 'Button', self.buttonsPanel)
41
42 panel.isTab = true
43 tab.tabPanel = panel
44 tab.tabBar = self
45 tab:setId('tab')
46 tab:setText(text)
47 tab:setWidth(tab:getTextSize().width + tab:getPaddingLeft() + tab:getPaddingRight())
48 tab.onClick = onTabClick
49 tab.onMouseRelease = onTabMouseRelease
50 tab.onDestroy = function() tab.tabPanel:destroy() end
51
52 table.insert(self.tabs, tab)
53 if #self.tabs == 1 then
54 self:selectTab(tab)
55 end
56
57 local tabStyle = {}
58 tabStyle['icon-source'] = icon
59 tab:mergeStyle(tabStyle)
60
61 return tab
62end
63
64function UITabBar:addButton(text, func, icon)
65 local button = g_ui.createWidget(self:getStyleName() .. 'Button', self.buttonsPanel)
66 button:setText(text)
67
68 local style = {}
69 style['icon-source'] = icon
70 button:mergeStyle(style)
71
72 button.onClick = func
73 return button
74end
75
76function UITabBar:removeTab(tab)
77 local index = table.find(self.tabs, tab)
78 if index == nil then return end
79 if self.currentTab == tab then
80 self:selectPrevTab()
81 end
82 table.remove(self.tabs, index)
83 tab:destroy()
84end
85
86function UITabBar:getTab(text)
87 for k,tab in pairs(self.tabs) do
88 if tab:getText():lower() == text:lower() then
89 return tab
90 end
91 end
92end
93
94function UITabBar:selectTab(tab)
95 if self.currentTab == tab then return end
96 if self.contentWidget then
97 local selectedWidget = self.contentWidget:getLastChild()
98 if selectedWidget and selectedWidget.isTab then
99 self.contentWidget:removeChild(selectedWidget)
100 end
101 self.contentWidget:addChild(tab.tabPanel)
102 tab.tabPanel:fill('parent')
103 end
104
105 if self.currentTab then
106 self.currentTab:setChecked(false)
107 end
108 signalcall(self.onTabChange, self, tab)
109 self.currentTab = tab
110 tab:setChecked(true)
111 tab:setOn(false)
112
113 local parent = tab:getParent()
114 if parent then
115 parent:focusChild(tab, MouseFocusReason)
116 end
117end
118
119function UITabBar:selectNextTab()
120 if self.currentTab == nil then return end
121 local index = table.find(self.tabs, self.currentTab)
122 if index == nil then return end
123 local nextTab = self.tabs[index + 1] or self.tabs[1]
124 if not nextTab then return end
125 self:selectTab(nextTab)
126end
127
128function UITabBar:selectPrevTab()
129 if self.currentTab == nil then return end
130 local index = table.find(self.tabs, self.currentTab)
131 if index == nil then return end
132 local prevTab = self.tabs[index - 1] or self.tabs[#self.tabs]
133 if not prevTab then return end
134 self:selectTab(prevTab)
135end
136
137function UITabBar:getTabPanel(tab)
138 return tab.tabPanel
139end
140
141function UITabBar:getCurrentTabPanel()
142 if self.currentTab then
143 return self.currentTab.tabPanel
144 end
145end
146
147function UITabBar:getCurrentTab()
148 return self.currentTab
149end
150
151function UITabBar:getTabs()
152 return self.tabs
153end
154
155function UITabBar:getTabsPanel()
156 return table.collect(self.tabs, function(_,tab) return tab.tabPanel end)
157end
158
159function UITabBar:clearTabs()
160 while #self.tabs > 0 do
161 self:removeTab(self.tabs[#self.tabs])
162 end
163end
164
Top