refactor(keybind-cheatsheet): use closure callbacks
This commit is contained in:
@@ -39,10 +39,6 @@ local refreshing = false
|
|||||||
local render
|
local render
|
||||||
local refresh
|
local refresh
|
||||||
|
|
||||||
local env = getfenv()
|
|
||||||
local dynamicCallbacks = {}
|
|
||||||
local callbackIndex = 0
|
|
||||||
|
|
||||||
local function tr(key, values)
|
local function tr(key, values)
|
||||||
return noctalia.tr(key, values)
|
return noctalia.tr(key, values)
|
||||||
end
|
end
|
||||||
@@ -60,22 +56,6 @@ local function startsWith(value, prefix)
|
|||||||
return value:sub(1, #prefix) == prefix
|
return value:sub(1, #prefix) == prefix
|
||||||
end
|
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 = {
|
local KEY_LABELS = {
|
||||||
XF86AudioRaiseVolume = "Vol Up",
|
XF86AudioRaiseVolume = "Vol Up",
|
||||||
XF86AudioLowerVolume = "Vol Down",
|
XF86AudioLowerVolume = "Vol Down",
|
||||||
@@ -566,7 +546,7 @@ local function bindingRow(binding, occurrence)
|
|||||||
local description = effectiveDescription(binding)
|
local description = effectiveDescription(binding)
|
||||||
local identity = binding.id .. "#" .. occurrence
|
local identity = binding.id .. "#" .. occurrence
|
||||||
if view == "edit" and editingId == identity then
|
if view == "edit" and editingId == identity then
|
||||||
local visibilityChanged = callback("visibility", function()
|
local visibilityChanged = function()
|
||||||
if hidden then
|
if hidden then
|
||||||
preferences.hidden[binding.id] = nil
|
preferences.hidden[binding.id] = nil
|
||||||
else
|
else
|
||||||
@@ -574,15 +554,15 @@ local function bindingRow(binding, occurrence)
|
|||||||
end
|
end
|
||||||
savePreferences()
|
savePreferences()
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
local inputChanged = callback("editChange", function(value) editDraft = value end)
|
local inputChanged = function(value) editDraft = value end
|
||||||
local submit = callback("editSubmit", function(value) saveCustomDescription(binding, value) end)
|
local submit = function(value) saveCustomDescription(binding, value) end
|
||||||
local save = callback("editSave", function() saveCustomDescription(binding, editDraft) end)
|
local save = function() saveCustomDescription(binding, editDraft) end
|
||||||
local cancel = callback("editCancel", function()
|
local cancel = function()
|
||||||
editingId = nil
|
editingId = nil
|
||||||
editDraft = ""
|
editDraft = ""
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
return ui.row({ key = "edit-" .. identity, gap = 6, align = "center", paddingV = 0 }, {
|
return ui.row({ key = "edit-" .. identity, gap = 6, align = "center", paddingV = 0 }, {
|
||||||
ui.row({ minWidth = 188, gap = 3, align = "center", opacity = contentOpacity }, pills),
|
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 }),
|
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),
|
ui.column({ flexGrow = 1, gap = 0, opacity = contentOpacity }, labels),
|
||||||
}
|
}
|
||||||
if view == "edit" then
|
if view == "edit" then
|
||||||
local edit = callback("edit", function()
|
local edit = function()
|
||||||
editingId = identity
|
editingId = identity
|
||||||
editDraft = description
|
editDraft = description
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
local visibilityChanged = callback("visibility", function()
|
local visibilityChanged = function()
|
||||||
if hidden then
|
if hidden then
|
||||||
preferences.hidden[binding.id] = nil
|
preferences.hidden[binding.id] = nil
|
||||||
else
|
else
|
||||||
@@ -622,7 +602,7 @@ local function bindingRow(binding, occurrence)
|
|||||||
end
|
end
|
||||||
savePreferences()
|
savePreferences()
|
||||||
render()
|
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 = "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 }))
|
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
|
end
|
||||||
@@ -667,16 +647,16 @@ local function hiddenCount()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function colorControl(bucket, property)
|
local function colorControl(bucket, property)
|
||||||
local choose = callback("chooseColor", function() chooseColor(bucket.id, property) end)
|
local choose = function() chooseColor(bucket.id, property) end
|
||||||
local paste = callback("pasteColor", function() pasteColor(bucket.id, property) end)
|
local paste = function() pasteColor(bucket.id, property) end
|
||||||
local reset = callback("resetColor", function()
|
local reset = function()
|
||||||
if preferences.colors[bucket.id] ~= nil then
|
if preferences.colors[bucket.id] ~= nil then
|
||||||
preferences.colors[bucket.id][property] = nil
|
preferences.colors[bucket.id][property] = nil
|
||||||
if next(preferences.colors[bucket.id]) == nil then preferences.colors[bucket.id] = nil end
|
if next(preferences.colors[bucket.id]) == nil then preferences.colors[bucket.id] = nil end
|
||||||
savePreferences()
|
savePreferences()
|
||||||
render()
|
render()
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
return ui.row({ gap = 5, align = "center", flexGrow = 1 }, {
|
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.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 }),
|
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
|
end
|
||||||
local resetAll = callback("resetAllColors", function()
|
local resetAll = function()
|
||||||
preferences.colors = {}
|
preferences.colors = {}
|
||||||
savePreferences()
|
savePreferences()
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
table.insert(rows, ui.separator({ spacing = 6 }))
|
table.insert(rows, ui.separator({ spacing = 6 }))
|
||||||
table.insert(rows, ui.row({ gap = 8, align = "center" }, {
|
table.insert(rows, ui.row({ gap = 8, align = "center" }, {
|
||||||
ui.button({ glyph = "restore", text = tr("reset_colors"), onClick = resetAll }),
|
ui.button({ glyph = "restore", text = tr("reset_colors"), onClick = resetAll }),
|
||||||
@@ -711,19 +691,19 @@ local function appearanceBody()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function renderHeader()
|
local function renderHeader()
|
||||||
local refreshClick = callback("refresh", function() refresh() end)
|
local refreshClick = function() refresh() end
|
||||||
local editMode = callback("editMode", function()
|
local editMode = function()
|
||||||
view = view == "edit" and "bindings" or "edit"
|
view = view == "edit" and "bindings" or "edit"
|
||||||
editingId = nil
|
editingId = nil
|
||||||
editDraft = ""
|
editDraft = ""
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
local appearance = callback("appearance", function()
|
local appearance = function()
|
||||||
view = view == "appearance" and "bindings" or "appearance"
|
view = view == "appearance" and "bindings" or "appearance"
|
||||||
editingId = nil
|
editingId = nil
|
||||||
editDraft = ""
|
editDraft = ""
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
local title = tr("title")
|
local title = tr("title")
|
||||||
if currentCompositor ~= "" then
|
if currentCompositor ~= "" then
|
||||||
title = (currentCompositor == "mango" and "Mango" or (currentCompositor == "niri" and "Niri" or "Hyprland")) .. " Keymap"
|
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
|
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
|
query = value
|
||||||
editingId = nil
|
editingId = nil
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
local clearSearch = callback("clearSearch", function()
|
local clearSearch = function()
|
||||||
query = ""
|
query = ""
|
||||||
searchRevision += 1
|
searchRevision += 1
|
||||||
render()
|
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.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 }))
|
table.insert(children, ui.button({ glyph = "x", width = 26, variant = "ghost", controlSize = "sm", enabled = query ~= "", tooltip = tr("clear"), onClick = clearSearch }))
|
||||||
end
|
end
|
||||||
@@ -753,11 +733,11 @@ local function renderHeader()
|
|||||||
end
|
end
|
||||||
if view == "edit" then
|
if view == "edit" then
|
||||||
local count = hiddenCount()
|
local count = hiddenCount()
|
||||||
local restoreHidden = callback("restoreHidden", function()
|
local restoreHidden = function()
|
||||||
restoreHiddenBindings(bindings, preferences.hidden)
|
restoreHiddenBindings(bindings, preferences.hidden)
|
||||||
savePreferences()
|
savePreferences()
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
table.insert(children, ui.label({ text = tr("hidden_count", { count = count }), color = "on_surface_variant", fontSize = 12 }))
|
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 }))
|
table.insert(children, ui.button({ glyph = "eye", variant = "ghost", controlSize = "sm", enabled = count > 0, tooltip = tr("restore_hidden"), onClick = restoreHidden }))
|
||||||
end
|
end
|
||||||
@@ -769,7 +749,6 @@ end
|
|||||||
|
|
||||||
render = function()
|
render = function()
|
||||||
if not panelOpen then return end
|
if not panelOpen then return end
|
||||||
resetCallbacks()
|
|
||||||
local contentState = "bindings"
|
local contentState = "bindings"
|
||||||
if view == "appearance" then
|
if view == "appearance" then
|
||||||
contentState = "appearance"
|
contentState = "appearance"
|
||||||
@@ -816,7 +795,6 @@ local function releasePanelState(clearModel)
|
|||||||
query = ""
|
query = ""
|
||||||
editingId = nil
|
editingId = nil
|
||||||
editDraft = ""
|
editDraft = ""
|
||||||
resetCallbacks()
|
|
||||||
if clearModel then
|
if clearModel then
|
||||||
bindings = {}
|
bindings = {}
|
||||||
parseWarnings = {}
|
parseWarnings = {}
|
||||||
@@ -871,7 +849,6 @@ local function lifecycleState()
|
|||||||
refreshing = refreshing,
|
refreshing = refreshing,
|
||||||
bindingCount = #bindings,
|
bindingCount = #bindings,
|
||||||
snapshotLoaded = snapshot ~= nil,
|
snapshotLoaded = snapshot ~= nil,
|
||||||
callbackCount = #dynamicCallbacks,
|
|
||||||
editDraft = editDraft,
|
editDraft = editDraft,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
id = "kenn/keybind-cheatsheet"
|
id = "kenn/keybind-cheatsheet"
|
||||||
name = "Keybind Cheatsheet"
|
name = "Keybind Cheatsheet"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
plugin_api = 4
|
plugin_api = 9
|
||||||
author = "kenn"
|
author = "kenn"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
dependencies = ["hyprctl"]
|
dependencies = ["hyprctl"]
|
||||||
|
|||||||
Reference in New Issue
Block a user