state persistance consolidation

This commit is contained in:
Michael Freno
2025-12-03 14:34:47 -05:00
parent 7bdb235504
commit 940353c1ad
6 changed files with 247 additions and 126 deletions

View File

@@ -3160,6 +3160,110 @@ function Element:setProperty(property, value)
end
end
-- ====================
-- State Persistence
-- ====================
--- Save all element state for immediate mode persistence
--- Collects state from all sub-modules and returns consolidated state
---@return ElementStateData state Complete state snapshot
function Element:saveState()
local state = {}
-- Element-owned state
state._focused = self._focused
-- EventHandler state (if exists)
if self._eventHandler then
state.eventHandler = self._eventHandler:getState()
end
-- TextEditor state (if exists)
if self._textEditor then
state.textEditor = self._textEditor:getState()
end
-- ScrollManager state (if exists)
if self._scrollManager then
state.scrollManager = self._scrollManager:getState()
end
-- Blur cache data (for cache invalidation)
if self.backdropBlur or self.contentBlur then
state.blur = {
_blurX = self.x,
_blurY = self.y,
_blurWidth = self._borderBoxWidth or (self.width + self.padding.left + self.padding.right),
_blurHeight = self._borderBoxHeight or (self.height + self.padding.top + self.padding.bottom),
}
if self.backdropBlur then
state.blur._backdropBlurIntensity = self.backdropBlur.intensity
state.blur._backdropBlurQuality = self.backdropBlur.quality
end
if self.contentBlur then
state.blur._contentBlurIntensity = self.contentBlur.intensity
state.blur._contentBlurQuality = self.contentBlur.quality
end
end
return state
end
--- Restore all element state from StateManager
--- Distributes state to all sub-modules
---@param state ElementStateData State to restore
function Element:restoreState(state)
if not state then
return
end
-- Restore element-owned state
if state._focused ~= nil then
self._focused = state._focused
end
-- Restore EventHandler state (if exists)
if self._eventHandler and state.eventHandler then
self._eventHandler:setState(state.eventHandler)
end
-- Restore TextEditor state (if exists)
if self._textEditor and state.textEditor then
self._textEditor:setState(state.textEditor)
end
-- Restore ScrollManager state (if exists)
if self._scrollManager and state.scrollManager then
self._scrollManager:setState(state.scrollManager)
end
-- Note: Blur cache data is used for invalidation, not restoration
end
--- Check if blur cache should be invalidated based on state changes
---@param oldState ElementStateData? Previous state
---@param newState ElementStateData Current state
---@return boolean shouldInvalidate True if blur cache should be cleared
function Element:shouldInvalidateBlurCache(oldState, newState)
if not oldState or not oldState.blur or not newState.blur then
return false
end
local old = oldState.blur
local new = newState.blur
-- Check if any blur-related property changed
return old._blurX ~= new._blurX
or old._blurY ~= new._blurY
or old._blurWidth ~= new._blurWidth
or old._blurHeight ~= new._blurHeight
or old._backdropBlurIntensity ~= new._backdropBlurIntensity
or old._backdropBlurQuality ~= new._backdropBlurQuality
or old._contentBlurIntensity ~= new._contentBlurIntensity
or old._contentBlurQuality ~= new._contentBlurQuality
end
--- Cleanup method to break circular references (for immediate mode)
--- Note: Cleans internal module state but keeps structure for inspection

View File

