refactor(ds4-color): use closure callbacks

This commit is contained in:
Lemmy
2026-07-24 22:38:38 -04:00
parent 8fdca50859
commit 947f9d873d
2 changed files with 7 additions and 17 deletions
+5 -15
View File
@@ -20,12 +20,15 @@ local function currentColor()
return noctalia.state.get("lastColor") or "990000"
end
local applyPreset
local function render()
local color = currentColor()
local presets = noctalia.state.get("presets") or {}
local presetRow = {}
for i, p in ipairs(presets) do
local presetIndex = i
local selected = (p.hex:lower() == color:lower())
table.insert(presetRow, ui.box({
width = 34,
@@ -34,7 +37,7 @@ local function render()
radius = 8,
border = "outline",
borderWidth = selected and 3 or 1,
onClick = "onPreset" .. i,
onClick = function() applyPreset(presetIndex) end,
}))
end
@@ -126,10 +129,7 @@ end
function onClose()
end
-- Presets are a fixed list, so define one global handler per index at module
-- load (Noctalia's _G is read-only at runtime — no dynamic _G["onPreset"..i]
-- assignment, which throws "attempt to modify a readonly table" in onOpen).
local function applyPreset(i)
applyPreset = function(i)
local presets = noctalia.state.get("presets") or {}
local p = presets[i]
if p == nil then return end
@@ -137,16 +137,6 @@ local function applyPreset(i)
render()
end
function onPreset1() applyPreset(1) end
function onPreset2() applyPreset(2) end
function onPreset3() applyPreset(3) end
function onPreset4() applyPreset(4) end
function onPreset5() applyPreset(5) end
function onPreset6() applyPreset(6) end
function onPreset7() applyPreset(7) end
function onPreset8() applyPreset(8) end
function onPreset9() applyPreset(9) end
function onOpen(context)
noctalia.state.watch("lastColor", function() render() end)
render()
+2 -2
View File
@@ -1,7 +1,7 @@
id = "hy4ri/ds4-color"
name = "DS4 Color"
version = "0.1.0"
plugin_api = 3
version = "0.1.1"
plugin_api = 9
author = "hy4ri"
license = "MIT"
icon = "device-gamepad-2"