Files

86 lines
2.0 KiB
Luau

local hsc = require("./lib/hsc.luau")
hsc.init({
device = noctalia.getConfig("device"),
mock = noctalia.getConfig("mock"),
wake_sink = noctalia.getConfig("wake_sink"),
})
local on = false
local battery = nil
local capsSidetone = false
local errorState = ""
local function render()
local label = on and noctalia.tr("shortcut.on") or noctalia.tr("shortcut.off")
if battery and battery.level and battery.level >= 0 then
label = label .. " \194\183 " .. battery.level .. "%"
end
shortcut.setLabel(label)
shortcut.setIcon("music")
shortcut.setActive(on)
end
local function currentSidetone()
local v = noctalia.state.get("sidetone")
if v ~= nil then return v end
return noctalia.getConfig("sidetone_default") or 64
end
local function apply(value)
hsc.sendCommand({ "-s", tostring(value) }, function(res)
if res.exitCode ~= 0 then
noctalia.notifyError(noctalia.tr("error.command_failed"), string.sub(res.stderr or "", 1, 200))
return
end
on = value > 0
noctalia.state.set("sidetone", value)
render()
end)
end
local function syncState()
local v = noctalia.state.get("caps")
if v then capsSidetone = v.sidetone == true end
v = noctalia.state.get("error")
if v ~= nil then errorState = v end
v = noctalia.state.get("sidetone")
if v ~= nil then
on = v > 0
end
end
function onCapsChanged(v)
capsSidetone = v ~= nil and v.sidetone == true or false
end
function onErrorChanged(v)
errorState = v or ""
end
function onSidetoneChanged(v)
if v ~= nil then
on = v > 0
render()
end
end
function onClick()
if not capsSidetone or errorState ~= "" then return end
if on then
apply(0)
else
apply(currentSidetone())
end
end
noctalia.state.watch("battery", function(v) battery = v or battery; render() end)
noctalia.state.watch("caps", onCapsChanged)
noctalia.state.watch("error", onErrorChanged)
noctalia.state.watch("sidetone", onSidetoneChanged)
local b = noctalia.state.get("battery")
if b then battery = b end
syncState()
render()