1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-18 06:34:49 +00:00
opus/sys/apis/ui/components/TabBar.lua
kepler155c@gmail.com 915085ac5f ui overhaul
2019-02-05 23:03:57 -05:00

46 lines
1.1 KiB
Lua

local class = require('class')
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
UI.TabBar = class(UI.MenuBar)
UI.TabBar.defaults = {
UIElement = 'TabBar',
buttonClass = 'TabBarMenuItem',
selectedBackgroundColor = colors.cyan,
}
function UI.TabBar:enable()
UI.MenuBar.enable(self)
if not Util.find(self.children, 'selected', true) then
local menuItem = self:getFocusables()[1]
if menuItem then
menuItem.selected = true
end
end
end
function UI.TabBar:eventHandler(event)
if event.type == 'tab_select' then
local selected, si = Util.find(self.children, 'uid', event.button.uid)
local previous, pi = Util.find(self.children, 'selected', true)
if si ~= pi then
selected.selected = true
if previous then
previous.selected = false
self:emit({ type = 'tab_change', current = si, last = pi, tab = selected })
end
end
UI.MenuBar.draw(self)
end
return UI.MenuBar.eventHandler(self, event)
end
function UI.TabBar:selectTab(text)
local menuItem = Util.find(self.children, 'text', text)
if menuItem then
menuItem.selected = true
end
end