input redo + env pollution

This commit is contained in:
kepler155c@gmail.com 2017-10-20 04:23:17 -04:00
parent fb0f3e567a
commit 31b3787695
9 changed files with 84 additions and 67 deletions

View File

@ -6,35 +6,35 @@ local os = _G.os
local modifiers = Util.transpose {
keys.leftCtrl, keys.rightCtrl,
keys.leftShift, keys.rightShift,
--keys.leftAlt, keys.rightAlt,
keys.leftAlt, keys.rightAlt,
}
local input = {
pressed = { },
}
function input:toCode(code, ch)
function input:toCode(ch, code)
ch = ch or keys.getName(code)
local result = { }
if self.pressed[keys.leftCtrl] or self.pressed[keys.rightCtrl] then
table.insert(result, 'control')
end
--if self.pressed[keys.leftAlt] or self.pressed[keys.rightAlt] then
-- table.insert(result, 'alt')
--end
if self.pressed[keys.leftShift] or self.pressed[keys.rightShift] then
if modifiers[code] or #ch > 1 then
table.insert(result, 'shift')
else
ch = ch:upper()
end
if self.pressed[keys.leftAlt] or self.pressed[keys.rightAlt] then
table.insert(result, 'alt')
end
if not modifiers[code] then
if self.pressed[keys.leftShift] or self.pressed[keys.rightShift] then
if code and modifiers[code] then
table.insert(result, 'shift')
elseif #ch == 1 then
table.insert(result, ch:upper())
else
table.insert(result, 'shift')
table.insert(result, keys.getName(code) or ch)
end
elseif not code or not modifiers[code] then
table.insert(result, ch)
end
@ -43,7 +43,6 @@ end
function input:reset()
self.pressed = { }
self.ch = nil
self.fired = nil
self.timer = nil
@ -55,25 +54,22 @@ function input:translate(event, code, p1, p2)
if event == 'key' then
if p1 then -- key is held down
if not modifiers[code] then
self.fired = input:toCode(code, self.ch)
self.fired = input:toCode(keys.getName(code), code)
return self.fired
end
else
self.fired = nil
self.ch = nil
self.pressed[code] = true
end
elseif event == 'char' then
self.ch = code
-- reset just in case
self.pressed[keys.leftCtrl] = nil
self.pressed[keys.rightCtrl] = nil
self.fired = true
return input:toCode(code)
elseif event == 'key_up' then
if not self.fired then
if self.pressed[code] then
self.fired = input:toCode(code, self.ch)
self.fired = input:toCode(keys.getName(code), code)
self.pressed[code] = nil
return self.fired
end
@ -81,11 +77,10 @@ function input:translate(event, code, p1, p2)
self.pressed[code] = nil
elseif event == 'paste' then
self.ch = 'paste'
self.pressed[keys.leftCtrl] = nil
self.pressed[keys.rightCtrl] = nil
self.fired = input:toCode(0, self.ch)
return self.fired
self.fired = true
return input:toCode('paste')
elseif event == 'mouse_click' then
local buttons = { 'mouse_click', 'mouse_rightclick' }
@ -93,9 +88,8 @@ function input:translate(event, code, p1, p2)
self.mfired = nil
elseif event == 'mouse_drag' then
self.mch = 'mouse_drag'
self.mfired = input:toCode(0, self.mch)
return self.mfired
self.mfired = true
return input:toCode('mouse_drag')
elseif event == 'mouse_up' then
if not self.mfired then
@ -111,11 +105,12 @@ function input:translate(event, code, p1, p2)
self.x = p1
self.y = p2
end
self.mfired = input:toCode(0, self.mch)
self.mfired = input:toCode(self.mch)
else
self.mch = 'mouse_up'
self.mfired = input:toCode(0, self.mch)
self.mfired = input:toCode(self.mch)
end
self.fired = true
return self.mfired
elseif event == "mouse_scroll" then
@ -123,8 +118,8 @@ function input:translate(event, code, p1, p2)
[ -1 ] = 'scrollUp',
[ 1 ] = 'scrollDown'
}
self.mch = directions[code]
return input:toCode(0, self.mch)
self.fired = true
return input:toCode(directions[code])
end
end

