Refactor touch zone handling for improved responsiveness and redraw efficiency
This commit is contained in:
@@ -75,9 +75,11 @@ local statusTimer = 0
|
|||||||
|
|
||||||
-- Touch zones: list of {x1, y1, x2, y2, action, data}
|
-- Touch zones: list of {x1, y1, x2, y2, action, data}
|
||||||
local touchZones = {}
|
local touchZones = {}
|
||||||
|
local pendingZones = {} -- built during draw, swapped in at end
|
||||||
|
local needsRedraw = false
|
||||||
|
|
||||||
local function addZone(x1, y1, x2, y2, action, data)
|
local function addZone(x1, y1, x2, y2, action, data)
|
||||||
table.insert(touchZones, {
|
table.insert(pendingZones, {
|
||||||
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
|
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
|
||||||
action = action, data = data
|
action = action, data = data
|
||||||
})
|
})
|
||||||
@@ -147,7 +149,7 @@ local function drawDashboard()
|
|||||||
if not mon then return end
|
if not mon then return end
|
||||||
|
|
||||||
local w, h = mon.getSize()
|
local w, h = mon.getSize()
|
||||||
touchZones = {}
|
pendingZones = {}
|
||||||
|
|
||||||
-- Create offscreen buffer (invisible)
|
-- Create offscreen buffer (invisible)
|
||||||
draw = window.create(mon, 1, 1, w, h, false)
|
draw = window.create(mon, 1, 1, w, h, false)
|
||||||
@@ -318,6 +320,9 @@ local function drawDashboard()
|
|||||||
|
|
||||||
-- Flush buffer to monitor in one go
|
-- Flush buffer to monitor in one go
|
||||||
draw.setVisible(true)
|
draw.setVisible(true)
|
||||||
|
|
||||||
|
-- Swap touch zones atomically (no yield between these two lines)
|
||||||
|
touchZones = pendingZones
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
@@ -429,7 +434,10 @@ end
|
|||||||
|
|
||||||
local function handleTouch(x, y)
|
local function handleTouch(x, y)
|
||||||
local action, data = hitTest(x, y)
|
local action, data = hitTest(x, y)
|
||||||
if not action then return end
|
if not action then
|
||||||
|
print("[TOUCH] No zone hit")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
if action == "amount" then
|
if action == "amount" then
|
||||||
selectedAmount = data
|
selectedAmount = data
|
||||||
@@ -449,8 +457,8 @@ local function handleTouch(x, y)
|
|||||||
print("[UI] Manual refresh")
|
print("[UI] Manual refresh")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Redraw immediately after touch
|
-- Flag redraw instead of drawing here (avoids yielding in touch handler)
|
||||||
pcall(drawDashboard)
|
needsRedraw = true
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
@@ -498,10 +506,16 @@ local function main()
|
|||||||
function()
|
function()
|
||||||
while true do
|
while true do
|
||||||
pcall(drawDashboard)
|
pcall(drawDashboard)
|
||||||
|
needsRedraw = false
|
||||||
if statusTimer > 0 then
|
if statusTimer > 0 then
|
||||||
statusTimer = statusTimer - DASH_REFRESH
|
statusTimer = statusTimer - 1
|
||||||
|
end
|
||||||
|
-- Short sleep so redraws triggered by touch happen quickly
|
||||||
|
local elapsed = 0
|
||||||
|
while elapsed < DASH_REFRESH and not needsRedraw do
|
||||||
|
sleep(0.5)
|
||||||
|
elapsed = elapsed + 0.5
|
||||||
end
|
end
|
||||||
sleep(DASH_REFRESH)
|
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user