105 lines
2.5 KiB
Lua
105 lines
2.5 KiB
Lua
local winhlp = require "winhlp"
|
|
|
|
local frame = term.current()
|
|
local tabCount = 0
|
|
local allTabs = {}
|
|
local tabOrder = {}
|
|
local currentTab = nil
|
|
|
|
local function setTerm(termlike)
|
|
frame = termlike
|
|
end
|
|
|
|
local function addTab(tab, orderIdx)
|
|
assert(tab.name, "tab needs a unique name")
|
|
local isCurrentTab = currentTab == nil
|
|
if isCurrentTab then
|
|
currentTab = tab
|
|
end
|
|
local tw, th = frame.getSize()
|
|
tab.window = window.create(frame, 1, 1, tw, th, isCurrentTab)
|
|
tab.setTerm(tab.window)
|
|
tab.scrollPos = 1
|
|
tab.pageDown = function()
|
|
local tw, th = tab.window.getSize()
|
|
tab.scrollPos = tab.scrollPos + th
|
|
end
|
|
tab.pageUp = function()
|
|
local tw, th = tab.window.getSize()
|
|
tab.scrollPos = math.max(1, tab.scrollPos - th)
|
|
end
|
|
allTabs[tab.name] = tab
|
|
tabOrder[orderIdx] = tab.name
|
|
tabCount = tabCount + 1
|
|
return tab
|
|
end
|
|
|
|
local function showTab(name)
|
|
if currentTab ~= nil then
|
|
currentTab.window.setVisible(false)
|
|
end
|
|
currentTab = allTabs[name]
|
|
if currentTab == nil then
|
|
-- backwards compatible idx lookup
|
|
currentTab = allTabs[tabOrder[name]]
|
|
end
|
|
assert(currentTab ~= nil, "tab name '" .. name .. "' was not registered")
|
|
currentTab.window.setVisible(true)
|
|
end
|
|
|
|
local function switchTab()
|
|
assert(tabCount > 0, "tabs need to be registered in order to switch tabs")
|
|
local idx = 0
|
|
if currentTab ~= nil then
|
|
currentTab.window.setVisible(false)
|
|
for i, name in pairs(tabOrder) do
|
|
if name == currentTab.name then
|
|
idx = i
|
|
break
|
|
end
|
|
end
|
|
end
|
|
idx = idx + 1
|
|
if idx > tabCount then
|
|
idx = 1
|
|
end
|
|
currentTab = allTabs[tabOrder[idx]]
|
|
currentTab.window.setVisible(true)
|
|
end
|
|
|
|
local function getCurrentTab()
|
|
return currentTab
|
|
end
|
|
|
|
local function getAllTabs()
|
|
return allTabs
|
|
end
|
|
|
|
local function updateSize()
|
|
local tw, th = frame.getSize()
|
|
for name, tab in pairs(allTabs) do
|
|
tab.window.reposition(1, 1, tw, th)
|
|
end
|
|
end
|
|
|
|
local function onTouch(touchX, touchY)
|
|
if currentTab ~= nil and
|
|
winhlp.contains(currentTab.window, touchX, touchY) and
|
|
currentTab.onTouch(currentTab, winhlp.translate(currentTab.window, touchX, touchY)) then
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
|
|
return {
|
|
setTerm = setTerm,
|
|
addTab = addTab,
|
|
showTab = showTab,
|
|
switchTab = switchTab,
|
|
updateSize = updateSize,
|
|
onTouch = onTouch,
|
|
currentTab = getCurrentTab,
|
|
allTabs = getAllTabs,
|
|
}
|