Refactor helpers.volume_control

This commit is contained in:
elenapan 2024-07-18 13:52:11 +03:00
parent 8f62d03921
commit d5cf4d5cbc
5 changed files with 32 additions and 28 deletions

View file

@ -53,16 +53,16 @@ local volume = create_button(volume_symbol, volume_unmuted_color, x.color8.."30"
volume:buttons(gears.table.join(
-- Left click - Mute / Unmute
awful.button({ }, 1, function ()
helpers.volume_control(0)
helpers.volume_control.toggle()
end),
-- Right click - Run or raise volume control client
awful.button({ }, 3, apps.volume),
-- Scroll - Increase / Decrease volume
awful.button({ }, 4, function ()
helpers.volume_control(5)
helpers.volume_control.increase(5)
end),
awful.button({ }, 5, function ()
helpers.volume_control(-5)
helpers.volume_control.decrease(5)
end)
))

View file

@ -279,16 +279,16 @@ local volume = format_progress_bar(volume_bar)
volume:buttons(gears.table.join(
-- Left click - Mute / Unmute
awful.button({ }, 1, function ()
helpers.volume_control(0)
helpers.volume_control.toggle()
end),
-- Right click - Run or raise pavucontrol
awful.button({ }, 3, apps.volume),
-- Scroll - Increase / Decrease volume
awful.button({ }, 4, function ()
helpers.volume_control(2)
awful.button({ }, 4, function ()
helpers.volume_control.increase(2)
end),
awful.button({ }, 5, function ()
helpers.volume_control(-2)
awful.button({ }, 5, function ()
helpers.volume_control.decrease(2)
end)
))

View file

@ -280,16 +280,16 @@ local volume = format_progress_bar(volume_bar, volume_icon)
volume:buttons(gears.table.join(
-- Left click - Mute / Unmute
awful.button({ }, 1, function ()
helpers.volume_control(0)
helpers.volume_control.toggle()
end),
-- Right click - Run or raise pavucontrol
awful.button({ }, 3, apps.volume),
-- Scroll - Increase / Decrease volume
awful.button({ }, 4, function ()
helpers.volume_control(2)
helpers.volume_control.increase(2)
end),
awful.button({ }, 5, function ()
helpers.volume_control(-2)
helpers.volume_control.decrease(2)
end)
))

View file

@ -247,16 +247,20 @@ function helpers.round(number, decimals)
return math.floor(number * power) / power
end
function helpers.volume_control(step)
local cmd
if step == 0 then
cmd = "pactl set-sink-mute @DEFAULT_SINK@ toggle"
else
sign = step > 0 and "+" or ""
cmd = "pactl set-sink-mute @DEFAULT_SINK@ 0 && pactl set-sink-volume @DEFAULT_SINK@ "..sign..tostring(step).."%"
helpers.volume_control = {
toggle = function()
awful.spawn.with_shell("pactl set-sink-mute @DEFAULT_SINK@ toggle")
end,
increase = function(value)
awful.spawn.with_shell("pactl set-sink-mute @DEFAULT_SINK@ 0 && pactl set-sink-volume @DEFAULT_SINK@ +"..tostring(value).."%")
end,
decrease = function(value)
awful.spawn.with_shell("pactl set-sink-volume @DEFAULT_SINK@ -"..tostring(value).."%")
end,
set = function(value)
awful.spawn.with_shell("pactl set-sink-mute @DEFAULT_SINK@ 0 && pactl set-sink-volume @DEFAULT_SINK@ "..tostring(value).."%")
end
awful.spawn.with_shell(cmd)
end
}
function helpers.send_key(c, key)
awful.spawn.with_shell("xdotool key --window "..tostring(c.window).." "..key)

View file

@ -60,8 +60,8 @@ keys.desktopbuttons = gears.table.join(
awful.button({ }, 5, awful.tag.viewnext),
-- Side buttons - Control volume
awful.button({ }, 9, function () helpers.volume_control(5) end),
awful.button({ }, 8, function () helpers.volume_control(-5) end)
awful.button({ }, 9, function () helpers.volume_control.increase(5) end),
awful.button({ }, 8, function () helpers.volume_control.decrease(5) end)
-- Side buttons - Minimize and restore minimized client
-- awful.button({ }, 8, function()
@ -371,34 +371,34 @@ keys.globalkeys = gears.table.join(
-- Volume Control with volume keys
awful.key( { }, "XF86AudioMute",
function()
helpers.volume_control(0)
helpers.volume_control.toggle()
end,
{description = "(un)mute volume", group = "volume"}),
awful.key( { }, "XF86AudioLowerVolume",
function()
helpers.volume_control(-5)
helpers.volume_control.decrease(5)
end,
{description = "lower volume", group = "volume"}),
awful.key( { }, "XF86AudioRaiseVolume",
function()
helpers.volume_control(5)
helpers.volume_control.increase(5)
end,
{description = "raise volume", group = "volume"}),
-- Volume Control with alt+F1/F2/F3
awful.key( { altkey }, "F1",
function()
helpers.volume_control(0)
helpers.volume_control.toggle()
end,
{description = "(un)mute volume", group = "volume"}),
awful.key( { altkey }, "F2",
function()
helpers.volume_control(-5)
helpers.volume_control.decrease(5)
end,
{description = "lower volume", group = "volume"}),
awful.key( { altkey }, "F3",
function()
helpers.volume_control(5)
helpers.volume_control.increase(5)
end,
{description = "raise volume", group = "volume"}),