Cleaned up volume widget

master
Mark 2022-11-05 09:47:15 -07:00
parent 76e7547151
commit bb27fcfaa0
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 197 additions and 144 deletions

View File

@ -8,9 +8,8 @@ return {
init = function ()
-- Make sure volume is unmuted
volume.commands:unmute()
-- Update volume widget at start
volume.commands:get(widget.update)
-- Implicitly updates widgets
volume.commands.unmute()
end,
for_each_screen = function (s)

View File

@ -75,7 +75,7 @@ return function(s)
bottom = beautiful.dpi(10),
widget = wibox.container.margin
},
bg ="#000000AA",
bg ="#000000CC",
widget = wibox.container.background,
ontop = true,
visible = true,
@ -94,7 +94,7 @@ return function(s)
screen = s,
placement = function(c)
awful.placement.centered(
awful.placement.bottom_right(
c,
{
honor_workarea = true,
@ -107,10 +107,14 @@ return function(s)
-- Only set corner radius here.
shape = function(cr, width, height)
gears.shape.rounded_rect(
gears.shape.partially_rounded_rect(
cr,
width,
height,
true,
false,
false,
false,
beautiful.dpi(20)
)
end
@ -122,10 +126,18 @@ return function(s)
}
local function update_popup()
volume.commands:get(function(status)
awesome.connect_signal("module::volume:update",
function(status)
awesome.emit_signal("widget::volume")
-- Update slider
widget.
container.
popup_layout.
icon_bar_layout.
bar_layout.
volume_bar:set_value(status.value)
-- Update text
widget.
container.
popup_layout.
@ -133,11 +145,7 @@ return function(s)
label_value_layout.
value:set_text(status.value .. "%")
awesome.emit_signal(
"widget::volume:update",
status.value
)
-- Update icon
local icon
if status.mute then
icon = beautiful.icons.volume.mute
@ -158,22 +166,8 @@ return function(s)
icon_margin1.
icon_margin2.
icon:set_image(icon)
end)
end
local update_slider = function()
volume.commands:get(function(status)
widget.
container.
popup_layout.
icon_bar_layout.
bar_layout.
volume_bar:set_value(status.value)
update_popup()
end)
end
update_slider()
)
local hide_popup_timer = gears.timer {
timeout = 1,
@ -183,9 +177,9 @@ return function(s)
end
}
awesome.connect_signal("module::volume_popup:show",
function()
update_slider()
if s == mouse.screen then
popup_container.visible = true
end
@ -198,11 +192,43 @@ return function(s)
end
)
popup_container:connect_signal("button::press",
awesome.connect_signal("module::volume_popup:show_stay",
function()
popup_container.visible = false
if s == mouse.screen then
popup_container.visible = true
end
if hide_popup_timer.started then
hide_popup_timer:stop()
end
end
)
popup_container:connect_signal("button::press",
function(_, _, _, button, mods)
-- Right-click
if (button == 1) then
volume.toggle_mute()
-- Scroll up
elseif (button == 4) then
volume.volume_up()
-- Scroll down
elseif (button == 5) then
volume.volume_down()
end
end
)
popup_container:connect_signal("mouse::enter", function(result)
awesome.emit_signal("module::volume_popup:show_stay")
end)
popup_container:connect_signal("mouse::leave", function(result)
awesome.emit_signal("module::volume_popup:show")
end)
end

View File

@ -1,5 +1,5 @@
local volume = {}
volume.commands = {}
-- Create pamixer option string
@ -8,31 +8,80 @@ if (config.volume.pamixer_sink ~= "") then
volume.pamixer_options = volume.pamixer_options .. " --sink " .. config.volume.pamixer_sink
end
function volume.commands:up()
awful.spawn("pamixer --increase 5" .. volume.pamixer_options, false)
end
volume.commands = {
up = function(callback)
awful.spawn.easy_async(
"pamixer --increase 5 " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
function volume.commands:down()
awful.spawn("pamixer --decrease 5" .. volume.pamixer_options, false)
end
down = function(callback)
awful.spawn.easy_async(
"pamixer --decrease 5 " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
function volume.commands:set(value)
awful.spawn("pamixer --set_volume" .. tostring(value) .. " " .. volume.pamixer_options, false)
end
set = function(value, callback)
awful.spawn.easy_async(
"pamixer --set_volume " .. tostring(value) .. " " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
function volume.commands:mute()
awful.spawn("pamixer --mute" .. volume.pamixer_options, false)
end
mute = function(callback)
awful.spawn.easy_async(
"pamixer --mute " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
function volume.commands:unmute()
awful.spawn("pamixer --unmute" .. volume.pamixer_options, false)
end
unmute = function(callback)
awful.spawn.easy_async(
"pamixer --unmute " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
awesome.emit_signal("module::volume:update_read")
if (callback ~= nil) then
callback()
end
end
)
end,
function volume.commands:get(callback)
watch = function(timeout, callback, widget)
awful.widget.watch(
"pamixer --get-mute --get-volume " .. volume.pamixer_options,
timeout, callback, widget
)
end,
get = function(callback)
local muted
local value
awful.spawn.easy_async("pamixer --get-mute --get-volume" .. volume.pamixer_options,
awful.spawn.easy_async(
"pamixer --get-mute --get-volume " .. volume.pamixer_options,
function(stdout, stderr, exitreason, exitcode)
muted = string.match(stdout, "(%w%w%w%w%w?) ") -- "true" or "false"
muted = (muted == "true")
@ -51,39 +100,30 @@ function volume.commands:get(callback)
})
end
)
end
function volume.commands:watch(timeout, callback, widget)
awful.widget.watch(
"pamixer --get-mute --get-volume " .. volume.pamixer_options,
timeout, callback, widget
)
end
volume.hooks = {}
volume.add_hook = function(callback)
volume.hooks[#volume.hooks + 1] = callback
end
volume.exec_hooks = function()
volume.commands:get(function(status)
for i=1, #volume.hooks do
volume.hooks[i](status)
end
end)
end
}
awesome.connect_signal("module::volume:update_read",
function()
volume.commands.get(
function(status)
awesome.emit_signal(
"module::volume:update",
status
)
end
)
end
)
volume.volume_up = function()
if volume.muted then
volume.unmute()
end
volume.commands:up()
volume.commands.up(function()
core.sound.play("volume_up")
volume.exec_hooks()
end)
end
volume.volume_down = function()
@ -91,28 +131,23 @@ volume.volume_down = function()
volume.unmute()
end
volume.commands:down()
volume.commands.down(function()
core.sound.play("volume_down")
volume.exec_hooks()
end)
end
volume.mute = function()
volume.commands:mute()
volume.muted = true
volume.exec_hooks()
volume.commands.mute()
end
volume.unmute = function()
volume.commands:unmute()
volume.muted = false
volume.exec_hooks()
volume.commands.unmute()
end
volume.toggle_mute = function()
volume.commands:get(function(status)
volume.commands.get(function(status)
if status.mute then
volume.unmute()
else

View File

@ -23,6 +23,7 @@ widget.arc = wibox.widget {
colors = {"#27D4CC", "#00446B"},
bg = "#FFFFFF30",
paddings = beautiful.dpi(2),
rounded_edge = true,
widget = wibox.container.arcchart
}
@ -69,10 +70,12 @@ widget.widget = wibox.widget {
widget.widget:connect_signal("mouse::enter", function(result)
widget.widget.bg = beautiful.color.bar.hover_bg
awesome.emit_signal("module::volume_popup:show_stay")
end)
widget.widget:connect_signal("mouse::leave", function(result)
widget.widget.bg = beautiful.color.transparent
awesome.emit_signal("module::volume_popup:show")
end)
widget.widget:connect_signal("button::press",
@ -90,13 +93,12 @@ widget.widget:connect_signal("button::press",
elseif (button == 5) then
volume.volume_down()
end
awesome.emit_signal("module::volume_popup:show")
awesome.emit_signal("module::volume_popup:update")
end
)
widget.update = function(status)
awesome.connect_signal("module::volume:update",
function(status)
if (status.value == false) then
widget.icon.image = beautiful.icons.volume.error
widget.arc.value = 100;
@ -123,16 +125,7 @@ widget.update = function(status)
widget.icon.image = beautiful.icons.volume.off
end
end
end
volume.add_hook(widget.update)
volume.commands:watch(
5,
function()
volume.commands:get(widget.update)
end,
widget.widget
end
)
return widget