ComputerCraft Archive

TabBar

computer utility kepler155c github

Description

ComputerCraft OS

Installation

Copy one of these commands into your ComputerCraft terminal:

wget:wget https://raw.githubusercontent.com/kepler155c/opus/develop-1.8/sys/modules/opus/ui/components/TabBar.lua tabbar
Archive:wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-ui-components-tabbar tabbar
Quick Install: wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-ui-components-tabbar TabBar

Usage

Run: TabBar

Tags

none

Source

View Original Source

Code Preview

local class = require('opus.class')
local UI    = require('opus.ui')
local Util  = require('opus.util')

UI.TabBar = class(UI.MenuBar)
UI.TabBar.defaults = {
	UIElement = 'TabBar',
	buttonClass = 'TabBarMenuItem',
	backgroundColor = 'black',
}
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
		self:draw(self)
	end
	return UI.MenuBar.eventHandler(self, event)
end