consolidation of focused element

This commit is contained in:
Michael Freno
2025-12-11 16:50:35 -05:00
parent 56c8e744d5
commit 3498ed7f24
8 changed files with 396 additions and 321 deletions

View File

@@ -18,6 +18,8 @@ local Context = {
_autoBeganFrame = false,
-- Z-index ordered element tracking for immediate mode
_zIndexOrderedElements = {}, -- Array of elements sorted by z-index (lowest to highest)
-- Focus management guard
_settingFocus = false,
}
---@return number, number -- scaleX, scaleY
@@ -143,4 +145,47 @@ function Context.getTopElementAt(x, y)
return nil
end
--- Set the focused element (centralizes focus management)
--- Automatically blurs the previously focused element if different
---@param element Element|nil The element to focus (nil to clear focus)
function Context.setFocused(element)
if Context._focusedElement == element then
return -- Already focused
end
-- Prevent re-entry during focus change
if Context._settingFocus then
return
end
Context._settingFocus = true
-- Blur previously focused element
if Context._focusedElement and Context._focusedElement ~= element then
if Context._focusedElement._textEditor then
Context._focusedElement._textEditor:blur(Context._focusedElement)
end
end
-- Set new focused element
Context._focusedElement = element
-- Focus the new element's text editor if it has one
if element and element._textEditor then
element._textEditor._focused = true
end
Context._settingFocus = false
end
--- Get the currently focused element
---@return Element|nil The focused element, or nil if none
function Context.getFocused()
return Context._focusedElement
end
--- Clear focus from any element
function Context.clearFocus()
Context.setFocused(nil)
end
return Context

View File

@@ -189,7 +189,7 @@ function TextEditor:restoreState(element)
if state then
if state._focused then
self._focused = true
self._Context._focusedElement = element
self._Context.setFocused(element)
end
if state._textBuffer and state._textBuffer ~= "" then
self._textBuffer = state._textBuffer
@@ -1044,15 +1044,9 @@ function TextEditor:focus(element)
return
end
if self._Context._focusedElement and self._Context._focusedElement ~= element then
-- Blur the previously focused element's text editor if it has one
if self._Context._focusedElement._textEditor then
self._Context._focusedElement._textEditor:blur(self._Context._focusedElement)
end
end
-- Use centralized Context focus management
self._Context.setFocused(element)
self._focused = true
self._Context._focusedElement = element
self:_resetCursorBlink(element)
@@ -1078,7 +1072,9 @@ function TextEditor:blur(element)
self._focused = false
if self._Context._focusedElement == element then
-- Clear focused element in Context if this element is currently focused
-- Use direct assignment to avoid circular call back to blur()
if self._Context.getFocused() == element then
self._Context._focusedElement = nil
end
@@ -1741,7 +1737,7 @@ function TextEditor:setState(state, element)
self._focused = state._focused
-- Restore focused element in Context if this element was focused
if self._focused and element then
self._Context._focusedElement = element
self._Context.setFocused(element)
end
end
end