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" return noctalia.state.get("lastColor") or "990000"
end end
local applyPreset
local function render() local function render()
local color = currentColor() local color = currentColor()
local presets = noctalia.state.get("presets") or {} local presets = noctalia.state.get("presets") or {}
local presetRow = {} local presetRow = {}
for i, p in ipairs(presets) do for i, p in ipairs(presets) do
local presetIndex = i
local selected = (p.hex:lower() == color:lower()) local selected = (p.hex:lower() == color:lower())
table.insert(presetRow, ui.box({ table.insert(presetRow, ui.box({
width = 34, width = 34,
@@ -34,7 +37,7 @@ local function render()
radius = 8, radius = 8,
border = "outline", border = "outline",
borderWidth = selected and 3 or 1, borderWidth = selected and 3 or 1,
onClick = "onPreset" .. i, onClick = function() applyPreset(presetIndex) end,
})) }))
end end
@@ -126,10 +129,7 @@ end
function onClose() function onClose()
end end
-- Presets are a fixed list, so define one global handler per index at module applyPreset = function(i)
-- 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)
local presets = noctalia.state.get("presets") or {} local presets = noctalia.state.get("presets") or {}
local p = presets[i] local p = presets[i]
if p == nil then return end if p == nil then return end
@@ -137,16 +137,6 @@ local function applyPreset(i)
render() render()
end 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) function onOpen(context)
noctalia.state.watch("lastColor", function() render() end) noctalia.state.watch("lastColor", function() render() end)
render() render()
+2 -2
View File
@@ -1,7 +1,7 @@
id = "hy4ri/ds4-color" id = "hy4ri/ds4-color"
name = "DS4 Color" name = "DS4 Color"
version = "0.1.0" version = "0.1.1"
plugin_api = 3 plugin_api = 9
author = "hy4ri" author = "hy4ri"
license = "MIT" license = "MIT"
icon = "device-gamepad-2" icon = "device-gamepad-2"