From 8db9a89f680cc7304ed478d95fa6d40a4d34ffe9 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Tue, 5 May 2020 17:26:44 -0600 Subject: [PATCH] nwm opacity / improvements - env cleanup --- common/Appstore.lua | 9 ++-- neural/apis/glasses.lua | 92 +++++++++++++++++++---------------------- neural/nwm.lua | 69 ++++++++++++++----------------- secure/autorun/lock.lua | 4 -- 4 files changed, 75 insertions(+), 99 deletions(-) diff --git a/common/Appstore.lua b/common/Appstore.lua index 7576f5f..7e0ad85 100644 --- a/common/Appstore.lua +++ b/common/Appstore.lua @@ -27,9 +27,6 @@ local function unregisterApp(key) end end -local sandboxEnv = Util.shallowCopy(_ENV) -setmetatable(sandboxEnv, { __index = _G }) - multishell.setTitle(multishell.getCurrent(), 'App Store') UI:configure('Appstore', ...) @@ -60,7 +57,7 @@ local function downloadApp(app) end local function runApp(app, checkExists, ...) - + local env = shell.makeEnv() local path, fn local args = { ... } @@ -81,14 +78,14 @@ local function runApp(app, checkExists, ...) error('Failed to download') end - _G.setfenv(fn, sandboxEnv) + _G.setfenv(fn, env) fn(table.unpack(args)) end end multishell.openTab({ title = app.name, - env = sandboxEnv, + env = env, path = path, fn = fn, focused = true, diff --git a/neural/apis/glasses.lua b/neural/apis/glasses.lua index f6b62a1..1d8a1d4 100644 --- a/neural/apis/glasses.lua +++ b/neural/apis/glasses.lua @@ -3,55 +3,64 @@ ]] local Terminal = require('opus.terminal') +local Util = require('opus.util') local colors = _G.colors local device = _G.device -local scale = .5 -local xs, ys = 6 * scale, 9 * scale - local Glasses = { } -function Glasses.create(name, sx, sy, w, h) - w, h = w or 46, h or 19 - sx, sy = sx or 1, sy or 20 +function Glasses.create(args) + local opts = { + x = 1, y = 20, + width = 51, height = 19, + scale = .5, + name = 'glasses', + opacity = 0xff, + } + Util.merge(opts, args) + local xs, ys = 6 * opts.scale, 9 * opts.scale local glasses = device['plethora:glasses'] local canvas = glasses.canvas() local _, cy = 1, 1 local lines = { } local map = { - ['0'] = 0xF0F0F0FF, - ['1'] = 0xF2B233FF, - ['2'] = 0xE57FD8FF, - ['3'] = 0x99B2F2FF, - ['4'] = 0xDEDE6CFF, - ['5'] = 0x7FCC19FF, - ['6'] = 0xF2B2CCFF, - ['7'] = 0x4C4C4CFF, - ['8'] = 0x999999FF, - ['9'] = 0x4C99B2FF, - ['a'] = 0xB266E5FF, - ['b'] = 0x3366CCFF, - ['c'] = 0x7F664CFF, - ['d'] = 0x57A64EFF, - ['e'] = 0xCC4C4CFF, - ['f'] = 0x191919FF, + ['0'] = 0xF0F0F000, + ['1'] = 0xF2B23300, + ['2'] = 0xE57FD800, + ['3'] = 0x99B2F200, + ['4'] = 0xDEDE6C00, + ['5'] = 0x7FCC1900, + ['6'] = 0xF2B2CC00, + ['7'] = 0x4C4C4C00, + ['8'] = 0x99999900, + ['9'] = 0x4C99B200, + ['a'] = 0xB266E500, + ['b'] = 0x3366CC00, + ['c'] = 0x7F664C00, + ['d'] = 0x57A64E00, + ['e'] = 0xCC4C4C00, + ['f'] = 0x19191900, } + for k,v in pairs(map) do + map[k] = v + opts.opacity + end + -- Position bottom left - local pos = { x = sx * xs, y = sy * ys } + local pos = { x = opts.x * xs, y = opts.y * ys } local function init(group) - for y = 1, h do + for y = 1, opts.height do lines[y] = { text = { }, bg = { } } - for x = 1, w do + for x = 1, opts.width do lines[y].bg[x] = group.addRectangle(x * xs, y * ys, xs, ys, 0xF0F0F04F) lines[y].text[x] = group.addText({ x * xs, y * ys }, '', 0x7FCC19FF) - lines[y].text[x].setScale(scale) + lines[y].text[x].setScale(opts.scale) end end end @@ -60,21 +69,9 @@ function Glasses.create(name, sx, sy, w, h) init(group) local gterm = Terminal.window({ - getSize = function() - return w, h - end, isColor = function() return true end, - clear = function() - for y = 1, h do - for x = 1, w do - local ln = lines[y] - ln.bg[x].setColor(0xF0F0F04F) - ln.text[x].setText('') - end - end - end, blit = function(text, fg, bg) for x = 1, #text do local ln = lines[cy] @@ -86,19 +83,11 @@ function Glasses.create(name, sx, sy, w, h) setCursorPos = function(_, y) cy = y -- full lines are always blit end, - getTextColor = function() - return colors.white - end, - setTextColor = function() end, - getBackgroundColor = function() - return colors.black - end, - setBackgroundColor = function() end, setCursorBlink = function() end, - }, 1, 1, w, h, true) + }, 1, 1, opts.width, opts.height, true) function gterm.setTextScale() end - function gterm.getPosition() return sx, sy end + function gterm.getPosition() return opts.x, opts.y end function gterm.setVisible() end function gterm.raise() local g = canvas.addGroup(pos) @@ -110,9 +99,12 @@ function Glasses.create(name, sx, sy, w, h) function gterm.destroy() group.remove() end + function gterm.getTextScale() + return opts.scale + end - gterm.name = name - gterm.side = name + gterm.name = opts.name + gterm.side = opts.name gterm.type = 'glasses' return gterm diff --git a/neural/nwm.lua b/neural/nwm.lua index b956e62..7e54db0 100644 --- a/neural/nwm.lua +++ b/neural/nwm.lua @@ -12,7 +12,7 @@ local kernel = _G.kernel local multishell = _ENV.multishell local shell = _ENV.shell -local sandbox = Util.shallowCopy(_ENV) +local config = Config.load('nwm', { session = { } }) -- TODO: figure out how to better define scaling local scale = .5 @@ -25,8 +25,6 @@ local events = { glasses_scroll = 'mouse_scroll', } -local hookEvents = { 'glasses_click', 'glasses_up', 'glasses_drag', 'glasses_scroll' } - local function hook(e, eventData) local currentTab = kernel.getFocused() local x = math.floor(eventData[2] / xs) @@ -61,19 +59,15 @@ local function hook(e, eventData) return true end -local config = Config.load('nwm', { session = { } }) +local hookEvents = Util.keys(events) +kernel.hook(hookEvents, hook) local function run(args) - local window = Glasses.create('glasses', args.x, args.y, args.w, args.h) - - local env = Util.shallowCopy(sandbox) - _G.requireInjector(env) + local window = Glasses.create(args) multishell.openTab({ path = args.path, args = args.args, - env = env, - focused = false, hidden = true, onDestroy = function() Util.removeByValue(config.session, args) @@ -84,66 +78,63 @@ local function run(args) }) end -kernel.hook(hookEvents, hook) - UI:setPage(UI.Page { form = UI.Form { values = { - x = 1, y = 25, w = 51, h = 19, + x = 1, y = 25, width = 51, height = 19, + opacity = 255, }, - path = UI.TextEntry { - y = 5, - formKey = 'path', formLabel = 'Run', required = true, + UI.TextEntry { + formKey = 'run', formLabel = 'Run', required = true, }, - args = UI.TextEntry { - y = 7, - formKey = 'args', formLabel = 'Args', + UI.Slider { + min = 0, max = 255, + formLabel = 'Opacity', formKey = 'opacity', formIndex = 3, }, UI.Text { - x = 7, y = 5, + x = 10, y = 5, textColor = 'yellow', value = ' x y' }, - wx = UI.TextEntry { - x = 7, y = 6, width = 7, limit = 3, + UI.TextEntry { + x = 10, y = 6, width = 7, limit = 3, transform = 'number', formKey = 'x', required = true, }, - wy = UI.TextEntry { - x = 15, y = 6, width = 7, limit = 4, + UI.TextEntry { + x = 18, y = 6, width = 7, limit = 4, transform = 'number', formKey = 'y', required = true, }, UI.Text { - x = 7, y = 8, + x = 10, y = 8, textColor = 'yellow', value = ' width height' }, - ww = UI.TextEntry { - x = 7, y = 9, width = 7, limit = 4, + UI.TextEntry { + x = 10, y = 9, width = 7, limit = 4, transform = 'number', - formKey = 'w', required = true, + formKey = 'width', required = true, }, - wh = UI.TextEntry { - x = 15, y = 9, width = 7, limit = 4, + UI.TextEntry { + x = 18, y = 9, width = 7, limit = 4, transform = 'number', - formKey = 'h', required = true, + formKey = 'height', required = true, }, }, notification = UI.Notification { }, eventHandler = function(self, event) if event.type == 'form_complete' then - local args = Util.shallowCopy(event.values) - args.path = shell.resolveProgram(args.path) - if not args.path then + local opts = Util.shallowCopy(event.values) + local words = Util.split(opts.run, '(.-) ') + opts.path = shell.resolveProgram(table.remove(words, 1)) + if not opts.path then self.notification:error('Invalid program') else - if args.args then - args.args = Util.split(args.args, '(.-) ') - end - table.insert(config.session, args) + opts.args = #words > 0 and words + table.insert(config.session, opts) Config.update('nwm', config) - run(args) + run(opts) self.notification:success('Started program') end end diff --git a/secure/autorun/lock.lua b/secure/autorun/lock.lua index a8965d7..eadff4c 100644 --- a/secure/autorun/lock.lua +++ b/secure/autorun/lock.lua @@ -17,9 +17,6 @@ local config = Config.load('secure', { local timer = config.enabled and os.startTimer(config.timeout) -local sandboxEnv = Util.shallowCopy(_ENV) -setmetatable(sandboxEnv, { __index = _G }) - local function buildLockScreen() _G.requireInjector(_ENV) @@ -81,7 +78,6 @@ local function showLockScreen() pinned = true, focused = true, title = 'Lock', - env = sandboxEnv, }) end