mirror of
https://github.com/kepler155c/opus
synced 2025-11-17 07:47:11 +00:00
tabs update + object identity
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
-- From http://lua-users.org/wiki/SimpleLuaClasses
|
-- From http://lua-users.org/wiki/SimpleLuaClasses
|
||||||
-- (with some modifications)
|
-- (with some modifications)
|
||||||
|
|
||||||
|
local uid = 1
|
||||||
|
|
||||||
-- class.lua
|
-- class.lua
|
||||||
-- Compatible with Lua 5.1 (not 5.0).
|
-- Compatible with Lua 5.1 (not 5.0).
|
||||||
return function(base)
|
return function(base)
|
||||||
@@ -19,7 +21,8 @@ return function(base)
|
|||||||
-- expose a constructor which can be called by <classname>(<args>)
|
-- expose a constructor which can be called by <classname>(<args>)
|
||||||
setmetatable(c, {
|
setmetatable(c, {
|
||||||
__call = function(class_tbl, ...)
|
__call = function(class_tbl, ...)
|
||||||
local obj = {}
|
local obj = { __uid = uid }
|
||||||
|
uid = uid + 1
|
||||||
setmetatable(obj,c)
|
setmetatable(obj,c)
|
||||||
if class_tbl.init then
|
if class_tbl.init then
|
||||||
class_tbl.init(obj, ...)
|
class_tbl.init(obj, ...)
|
||||||
|
|||||||
@@ -21,16 +21,13 @@ if not http._patched then
|
|||||||
else
|
else
|
||||||
syncLocks[key] = { }
|
syncLocks[key] = { }
|
||||||
end
|
end
|
||||||
local s, m = pcall(fn)
|
fn()
|
||||||
local co = table.remove(syncLocks[key], 1)
|
local co = table.remove(syncLocks[key], 1)
|
||||||
if co then
|
if co then
|
||||||
os.queueEvent('sync_lock', co)
|
os.queueEvent('sync_lock', co)
|
||||||
else
|
else
|
||||||
syncLocks[key] = nil
|
syncLocks[key] = nil
|
||||||
end
|
end
|
||||||
if not s then
|
|
||||||
error(m)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- todo -- completely replace http.get with function that
|
-- todo -- completely replace http.get with function that
|
||||||
|
|||||||
103
sys/apis/ui.lua
103
sys/apis/ui.lua
@@ -9,6 +9,13 @@ local _sub = string.sub
|
|||||||
local colors = _G.colors
|
local colors = _G.colors
|
||||||
local keys = _G.keys
|
local keys = _G.keys
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Using the shorthand window definition, elements are created from
|
||||||
|
the bottom up. Once reaching the top, setParent is called top down.
|
||||||
|
|
||||||
|
On :init(), elements do not know the parent or can calculate sizing.
|
||||||
|
]]
|
||||||
|
|
||||||
local function safeValue(v)
|
local function safeValue(v)
|
||||||
local t = type(v)
|
local t = type(v)
|
||||||
if t == 'string' or t == 'number' then
|
if t == 'string' or t == 'number' then
|
||||||
@@ -526,7 +533,6 @@ UI.Window.defaults = {
|
|||||||
function UI.Window:init(args)
|
function UI.Window:init(args)
|
||||||
local defaults = UI:getDefaults(UI.Window, args)
|
local defaults = UI:getDefaults(UI.Window, args)
|
||||||
UI:setProperties(self, defaults)
|
UI:setProperties(self, defaults)
|
||||||
|
|
||||||
if self.parent then
|
if self.parent then
|
||||||
self:setParent()
|
self:setParent()
|
||||||
end
|
end
|
||||||
@@ -2024,6 +2030,7 @@ UI.MenuBar.defaults = {
|
|||||||
backgroundColor = colors.lightGray,
|
backgroundColor = colors.lightGray,
|
||||||
textColor = colors.black,
|
textColor = colors.black,
|
||||||
spacing = 2,
|
spacing = 2,
|
||||||
|
lastx = 1,
|
||||||
showBackButton = false,
|
showBackButton = false,
|
||||||
buttonClass = 'MenuItem',
|
buttonClass = 'MenuItem',
|
||||||
}
|
}
|
||||||
@@ -2031,23 +2038,40 @@ UI.MenuBar.spacer = { spacer = true, text = 'spacer', inactive = true }
|
|||||||
|
|
||||||
function UI.MenuBar:init(args)
|
function UI.MenuBar:init(args)
|
||||||
local defaults = UI:getDefaults(UI.MenuBar, args)
|
local defaults = UI:getDefaults(UI.MenuBar, args)
|
||||||
UI:setProperties(self, defaults)
|
UI.Window.init(self, defaults)
|
||||||
|
|
||||||
if not self.children then
|
if not self.children then
|
||||||
self.children = { }
|
self.children = { }
|
||||||
end
|
end
|
||||||
|
|
||||||
local x = 1
|
self:addButtons(self.buttons)
|
||||||
for _,button in pairs(self.buttons) do
|
if self.showBackButton then
|
||||||
|
table.insert(self.children, UI.MenuItem({
|
||||||
|
x = UI.term.width - 2,
|
||||||
|
width = 3,
|
||||||
|
backgroundColor = self.backgroundColor,
|
||||||
|
textColor = self.textColor,
|
||||||
|
text = '^-',
|
||||||
|
event = 'back',
|
||||||
|
}))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function UI.MenuBar:addButtons(buttons)
|
||||||
|
if not self.children then
|
||||||
|
self.children = { }
|
||||||
|
end
|
||||||
|
|
||||||
|
for _,button in pairs(buttons) do
|
||||||
if button.UIElement then
|
if button.UIElement then
|
||||||
table.insert(self.children, button)
|
table.insert(self.children, button)
|
||||||
else
|
else
|
||||||
local buttonProperties = {
|
local buttonProperties = {
|
||||||
x = x,
|
x = self.lastx,
|
||||||
width = #button.text + self.spacing,
|
width = #button.text + self.spacing,
|
||||||
centered = false,
|
centered = false,
|
||||||
}
|
}
|
||||||
x = x + buttonProperties.width
|
self.lastx = self.lastx + buttonProperties.width
|
||||||
UI:setProperties(buttonProperties, button)
|
UI:setProperties(buttonProperties, button)
|
||||||
|
|
||||||
button = UI[self.buttonClass](buttonProperties)
|
button = UI[self.buttonClass](buttonProperties)
|
||||||
@@ -2059,21 +2083,10 @@ function UI.MenuBar:init(args)
|
|||||||
|
|
||||||
if button.dropdown then
|
if button.dropdown then
|
||||||
button.dropmenu = UI.DropMenu { buttons = button.dropdown }
|
button.dropmenu = UI.DropMenu { buttons = button.dropdown }
|
||||||
--table.insert(self.children, buttonProperties.dropmenu)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if self.showBackButton then
|
self:initChildren()
|
||||||
table.insert(self.children, UI.MenuItem({
|
|
||||||
x = UI.term.width - 2,
|
|
||||||
width = 3,
|
|
||||||
backgroundColor = self.backgroundColor,
|
|
||||||
textColor = self.textColor,
|
|
||||||
text = '^-',
|
|
||||||
event = 'back',
|
|
||||||
}))
|
|
||||||
end
|
|
||||||
UI.Window.init(self, defaults)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function UI.MenuBar:getActive(menuItem)
|
function UI.MenuBar:getActive(menuItem)
|
||||||
@@ -2238,42 +2251,44 @@ UI.Tabs.defaults = {
|
|||||||
}
|
}
|
||||||
function UI.Tabs:init(args)
|
function UI.Tabs:init(args)
|
||||||
local defaults = UI:getDefaults(UI.Tabs, args)
|
local defaults = UI:getDefaults(UI.Tabs, args)
|
||||||
|
UI.Window.init(self, defaults)
|
||||||
|
|
||||||
|
self:add(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
function UI.Tabs:add(children)
|
||||||
|
|
||||||
local buttons = { }
|
local buttons = { }
|
||||||
for _,child in pairs(defaults) do
|
for _,child in pairs(children) do
|
||||||
if type(child) == 'table' and child.UIElement then
|
if type(child) == 'table' and child.UIElement then
|
||||||
|
child.y = 2
|
||||||
table.insert(buttons, {
|
table.insert(buttons, {
|
||||||
text = child.tabTitle or '', event = 'tab_select',
|
text = child.tabTitle or '',
|
||||||
|
event = 'tab_select',
|
||||||
|
tabUid = child.__uid,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
self.tabBar = UI.TabBar({
|
if not self.tabBar then
|
||||||
buttons = buttons,
|
self.tabBar = UI.TabBar({
|
||||||
})
|
buttons = buttons,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
self.tabBar:addButtons(buttons)
|
||||||
|
end
|
||||||
|
|
||||||
UI.Window.init(self, defaults)
|
if self.parent then
|
||||||
end
|
return UI.Window.add(self, children)
|
||||||
|
|
||||||
function UI.Tabs:setParent()
|
|
||||||
UI.Window.setParent(self)
|
|
||||||
|
|
||||||
for _,child in pairs(self.children) do
|
|
||||||
if child ~= self.tabBar then
|
|
||||||
child.oy = 2
|
|
||||||
--child.height = self.height - 1
|
|
||||||
child:resize()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function UI.Tabs:enable()
|
function UI.Tabs:enable()
|
||||||
self.enabled = true
|
self.enabled = true
|
||||||
for _,child in ipairs(self.children) do
|
|
||||||
if child.tabTitle == self.tabBar.buttons[1].text then
|
local _, menuItem = Util.first(self.tabBar.children, function(a, b) return a.x < b.x end)
|
||||||
self:activateTab(child)
|
if menuItem then
|
||||||
break
|
self:activateTab(Util.find(self.children, '__uid', menuItem.tabUid))
|
||||||
end
|
|
||||||
end
|
end
|
||||||
self.tabBar:enable()
|
self.tabBar:enable()
|
||||||
end
|
end
|
||||||
@@ -2292,11 +2307,9 @@ end
|
|||||||
|
|
||||||
function UI.Tabs:eventHandler(event)
|
function UI.Tabs:eventHandler(event)
|
||||||
if event.type == 'tab_select' then
|
if event.type == 'tab_select' then
|
||||||
for _,child in ipairs(self.children) do
|
local child = Util.find(self.children, '__uid', event.button.tabUid)
|
||||||
if child.tabTitle == event.button.text then
|
if child then
|
||||||
self:activateTab(child)
|
self:activateTab(child)
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
elseif event.type == 'tab_change' then
|
elseif event.type == 'tab_change' then
|
||||||
for _,tab in ipairs(self.children) do
|
for _,tab in ipairs(self.children) do
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ local Util = require('util')
|
|||||||
local fs = _G.fs
|
local fs = _G.fs
|
||||||
local multishell = _ENV.multishell
|
local multishell = _ENV.multishell
|
||||||
local os = _G.os
|
local os = _G.os
|
||||||
|
local settings = _G.settings
|
||||||
local shell = _ENV.shell
|
local shell = _ENV.shell
|
||||||
|
|
||||||
multishell.setTitle(multishell.getCurrent(), 'System')
|
multishell.setTitle(multishell.getCurrent(), 'System')
|
||||||
@@ -57,7 +58,6 @@ local systemPage = UI.Page {
|
|||||||
},
|
},
|
||||||
grid = UI.Grid {
|
grid = UI.Grid {
|
||||||
y = 5,
|
y = 5,
|
||||||
autospace = true,
|
|
||||||
sortColumn = 'alias',
|
sortColumn = 'alias',
|
||||||
columns = {
|
columns = {
|
||||||
{ heading = 'Alias', key = 'alias' },
|
{ heading = 'Alias', key = 'alias' },
|
||||||
@@ -108,6 +108,43 @@ local systemPage = UI.Page {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if settings then
|
||||||
|
local values = { }
|
||||||
|
for _,v in pairs(settings.getNames()) do
|
||||||
|
table.insert(values, {
|
||||||
|
name = v,
|
||||||
|
value = not not settings.get(v),
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
systemPage.tabs:add({
|
||||||
|
systemTab = UI.Window {
|
||||||
|
tabTitle = 'Settings',
|
||||||
|
grid = UI.Grid {
|
||||||
|
y = 1,
|
||||||
|
values = values,
|
||||||
|
--autospace = true,
|
||||||
|
sortColumn = 'name',
|
||||||
|
columns = {
|
||||||
|
{ heading = 'Setting', key = 'name' },
|
||||||
|
{ heading = 'Value', key = 'value' },
|
||||||
|
},
|
||||||
|
accelerators = {
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
function systemPage.tabs.systemTab:eventHandler(event)
|
||||||
|
if event.type == 'grid_select' then
|
||||||
|
event.selected.value = not event.selected.value
|
||||||
|
settings.set(event.selected.name, event.selected.value)
|
||||||
|
settings.save('.settings')
|
||||||
|
self.grid:draw()
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function systemPage.tabs.pathTab.grid:draw()
|
function systemPage.tabs.pathTab.grid:draw()
|
||||||
self.values = { }
|
self.values = { }
|
||||||
for _,v in ipairs(Util.split(env.path, '(.-):')) do
|
for _,v in ipairs(Util.split(env.path, '(.-):')) do
|
||||||
|
|||||||
Reference in New Issue
Block a user