View File

@ -10,8 +10,6 @@ local _sub = string.sub
local colors = _G.colors
local device = _G.device
local fs = _G.fs
local keys = _G.keys
local multishell = _ENV.multishell
local os = _G.os
local term = _G.term
@ -121,7 +119,7 @@ function Manager:init()
if ch == 'control-shift-mouse_click' then -- hack
local event = self:pointToChild(self.target, x, y)
multishell.openTab({
_ENV.multishell.openTab({
path = 'sys/apps/Lua.lua',
args = { event.element, self:dump(self.currentPage) },
focused = true })
@ -152,12 +150,8 @@ function Manager:init()
self.currentPage:sync()
end)
singleThread('char', function(ch)
Input:translate('char', ch)
end)
singleThread('key_up', function(code)
local ch = Input:translate('key_up', code)
local function keyFunction(event, code, held)
local ch = Input:translate(event, code, held)
if ch and self.currentPage then
local target = self.currentPage.focused or self.currentPage
@ -165,18 +159,11 @@ function Manager:init()
{ type = 'key', key = ch, element = target })
self.currentPage:sync()
end
end)
end
singleThread('key', function(code, held)
local ch = Input:translate('key', code, held)
if ch and self.currentPage then
local target = self.currentPage.focused or self.currentPage
self:inputEvent(target,
{ type = 'key', key = ch, element = target })
self.currentPage:sync()
end
end)
singleThread('char', function(code, held) keyFunction('char', code, held) end)
singleThread('key_up', function(code, held) keyFunction('key_up', code, held) end)
singleThread('key', function(code, held) keyFunction('key', code, held) end)
end
function Manager:configure(appName, ...)
@ -1156,6 +1143,7 @@ function UI.Page:setParent()
UI.Window.setParent(self)
if self.z then
self.canvas = self:addLayer(self.backgroundColor, self.textColor)
self.canvas:clear(self.backgroundColor, self.textColor)
else
self.canvas = self.parent.canvas
end
@ -2281,6 +2269,19 @@ function UI.Wizard:postInit()
end
end
function UI.Wizard:add(pages)
Util.merge(self.pages, pages)
Util.merge(self, pages)
for _, child in pairs(self.pages) do
child.ey = child.ey or -2
end
if self.parent then
self:initChildren()
end
end
function UI.Wizard:enable()
self.enabled = true
for _,child in ipairs(self.children) do
@ -2300,10 +2301,10 @@ function UI.Wizard:nextView()
local nextView = Util.find(self.pages, 'index', currentView.index + 1)
if nextView then
self:emit({ type = 'enable_view', view = nextView })
self:addTransition('slideLeft')
currentView:disable()
nextView:enable()
self:emit({ type = 'enable_view', view = nextView })
end
end
@ -2312,10 +2313,10 @@ function UI.Wizard:prevView()
local nextView = Util.find(self.pages, 'index', currentView.index - 1)
if nextView then
self:emit({ type = 'enable_view', view = nextView })
self:addTransition('slideRight')
currentView:disable()
nextView:enable()
self:emit({ type = 'enable_view', view = nextView })
end
end
@ -2323,10 +2324,12 @@ function UI.Wizard:eventHandler(event)
if event.type == 'nextView' then
self:nextView()
self:draw()
return true
elseif event.type == 'previousView' then
self:prevView()
self:draw()
return true
elseif event.type == 'enable_view' then
if Util.find(self.pages, 'index', event.view.index - 1) then

View File

