From f59cd869411b03556e65e4c78368ec1df656b5d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20LAJOIE?= Date: Wed, 18 Sep 2019 01:21:34 +0200 Subject: [PATCH] [Volume] Add an option to display a notification --- volume-widget/README.md | 12 +++++---- volume-widget/volume.lua | 53 +++++++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/volume-widget/README.md b/volume-widget/README.md index 24c2d76..6086074 100644 --- a/volume-widget/README.md +++ b/volume-widget/README.md @@ -12,6 +12,8 @@ It is possible to customize widget by providing a table with all or some of the | Name | Default | Description | |---|---|---| | `volume_audio_controller` | `pulse` | audio device | +| `notification` | `false` | Display a notification on mouseover | +| `notification_position` | `top_right` | The notification position | ## Installation @@ -27,16 +29,16 @@ s.mytasklist, -- Middle widget layout = wibox.layout.fixed.horizontal, ... volume_widget(), - ... + ... ``` - _Optional step._ In Arc icon theme the muted audio level icon (![Volume-widget](./audio-volume-muted-symbolic.png)) looks like 0 level icon, which could be a bit misleading. So I decided to use original muted icon for low audio level, and the same icon, but colored in red for muted audio level. Fortunately icons are in svg format, so you can easily recolor them with `sed`, so it would look like this (![Volume Widget](./audio-volume-muted-symbolic_red.png)): - + ```bash - cd /usr/share/icons/Arc/status/symbolic && - sudo cp audio-volume-muted-symbolic.svg audio-volume-muted-symbolic_red.svg && - sudo sed -i 's/bebebe/ed4737/g' ./audio-volume-muted-symbolic_red.svg + cd /usr/share/icons/Arc/status/symbolic && + sudo cp audio-volume-muted-symbolic.svg audio-volume-muted-symbolic_red.svg && + sudo sed -i 's/bebebe/ed4737/g' ./audio-volume-muted-symbolic_red.svg ``` ### Pulse or ALSA only diff --git a/volume-widget/volume.lua b/volume-widget/volume.lua index 6172f5e..2544d14 100644 --- a/volume-widget/volume.lua +++ b/volume-widget/volume.lua @@ -8,10 +8,12 @@ -- @copyright 2018 Pavel Makhov ------------------------------------------------- -local awful = require("awful") local wibox = require("wibox") local watch = require("awful.widget.watch") local spawn = require("awful.spawn") +local naughty = require("naughty") +local dpi = require('beautiful').xresources.apply_dpi + local path_to_icons = "/usr/share/icons/Arc/status/symbolic/" @@ -22,7 +24,8 @@ local function worker(args) local args = args or {} local volume_audio_controller = args.volume_audio_controller or 'pulse' - + local display_notification = args.notification or 'false' + local position = args.notification_position or "top_right" local device_arg = '' if volume_audio_controller == 'pulse' then device_arg = '-D pulse' @@ -47,11 +50,41 @@ local function worker(args) end } + local notification = {} + local volume_icon_name="audio-volume-high-symbolic" + + local function get_notification_text(txt) + local mute = string.match(txt, "%[(o%a%a?)%]") + local volume = string.match(txt, "(%d?%d?%d)%%") + volume = tonumber(string.format("% 3d", volume)) + if mute == "off" then + return volume.."% Mute" + else + return volume .."%" + end + end + + local function show_volume(val) + spawn.easy_async(GET_VOLUME_CMD, + function(stdout, _, _, _) + notification = naughty.notify{ + text = get_notification_text(stdout), + icon=path_to_icons .. val .. ".svg", + icon_size = dpi(16), + title = "Volume", + position = position, + timeout = 0, hover_timeout = 0.5, + width = 200, + } + end + ) + end + + local update_graphic = function(widget, stdout, _, _, _) 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_red" 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" @@ -59,6 +92,10 @@ local function worker(args) elseif (volume <= 100) then volume_icon_name="audio-volume-high-symbolic" end widget.image = path_to_icons .. volume_icon_name .. ".svg" + if display_notification then + notification.iconbox.image = path_to_icons .. volume_icon_name .. ".svg" + naughty.replace_text(notification, "Volume", get_notification_text(stdout)) + end end --[[ allows control volume level by: @@ -66,9 +103,9 @@ local function worker(args) - scrolling when cursor is over the widget ]] volume_widget:connect_signal("button::press", function(_,_,_,button) - if (button == 4) then awful.spawn(INC_VOLUME_CMD, false) - elseif (button == 5) then awful.spawn(DEC_VOLUME_CMD, false) - elseif (button == 1) then awful.spawn(TOG_VOLUME_CMD, false) + if (button == 4) then spawn(INC_VOLUME_CMD, false) + elseif (button == 5) then spawn(DEC_VOLUME_CMD, false) + elseif (button == 1) then spawn(TOG_VOLUME_CMD, false) end spawn.easy_async(GET_VOLUME_CMD, function(stdout, stderr, exitreason, exitcode) @@ -76,6 +113,10 @@ local function worker(args) end) end) + if display_notification then + volume_widget:connect_signal("mouse::enter", function() show_volume(volume_icon_name) end) + volume_widget:connect_signal("mouse::leave", function() naughty.destroy(notification) end) + end watch(GET_VOLUME_CMD, 1, update_graphic, volume_widget) return volume_widget