From 430a4f0d47f0066c0112534844741c7a4e9141f5 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 26 Oct 2021 21:11:53 +0200 Subject: utils: factor out singleton notifications --- bindings.lua | 2 +- utils.lua | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/bindings.lua b/bindings.lua index ebaf864..2ffba5b 100644 --- a/bindings.lua +++ b/bindings.lua @@ -35,7 +35,7 @@ function M.create(workspace) awful.key({ modkey }, "Prior", function () utils.vol_control(1) end), awful.key({ modkey }, "Next", function () utils.vol_control(-1) end), awful.key({ modkey }, "End", utils.vol_mute_toggle), - awful.key({ modkey }, "Delete", function () utils.auto_mute_toggle(workspace.notify_tbl, 'auto_mute') end), + awful.key({ modkey }, "Delete", function () utils.auto_mute_toggle(workspace) end), awful.key({ modkey }, "grave", function () awful.spawn("mpc toggle") end), awful.key({ modkey }, "b", function () awful.spawn("mpc next") end), awful.key({ modkey }, "p", function () awful.spawn("mpc prev") end), diff --git a/utils.lua b/utils.lua index 6fc40c5..5b40dbb 100644 --- a/utils.lua +++ b/utils.lua @@ -40,7 +40,7 @@ function M.vol_mute_toggle() awful.spawn("amixer -q set Master toggle") end -function M.auto_mute_toggle(notify_tbl, notify_tbl_key) +function M.auto_mute_toggle(wsp) local control = "'Auto-Mute Mode'" awful.spawn.easy_async("amixer sget " .. control, function(stdout, stderr, exitreason, exitcode) @@ -81,14 +81,8 @@ function M.auto_mute_toggle(notify_tbl, notify_tbl_key) return end - local args = { title = "Auto-mute", text = next_item } - - local prev_notify = notify_tbl[notify_tbl_key] - if prev_notify and prev_notify.box.visible then - args.replaces_id = prev_notify.id - end - - notify_tbl[notify_tbl_key] = naughty.notify(args) + M.notify_singleton(wsp, 'auto_mute', + { title = "Auto-mute", text = next_item }) end ) end) @@ -111,4 +105,12 @@ function M.screen_lock() end +function M.notify_singleton(wsp, id, args) + local prev_notify = wsp.notify_tbl[id] + if prev_notify and prev_notify.box.visible then + args.replaces_id = prev_notify.id + end + wsp.notify_tbl[id] = naughty.notify(args) +end + return M -- cgit v1.2.3