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 () init = function ()
-- Make sure volume is unmuted -- Make sure volume is unmuted
volume.commands:unmute() -- Implicitly updates widgets
-- Update volume widget at start volume.commands.unmute()
volume.commands:get(widget.update)
end, end,
for_each_screen = function (s) for_each_screen = function (s)

View File

@ -75,7 +75,7 @@ return function(s)
bottom = beautiful.dpi(10), bottom = beautiful.dpi(10),
widget = wibox.container.margin widget = wibox.container.margin
}, },
bg ="#000000AA", bg ="#000000CC",
widget = wibox.container.background, widget = wibox.container.background,
ontop = true, ontop = true,
visible = true, visible = true,
@ -94,7 +94,7 @@ return function(s)
screen = s, screen = s,
placement = function(c) placement = function(c)
awful.placement.centered( awful.placement.bottom_right(
c, c,
{ {
honor_workarea = true, honor_workarea = true,
@ -107,10 +107,14 @@ return function(s)
-- Only set corner radius here. -- Only set corner radius here.
shape = function(cr, width, height) shape = function(cr, width, height)
gears.shape.rounded_rect( gears.shape.partially_rounded_rect(
cr, cr,
width, width,
height, height,
true,
false,
false,
false,
beautiful.dpi(20) beautiful.dpi(20)
) )
end end
@ -122,10 +126,18 @@ return function(s)
} }
local function update_popup() awesome.connect_signal("module::volume:update",
volume.commands:get(function(status) 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. widget.
container. container.
popup_layout. popup_layout.
@ -133,11 +145,7 @@ return function(s)
label_value_layout. label_value_layout.
value:set_text(status.value .. "%") value:set_text(status.value .. "%")
awesome.emit_signal( -- Update icon
"widget::volume:update",
status.value
)
local icon local icon
if status.mute then if status.mute then
icon = beautiful.icons.volume.mute icon = beautiful.icons.volume.mute
@ -158,22 +166,8 @@ return function(s)
icon_margin1. icon_margin1.
icon_margin2. icon_margin2.
icon:set_image(icon) icon:set_image(icon)
end)
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 { local hide_popup_timer = gears.timer {
timeout = 1, timeout = 1,
@ -183,9 +177,9 @@ return function(s)
end end
} }
awesome.connect_signal("module::volume_popup:show", awesome.connect_signal("module::volume_popup:show",
function() function()
update_slider()
if s == mouse.screen then if s == mouse.screen then
popup_container.visible = true popup_container.visible = true
end end
@ -198,11 +192,43 @@ return function(s)
end end
) )
popup_container:connect_signal("button::press", awesome.connect_signal("module::volume_popup:show_stay",
function() 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() hide_popup_timer:stop()
end 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 end

View File

@ -1,5 +1,5 @@
local volume = {} local volume = {}
volume.commands = {}
-- Create pamixer option string -- 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 volume.pamixer_options = volume.pamixer_options .. " --sink " .. config.volume.pamixer_sink
end end
function volume.commands:up() volume.commands = {
awful.spawn("pamixer --increase 5" .. volume.pamixer_options, false) up = function(callback)
end 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() down = function(callback)
awful.spawn("pamixer --decrease 5" .. volume.pamixer_options, false) awful.spawn.easy_async(
end "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) set = function(value, callback)
awful.spawn("pamixer --set_volume" .. tostring(value) .. " " .. volume.pamixer_options, false) awful.spawn.easy_async(
end "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() mute = function(callback)
awful.spawn("pamixer --mute" .. volume.pamixer_options, false) awful.spawn.easy_async(
end "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() unmute = function(callback)
awful.spawn("pamixer --unmute" .. volume.pamixer_options, false) awful.spawn.easy_async(
end "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 muted
local value 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) function(stdout, stderr, exitreason, exitcode)
muted = string.match(stdout, "(%w%w%w%w%w?) ") -- "true" or "false" muted = string.match(stdout, "(%w%w%w%w%w?) ") -- "true" or "false"
muted = (muted == "true") muted = (muted == "true")
@ -51,39 +100,30 @@ function volume.commands:get(callback)
}) })
end 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) }
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() volume.volume_up = function()
if volume.muted then if volume.muted then
volume.unmute() volume.unmute()
end end
volume.commands:up() volume.commands.up(function()
core.sound.play("volume_up") core.sound.play("volume_up")
end)
volume.exec_hooks()
end end
volume.volume_down = function() volume.volume_down = function()
@ -91,28 +131,23 @@ volume.volume_down = function()
volume.unmute() volume.unmute()
end end
volume.commands:down() volume.commands.down(function()
core.sound.play("volume_down") core.sound.play("volume_down")
end)
volume.exec_hooks()
end end
volume.mute = function() volume.mute = function()
volume.commands:mute()
volume.muted = true volume.muted = true
volume.commands.mute()
volume.exec_hooks()
end end
volume.unmute = function() volume.unmute = function()
volume.commands:unmute()
volume.muted = false volume.muted = false
volume.commands.unmute()
volume.exec_hooks()
end end
volume.toggle_mute = function() volume.toggle_mute = function()
volume.commands:get(function(status) volume.commands.get(function(status)
if status.mute then if status.mute then
volume.unmute() volume.unmute()
else else

View File

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