refactor(keybind-cheatsheet): use closure callbacks

This commit is contained in:
Lemmy
2026-07-24 22:38:38 -04:00
parent 2f9943777c
commit 99f7ad3084
2 changed files with 30 additions and 53 deletions
+28 -51
View File
@@ -39,10 +39,6 @@ local refreshing = false
local render
local refresh
local env = getfenv()
local dynamicCallbacks = {}
local callbackIndex = 0
local function tr(key, values)
return noctalia.tr(key, values)
end
@@ -60,22 +56,6 @@ local function startsWith(value, prefix)
return value:sub(1, #prefix) == prefix
end
local function resetCallbacks()
for _, name in ipairs(dynamicCallbacks) do
env[name] = nil
end
dynamicCallbacks = {}
callbackIndex = 0
end
local function callback(prefix, fn)
callbackIndex += 1
local name = "keybindCheatsheet_" .. prefix .. "_" .. callbackIndex
env[name] = fn
table.insert(dynamicCallbacks, name)
return name
end
local KEY_LABELS = {
XF86AudioRaiseVolume = "Vol Up",
XF86AudioLowerVolume = "Vol Down",
@@ -566,7 +546,7 @@ local function bindingRow(binding, occurrence)
local description = effectiveDescription(binding)
local identity = binding.id .. "#" .. occurrence
if view == "edit" and editingId == identity then
local visibilityChanged = callback("visibility", function()
local visibilityChanged = function()
if hidden then
preferences.hidden[binding.id] = nil
else
@@ -574,15 +554,15 @@ local function bindingRow(binding, occurrence)
end
savePreferences()
render()
end)
local inputChanged = callback("editChange", function(value) editDraft = value end)
local submit = callback("editSubmit", function(value) saveCustomDescription(binding, value) end)
local save = callback("editSave", function() saveCustomDescription(binding, editDraft) end)
local cancel = callback("editCancel", function()
end
local inputChanged = function(value) editDraft = value end
local submit = function(value) saveCustomDescription(binding, value) end
local save = function() saveCustomDescription(binding, editDraft) end
local cancel = function()
editingId = nil
editDraft = ""
render()
end)
end
return ui.row({ key = "edit-" .. identity, gap = 6, align = "center", paddingV = 0 }, {
ui.row({ minWidth = 188, gap = 3, align = "center", opacity = contentOpacity }, pills),
ui.input({ key = "description-" .. identity, value = editDraft, placeholder = tr("edit_description"), focus = true, controlSize = "sm", flexGrow = 1, onChange = inputChanged, onSubmit = submit }),
@@ -609,12 +589,12 @@ local function bindingRow(binding, occurrence)
ui.column({ flexGrow = 1, gap = 0, opacity = contentOpacity }, labels),
}
if view == "edit" then
local edit = callback("edit", function()
local edit = function()
editingId = identity
editDraft = description
render()
end)
local visibilityChanged = callback("visibility", function()
end
local visibilityChanged = function()
if hidden then
preferences.hidden[binding.id] = nil
else
@@ -622,7 +602,7 @@ local function bindingRow(binding, occurrence)
end
savePreferences()
render()
end)
end
table.insert(row, ui.button({ glyph = "pencil", width = 22, height = 22, glyphSize = 12, variant = "ghost", controlSize = "sm", tooltip = tr("edit_description"), onClick = edit }))
table.insert(row, ui.button({ glyph = hidden and "eye-off" or "eye", width = 22, height = 22, glyphSize = 13, variant = "ghost", controlSize = "sm", selected = hidden, tooltip = hidden and tr("show_binding") or tr("hide_binding"), onClick = visibilityChanged }))
end
@@ -667,16 +647,16 @@ local function hiddenCount()
end
local function colorControl(bucket, property)
local choose = callback("chooseColor", function() chooseColor(bucket.id, property) end)
local paste = callback("pasteColor", function() pasteColor(bucket.id, property) end)
local reset = callback("resetColor", function()
local choose = function() chooseColor(bucket.id, property) end
local paste = function() pasteColor(bucket.id, property) end
local reset = function()
if preferences.colors[bucket.id] ~= nil then
preferences.colors[bucket.id][property] = nil
if next(preferences.colors[bucket.id]) == nil then preferences.colors[bucket.id] = nil end
savePreferences()
render()
end
end)
end
return ui.row({ gap = 5, align = "center", flexGrow = 1 }, {
ui.box({ width = 24, height = 24, radius = 5, fill = colorValue(bucket.id, property), border = "outline", borderWidth = 1 }),
ui.button({ text = property == "background" and tr("background") or tr("text"), variant = "ghost", controlSize = "sm", flexGrow = 1, onClick = choose }),
@@ -698,11 +678,11 @@ local function appearanceBody()
}),
}))
end
local resetAll = callback("resetAllColors", function()
local resetAll = function()
preferences.colors = {}
savePreferences()
render()
end)
end
table.insert(rows, ui.separator({ spacing = 6 }))
table.insert(rows, ui.row({ gap = 8, align = "center" }, {
ui.button({ glyph = "restore", text = tr("reset_colors"), onClick = resetAll }),
@@ -711,19 +691,19 @@ local function appearanceBody()
end
local function renderHeader()
local refreshClick = callback("refresh", function() refresh() end)
local editMode = callback("editMode", function()
local refreshClick = function() refresh() end
local editMode = function()
view = view == "edit" and "bindings" or "edit"
editingId = nil
editDraft = ""
render()
end)
local appearance = callback("appearance", function()
end
local appearance = function()
view = view == "appearance" and "bindings" or "appearance"
editingId = nil
editDraft = ""
render()
end)
end
local title = tr("title")
if currentCompositor ~= "" then
title = (currentCompositor == "mango" and "Mango" or (currentCompositor == "niri" and "Niri" or "Hyprland")) .. " Keymap"
@@ -735,16 +715,16 @@ local function renderHeader()
}),
}
if (view == "bindings" or view == "edit") and not loading and panelError == nil then
local searchChanged = callback("search", function(value)
local searchChanged = function(value)
query = value
editingId = nil
render()
end)
local clearSearch = callback("clearSearch", function()
end
local clearSearch = function()
query = ""
searchRevision += 1
render()
end)
end
table.insert(children, ui.input({ key = "search-" .. searchRevision, value = query, placeholder = tr("search_placeholder"), controlSize = "sm", width = 300, onChange = searchChanged }))
table.insert(children, ui.button({ glyph = "x", width = 26, variant = "ghost", controlSize = "sm", enabled = query ~= "", tooltip = tr("clear"), onClick = clearSearch }))
end
@@ -753,11 +733,11 @@ local function renderHeader()
end
if view == "edit" then
local count = hiddenCount()
local restoreHidden = callback("restoreHidden", function()
local restoreHidden = function()
restoreHiddenBindings(bindings, preferences.hidden)
savePreferences()
render()
end)
end
table.insert(children, ui.label({ text = tr("hidden_count", { count = count }), color = "on_surface_variant", fontSize = 12 }))
table.insert(children, ui.button({ glyph = "eye", variant = "ghost", controlSize = "sm", enabled = count > 0, tooltip = tr("restore_hidden"), onClick = restoreHidden }))
end
@@ -769,7 +749,6 @@ end
render = function()
if not panelOpen then return end
resetCallbacks()
local contentState = "bindings"
if view == "appearance" then
contentState = "appearance"
@@ -816,7 +795,6 @@ local function releasePanelState(clearModel)
query = ""
editingId = nil
editDraft = ""
resetCallbacks()
if clearModel then
bindings = {}
parseWarnings = {}
@@ -871,7 +849,6 @@ local function lifecycleState()
refreshing = refreshing,
bindingCount = #bindings,
snapshotLoaded = snapshot ~= nil,
callbackCount = #dynamicCallbacks,
editDraft = editDraft,
}
end
+2 -2
View File
@@ -1,7 +1,7 @@
id = "kenn/keybind-cheatsheet"
name = "Keybind Cheatsheet"
version = "0.2.0"
plugin_api = 4
version = "0.2.1"
plugin_api = 9
author = "kenn"
license = "MIT"
dependencies = ["hyprctl"]