From 5eeed4530bbcc669a926b173c22644c34481b656 Mon Sep 17 00:00:00 2001 From: Igor Timofeev Date: Mon, 23 Oct 2017 11:25:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20event-=D0=BB=D0=B8=D0=B1=D1=8B=20?= =?UTF-8?q?=D0=B8=20=D1=81=D0=BE=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BA=D1=80=D0=BE=D0=BB=D0=BB=D0=B1=D0=B0=D1=80=D0=B0=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BD=D0=B4=D0=B5=D1=80=D0=B0,=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=B3=D0=B4=D0=B0=20=D0=BE=D0=BD=20=D0=BD=D0=B5=20=D1=82?= =?UTF-8?q?=D1=80=D0=B5=D0=B1=D1=83=D0=B5=D1=82=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications.cfg | 6 +- Applications/Finder/Main.lua | 13 +++- lib/MineOSNetwork.lua | 5 +- lib/event.lua | 120 +++++++++++++++++------------------ 4 files changed, 76 insertions(+), 68 deletions(-) diff --git a/Applications.cfg b/Applications.cfg index 3e73af3e..c4bd0716 100644 --- a/Applications.cfg +++ b/Applications.cfg @@ -235,7 +235,7 @@ path="/lib/MineOSNetwork.lua", url="https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/MineOSNetwork.lua", type="Library", - version=1.05, + version=1.06, }, { path="/lib/MineOSInterface.lua", @@ -268,7 +268,7 @@ path="/lib/event.lua", url="https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/event.lua", type="Library", - version=1.08, + version=1.09, }, { path="/lib/ECSAPI.lua", @@ -603,7 +603,7 @@ type="Application", icon="https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/Applications/Finder/Icon.pic", forceDownload=true, - version=1.29, + version=1.30, }, { path="/MineOS/Applications/Weather", diff --git a/Applications/Finder/Main.lua b/Applications/Finder/Main.lua index 98edc7b6..3b44010f 100644 --- a/Applications/Finder/Main.lua +++ b/Applications/Finder/Main.lua @@ -204,7 +204,9 @@ window.searchInput.onInputFinished = function() end local function updateScrollBar() - local horizontalLines = math.ceil((#window.iconField.fileList - window.iconField.fromFile + 1) / window.iconField.iconCount.horizontal) + local shownFilesCount = #window.iconField.fileList - window.iconField.fromFile + 1 + + local horizontalLines = math.ceil(shownFilesCount / window.iconField.iconCount.horizontal) local minimumOffset = 3 - (horizontalLines - 1) * (MineOSCore.properties.iconHeight + MineOSCore.properties.iconVerticalSpaceBetween) - MineOSCore.properties.iconVerticalSpaceBetween if window.iconField.yOffset > iconFieldYOffset then @@ -213,8 +215,13 @@ local function updateScrollBar() window.iconField.yOffset = minimumOffset end - window.scrollBar.maximumValue = math.abs(minimumOffset) - window.scrollBar.value = math.abs(window.iconField.yOffset - iconFieldYOffset) + if shownFilesCount > window.iconField.iconCount.total then + window.scrollBar.hidden = false + window.scrollBar.maximumValue = math.abs(minimumOffset) + window.scrollBar.value = math.abs(window.iconField.yOffset - iconFieldYOffset) + else + window.scrollBar.hidden = true + end end local overrideUpdateFileList = window.iconField.updateFileList diff --git a/lib/MineOSNetwork.lua b/lib/MineOSNetwork.lua index de72d461..d4619a82 100755 --- a/lib/MineOSNetwork.lua +++ b/lib/MineOSNetwork.lua @@ -327,7 +327,7 @@ end function MineOSNetwork.disable() if MineOSNetwork.eventHandlerID then - event.cancel(MineOSNetwork.eventHandlerID) + event.removeHandler(MineOSNetwork.eventHandlerID) end MineOSNetwork.unmountAll() end @@ -335,8 +335,9 @@ end function MineOSNetwork.enable() MineOSNetwork.disable() - MineOSNetwork.eventHandlerID = event.register(function(...) + MineOSNetwork.eventHandlerID = event.addHandler(function(...) local eventData = {...} + if eventData[1] == "component_added" or eventData[1] == "component_removed" then MineOSNetwork.updateModemState() elseif eventData[1] == "modem_message" and MineOSCore.properties.network.enabled and eventData[6] == "MineOSNetwork" then diff --git a/lib/event.lua b/lib/event.lua index cc9dde2b..a66ac4c1 100755 --- a/lib/event.lua +++ b/lib/event.lua @@ -6,9 +6,8 @@ local computer = require("computer") -local event = { +local event, handlers, interruptingKeysDown, lastInterrupt = { push = computer.pushSignal, - handlers = {}, interruptingEnabled = true, interruptingDelay = 1, interruptingKeyCodes = { @@ -17,33 +16,24 @@ local event = { [56] = true }, onError = function(errorMessage) - -- require("GUI").error("CYKA: " .. tostring(errorMessage)) + -- require("GUI").error("Event handler error: \"" .. tostring(errorMessage) .. "\"") end -} - -local lastInterrupt, interruptingKeysDown = 0, {} +}, {}, {}, 0 -------------------------------------------------------------------------------------------------------- -function event.register(callback, signalType, times, interval) +function event.addHandler(callback, signalType, times, interval) checkArg(1, callback, "function") checkArg(2, signalType, "string", "nil") checkArg(3, times, "number", "nil") checkArg(4, nextTriggerTime, "number", "nil") - local newID - while not newID do - newID = math.random(1, 0x7FFFFFFF) - for ID, handler in pairs(event.handlers) do - if ID == newID then - newID = nil - break - end - end + local ID = math.random(1, 0x7FFFFFFF) + while handlers[ID] do + ID = math.random(1, 0x7FFFFFFF) end - event.handlers[newID] = { - alive = true, + handlers[ID] = { signalType = signalType, callback = callback, times = times or math.huge, @@ -51,7 +41,28 @@ function event.register(callback, signalType, times, interval) nextTriggerTime = interval and (computer.uptime() + interval) or nil } - return newID + return ID +end + +function event.removeHandler(ID) + checkArg(1, ID, "number") + + if handlers[ID] then + handlers[ID] = nil + return true + else + return false, "No registered handlers found for ID \"" .. ID .. "\"" + end +end + +function event.getHandler(ID) + checkArg(1, ID, "number") + + if handlers[ID] then + return handlers[ID] + else + return false, "No registered handlers found for ID \"" .. ID .. "\"" + end end -------------------------------------------------------------------------------------------------------- @@ -60,13 +71,13 @@ function event.listen(signalType, callback) checkArg(1, signalType, "string") checkArg(2, callback, "function") - for ID, handler in pairs(event.handlers) do + for ID, handler in pairs(handlers) do if handler.callback == callback then return false, "Callback method " .. tostring(callback) .. " is already registered" end end - event.register(callback, signalType) + event.addHandler(callback, signalType) return true end @@ -74,9 +85,9 @@ function event.ignore(signalType, callback) checkArg(1, signalType, "string") checkArg(2, callback, "function") - for ID, handler in pairs(event.handlers) do + for ID, handler in pairs(handlers) do if handler.signalType == signalType and handler.callback == callback then - handler.alive = false + handlers[ID] = nil return true end end @@ -91,19 +102,10 @@ function event.timer(interval, callback, times) checkArg(2, callback, "function") checkArg(3, times, "number", "nil") - return event.register(callback, nil, times, interval) + return event.addHandler(callback, nil, times, interval) end -function event.cancel(ID) - checkArg(1, ID, "number") - - if event.handlers[ID] then - event.handlers[ID].alive = false - return true - else - return false, "No registered handlers found for ID \"" .. ID .. "\"" - end -end +event.cancel = event.removeHandler -------------------------------------------------------------------------------------------------------- @@ -118,17 +120,6 @@ local function executeHandlerCallback(callback, ...) end end -local function getNearestHandlerTriggerTime() - local nearestTriggerTime - for ID, handler in pairs(event.handlers) do - if handler.nextTriggerTime then - nearestTriggerTime = math.min(nearestTriggerTime or math.huge, handler.nextTriggerTime) - end - end - - return nearestTriggerTime -end - function event.skip(signalType) event.skipSignalType = signalType end @@ -136,37 +127,46 @@ end function event.pull(...) local args = {...} - local args1Type, timeout, signalType = type(args[1]) + local args1Type, preferredTimeout, signalType = type(args[1]) if args1Type == "string" then - timeout, signalType = math.huge, args[1] + preferredTimeout, signalType = math.huge, args[1] elseif args1Type == "number" then - timeout, signalType = args[1], type(args[2]) == "string" and args[2] or nil + preferredTimeout, signalType = args[1], type(args[2]) == "string" and args[2] or nil end - local uptime, eventData = computer.uptime() - local deadline = uptime + (timeout or math.huge) + local uptime, eventData, timeout = computer.uptime() + local deadline = uptime + (preferredTimeout or math.huge) + while uptime <= deadline do - uptime = computer.uptime() - eventData = {computer.pullSignal((getNearestHandlerTriggerTime() or deadline) - computer.uptime())} - + -- Determining pullSignal timeout + timeout = deadline + for ID, handler in pairs(handlers) do + if handler.nextTriggerTime then + timeout = math.min(timeout, handler.nextTriggerTime) + end + end + + -- Pulling signal data + eventData = { computer.pullSignal(timeout - computer.uptime()) } + -- Handlers processing - for ID, handler in pairs(event.handlers) do - if handler.times > 0 and handler.alive then + for ID, handler in pairs(handlers) do + if handler.times > 0 then + uptime = computer.uptime() + if (not handler.signalType or handler.signalType == eventData[1]) and (not handler.nextTriggerTime or handler.nextTriggerTime <= uptime) then - executeHandlerCallback(handler.callback, table.unpack(eventData)) - uptime = computer.uptime() - handler.times = handler.times - 1 - if handler.nextTriggerTime then handler.nextTriggerTime = uptime + handler.interval end + + executeHandlerCallback(handler.callback, table.unpack(eventData)) end else - event.handlers[ID] = nil + handlers[ID] = nil end end