Use lgi to get icons directly from GTK

Rather than setting a path to the icons, use lgi to get the current GTK theme
and then look up the icons. Then set the image as a cairo surface by using the
GTK api directly.

With this change, one can modify their GTK icon theme (e.g. with lxappearance),
restart AwesomeWM and have the widget's icon automatically updated.
This commit is contained in:
Stefano Mazzucco 2019-05-08 15:35:42 +01:00
parent 392d37c0b4
commit c60aa07d98
2 changed files with 27 additions and 17 deletions

View File

@ -57,10 +57,10 @@ documentation of your display manager of choice for more information.
# Configuration
The widget displays volume icons that are searched in the folder defined by
`beautiful.pulse_icon_theme` with extension `beautiful.pulse_icon_extension`.
The default is to look into `"/usr/share/icons/Adwaita/scalable/status"` for
icons whose extension is `".svg"`.
The widget will display the audio icons defined in your GTK+ theme and it will
resize them to fit in the available space. This means that you can switch your
icon theme, for example using `lxappearance`, and update the widget by
restarting AwesomWM.
Specifically, you will need icons named:

View File

@ -23,22 +23,32 @@ local awful = require("awful")
local gears = require("gears")
local wibox = require("wibox")
local beautiful = require("beautiful")
local naughty = require("naughty")
local pulse = require("pulseaudio_dbus")
local icon_theme = "/usr/share/icons/Adwaita/scalable/status"
local icon_extension = ".svg"
local lgi = require('lgi')
local icon_theme = lgi.Gtk.IconTheme.get_default()
local IconLookupFlags = lgi.Gtk.IconLookupFlags
icon_theme = beautiful.pulse_icon_theme or icon_theme
icon_extension = beautiful.pulse_icon_extension or icon_extension
local icon_size = 64
local icon_flags = {IconLookupFlags.GENERIC_FALLBACK}
local function lookup_icon(name)
return icon_theme:lookup_icon(name, icon_size, icon_flags)
end
local function load_icon(icon)
if icon ~= nil then
return icon:load_surface()
end
end
local icon = {
high = icon_theme .. "/audio-volume-high-symbolic" .. icon_extension,
med = icon_theme .. "/audio-volume-medium-symbolic" .. icon_extension,
low = icon_theme .. "/audio-volume-low-symbolic" .. icon_extension,
muted = icon_theme .. "/audio-volume-muted-symbolic" .. icon_extension
high = lookup_icon("audio-volume-high-symbolic"),
med = lookup_icon("audio-volume-medium-symbolic"),
low = lookup_icon("audio-volume-low-symbolic"),
muted = lookup_icon("audio-volume-muted-symbolic"),
}
local widget = wibox.widget {
@ -53,16 +63,16 @@ function widget:update_appearance(v)
if v == "Muted" then
msg = v
i = icon.muted
i = load_icon(icon.muted)
else
v = v == "Unmuted" and self.sink:get_volume_percent()[1] or tonumber(v)
msg = string.format("%d%%", v)
if v <= 33 then
i = icon.low
i = load_icon(icon.low)
elseif v <= 66 then
i = icon.med
i = load_icon(icon.med)
else
i = icon.high
i = load_icon(icon.high)
end
end