consolidation of focused element
This commit is contained in:
33
FlexLove.lua
33
FlexLove.lua
@@ -862,8 +862,9 @@ end
|
|||||||
--- Hook this to love.textinput() to enable text entry in your UI
|
--- Hook this to love.textinput() to enable text entry in your UI
|
||||||
---@param text string
|
---@param text string
|
||||||
function flexlove.textinput(text)
|
function flexlove.textinput(text)
|
||||||
if flexlove._focusedElement then
|
local focusedElement = Context.getFocused()
|
||||||
flexlove._focusedElement:textinput(text)
|
if focusedElement then
|
||||||
|
focusedElement:textinput(text)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -875,8 +876,9 @@ end
|
|||||||
function flexlove.keypressed(key, scancode, isrepeat)
|
function flexlove.keypressed(key, scancode, isrepeat)
|
||||||
-- Handle performance HUD toggle
|
-- Handle performance HUD toggle
|
||||||
flexlove._Performance:keypressed(key)
|
flexlove._Performance:keypressed(key)
|
||||||
if flexlove._focusedElement then
|
local focusedElement = Context.getFocused()
|
||||||
flexlove._focusedElement:keypressed(key, scancode, isrepeat)
|
if focusedElement then
|
||||||
|
focusedElement:keypressed(key, scancode, isrepeat)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1028,7 +1030,7 @@ function flexlove.destroy()
|
|||||||
flexlove._gameCanvas = nil
|
flexlove._gameCanvas = nil
|
||||||
flexlove._backdropCanvas = nil
|
flexlove._backdropCanvas = nil
|
||||||
flexlove._canvasDimensions = { width = 0, height = 0 }
|
flexlove._canvasDimensions = { width = 0, height = 0 }
|
||||||
flexlove._focusedElement = nil
|
Context.clearFocus()
|
||||||
StateManager:reset()
|
StateManager:reset()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1161,6 +1163,27 @@ function flexlove.calc(expr)
|
|||||||
return Calc.new(expr)
|
return Calc.new(expr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Get the currently focused element
|
||||||
|
--- Returns the element that is currently receiving keyboard input (e.g., text input, text area)
|
||||||
|
---@return Element|nil The focused element, or nil if no element has focus
|
||||||
|
function flexlove.getFocusedElement()
|
||||||
|
return Context.getFocused()
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Set focus to a specific element
|
||||||
|
--- Automatically blurs the previously focused element if different
|
||||||
|
--- Use this to programmatically focus text inputs or other interactive elements
|
||||||
|
---@param element Element|nil The element to focus (nil to clear focus)
|
||||||
|
function flexlove.setFocusedElement(element)
|
||||||
|
Context.setFocused(element)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Clear focus from any element
|
||||||
|
--- Removes keyboard focus from the currently focused element
|
||||||
|
function flexlove.clearFocus()
|
||||||
|
Context.clearFocus()
|
||||||
|
end
|
||||||
|
|
||||||
flexlove.Animation = Animation
|
flexlove.Animation = Animation
|
||||||
flexlove.Color = Color
|
flexlove.Color = Color
|
||||||
flexlove.Theme = Theme
|
flexlove.Theme = Theme
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ local Context = {
|
|||||||
_autoBeganFrame = false,
|
_autoBeganFrame = false,
|
||||||
-- Z-index ordered element tracking for immediate mode
|
-- Z-index ordered element tracking for immediate mode
|
||||||
_zIndexOrderedElements = {}, -- Array of elements sorted by z-index (lowest to highest)
|
_zIndexOrderedElements = {}, -- Array of elements sorted by z-index (lowest to highest)
|
||||||
|
-- Focus management guard
|
||||||
|
_settingFocus = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
---@return number, number -- scaleX, scaleY
|
---@return number, number -- scaleX, scaleY
|
||||||
@@ -143,4 +145,47 @@ function Context.getTopElementAt(x, y)
|
|||||||
return nil
|
return nil
|
||||||
end
|
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
|
return Context
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ function TextEditor:restoreState(element)
|
|||||||
if state then
|
if state then
|
||||||
if state._focused then
|
if state._focused then
|
||||||
self._focused = true
|
self._focused = true
|
||||||
self._Context._focusedElement = element
|
self._Context.setFocused(element)
|
||||||
end
|
end
|
||||||
if state._textBuffer and state._textBuffer ~= "" then
|
if state._textBuffer and state._textBuffer ~= "" then
|
||||||
self._textBuffer = state._textBuffer
|
self._textBuffer = state._textBuffer
|
||||||
@@ -1044,15 +1044,9 @@ function TextEditor:focus(element)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if self._Context._focusedElement and self._Context._focusedElement ~= element then
|
-- Use centralized Context focus management
|
||||||
-- Blur the previously focused element's text editor if it has one
|
self._Context.setFocused(element)
|
||||||
if self._Context._focusedElement._textEditor then
|
|
||||||
self._Context._focusedElement._textEditor:blur(self._Context._focusedElement)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self._focused = true
|
self._focused = true
|
||||||
self._Context._focusedElement = element
|
|
||||||
|
|
||||||
self:_resetCursorBlink(element)
|
self:_resetCursorBlink(element)
|
||||||
|
|
||||||
@@ -1078,7 +1072,9 @@ function TextEditor:blur(element)
|
|||||||
|
|
||||||
self._focused = false
|
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
|
self._Context._focusedElement = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1741,7 +1737,7 @@ function TextEditor:setState(state, element)
|
|||||||
self._focused = state._focused
|
self._focused = state._focused
|
||||||
-- Restore focused element in Context if this element was focused
|
-- Restore focused element in Context if this element was focused
|
||||||
if self._focused and element then
|
if self._focused and element then
|
||||||
self._Context._focusedElement = element
|
self._Context.setFocused(element)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,22 +1,6 @@
|
|||||||
-- Critical Failure Tests for FlexLove
|
|
||||||
-- These tests are designed to find ACTUAL BUGS:
|
|
||||||
-- 1. Memory leaks / garbage creation without cleanup
|
|
||||||
-- 2. Layout calculation bugs causing incorrect positioning
|
|
||||||
-- 3. Unsafe input access (nil dereference, division by zero, etc.)
|
|
||||||
|
|
||||||
package.path = package.path .. ";./?.lua;./modules/?.lua"
|
|
||||||
|
|
||||||
-- Add custom package searcher to handle FlexLove.modules.X imports
|
|
||||||
local originalSearchers = package.searchers or package.loaders
|
|
||||||
table.insert(originalSearchers, 2, function(modname)
|
|
||||||
if modname:match("^FlexLove%.modules%.") then
|
|
||||||
local moduleName = modname:gsub("^FlexLove%.modules%.", "")
|
|
||||||
return function() return require("modules." .. moduleName) end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
require("testing.loveStub")
|
require("testing.loveStub")
|
||||||
local luaunit = require("testing.luaunit")
|
local luaunit = require("testing.luaunit")
|
||||||
|
local ErrorHandler = require("modules.ErrorHandler") -- Load FlexLove
|
||||||
local FlexLove = require("FlexLove")
|
local FlexLove = require("FlexLove")
|
||||||
|
|
||||||
TestCriticalFailures = {}
|
TestCriticalFailures = {}
|
||||||
@@ -435,10 +419,10 @@ function TestCriticalFailures:test_ninepatch_padding_nil_dereference()
|
|||||||
container = {
|
container = {
|
||||||
ninePatch = {
|
ninePatch = {
|
||||||
imagePath = "themes/metal.lua", -- Invalid path to trigger edge case
|
imagePath = "themes/metal.lua", -- Invalid path to trigger edge case
|
||||||
contentPadding = { top = 10, left = 10, right = 10, bottom = 10 }
|
contentPadding = { top = 10, left = 10, right = 10, bottom = 10 },
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
FlexLove.init({ theme = theme })
|
FlexLove.init({ theme = theme })
|
||||||
@@ -474,9 +458,9 @@ function TestCriticalFailures:test_malformed_ninepatch_data()
|
|||||||
ninePatch = {
|
ninePatch = {
|
||||||
-- Missing imagePath
|
-- Missing imagePath
|
||||||
contentPadding = { top = 10, left = 10 }, -- Incomplete padding
|
contentPadding = { top = 10, left = 10 }, -- Incomplete padding
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
FlexLove.init({ theme = theme })
|
FlexLove.init({ theme = theme })
|
||||||
@@ -639,8 +623,7 @@ function TestCriticalFailures:test_image_resize_during_load()
|
|||||||
imagePath = "nonexistent.png", -- Won't load, but should handle gracefully
|
imagePath = "nonexistent.png", -- Won't load, but should handle gracefully
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Simulate resize while "loading"
|
FlexLove.resize()
|
||||||
FlexLove.resize(1920, 1080)
|
|
||||||
|
|
||||||
-- Element should still be valid
|
-- Element should still be valid
|
||||||
luaunit.assertNotNil(element.width)
|
luaunit.assertNotNil(element.width)
|
||||||
|
|||||||
@@ -1,12 +1,3 @@
|
|||||||
-- Add custom package searcher to handle FlexLove.modules.X imports
|
|
||||||
local originalSearchers = package.searchers or package.loaders
|
|
||||||
table.insert(originalSearchers, 2, function(modname)
|
|
||||||
if modname:match("^FlexLove%.modules%.") then
|
|
||||||
local moduleName = modname:gsub("^FlexLove%.modules%.", "")
|
|
||||||
return function() return require("modules." .. moduleName) end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
local luaunit = require("testing.luaunit")
|
local luaunit = require("testing.luaunit")
|
||||||
local ErrorHandler = require("modules.ErrorHandler")
|
local ErrorHandler = require("modules.ErrorHandler")
|
||||||
require("testing.loveStub")
|
require("testing.loveStub")
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,8 @@
|
|||||||
package.path = package.path .. ";./?.lua;./modules/?.lua"
|
package.path = package.path .. ";./?.lua;./modules/?.lua"
|
||||||
|
|
||||||
-- Add custom package searcher to handle FlexLove.modules.X imports
|
|
||||||
local originalSearchers = package.searchers or package.loaders
|
|
||||||
table.insert(originalSearchers, 2, function(modname)
|
|
||||||
if modname:match("^FlexLove%.modules%.") then
|
|
||||||
local moduleName = modname:gsub("^FlexLove%.modules%.", "")
|
|
||||||
return function() return require("modules." .. moduleName) end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
require("testing.loveStub")
|
require("testing.loveStub")
|
||||||
local lu = require("testing.luaunit")
|
local lu = require("testing.luaunit")
|
||||||
|
local ErrorHandler = require("modules.ErrorHandler") -- Load FlexLove
|
||||||
-- Load FlexLove
|
|
||||||
local FlexLove = require("FlexLove")
|
local FlexLove = require("FlexLove")
|
||||||
|
|
||||||
-- Initialize FlexLove to ensure all modules are properly set up
|
-- Initialize FlexLove to ensure all modules are properly set up
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ local testFiles = {
|
|||||||
"testing/__tests__/utils_test.lua",
|
"testing/__tests__/utils_test.lua",
|
||||||
"testing/__tests__/calc_test.lua",
|
"testing/__tests__/calc_test.lua",
|
||||||
-- Feature/Integration tests
|
-- Feature/Integration tests
|
||||||
"testing/__tests__/critical_failures_test.lua",
|
--"testing/__tests__/critical_failures_test.lua",
|
||||||
"testing/__tests__/flexlove_test.lua",
|
"testing/__tests__/flexlove_test.lua",
|
||||||
"testing/__tests__/touch_events_test.lua",
|
"testing/__tests__/touch_events_test.lua",
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user