refactor(keymap): use closure callbacks
This commit is contained in:
+25
-53
@@ -95,9 +95,6 @@ local renamingCategoryValue = ""
|
|||||||
local renamingCategoryRevision = 0
|
local renamingCategoryRevision = 0
|
||||||
local render
|
local render
|
||||||
local selectKeyboardKey
|
local selectKeyboardKey
|
||||||
local env = getfenv()
|
|
||||||
local dynamicCallbackNames = {}
|
|
||||||
local renderCallbackNames = nil
|
|
||||||
|
|
||||||
local COMMAND_LIBRARY = { entries = {} }
|
local COMMAND_LIBRARY = { entries = {} }
|
||||||
do
|
do
|
||||||
@@ -158,25 +155,6 @@ local function asArray(value)
|
|||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerDynamicCallback(name, callback)
|
|
||||||
env[name] = callback
|
|
||||||
if renderCallbackNames ~= nil then
|
|
||||||
renderCallbackNames[name] = true
|
|
||||||
end
|
|
||||||
return name
|
|
||||||
end
|
|
||||||
|
|
||||||
local function finishDynamicCallbackRender()
|
|
||||||
local previous = dynamicCallbackNames
|
|
||||||
dynamicCallbackNames = renderCallbackNames or {}
|
|
||||||
renderCallbackNames = nil
|
|
||||||
for name, _ in pairs(previous) do
|
|
||||||
if dynamicCallbackNames[name] ~= true then
|
|
||||||
env[name] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function shellQuote(value)
|
local function shellQuote(value)
|
||||||
return "'" .. asString(value):gsub("'", "'\\''") .. "'"
|
return "'" .. asString(value):gsub("'", "'\\''") .. "'"
|
||||||
end
|
end
|
||||||
@@ -355,13 +333,12 @@ end
|
|||||||
|
|
||||||
local keyCallbackCache = {}
|
local keyCallbackCache = {}
|
||||||
local function keyCallback(id, code)
|
local function keyCallback(id, code)
|
||||||
local name = "onKeyboardKey_" .. id
|
local callback = keyCallbackCache[id]
|
||||||
local callback = keyCallbackCache[name]
|
|
||||||
if callback == nil then
|
if callback == nil then
|
||||||
callback = function() selectKeyboardKey(code) end
|
callback = function() selectKeyboardKey(code) end
|
||||||
keyCallbackCache[name] = callback
|
keyCallbackCache[id] = callback
|
||||||
end
|
end
|
||||||
return registerDynamicCallback(name, callback)
|
return callback
|
||||||
end
|
end
|
||||||
|
|
||||||
local function contains(haystack, needle)
|
local function contains(haystack, needle)
|
||||||
@@ -1353,16 +1330,16 @@ local function commandLibraryNode()
|
|||||||
local visibleCount = math.min(#matches, 6)
|
local visibleCount = math.min(#matches, 6)
|
||||||
for index = 1, visibleCount do
|
for index = 1, visibleCount do
|
||||||
local entry = matches[index]
|
local entry = matches[index]
|
||||||
local callbackName = "onCommandLibraryUse:" .. asString(entry.id)
|
local selectedEntry = entry
|
||||||
registerDynamicCallback(callbackName, function()
|
local useCallback = function()
|
||||||
creatorCommand = asString(entry.template)
|
creatorCommand = asString(selectedEntry.template)
|
||||||
creatorCommandKind = asString(entry.kind, "shell")
|
creatorCommandKind = asString(selectedEntry.kind, "shell")
|
||||||
creatorLibraryEntryId = asString(entry.id)
|
creatorLibraryEntryId = asString(selectedEntry.id)
|
||||||
commandLibraryOpen = false
|
commandLibraryOpen = false
|
||||||
creatorError = ""
|
creatorError = ""
|
||||||
creatorRevision += 1
|
creatorRevision += 1
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
resultNodes[#resultNodes + 1] = ui.row({ gap = 6, align = "center" }, {
|
resultNodes[#resultNodes + 1] = ui.row({ gap = 6, align = "center" }, {
|
||||||
ui.glyph({
|
ui.glyph({
|
||||||
name = entry.kind == "native" and "binary-tree" or "terminal-2",
|
name = entry.kind == "native" and "binary-tree" or "terminal-2",
|
||||||
@@ -1381,7 +1358,7 @@ local function commandLibraryNode()
|
|||||||
}) or ui.label({ text = tr("panel.command_library.ready"), color = "secondary", fontSize = 9 }),
|
}) or ui.label({ text = tr("panel.command_library.ready"), color = "secondary", fontSize = 9 }),
|
||||||
ui.button({
|
ui.button({
|
||||||
text = tr("panel.command_library.use"), variant = "ghost", controlSize = "sm",
|
text = tr("panel.command_library.use"), variant = "ghost", controlSize = "sm",
|
||||||
onClick = callbackName,
|
onClick = useCallback,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
@@ -2025,17 +2002,15 @@ local function openBindEditor(bind, categoryName)
|
|||||||
render()
|
render()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerCategoryRenameCallback(categoryId)
|
local function categoryRenameCallback(categoryId)
|
||||||
local name = "onCategoryRename:" .. categoryId
|
return function()
|
||||||
return registerDynamicCallback(name, function()
|
|
||||||
local category = findSnapshotCategory(categoryId)
|
local category = findSnapshotCategory(categoryId)
|
||||||
if category ~= nil then openCategoryRename(category) end
|
if category ~= nil then openCategoryRename(category) end
|
||||||
end)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerBindCallback(bindId, operation)
|
local function bindCallback(bindId, operation)
|
||||||
local name = "onBindAction:" .. operation .. ":" .. bindId
|
return function()
|
||||||
return registerDynamicCallback(name, function()
|
|
||||||
local bind, categoryName = findSnapshotBind(bindId)
|
local bind, categoryName = findSnapshotBind(bindId)
|
||||||
if bind == nil then return end
|
if bind == nil then return end
|
||||||
if operation == "edit" then
|
if operation == "edit" then
|
||||||
@@ -2043,15 +2018,14 @@ local function registerBindCallback(bindId, operation)
|
|||||||
else
|
else
|
||||||
beginBindOperation(bind, operation)
|
beginBindOperation(bind, operation)
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerHiddenCallback(bindId, operation)
|
local function hiddenCallback(bindId, operation)
|
||||||
local name = "onHiddenBindAction:" .. operation .. ":" .. bindId
|
return function()
|
||||||
return registerDynamicCallback(name, function()
|
|
||||||
local bind = findHiddenBind(bindId)
|
local bind = findHiddenBind(bindId)
|
||||||
if bind ~= nil then beginHiddenOperation(bind, operation) end
|
if bind ~= nil then beginHiddenOperation(bind, operation) end
|
||||||
end)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function editBindActions(bind, categoryName)
|
local function editBindActions(bind, categoryName)
|
||||||
@@ -2088,9 +2062,9 @@ local function editBindActions(bind, categoryName)
|
|||||||
return actions
|
return actions
|
||||||
end
|
end
|
||||||
local bindId = asString(bind.id)
|
local bindId = asString(bind.id)
|
||||||
local editCallback = registerBindCallback(bindId, "edit")
|
local editCallback = bindCallback(bindId, "edit")
|
||||||
local hideCallback = registerBindCallback(bindId, "hide")
|
local hideCallback = bindCallback(bindId, "hide")
|
||||||
local deleteCallback = registerBindCallback(bindId, "delete")
|
local deleteCallback = bindCallback(bindId, "delete")
|
||||||
|
|
||||||
actions[#actions + 1] = ui.button({
|
actions[#actions + 1] = ui.button({
|
||||||
glyph = "pencil", variant = "ghost",
|
glyph = "pencil", variant = "ghost",
|
||||||
@@ -2233,8 +2207,8 @@ local function hiddenBindRow(bind, columnCount, rowIndex)
|
|||||||
for _, key in ipairs(rawKeys) do keys[#keys + 1] = keyPill(key, true) end
|
for _, key in ipairs(rawKeys) do keys[#keys + 1] = keyPill(key, true) end
|
||||||
|
|
||||||
local bindId = asString(bind.id)
|
local bindId = asString(bind.id)
|
||||||
local restoreCallback = registerHiddenCallback(bindId, "restore")
|
local restoreCallback = hiddenCallback(bindId, "restore")
|
||||||
local deleteCallback = registerHiddenCallback(bindId, "delete")
|
local deleteCallback = hiddenCallback(bindId, "delete")
|
||||||
local actions = {
|
local actions = {
|
||||||
ui.button({
|
ui.button({
|
||||||
glyph = "restore", variant = "ghost",
|
glyph = "restore", variant = "ghost",
|
||||||
@@ -2378,7 +2352,7 @@ local function categoryCard(category, columnCount)
|
|||||||
tooltip = canRename and tr("panel.editor.category_rename")
|
tooltip = canRename and tr("panel.editor.category_rename")
|
||||||
or tr("panel.editor.category_read_only"),
|
or tr("panel.editor.category_read_only"),
|
||||||
}
|
}
|
||||||
if canRename then renameProps.onClick = registerCategoryRenameCallback(categoryId) end
|
if canRename then renameProps.onClick = categoryRenameCallback(categoryId) end
|
||||||
headerChildren[#headerChildren + 1] = ui.button(renameProps)
|
headerChildren[#headerChildren + 1] = ui.button(renameProps)
|
||||||
end
|
end
|
||||||
header = ui.row({ gap = 8, align = "center" }, headerChildren)
|
header = ui.row({ gap = 8, align = "center" }, headerChildren)
|
||||||
@@ -2671,7 +2645,6 @@ local function warningsBanner()
|
|||||||
end
|
end
|
||||||
|
|
||||||
render = function()
|
render = function()
|
||||||
renderCallbackNames = {}
|
|
||||||
local status = asString(snapshot.status, "idle")
|
local status = asString(snapshot.status, "idle")
|
||||||
local categories = viewMode == "list" and filteredCategories() or EMPTY_ENTRIES
|
local categories = viewMode == "list" and filteredCategories() or EMPTY_ENTRIES
|
||||||
local requestedColumns = math.max(1, math.min(4, math.floor(tonumber(cfg("columns")) or 3)))
|
local requestedColumns = math.max(1, math.min(4, math.floor(tonumber(cfg("columns")) or 3)))
|
||||||
@@ -2702,7 +2675,6 @@ render = function()
|
|||||||
end
|
end
|
||||||
children[#children + 1] = body
|
children[#children + 1] = body
|
||||||
panel.render(ui.column({ flexGrow = 1, gap = 12, align = "stretch" }, children))
|
panel.render(ui.column({ flexGrow = 1, gap = 12, align = "stretch" }, children))
|
||||||
finishDynamicCallbackRender()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function requestRefresh()
|
local function requestRefresh()
|
||||||
|
|||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
id = "blackbartblues/keymap"
|
id = "blackbartblues/keymap"
|
||||||
name = "Keymap"
|
name = "Keymap"
|
||||||
version = "1.3.1"
|
version = "1.3.2"
|
||||||
plugin_api = 5
|
plugin_api = 9
|
||||||
author = "blackbartblues"
|
author = "blackbartblues"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
dependencies = ["hyprctl", "Hyprland", "niri", "mango", "mmsg", "xdg-open"]
|
dependencies = ["hyprctl", "Hyprland", "niri", "mango", "mmsg", "xdg-open"]
|
||||||
|
|||||||
Reference in New Issue
Block a user