- Fixed a container bug

- Added onDone to Program
This commit is contained in:
Robert Jelic
2025-06-20 14:19:05 +02:00
parent ea0f31fe37
commit 8067f23d42
16 changed files with 671 additions and 5 deletions

View File

@@ -102,6 +102,14 @@ end
function Container:init(props, basalt)
VisualElement.init(self, props, basalt)
self.set("type", "Container")
self:observe("width", function()
self.set("childrenSorted", false)
self.set("childrenEventsSorted", false)
end)
self:observe("height", function()
self.set("childrenSorted", false)
self.set("childrenEventsSorted", false)
end)
end
--- Returns whether a child is visible
@@ -339,11 +347,13 @@ end
local function convertMousePosition(self, event, ...)
local args = {...}
if event:find("mouse_") then
local button, absX, absY = ...
local xOffset, yOffset = self.get("offsetX"), self.get("offsetY")
local relX, relY = self:getRelativePosition(absX + xOffset, absY + yOffset)
args = {button, relX, relY}
if event then
if event:find("mouse_") then
local button, absX, absY = ...
local xOffset, yOffset = self.get("offsetX"), self.get("offsetY")
local relX, relY = self:getRelativePosition(absX + xOffset, absY + yOffset)
args = {button, relX, relY}
end
end
return args
end
@@ -680,6 +690,9 @@ end
--- @private
function Container:destroy()
if not self:isType("BaseFrame") then
for _, child in ipairs(self.get("children")) do
child:destroy()
end
self.set("childrenSorted", false)
VisualElement.destroy(self)
return self

View File

@@ -787,6 +787,7 @@ function Flexbox:addChild(element)
return self
end
--- Removes a child element from the flexbox
--- @shortDescription Removes a child element from the flexbox
--- @param element Element The child element to remove
--- @return Flexbox self The flexbox instance

View File

@@ -17,6 +17,8 @@ Program.defineProperty(Program, "path", {default = "", type = "string"})
Program.defineProperty(Program, "running", {default = false, type = "boolean"})
--- @property errorCallback function nil The error callback function
Program.defineProperty(Program, "errorCallback", {default = nil, type = "function"})
--- @property doneCallback function nil The done callback function
Program.defineProperty(Program, "doneCallback", {default = nil, type = "function"})
Program.defineEvent(Program, "*")
@@ -82,6 +84,10 @@ function BasaltProgram:run(path, width, height)
local ok, result = coroutine.resume(self.coroutine)
term.redirect(current)
if not ok then
local doneCallback = self.program.get("doneCallback")
if doneCallback then
doneCallback(self.program, ok, result)
end
local errorCallback = self.program.get("errorCallback")
if errorCallback then
local trace = debug.traceback(self.coroutine, result)
@@ -94,6 +100,14 @@ function BasaltProgram:run(path, width, height)
errorManager.header = "Basalt Program Error ".. path
errorManager.error(result)
end
if coroutine.status(self.coroutine)=="dead" then
self.program.set("running", false)
self.program.set("program", nil)
local doneCallback = self.program.get("doneCallback")
if doneCallback then
doneCallback(self.program, ok, result)
end
end
else
errorManager.header = "Basalt Program Error ".. path
errorManager.error("File not found")
@@ -128,10 +142,23 @@ function BasaltProgram:resume(event, ...)
if ok then
self.filter = result
if coroutine.status(self.coroutine)=="dead" then
self.program.set("running", false)
self.program.set("program", nil)
local doneCallback = self.program.get("doneCallback")
if doneCallback then
doneCallback(self.program, ok, result)
end
end
else
local doneCallback = self.program.get("doneCallback")
if doneCallback then
doneCallback(self.program, ok, result)
end
local errorCallback = self.program.get("errorCallback")
if errorCallback then
local trace = debug.traceback(self.coroutine, result)
trace = trace == nil and "" or trace
local _result = errorCallback(self.program, result, trace:gsub(result, ""))
if(_result==false)then
self.filter = nil
@@ -235,6 +262,15 @@ function Program:onError(fn)
return self
end
--- Registers a callback for the program's done event
--- @shortDescription Registers a callback for the program's done event
--- @param fn function The callback function to register
--- @return Program self The Program instance
function Program:onDone(fn)
self.set("doneCallback", fn)
return self
end
--- @shortDescription Handles all incomming events
--- @param event string The event to handle
--- @param ... any The event arguments