@ -312,7 +312,6 @@ function Canvas:applyPalette(palette)
end
function Canvas.convertWindow(win, parent, wx, wy)
local w, h = win.getSize()
win.canvas = Canvas({
@ -355,7 +354,7 @@ function Canvas.convertWindow(win, parent, wx, wy)
end
function win.scroll()
error('CWin:scroll: not implemented')
error('scroll: not implemented')
end
function win.reposition(x, y, width, height)

View File

@ -141,7 +141,7 @@ function Util.key(t, value)
end
function Util.keys(t)
local keys = {}
local keys = { }
for k in pairs(t) do
keys[#keys+1] = k
end
@ -283,6 +283,19 @@ function Util.each(list, func)
end
end
function Util.rpairs(t)
local tkeys = Util.keys(t)
local i = #tkeys
return function()
local key = tkeys[i]
local k,v = key, t[key]
i = i - 1
if v then
return k, v
end
end
end
-- http://stackoverflow.com/questions/15706270/sort-a-table-in-lua
function Util.spairs(t, order)
local keys = Util.keys(t)

View File

@ -64,7 +64,6 @@ function topicPage:eventHandler(event)
end
function page:eventHandler(event)
if event.type == 'quit' then
UI:exitPullEvents()

View File

@ -1,6 +1,6 @@
local defaultEnv = { }
local sandboxEnv = { }
for k,v in pairs(_ENV) do
defaultEnv[k] = v
sandboxEnv[k] = v
end
_G.requireInjector()
@ -127,7 +127,7 @@ local function selectTab(tab)
if coroutine.status(currentTab.co) == 'suspended' then
-- the process that opens a new tab won't get the lose focus event
-- os.queueEvent('multishell_notifyfocus', currentTab.tabId)
resumeTab(currentTab, 'multishell_losefocus')
--resumeTab(currentTab, 'multishell_losefocus')
end
if tab and not currentTab.hidden then
tab.previousTabId = currentTab.tabId
@ -151,7 +151,7 @@ local function launchProcess(tab)
tab.timestamp = os.clock()
tab.window = window.create(parentTerm, 1, 2, w, h - 1, false)
tab.terminal = tab.window
tab.env = Util.shallowCopy(tab.env or defaultEnv)
tab.env = Util.shallowCopy(tab.env or sandboxEnv)
tab.co = coroutine.create(function()
@ -229,7 +229,9 @@ end
function multishell.setTitle(tabId, title)
local tab = tabs[tabId]
if tab then
tab.title = title or ''
if not tab.isOverview then
tab.title = title or ''
end
redrawMenu()
end
end

View File

@ -329,7 +329,8 @@ end
local tArgs = { ... }
if #tArgs > 0 then
return run(_ENV, ...)
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
return run(env, ...)
end
local Config = require('config')

View File

@ -36,9 +36,14 @@ term.setCursorPos(w, h)
local GIT_REPO = 'kepler155c/opus/develop'
local BASE = 'https://raw.githubusercontent.com/' .. GIT_REPO
local sandboxEnv = setmetatable({ }, { __index = _G })
for k,v in pairs(_ENV) do
sandboxEnv[k] = v
end
local function makeEnv()
local env = setmetatable({ }, { __index = _G })
for k,v in pairs(_ENV) do
for k,v in pairs(sandboxEnv) do
env[k] = v
end
return env
@ -113,7 +118,7 @@ if config.aliases then
end
end
shell.setPath(config.path)
_ENV.LUA_PATH = config.lua_path
sandboxEnv.LUA_PATH = config.lua_path
-- extensions
local dir = 'sys/extensions'

View File

@ -133,7 +133,7 @@ local methods = { 'delete', 'getFreeSpace', 'exists', 'isDir', 'getSize',
for _,m in pairs(methods) do
fs[m] = function(dir, ...)
dir = fs.combine(dir, '')
dir = fs.combine(dir or '', '')
local node = getNode(dir)
return node.fs[m](node, dir, ...)
end