Update nightwatch75/todo to 0.0.9 — drag-and-drop reorder (plugin API 5) (#91)

Manual-mode reordering now uses declarative drag-and-drop (ui.dragSource
/ ui.dropZone, plugin_api 5) instead of the two-click grip; also folds in
the 0.0.8 crash-loop fix (strike() gated on utf8.len) and an onExit
autosave flush. Translation keys nested for the validator.

Co-authored-by: nightwatch75 <nightwatch75@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
nightwatch75
2026-07-24 14:10:58 -04:00
committed by GitHub
co-authored by nightwatch75 Claude Opus 4.8
parent 7fa3903113
commit 5e7655dc73
4 changed files with 98 additions and 77 deletions
+5 -8
View File
@@ -28,7 +28,7 @@ noctalia msg panel-toggle nightwatch75/todo:panel
| **+** (panel header) | Add a new task and start typing it | | **+** (panel header) | Add a new task and start typing it |
| Sort toggle (panel header) | Switch ordering between **Priority** and **Manual** | | Sort toggle (panel header) | Switch ordering between **Priority** and **Manual** |
| Colour chip (row) | Cycle the task's priority: important → medium → low | | Colour chip (row) | Cycle the task's priority: important → medium → low |
| ☰ grip (row, manual only) | Pick the row up / drop the held row here (reorder) | | ☰ grip (row, manual only) | Drag the row to a new position (reorder) |
| Click the text, or ✎ (pencil) | Edit the task's text | | Click the text, or ✎ (pencil) | Edit the task's text |
| **Enter**, or ✓ (row) | Commit the edit — the row goes back to a static line | | **Enter**, or ✓ (row) | Commit the edit — the row goes back to a static line |
| ☐ / ☑ button (row) | Toggle done/to-do (done tasks are struck through) | | ☐ / ☑ button (row) | Toggle done/to-do (done tasks are struck through) |
@@ -64,12 +64,9 @@ ordering.
### Reordering in manual mode ### Reordering in manual mode
The noctalia plugin UI exposes no drag callbacks (only clicks), so the ☰ grip Grab a row by its ☰ grip and drag it. Thin insertion zones open up between the
reorders with two clicks instead of a drag: rows as you drag; drop the row on one to move it there. This uses noctalia's
declarative drag-and-drop, which needs plugin API ≥ 5.
1. Click a row's ☰ grip — it lights up; that row is now "held".
2. Click another row's ☰ grip — the held row drops in just above it.
3. Click the held row's own grip again to cancel.
## Editing ## Editing
@@ -114,7 +111,7 @@ noctalia msg plugins enable nightwatch75/todo
## Requirements ## Requirements
- noctalia ≥ 5.0.0 - noctalia with plugin API ≥ 5 (declarative drag-and-drop)
- No external dependencies - No external dependencies
## License ## License
+89 -64
View File
@@ -15,11 +15,9 @@
-- * "priority" — rows display sorted important → medium → low; equal-priority -- * "priority" — rows display sorted important → medium → low; equal-priority
-- rows keep their manual relative order. No drag handles are shown. -- rows keep their manual relative order. No drag handles are shown.
-- * "manual" — the stored order is shown verbatim. A ☰ grip on each row -- * "manual" — the stored order is shown verbatim. A ☰ grip on each row
-- reorders it (see below); changing a priority only recolours the chip and -- drags it to a new position (declarative drag-and-drop, plugin_api >= 5):
-- never moves the row. Because the noctalia plugin UI exposes no pointer -- grab the grip and drop onto one of the thin insertion zones between rows.
-- drag callbacks (only onClick), the grip works as a two-click grab/drop: -- Changing a priority only recolours the chip and never moves the row.
-- click a row's grip to pick it up (it highlights), click another row's
-- grip to drop the held row just above it, or click the held grip to cancel.
-- --
-- Interaction: a row is a static line by default. Click its text or the pencil -- Interaction: a row is a static line by default. Click its text or the pencil
-- button to edit; press Enter or the ✓ button to commit back to the static -- button to edit; press Enter or the ✓ button to commit back to the static
@@ -37,6 +35,7 @@ local RANK = { important = 1, medium = 2, low = 3 }
local COLORS = { important = "#e06c75", medium = "#e5c07b", low = "#98c379" } local COLORS = { important = "#e06c75", medium = "#e5c07b", low = "#98c379" }
local FILE_NAME = "todo.json" local FILE_NAME = "todo.json"
local AUTOSAVE_IDLE_TICKS = 2 -- 1s panel ticks with no edits before a flush local AUTOSAVE_IDLE_TICKS = 2 -- 1s panel ticks with no edits before a flush
local DRAG_TYPE = "todo-row" -- drag identifier matched by the row drop zones
local items = {} -- array, ALWAYS in manual order (priority mode sorts a copy for display) local items = {} -- array, ALWAYS in manual order (priority mode sorts a copy for display)
local nextId = 1 -- monotonic id source (identity only, not a sort key) local nextId = 1 -- monotonic id source (identity only, not a sort key)
@@ -46,8 +45,7 @@ local dirty = false -- unsaved inline text edits
local idleTicks = 0 local idleTicks = 0
local editingId = nil -- id of the row currently in edit mode (at most one) local editingId = nil -- id of the row currently in edit mode (at most one)
local loaded = false -- guard so an early save() can never truncate the file local loaded = false -- guard so an early save() can never truncate the file
local sortMode = "priority" -- "priority" (auto sort) or "manual" (grip reorder) local sortMode = "priority" -- "priority" (auto sort) or "manual" (drag reorder)
local grabbedId = nil -- id of the row "picked up" by its grip, awaiting a drop
local confirmingClear = false -- header trash pressed, awaiting confirm/cancel local confirmingClear = false -- header trash pressed, awaiting confirm/cancel
local render local render
@@ -83,9 +81,12 @@ end
-- Overlay every character with U+0336 (combining long stroke) so a plain label -- Overlay every character with U+0336 (combining long stroke) so a plain label
-- renders struck through — labels/buttons have no line-through prop. -- renders struck through — labels/buttons have no line-through prop.
-- utf8.codes THROWS on invalid UTF-8, and render() would re-raise it on every
-- frame until the host disables the plugin; utf8.len returns nil instead, so
-- gate on it and fall back to the plain text (the ☑ still marks it done).
local function strike(text) local function strike(text)
if text == "" then if text == "" or utf8.len(text) == nil then
return "" return text
end end
local out = {} local out = {}
for _, code in utf8.codes(text) do for _, code in utf8.codes(text) do
@@ -223,9 +224,6 @@ local function deleteItem(id)
if editingId == id then if editingId == id then
editingId = nil editingId = nil
end end
if grabbedId == id then
grabbedId = nil
end
save() save()
render() render()
end end
@@ -279,45 +277,38 @@ end
-- Switch ordering mode. Only the `sort` choice is persisted — the task array -- Switch ordering mode. Only the `sort` choice is persisted — the task array
-- is never rewritten, so the manual order survives any number of round trips -- is never rewritten, so the manual order survives any number of round trips
-- through priority mode. Any half-finished grab is dropped. -- through priority mode.
local function setSortMode(mode) local function setSortMode(mode)
if mode ~= "manual" and mode ~= "priority" then if mode ~= "manual" and mode ~= "priority" then
return return
end end
sortMode = mode sortMode = mode
grabbedId = nil
save() save()
render() render()
end end
-- Move the grabbed row to sit just above `targetId` (manual reorder via grips). -- Move task `id` to sit at manual position `insertAt` (1-based, in `items`).
-- A drop onto the held row itself, or onto a vanished target, simply cancels. -- The insertion zones number the gaps 1..#items+1, so `insertAt` is where the
local function dropGrabbedOn(targetId) -- row lands before removing itself; drop onto its own gap is a no-op. Only
if grabbedId == nil or grabbedId == targetId then -- reachable in manual mode, where the render order IS the `items` order.
grabbedId = nil local function moveItemTo(id, insertAt)
render() local fromIndex = nil
return
end
local grabbed = nil
for i, item in ipairs(items) do for i, item in ipairs(items) do
if item.id == grabbedId then if item.id == id then
grabbed = table.remove(items, i) fromIndex = i
break break
end end
end end
grabbedId = nil if fromIndex == nil or type(insertAt) ~= "number" then
if grabbed == nil then
render()
return return
end end
local targetIndex = #items + 1 local moved = table.remove(items, fromIndex)
for i, item in ipairs(items) do -- Removing the row shifts every later gap down by one.
if item.id == targetId then if fromIndex < insertAt then
targetIndex = i insertAt -= 1
break
end end
end insertAt = math.max(1, math.min(insertAt, #items + 1))
table.insert(items, targetIndex, grabbed) table.insert(items, insertAt, moved)
save() save()
render() render()
end end
@@ -332,8 +323,8 @@ local function doneCount()
return n return n
end end
-- Remove every done row (the header trash, after confirmation). Editing or -- Remove every done row (the header trash, after confirmation). Editing state
-- grab state pointing at a removed row is dropped with it. -- pointing at a removed row is dropped with it.
local function clearDone() local function clearDone()
local kept = {} local kept = {}
for _, item in ipairs(items) do for _, item in ipairs(items) do
@@ -346,9 +337,6 @@ local function clearDone()
if editingId ~= nil and findItem(editingId) == nil then if editingId ~= nil and findItem(editingId) == nil then
editingId = nil editingId = nil
end end
if grabbedId ~= nil and findItem(grabbedId) == nil then
grabbedId = nil
end
save() save()
end end
confirmingClear = false confirmingClear = false
@@ -358,26 +346,27 @@ end
local function taskRow(item) local function taskRow(item)
local id = item.id local id = item.id
local editing = editingId == id local editing = editingId == id
local grabbed = grabbedId == id
-- Manual mode only: a ☰ grip that "picks up" the row, or drops the held one -- Manual mode only: a ☰ grip that drags the whole row to a new position.
-- here. Same glyph noctalia's own bar reorder uses ("menu-2"); it lights up -- The grip is a drag source holding the glyph, but previewAncestor = 1
-- (primary) while this row is the one being held. -- makes the drag ghost the whole row and liftFromLayout pulls the row out
-- of the list while it moves; the thin insertion zones between rows are the
-- drop targets. Same glyph noctalia's own bar reorder uses ("menu-2").
local grip = nil local grip = nil
if sortMode == "manual" then if sortMode == "manual" then
grip = ui.button({ grip = ui.dragSource({
key = "grip-" .. id .. (grabbed and "-on" or ""), key = "grip-" .. id,
glyph = "menu-2", dragType = DRAG_TYPE,
variant = grabbed and "primary" or "ghost", payload = tostring(id),
tooltip = tr(grabbed and "tip_grip_drop" or "tip_grip"), previewAncestor = 1,
onClick = rowCallback("todoGrip", id, function() liftFromLayout = true,
if grabbedId == nil then width = 24,
grabbedId = id height = 24,
render() align = "center",
else justify = "center",
dropGrabbedOn(id) tooltip = tr("tip_grip"),
end }, {
end), ui.glyph({ name = "menu-2", size = 14, color = "on_surface_variant" }),
}) })
end end
@@ -465,13 +454,12 @@ local function taskRow(item)
})) }))
return ui.row({ return ui.row({
-- edit/done/mode/grab state flips the row's controls; key it to recreate cleanly -- edit/done/mode state flips the row's controls; key it to recreate cleanly
key = "row-" key = "row-"
.. id .. id
.. (editing and "-edit" or "-view") .. (editing and "-edit" or "-view")
.. (item.done and "-done" or "") .. (item.done and "-done" or "")
.. (sortMode == "manual" and "-m" or "") .. (sortMode == "manual" and "-m" or ""),
.. (grabbed and "-grab" or ""),
gap = 8, gap = 8,
align = "center", align = "center",
}, children) }, children)
@@ -485,6 +473,22 @@ local function legendEntry(priority)
}) })
end end
-- A thin drop target in the gap before manual-order row `index` (and one past
-- the end, at #items+1). expandOnDrag opens a row-height gap where the dragged
-- row will land; hitSlop makes the 3px line reachable from the rows around it.
local function insertionZone(index)
return ui.dropZone({
key = "gap-" .. index,
accepts = { DRAG_TYPE },
value = tostring(index),
onDrop = "onTodoDrop",
height = 3,
radius = 6,
expandOnDrag = true,
hitSlop = 28,
})
end
render = function() render = function()
-- Ordering toggle: shows the current mode and flips it. "menu-2" (the grip -- Ordering toggle: shows the current mode and flips it. "menu-2" (the grip
-- glyph) for manual, "palette" for the colour/priority sort. -- glyph) for manual, "palette" for the colour/priority sort.
@@ -548,9 +552,19 @@ render = function()
}) })
else else
local rows = {} local rows = {}
for _, item in ipairs(displayItems()) do -- Manual mode interleaves an insertion zone before each row (and one
-- after the last) so a drag can land in any gap. In manual mode the
-- render order IS the items order, so gap N targets items index N.
local manual = sortMode == "manual"
for index, item in ipairs(displayItems()) do
if manual then
table.insert(rows, insertionZone(index))
end
table.insert(rows, taskRow(item)) table.insert(rows, taskRow(item))
end end
if manual then
table.insert(rows, insertionZone(#items + 1))
end
body = ui.scroll({ flexGrow = 1, gap = 6 }, rows) body = ui.scroll({ flexGrow = 1, gap = 6 }, rows)
end end
@@ -572,7 +586,6 @@ function onOpen(_context)
end end
load() load()
editingId = nil editingId = nil
grabbedId = nil
confirmingClear = false confirmingClear = false
dirty = false dirty = false
idleTicks = 0 idleTicks = 0
@@ -583,7 +596,6 @@ end
function onClose() function onClose()
editingId = nil editingId = nil
grabbedId = nil
confirmingClear = false confirmingClear = false
if dirty then if dirty then
save() save()
@@ -591,6 +603,14 @@ function onClose()
noctalia.state.set("todo_open", false) noctalia.state.set("todo_open", false)
end end
-- Fires on every teardown (shutdown and reload), even paths that skip onClose;
-- flush a pending inline edit so it is never lost.
function onExit()
if dirty then
save()
end
end
function onConfigChanged() function onConfigChanged()
if dirty then if dirty then
save() save()
@@ -599,7 +619,6 @@ function onConfigChanged()
filePath = folder .. "/" .. FILE_NAME filePath = folder .. "/" .. FILE_NAME
noctalia.mkdirAll(folder) noctalia.mkdirAll(folder)
editingId = nil editingId = nil
grabbedId = nil
confirmingClear = false confirmingClear = false
load() load()
render() render()
@@ -622,6 +641,12 @@ function onSortToggle()
setSortMode(sortMode == "manual" and "priority" or "manual") setSortMode(sortMode == "manual" and "priority" or "manual")
end end
-- Drop callback for the insertion zones (declarative drag-and-drop). payload is
-- the dragged row's id as text, value the target gap index as text.
function onTodoDrop(payload, value)
moveItemTo(tonumber(payload), tonumber(value))
end
function onClearDone() function onClearDone()
confirmingClear = true confirmingClear = true
render() render()
+3 -3
View File
@@ -2,14 +2,14 @@
# of editable task rows: add with +, tick to complete (the text is struck # of editable task rows: add with +, tick to complete (the text is struck
# through), delete, and click each row's colour chip to cycle its priority # through), delete, and click each row's colour chip to cycle its priority
# (important → medium → low). A header toggle switches ordering between priority # (important → medium → low). A header toggle switches ordering between priority
# (auto-sorted) and manual, where a ☰ grip on each row reorders it by clicking. # (auto-sorted) and manual, where a ☰ grip on each row drags it to reorder.
# The whole list is a single JSON file in the configured folder; no external # The whole list is a single JSON file in the configured folder; no external
# commands are run. # commands are run.
id = "nightwatch75/todo" id = "nightwatch75/todo"
name = "To Do" name = "To Do"
version = "0.0.7" version = "0.0.9"
plugin_api = 3 plugin_api = 5
author = "nightwatch75" author = "nightwatch75"
license = "MIT" license = "MIT"
dependencies = [] dependencies = []
+1 -2
View File
@@ -27,8 +27,7 @@
"tip_delete": "Delete task", "tip_delete": "Delete task",
"tip_done": "Mark as done", "tip_done": "Mark as done",
"tip_edit": "Edit", "tip_edit": "Edit",
"tip_grip": "Reorder — click to pick this row up", "tip_grip": "Drag to reorder",
"tip_grip_drop": "Click another grip to drop here, or this one to cancel",
"tip_sort": "Switch ordering mode", "tip_sort": "Switch ordering mode",
"tip_undone": "Mark as to do", "tip_undone": "Mark as to do",
"title": "To Do", "title": "To Do",