fix: onEvent not correctly triggering in immediate mode (#2)

This commit is contained in:
2026-02-26 01:47:49 -05:00
parent 71f7776f78
commit c4fc62af20

View File

@@ -64,6 +64,19 @@ function Context.clearFrameElements()
end end
end end
--- Calculate the depth (nesting level) of an element
---@param elem Element
---@return number
local function getElementDepth(elem)
local depth = 0
local current = elem.parent
while current do
depth = depth + 1
current = current.parent
end
return depth
end
--- Sort elements by z-index (called after all elements are registered) --- Sort elements by z-index (called after all elements are registered)
function Context.sortElementsByZIndex() function Context.sortElementsByZIndex()
-- Sort elements by z-index (lowest to highest) -- Sort elements by z-index (lowest to highest)
@@ -80,7 +93,13 @@ function Context.sortElementsByZIndex()
return z return z
end end
return getEffectiveZIndex(a) < getEffectiveZIndex(b) local za = getEffectiveZIndex(a)
local zb = getEffectiveZIndex(b)
if za ~= zb then
return za < zb
end
-- Tiebreaker: deeper elements (children) sort higher
return getElementDepth(a) < getElementDepth(b)
end) end)
end end
@@ -153,6 +172,7 @@ function Context.getTopElementAt(x, y)
return nil return nil
end end
local fallback = nil
for i = #Context._zIndexOrderedElements, 1, -1 do for i = #Context._zIndexOrderedElements, 1, -1 do
local element = Context._zIndexOrderedElements[i] local element = Context._zIndexOrderedElements[i]
@@ -161,11 +181,15 @@ function Context.getTopElementAt(x, y)
if interactive then if interactive then
return interactive return interactive
end end
return element -- Non-interactive element hit: remember as fallback but keep looking
-- for interactive children/siblings at same or lower z-index
if not fallback then
fallback = element
end
end end
end end
return nil return fallback
end end
--- Set the focused element (centralizes focus management) --- Set the focused element (centralizes focus management)