Add instant update to volume graphic upon changes

Added functionality to audio widget so the graphic updates immediately when you modify the volume through it, rather than taking up to a second to reflect changes.

Additionally made some minor changes to code to make it more consistent.
This commit is contained in:
Ashley 2017-05-30 01:09:44 +01:00 committed by GitHub
parent be8156ea12
commit 1fa4dc3508
1 changed files with 24 additions and 23 deletions

View File

@ -1,8 +1,10 @@
local awful = require("awful") local awful = require("awful")
local wibox = require("wibox") local wibox = require("wibox")
local watch = require("awful.widget.watch") local watch = require("awful.widget.watch")
local spawn = require("awful.spawn")
local path_to_icons = "/usr/share/icons/Arc/status/symbolic/" local path_to_icons = "/usr/share/icons/Arc/status/symbolic/"
local request_command = 'amixer -D pulse sget Master'
volume_widget = wibox.widget { volume_widget = wibox.widget {
{ {
@ -17,34 +19,33 @@ volume_widget = wibox.widget {
end end
} }
local update_graphic = function(widget, stdout, stderr, reason, exit_code)
local mute = string.match(stdout, "%[(o%D%D?)%]")
local volume = string.match(stdout, "(%d?%d?%d)%%")
volume = tonumber(string.format("% 3d", volume))
local volume_icon_name
if mute == "off" then volume_icon_name="audio-volume-muted-symbolic"
elseif (volume >= 0 and volume < 25) then volume_icon_name="audio-volume-muted-symbolic"
elseif (volume < 50) then volume_icon_name="audio-volume-low-symbolic"
elseif (volume < 75) then volume_icon_name="audio-volume-medium-symbolic"
elseif (volume <= 100) then volume_icon_name="audio-volume-high-symbolic"
end
widget.image = path_to_icons .. volume_icon_name .. ".svg"
end
--[[ allows control volume level by: --[[ allows control volume level by:
- clicking on the widget to mute/unmute - clicking on the widget to mute/unmute
- scrolling when curson is over the widget - scrolling when curson is over the widget
]] ]]
volume_widget:connect_signal("button::press", function(_,_,_,button) volume_widget:connect_signal("button::press", function(_,_,_,button)
if (button == 4) then if (button == 4) then awful.spawn("amixer -D pulse sset Master 5%+", false)
awful.spawn("amixer -D pulse sset Master 5%+", false) elseif (button == 5) then awful.spawn("amixer -D pulse sset Master 5%-", false)
elseif (button == 5) then elseif (button == 1) then awful.spawn("amixer -D pulse sset Master toggle", false)
awful.spawn("amixer -D pulse sset Master 5%-", false)
elseif (button == 1) then
awful.spawn("amixer -D pulse sset Master toggle", false)
end end
spawn.easy_async(request_command, function(stdout, stderr, exitreason, exitcode)
update_graphic(volume_widget, stdout, stderr, exitreason, exitcode)
end)
end) end)
watch( watch(request_command, 1, update_graphic, volume_widget)
'amixer -D pulse sget Master', 1,
function(widget, stdout, stderr, reason, exit_code)
local mute = string.match(stdout, "%[(o%D%D?)%]")
local volume = string.match(stdout, "(%d?%d?%d)%%")
volume = tonumber(string.format("% 3d", volume))
local volume_icon_name
if mute == "off" then volume_icon_name="audio-volume-muted-symbolic"
elseif (volume >= 0 and volume < 25) then volume_icon_name="audio-volume-muted-symbolic"
elseif (volume >= 25 and volume < 50) then volume_icon_name="audio-volume-low-symbolic"
elseif (volume >= 50 and volume < 75) then volume_icon_name="audio-volume-medium-symbolic"
elseif (volume >= 75 and volume <= 100) then volume_icon_name="audio-volume-high-symbolic"
end
widget.image = path_to_icons .. volume_icon_name .. ".svg"
end,
volume_widget
)