1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-27 04:48:05 +11:00

Optimize Icecap Nexus

The things I have to destroy, the joy in it!
For all I remove the more I save, the better it is!
Perfection is when there is nothing to take away, and I revel in this task.
This commit is contained in:
20kdc 2018-04-25 22:57:25 +01:00
parent a22ac86b2d
commit 921425ada4
4 changed files with 185 additions and 240 deletions

View File

@ -26,72 +26,41 @@ local function addOnReg(p, f)
table.insert(onReg[p], f)
end
local function resumeWF(...)
local ok, e = coroutine.resume(...)
if not ok then
e = tostring(e)
neo.emergency(e)
nexus.startDialog(e, "ice")
end
return ok, e
end
nexus = {
createNexusThread = function (f, ...)
local t = coroutine.create(f)
local ok, cbi = resumeWF(t, ...)
if not ok then return end
local early = neo.requestAccess("x.neo.pub.window", theEventHandler)
if early then
local r = onReg["x.neo.pub.window"]
-- r should not be nil here
onReg["x.neo.pub.window"] = nil
for k, v in ipairs(r) do
v()
end
end
return function ()
local r = onReg["x.neo.pub.window"]
if not r then return end
for k, v in ipairs(r) do
if v == cbi then
table.remove(r, k)
return
end
end
end
end,
create = function (w, h, t)
local thr = coroutine.running()
create = function (w, h, t, c)
local function cb()
coroutine.resume(thr, neo.requestAccess("x.neo.pub.window"))
local e = neo.requestAccess("x.neo.pub.window", theEventHandler)
if e then
if onReg["x.neo.pub.window"] then
neo.emergency("icecap nexus prereg issue")
theEventHandler("k.registration", "x.neo.pub.window")
end
local dw = e(w, h, t)
c(dw)
everestWindows[dw.id] = function (...)
return c(dw, ...)
end
return true
end
end
if not cb() then
addOnReg("x.neo.pub.window", cb)
local everest = coroutine.yield(cb)
local dw = everest(w, h, title)
everestWindows[dw.id] = thr
end
return dw
end,
windows = everestWindows,
startDialog = function (tx, ti)
local fmt = require("fmttext")
local txl = fmt.fmtText(unicode.safeTextFormat(tx), 40)
fmt = nil
nexus.createNexusThread(function ()
local w = nexus.create(40, #txl, ti)
while true do
local ev, a = coroutine.yield()
local txl = require("fmttext").fmtText(unicode.safeTextFormat(tx), 40)
nexus.create(40, #txl, ti, function (w, ev, a)
if ev == "line" then
w.span(1, a, txl[a], 0xFFFFFF, 0)
if not pcall(w.span, 1, a, txl[a], 0xFFFFFF, 0) then
everestWindows[dw.id] = nil
end
elseif ev == "close" then
w.close()
return
end
everestWindows[dw.id] = nil
end
end)
end,
close = function (wnd)
wnd.close()
everestWindows[wnd.id] = nil
end
}
@ -344,10 +313,7 @@ function theEventHandler(...)
elseif ev[1] == "x.neo.pub.window" then
local v = everestWindows[ev[2]]
if v then
resumeWF(v, table.unpack(ev, 3))
if coroutine.status(v) == "dead" then
everestWindows[ev[2]] = nil
end
v(table.unpack(ev, 3))
end
end
end

View File

@ -3,6 +3,7 @@
-- just don't bother with proper indent here
return function (event, nexus, retFunc, fs, pkg, mode)
local fmt = require("fmttext")
local class = "manage"
if mode ~= nil then
@ -13,36 +14,19 @@ if mode ~= nil then
end
end
local prepareNode
-- State
local w, h, ctrl = 30, 8, false
local l, selection, unknownTx
local node, wnd
local ccb = nil
local function cb(...)
local res, e = pcall(ccb, ...)
if not res then
prepareNode({
name = "F.M. Error",
list = function ()
local l = {}
for k, v in ipairs(fmt.fmtText(unicode.safeTextFormat(e), 25)) do
l[k] = {v, function () return true end}
end
return l
end,
unknownAvailable = false,
selectUnknown = function (text) end
})
end
local function prepareNode(n)
node = n
l = node.list()
selection, unknownTx = 1, ""
wnd.setSize(w, h)
end
local w, h = 30, 8
local function prepareNodeI(node)
local l = node.list()
-- Local State
-- Selection. Having this equal to #l + 1 means typing area ('unknown')
local selection = 1
local unknownTx = ""
--
local function format(a)
if a <= 1 then
return false, fmt.pad(unicode.safeTextFormat(node.name), w, true, true)
@ -58,7 +42,8 @@ local function prepareNodeI(node)
end
return selection == idx, utx
end
local function updateLine(wnd, a)
local function updateLine(a)
local colA, colB = 0xFFFFFF, 0
local sel, text = format(a)
if sel then
@ -66,13 +51,14 @@ local function prepareNodeI(node)
end
wnd.span(1, a, text, colA, colB)
end
local function flush(wnd)
local function flush()
for i = 1, h do
updateLine(wnd, i)
updateLine(i)
end
end
local ctrl = false
local function key(wnd, ka, kc, down)
local function key(ka, kc, down)
if kc == 29 then
ctrl = down
end
@ -91,8 +77,7 @@ local function prepareNodeI(node)
end
wnd.setSize(w, h)
return
end
if (ka == 9) or (kc == 208) then
elseif (ka == 9) or (kc == 208) then
local lo = selection
selection = selection + 1
local max = #l
@ -102,10 +87,7 @@ local function prepareNodeI(node)
if selection > max then
selection = 1
end
flush(wnd)
return
end
if kc == 200 then
elseif kc == 200 then
local lo = selection
selection = selection - 1
local max = #l
@ -115,10 +97,7 @@ local function prepareNodeI(node)
if selection == 0 then
selection = max
end
flush(wnd)
return
end
if ka == 13 then
elseif ka == 13 then
local aResult, res
if selection ~= #l + 1 then
aResult, res = l[selection][2]()
@ -127,77 +106,77 @@ local function prepareNodeI(node)
end
if aResult then
retFunc(res)
nexus.close(wnd)
nexus.windows[wnd.id] = nil
wnd.close()
else
prepareNode(res)
end
return
end
if selection == #l + 1 then
elseif selection == #l + 1 then
if ka == 8 then
unknownTx = unicode.sub(unknownTx, 1, unicode.len(unknownTx) - 1)
flush(wnd)
return
end
if ka ~= 0 then
elseif ka ~= 0 then
unknownTx = unknownTx .. unicode.char(ka)
flush(wnd)
end
end
flush()
end
return w, h, function (wnd, evt, a, b, c)
if evt == "key" then
key(wnd, a, b, c)
local function key2(...)
local res, e = pcall(key, ...)
if not res then
prepareNode({
name = "F.M. Error",
list = function ()
local l = {}
for k, v in ipairs(fmt.fmtText(unicode.safeTextFormat(e), w)) do
l[k] = {v, function () return true end}
end
if evt == "touch" then
return l
end,
unknownAvailable = false,
selectUnknown = function (text) end
})
end
end
nexus.create(w, h, class .. " " .. pkg, function (w, ev, a, b, c)
if not wnd then
wnd = w
prepareNode(require("sys-filevfs")(fs, mode))
end
if ev == "key" then
key2(a, b, c)
end
if ev == "touch" then
local ns = b + math.max(1, selection - 3) - 2
local max = #l
if node.unknownAvailable then
max = max + 1
end
if ns == selection and ((selection ~= #l + 1) or (a <= 4)) then
key(wnd, 13, 0, true)
key2(13, 0, true)
else
selection = math.min(math.max(1, ns), max)
flush(wnd)
flush()
end
end
if evt == "line" then
updateLine(wnd, a)
if ev == "line" then
updateLine(a)
end
if evt == "close" then
if ev == "close" then
retFunc(nil)
nexus.close(wnd)
end
end
end
local text = class .. " " .. pkg
local window
function prepareNode(node)
local w, h, c = prepareNodeI(node)
ccb = c
window.setSize(w, h)
end
local closer = nexus.createNexusThread(function ()
window = nexus.create(25, 10, text)
prepareNode(require("sys-filevfs")(fs, mode))
while window do
cb(window, coroutine.yield())
nexus.windows[wnd.id] = nil
wnd.close()
end
end)
if not closer then
retFunc()
return
end
return function ()
retFunc()
closer()
if window then
nexus.close(window)
window = nil
if wnd then
nexus.windows[wnd.id] = nil
wnd.close()
wnd = nil
end
end

View File

@ -88,10 +88,10 @@ function getFsNode(fs, parent, fsc, path, mode)
return nil, parent
end}
for k, v in ipairs(fsc.list(path)) do
local nm = "[F] " .. v
local nm = "F: " .. v
local fp = path .. v
if fsc.isDirectory(fp) then
nm = "[D] " .. v
nm = "D: " .. v
end
n[k + 1] = {nm, function () return nil, getFsNode(fs, t, fsc, fp, mode) end}
end

View File

@ -56,31 +56,31 @@ return function (nexus, settings, pkg, pid, perm, rsp, matchesSvc)
local buttons = {
{"<No>", function (w)
rsp(false)
nexus.close(w)
nexus.windows[w.id] = nil
w.close()
end},
{"<Always>", function (w)
if settings then
settings.setSetting("perm|" .. pkg .. "|" .. perm, "allow")
end
rsp(true)
nexus.close(w)
nexus.windows[w.id] = nil
w.close()
end},
{"<Yes>", function (w)
rsp(true)
nexus.close(w)
nexus.windows[w.id] = nil
w.close()
end}
}
nexus.createNexusThread(function ()
local window = nexus.create(totalW, #fmt, "security")
local cButton = 0
local ev, a, b, c
while true do
if not ev then
ev, a, b, c = coroutine.yield()
end
nexus.create(totalW, #fmt, "security", function (window, ev, a, b, c)
while ev do
if ev == "line" or ev == "touch" then
local cor = b
if ev == "line" then
local iev = ev
ev = nil
if iev == "line" then
cor = a
if fmt[a] then
window.span(1, a, fmt[a], 0xFFFFFF, 0)
@ -89,7 +89,7 @@ return function (nexus, settings, pkg, pid, perm, rsp, matchesSvc)
if cor == #fmt then
local x = 1
for k, v in ipairs(buttons) do
if ev == "line" then
if iev == "line" then
if k ~= cButton + 1 then
window.span(x, a, v[1], 0xFFFFFF, 0)
else
@ -108,10 +108,10 @@ return function (nexus, settings, pkg, pid, perm, rsp, matchesSvc)
end
elseif ev == "close" then
rsp(false)
nexus.close(window)
return
end
if ev == "key" then
nexus.windows[w.id] = nil
w.close()
ev = nil
elseif ev == "key" then
if c and (a == 9 or b == 205) then
cButton = (cButton + 1) % #buttons
ev = "line"