refactor(todo): use closure callbacks

This commit is contained in:
Lemmy
2026-07-24 22:38:38 -04:00
parent a98cb6d5ee
commit 8fefd6c97e
2 changed files with 18 additions and 29 deletions
+16 -27
View File
@@ -50,17 +50,6 @@ local confirmingClear = false -- header trash pressed, awaiting confirm/cancel
local render local render
-- Per-row callbacks need distinct global names; the reconciler dispatches
-- callbacks by name only. getfenv() is the script environment lua_getglobal
-- reads from, so assigning into it defines the callback the host will find.
-- Keyed by the stable id, so a callback stays valid as rows reorder.
local env = getfenv()
local function rowCallback(prefix, id, fn)
local name = prefix .. "_" .. id
env[name] = fn
return name
end
local function tr(key, args) local function tr(key, args)
return noctalia.tr(key, args) return noctalia.tr(key, args)
end end
@@ -377,9 +366,9 @@ local function taskRow(item)
height = 16, height = 16,
radius = 4, radius = 4,
fill = COLORS[item.priority], fill = COLORS[item.priority],
onClick = rowCallback("todoPrio", id, function() onClick = function()
cyclePriority(id) cyclePriority(id)
end), end,
}) })
local children = {} local children = {}
if grip ~= nil then if grip ~= nil then
@@ -394,23 +383,23 @@ local function taskRow(item)
placeholder = tr("placeholder"), placeholder = tr("placeholder"),
focus = true, focus = true,
flexGrow = 1, flexGrow = 1,
onChange = rowCallback("todoText", id, function(value) onChange = function(value)
item.text = value item.text = value
dirty = true dirty = true
idleTicks = 0 idleTicks = 0
end), end,
onSubmit = rowCallback("todoSubmit", id, function(value) onSubmit = function(value)
commitEdit(id, value) commitEdit(id, value)
end), end,
})) }))
-- ✓ commits the edit, mirroring Enter. -- ✓ commits the edit, mirroring Enter.
table.insert(children, ui.button({ table.insert(children, ui.button({
glyph = "check", glyph = "check",
variant = "primary", variant = "primary",
tooltip = tr("tip_commit"), tooltip = tr("tip_commit"),
onClick = rowCallback("todoOk", id, function() onClick = function()
commitEdit(id, item.text) commitEdit(id, item.text)
end), end,
})) }))
else else
-- Static text; clicking it re-opens the editor ("press on the text"). -- Static text; clicking it re-opens the editor ("press on the text").
@@ -420,27 +409,27 @@ local function taskRow(item)
variant = "ghost", variant = "ghost",
contentAlign = "start", contentAlign = "start",
flexGrow = 1, flexGrow = 1,
onClick = rowCallback("todoEdit", id, function() onClick = function()
enterEdit(id) enterEdit(id)
end), end,
})) }))
-- Done toggle: strikes the text through. -- Done toggle: strikes the text through.
table.insert(children, ui.button({ table.insert(children, ui.button({
glyph = item.done and "square-check" or "square", glyph = item.done and "square-check" or "square",
variant = "ghost", variant = "ghost",
tooltip = tr(item.done and "tip_undone" or "tip_done"), tooltip = tr(item.done and "tip_undone" or "tip_done"),
onClick = rowCallback("todoDone", id, function() onClick = function()
toggleDone(id) toggleDone(id)
end), end,
})) }))
-- Explicit edit affordance next to the row. -- Explicit edit affordance next to the row.
table.insert(children, ui.button({ table.insert(children, ui.button({
glyph = "pencil", glyph = "pencil",
variant = "ghost", variant = "ghost",
tooltip = tr("tip_edit"), tooltip = tr("tip_edit"),
onClick = rowCallback("todoPencil", id, function() onClick = function()
enterEdit(id) enterEdit(id)
end), end,
})) }))
end end
@@ -448,9 +437,9 @@ local function taskRow(item)
glyph = "trash", glyph = "trash",
variant = "ghost", variant = "ghost",
tooltip = tr("tip_delete"), tooltip = tr("tip_delete"),
onClick = rowCallback("todoDel", id, function() onClick = function()
deleteItem(id) deleteItem(id)
end), end,
})) }))
return ui.row({ return ui.row({
+2 -2
View File
@@ -8,8 +8,8 @@
id = "nightwatch75/todo" id = "nightwatch75/todo"
name = "To Do" name = "To Do"
version = "0.0.9" version = "0.0.10"
plugin_api = 5 plugin_api = 9
author = "nightwatch75" author = "nightwatch75"
license = "MIT" license = "MIT"
dependencies = [] dependencies = []