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 16:41:39 +01:00
parent 65e0f96115
commit 6f5d566ec0
2 changed files with 29 additions and 29 deletions

View File

@ -53,20 +53,16 @@ documentation of your display manager of choice for more information.
# Configuration
The widget displays power icons that are searched in the folders defined
in the table `beautiful.upower_icon_theme_dirs` with extensions defined
in the table `beautiful.upower_icon_extensions`.
The default is to look into `"/usr/share/icons/Adwaita/scalable/devices/"`
and `"/usr/share/icons/Adwaita/scalable/status/"`for
icons whose extension is `"svg"`. Note that the directory paths *must* end
with a slash and that the extensions *must not* contain a dot.
The icons are searched using Awesome's
[`awful.util.geticonpath` function](https://awesomewm.org/doc/api/modules/awful.util.html#geticonpath).
The widget will display the battery 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 AwesomeWM.
You can specify a GUI client to be launched when the widget is right-clicked.
This can be done by changing the `gui_client` field of the widget. The default
is to have no client. For example, you could use the [XFCE4 Power Manager](http://goodies.xfce.org/projects/applications/xfce4-power-manager)
or the [GNOME one](https://projects.gnome.org/gnome-power-manager/).
is to have no client. For example, you could use the [XFCE4 Power
Manager](http://goodies.xfce.org/projects/applications/xfce4-power-manager) or
the [GNOME one](https://projects.gnome.org/gnome-power-manager/).
You can set the critical battery percentage at which a warning will be
displayed using the `critical_percentage` property (defaults to `5`).

View File

@ -1,5 +1,5 @@
--[[
Copyright 2017 Stefano Mazzucco <stefano AT curso DOT re>
Copyright 2017-2019 Stefano Mazzucco <stefano AT curso DOT re>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -15,37 +15,41 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
local beautiful = require("beautiful")
local wibox = require("wibox")
local awful = require("awful")
local naughty = require("naughty")
local lgi = require('lgi')
local icon_theme = lgi.Gtk.IconTheme.get_default()
local IconLookupFlags = lgi.Gtk.IconLookupFlags
local power = require("upower_dbus")
local WarningLevel = power.enums.BatteryWarningLevel
local spawn_with_shell = awful.spawn.with_shell or awful.util.spawn_with_shell
local icon_theme_dirs = { -- The trailing slash is mandatory!
"/usr/share/icons/Adwaita/scalable/status/",
"/usr/share/icons/Adwaita/scalable/devices/"}
local icon_theme_extensions = {"svg"}
icon_theme_dirs = beautiful.upower_icon_theme_dirs or icon_theme_dirs
icon_theme_extensions = beautiful.upower_icon_theme_extension or icon_theme_extensions
local widget = wibox.widget.imagebox()
local icon_size = 64
local icon_flags = {IconLookupFlags.GENERIC_FALLBACK}
local widget = wibox.widget {
resize = true,
widget = wibox.widget.imagebox
}
widget.critical_percentage = 5
local function build_icon_path(device)
if device.IconName then
return awful.util.geticonpath(device.IconName, icon_theme_extensions, icon_theme_dirs)
end
return ""
end
function widget:update()
self.device:update_mappings()
self:set_image(build_icon_path(self.device))
local icon = icon_theme:lookup_icon(
self.device.IconName,
icon_size,
icon_flags
)
if icon then
self.image = icon:load_surface()
end
if self.device.IsPresent then