feat(audio-switcher): migrate to widget.actions and fix mute status logic (#200)
* refactor(audio-switcher): remove unused translation key * feat(audio-switcher)!: migrate scroll handling to widget actions * feat(audio-switcher): migrate click handling to widget actions * refactor(audio-switcher): migrate to openSettings * fix(audio-switcher): fix incorrect mute status check for input and output
This commit is contained in:
@@ -60,7 +60,6 @@ Use the settings button in the panel header to open Noctalia's plugin settings.
|
|||||||
| Plugin | `show_percentage` | `bool` | `true` | Show the output volume beside the bar icon; disable it for an icon-only widget. |
|
| Plugin | `show_percentage` | `bool` | `true` | Show the output volume beside the bar icon; disable it for an icon-only widget. |
|
||||||
| Plugin | `show_notification_on_switch` | `bool` | `true` | Show a notification when switching device. |
|
| Plugin | `show_notification_on_switch` | `bool` | `true` | Show a notification when switching device. |
|
||||||
| Plugin | `show_actions_in_tooltip` | `bool` | `true` | Show actions in tooltip. |
|
| Plugin | `show_actions_in_tooltip` | `bool` | `true` | Show actions in tooltip. |
|
||||||
| Plugin | `scroll_step` | `int` | `5` | Volume points changed by each wheel step over the bar widget (1–25). |
|
|
||||||
|
|
||||||
## IPC and keybinds
|
## IPC and keybinds
|
||||||
|
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ local function volumeCard(kind)
|
|||||||
local isOutput = kind == "output"
|
local isOutput = kind == "output"
|
||||||
local device = activeDevice(kind)
|
local device = activeDevice(kind)
|
||||||
local volume = tonumber(isOutput and snapshot.outputVolume or snapshot.inputVolume) or 0
|
local volume = tonumber(isOutput and snapshot.outputVolume or snapshot.inputVolume) or 0
|
||||||
local muted = isOutput and snapshot.outputMuted == true or snapshot.inputMuted == true
|
local muted = (isOutput and snapshot.outputMuted == true) or (not isOutput and snapshot.inputMuted == true)
|
||||||
local icon = isOutput and (muted and "volume-off" or "volume") or (muted and "microphone-mute" or "microphone")
|
local icon = isOutput and (muted and "volume-off" or "volume") or (muted and "microphone-mute" or "microphone")
|
||||||
return ui.column({ flexGrow = 1, gap = 8, padding = 12, radius = 12, fill = "surface_variant/0.45" }, {
|
return ui.column({ flexGrow = 1, gap = 8, padding = 12, radius = 12, fill = "surface_variant/0.45" }, {
|
||||||
ui.row({ align = "center", gap = 8 }, {
|
ui.row({ align = "center", gap = 8 }, {
|
||||||
@@ -461,16 +461,7 @@ function onRefresh() sendCommand("refresh") end
|
|||||||
function onScanBluetooth() sendCommand("scan_bluetooth") end
|
function onScanBluetooth() sendCommand("scan_bluetooth") end
|
||||||
function onCycleOutput() sendCommand("cycle_output") end
|
function onCycleOutput() sendCommand("cycle_output") end
|
||||||
function onCycleInput() sendCommand("cycle_input") end
|
function onCycleInput() sendCommand("cycle_input") end
|
||||||
function onOpenSettingsClicked()
|
function onOpenSettingsClicked() noctalia.openSettings() end
|
||||||
local started = noctalia.runAsync("noctalia msg settings-open plugins", function(result)
|
|
||||||
if result.timedOut == true or tonumber(result.exitCode) ~= 0 then
|
|
||||||
noctalia.notifyError(tr("title"), tr("errors.command_start"))
|
|
||||||
end
|
|
||||||
end, 5000)
|
|
||||||
if not started then
|
|
||||||
noctalia.notifyError(tr("title"), tr("errors.command_start"))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function onShowOutputs()
|
function onShowOutputs()
|
||||||
activeTab = "output"
|
activeTab = "output"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
id = "blackbartblues/audio-switcher"
|
id = "blackbartblues/audio-switcher"
|
||||||
name = "Audio Switcher"
|
name = "Audio Switcher"
|
||||||
version = "0.3.0"
|
version = "0.4.0"
|
||||||
plugin_api = 9
|
plugin_api = 15
|
||||||
author = "blackbartblues"
|
author = "blackbartblues"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
icon = "switch-horizontal"
|
icon = "switch-horizontal"
|
||||||
@@ -30,20 +30,17 @@ label_key = "settings.show_actions_in_tooltip.label"
|
|||||||
description_key = "settings.show_actions_in_tooltip.description"
|
description_key = "settings.show_actions_in_tooltip.description"
|
||||||
default = true
|
default = true
|
||||||
|
|
||||||
[[setting]]
|
|
||||||
key = "scroll_step"
|
|
||||||
type = "int"
|
|
||||||
label_key = "settings.scroll_step.label"
|
|
||||||
description_key = "settings.scroll_step.description"
|
|
||||||
default = 5
|
|
||||||
min = 1
|
|
||||||
max = 25
|
|
||||||
step = 1
|
|
||||||
|
|
||||||
[[widget]]
|
[[widget]]
|
||||||
id = "widget"
|
id = "widget"
|
||||||
entry = "widget.luau"
|
entry = "widget.luau"
|
||||||
|
|
||||||
|
[widget.actions]
|
||||||
|
scroll_up = "volume-up"
|
||||||
|
scroll_down = "volume-down"
|
||||||
|
left = "panel-toggle blackbartblues/audio-switcher:audio-switcher"
|
||||||
|
right = "plugin blackbartblues/audio-switcher:service all cycle-output"
|
||||||
|
middle = "plugin blackbartblues/audio-switcher:service all cycle-input"
|
||||||
|
|
||||||
[[panel]]
|
[[panel]]
|
||||||
id = "audio-switcher"
|
id = "audio-switcher"
|
||||||
entry = "panel.luau"
|
entry = "panel.luau"
|
||||||
|
|||||||
@@ -95,10 +95,6 @@
|
|||||||
"visible_count": "Visible {count}"
|
"visible_count": "Visible {count}"
|
||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"scroll_step": {
|
|
||||||
"description": "How many percentage points each mouse-wheel step changes volume on the bar widget.",
|
|
||||||
"label": "Scroll step"
|
|
||||||
},
|
|
||||||
"show_actions_in_tooltip": {
|
"show_actions_in_tooltip": {
|
||||||
"description": "Show actions in tooltip.",
|
"description": "Show actions in tooltip.",
|
||||||
"label": "Show actions in tooltip"
|
"label": "Show actions in tooltip"
|
||||||
@@ -137,7 +133,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"input": "Input",
|
"input": "Input",
|
||||||
"output": "Output",
|
"output": "Output"
|
||||||
"tooltip": "Output: {output} ({output_volume})\nInput: {input} ({input_volume})\nScroll: change output volume\nLeft click: open Audio Switcher\nRight click: cycle outputs\nMiddle click: cycle inputs"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
--!nonstrict
|
--!nonstrict
|
||||||
|
|
||||||
local PANEL_ID = "blackbartblues/audio-switcher:audio-switcher"
|
|
||||||
local SNAPSHOT_KEY = "audio_switcher_snapshot"
|
local SNAPSHOT_KEY = "audio_switcher_snapshot"
|
||||||
local COMMAND_KEY = "audio_switcher_command"
|
|
||||||
local RESULT_KEY = "audio_switcher_result"
|
|
||||||
|
|
||||||
local snapshot = noctalia.state.get(SNAPSHOT_KEY) or {
|
local snapshot = noctalia.state.get(SNAPSHOT_KEY) or {
|
||||||
available = false,
|
available = false,
|
||||||
@@ -14,8 +11,6 @@ local snapshot = noctalia.state.get(SNAPSHOT_KEY) or {
|
|||||||
inputVolume = 0,
|
inputVolume = 0,
|
||||||
outputMuted = false,
|
outputMuted = false,
|
||||||
}
|
}
|
||||||
local requestId = 0
|
|
||||||
local outputVolumeDraft = nil
|
|
||||||
|
|
||||||
local function activeDevice(devices, fallback)
|
local function activeDevice(devices, fallback)
|
||||||
for _, device in ipairs(type(devices) == "table" and devices or {}) do
|
for _, device in ipairs(type(devices) == "table" and devices or {}) do
|
||||||
@@ -33,7 +28,7 @@ local function render()
|
|||||||
local inputDevice = activeDevice(snapshot.inputs)
|
local inputDevice = activeDevice(snapshot.inputs)
|
||||||
local outputName = outputDevice and tostring(outputDevice.name or "") or noctalia.tr("panel.no_output")
|
local outputName = outputDevice and tostring(outputDevice.name or "") or noctalia.tr("panel.no_output")
|
||||||
local inputName = inputDevice and tostring(inputDevice.name or "") or noctalia.tr("panel.no_input")
|
local inputName = inputDevice and tostring(inputDevice.name or "") or noctalia.tr("panel.no_input")
|
||||||
local outputVolume = percent(outputVolumeDraft or snapshot.outputVolume)
|
local outputVolume = percent(snapshot.outputVolume)
|
||||||
local inputVolume = percent(snapshot.inputVolume)
|
local inputVolume = percent(snapshot.inputVolume)
|
||||||
|
|
||||||
local glyph = snapshot.outputMuted == true and "volume-off" or "volume"
|
local glyph = snapshot.outputMuted == true and "volume-off" or "volume"
|
||||||
@@ -63,67 +58,15 @@ local function render()
|
|||||||
barWidget.setTooltip(tooltipItems)
|
barWidget.setTooltip(tooltipItems)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function dispatch(action)
|
|
||||||
requestId += 1
|
|
||||||
noctalia.state.set(COMMAND_KEY, {
|
|
||||||
action = action,
|
|
||||||
requestId = "widget-" .. tostring(os.time()) .. "-" .. tostring(requestId),
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
noctalia.state.watch(SNAPSHOT_KEY, function(value)
|
noctalia.state.watch(SNAPSHOT_KEY, function(value)
|
||||||
if type(value) == "table" then
|
if type(value) == "table" then
|
||||||
if outputVolumeDraft ~= nil and tonumber(value.outputVolume) == outputVolumeDraft then
|
|
||||||
outputVolumeDraft = nil
|
|
||||||
end
|
|
||||||
snapshot = value
|
snapshot = value
|
||||||
render()
|
render()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
noctalia.state.watch(RESULT_KEY, function(result)
|
|
||||||
if type(result) ~= "table" or not tostring(result.requestId or ""):match("^widget%-scroll%-") then return end
|
|
||||||
if result.ok ~= true then
|
|
||||||
outputVolumeDraft = nil
|
|
||||||
render()
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
function update()
|
|
||||||
render()
|
|
||||||
end
|
|
||||||
|
|
||||||
function onConfigChanged()
|
function onConfigChanged()
|
||||||
render()
|
render()
|
||||||
end
|
end
|
||||||
|
|
||||||
function onClick()
|
|
||||||
noctalia.togglePanel(PANEL_ID)
|
|
||||||
end
|
|
||||||
|
|
||||||
function onRightClick()
|
|
||||||
dispatch("cycle_output")
|
|
||||||
end
|
|
||||||
|
|
||||||
function onMiddleClick()
|
|
||||||
dispatch("cycle_input")
|
|
||||||
end
|
|
||||||
|
|
||||||
function onScroll(axis, steps)
|
|
||||||
if axis ~= "vertical" then return end
|
|
||||||
local detents = tonumber(steps) or 0
|
|
||||||
if detents == 0 then return end
|
|
||||||
local step = math.max(1, math.min(25, math.floor(tonumber(noctalia.getConfig("scroll_step")) or 5)))
|
|
||||||
local current = tonumber(outputVolumeDraft or snapshot.outputVolume) or 0
|
|
||||||
outputVolumeDraft = math.max(0, math.min(100, math.floor(current - detents * step + 0.5)))
|
|
||||||
render()
|
|
||||||
requestId += 1
|
|
||||||
noctalia.state.set(COMMAND_KEY, {
|
|
||||||
action = "set_output_volume",
|
|
||||||
value = outputVolumeDraft,
|
|
||||||
requestId = "widget-scroll-" .. tostring(os.time()) .. "-" .. tostring(requestId),
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
noctalia.setUpdateInterval(60000)
|
|
||||||
render()
|
render()
|
||||||
|
|||||||
Reference in New Issue
Block a user