refactor(audio-switcher): use closure callbacks
This commit is contained in:
@@ -37,7 +37,6 @@ local requestCounter = 0
|
|||||||
local outputVolumeCommitValue = nil
|
local outputVolumeCommitValue = nil
|
||||||
local inputVolumeCommitValue = nil
|
local inputVolumeCommitValue = nil
|
||||||
local volumeControlRevision = 0
|
local volumeControlRevision = 0
|
||||||
local env = getfenv()
|
|
||||||
local render
|
local render
|
||||||
|
|
||||||
local ICON_STYLE_VALUES = { "automatic", "speaker", "over_ear", "tws", "wired" }
|
local ICON_STYLE_VALUES = { "automatic", "speaker", "over_ear", "tws", "wired" }
|
||||||
@@ -64,12 +63,6 @@ local function sendCommand(action, values)
|
|||||||
return command.requestId
|
return command.requestId
|
||||||
end
|
end
|
||||||
|
|
||||||
local function rowCallback(prefix, index, callback)
|
|
||||||
local name = prefix .. "_" .. tostring(index)
|
|
||||||
env[name] = callback
|
|
||||||
return name
|
|
||||||
end
|
|
||||||
|
|
||||||
local function activeDevice(kind)
|
local function activeDevice(kind)
|
||||||
local devices = kind == "output" and asArray(snapshot.outputs) or asArray(snapshot.inputs)
|
local devices = kind == "output" and asArray(snapshot.outputs) or asArray(snapshot.inputs)
|
||||||
for _, device in ipairs(devices) do
|
for _, device in ipairs(devices) do
|
||||||
@@ -203,21 +196,21 @@ local function editor(device)
|
|||||||
return ui.column({ gap = 7, padding = 10, radius = 9, fill = "surface_variant/0.45", border = "primary/0.35", borderWidth = 1 }, children)
|
return ui.column({ gap = 7, padding = 10, radius = 9, fill = "surface_variant/0.45", border = "primary/0.35", borderWidth = 1 }, children)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function deviceRow(device, index, kind)
|
local function deviceRow(device, kind)
|
||||||
local useAction = rowCallback("onUse", index, function()
|
local useAction = function()
|
||||||
sendCommand(kind == "output" and "set_output" or "set_input", { id = device.id })
|
sendCommand(kind == "output" and "set_output" or "set_input", { id = device.id })
|
||||||
end)
|
end
|
||||||
local editAction = rowCallback("onEdit", index, function()
|
local editAction = function()
|
||||||
editingId = device.id
|
editingId = device.id
|
||||||
editAlias = tostring(device.name or "")
|
editAlias = tostring(device.name or "")
|
||||||
editIconStyle = tostring(device.iconStyle or "automatic")
|
editIconStyle = tostring(device.iconStyle or "automatic")
|
||||||
editSlot = device.slot and tostring(device.slot) or ""
|
editSlot = device.slot and tostring(device.slot) or ""
|
||||||
feedback = ""
|
feedback = ""
|
||||||
render()
|
render()
|
||||||
end)
|
end
|
||||||
local hideAction = rowCallback("onHide", index, function()
|
local hideAction = function()
|
||||||
sendCommand("set_hidden", { id = device.id, kind = kind, hidden = not device.hidden })
|
sendCommand("set_hidden", { id = device.id, kind = kind, hidden = not device.hidden })
|
||||||
end)
|
end
|
||||||
local actionText = device.active and tr("device.current")
|
local actionText = device.active and tr("device.current")
|
||||||
or (device.bluetooth and not device.available and tr("actions.connect_and_use") or tr("actions.use"))
|
or (device.bluetooth and not device.available and tr("actions.connect_and_use") or tr("actions.use"))
|
||||||
local actionVariant = device.active and "ghost" or "primary"
|
local actionVariant = device.active and "ghost" or "primary"
|
||||||
@@ -266,11 +259,9 @@ end
|
|||||||
local function deviceList(kind)
|
local function deviceList(kind)
|
||||||
local source = kind == "output" and asArray(snapshot.outputs) or asArray(snapshot.inputs)
|
local source = kind == "output" and asArray(snapshot.outputs) or asArray(snapshot.inputs)
|
||||||
local rows = {}
|
local rows = {}
|
||||||
local callbackIndex = kind == "output" and 0 or 1000
|
|
||||||
for _, device in ipairs(source) do
|
for _, device in ipairs(source) do
|
||||||
if showHidden or not device.hidden then
|
if showHidden or not device.hidden then
|
||||||
table.insert(rows, deviceRow(device, callbackIndex, kind))
|
table.insert(rows, deviceRow(device, kind))
|
||||||
callbackIndex += 1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if #rows == 0 then
|
if #rows == 0 then
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
id = "blackbartblues/audio-switcher"
|
id = "blackbartblues/audio-switcher"
|
||||||
name = "Audio Switcher"
|
name = "Audio Switcher"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
plugin_api = 5
|
plugin_api = 9
|
||||||
author = "blackbartblues"
|
author = "blackbartblues"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
icon = "switch-horizontal"
|
icon = "switch-horizontal"
|
||||||
|
|||||||
Reference in New Issue
Block a user