@@ -617,11 +617,13 @@ end
---@return table State data
function ScrollManager:getState()
return {
scrollX = self._scrollX,
scrollY = self._scrollY,
scrollbarDragging = self._scrollbarDragging,
hoveredScrollbar = self._hoveredScrollbar,
scrollbarDragOffset = self._scrollbarDragOffset,
_scrollX = self._scrollX or 0,
_scrollY = self._scrollY or 0,
_scrollbarDragging = self._scrollbarDragging or false,
_hoveredScrollbar = self._hoveredScrollbar,
_scrollbarDragOffset = self._scrollbarDragOffset or 0,
_scrollbarHoveredVertical = self._scrollbarHoveredVertical or false,
_scrollbarHoveredHorizontal = self._scrollbarHoveredHorizontal or false,
}
end
@@ -632,21 +634,44 @@ function ScrollManager:setState(state)
return
end
if state.scrollX then
-- Support both old (scrollX) and new (_scrollX) field names for backward compatibility
if state._scrollX ~= nil then
self._scrollX = state._scrollX
elseif state.scrollX ~= nil then
self._scrollX = state.scrollX
end
if state.scrollY then
if state._scrollY ~= nil then
self._scrollY = state._scrollY
elseif state.scrollY ~= nil then
self._scrollY = state.scrollY
end
if state.scrollbarDragging ~= nil then
if state._scrollbarDragging ~= nil then
self._scrollbarDragging = state._scrollbarDragging
elseif state.scrollbarDragging ~= nil then
self._scrollbarDragging = state.scrollbarDragging
end
if state.hoveredScrollbar then
if state._hoveredScrollbar ~= nil then
self._hoveredScrollbar = state._hoveredScrollbar
elseif state.hoveredScrollbar ~= nil then
self._hoveredScrollbar = state.hoveredScrollbar
end
if state.scrollbarDragOffset then
if state._scrollbarDragOffset ~= nil then
self._scrollbarDragOffset = state._scrollbarDragOffset
elseif state.scrollbarDragOffset ~= nil then
self._scrollbarDragOffset = state.scrollbarDragOffset
end
if state._scrollbarHoveredVertical ~= nil then
self._scrollbarHoveredVertical = state._scrollbarHoveredVertical
end
if state._scrollbarHoveredHorizontal ~= nil then
self._scrollbarHoveredHorizontal = state._scrollbarHoveredHorizontal
end
end
--- Handle touch press for scrolling

View File

@@ -1681,6 +1681,66 @@ function TextEditor:_getFont(element)
return element._renderer:getFont(element)
end
--- Get current state for persistence
---@return table state TextEditor state snapshot
function TextEditor:getState()
return {
_cursorPosition = self._cursorPosition,
_selectionStart = self._selectionStart,
_selectionEnd = self._selectionEnd,
_textBuffer = self._textBuffer,
_cursorBlinkTimer = self._cursorBlinkTimer,
_cursorVisible = self._cursorVisible,
_cursorBlinkPaused = self._cursorBlinkPaused,
_cursorBlinkPauseTimer = self._cursorBlinkPauseTimer,
_focused = self._focused,
}
end
--- Restore state from persistence
---@param state table State to restore
function TextEditor:setState(state)
if not state then
return
end
if state._cursorPosition ~= nil then
self._cursorPosition = state._cursorPosition
end
if state._selectionStart ~= nil then
self._selectionStart = state._selectionStart
end
if state._selectionEnd ~= nil then
self._selectionEnd = state._selectionEnd
end
if state._textBuffer ~= nil then
self._textBuffer = state._textBuffer
end
if state._cursorBlinkTimer ~= nil then
self._cursorBlinkTimer = state._cursorBlinkTimer
end
if state._cursorVisible ~= nil then
self._cursorVisible = state._cursorVisible
end
if state._cursorBlinkPaused ~= nil then
self._cursorBlinkPaused = state._cursorBlinkPaused
end
if state._cursorBlinkPauseTimer ~= nil then
self._cursorBlinkPauseTimer = state._cursorBlinkPauseTimer
end
if state._focused ~= nil then
self._focused = state._focused
end
end
---Save state to StateManager (for immediate mode)
---@param element Element? The parent element
function TextEditor:_saveState(element)

View File

@@ -187,3 +187,23 @@ local TransformProps
---@field gcStepSize number? -- Work units per GC step, higher = more aggressive (default: 200)
---@field immediateModeBlurOptimizations boolean? -- Cache blur canvases in immediate mode to avoid re-rendering each frame (default: true)
local FlexLoveConfig = {}
--=====================================--
-- For State Persistence
--=====================================--
---@class ElementStateData
---@field _focused boolean?
---@field eventHandler table? -- EventHandler state
---@field textEditor table? -- TextEditor state
---@field scrollManager table? -- ScrollManager state
---@field blur BlurCacheData? -- Blur cache invalidation data
---@class BlurCacheData
---@field _blurX number
---@field _blurY number
---@field _blurWidth number
---@field _blurHeight number
---@field _backdropBlurIntensity number?
---@field _backdropBlurQuality string?
---@field _contentBlurIntensity number?
---@field _contentBlurQuality string?