This commit is contained in:
BZ 2020-06-18 15:45:52 +02:00
parent 120c84126b
commit 10d2c9a815
1 changed files with 78 additions and 10 deletions

View File

@ -1,22 +1,90 @@
local wibox = require("wibox")
local gears = require("gears") local gears = require("gears")
local awful = require("awful") local awful = require("awful")
local theme = require("beautiful")
local dpi = theme.xresources.apply_dpi
local module = {} local module = {}
local function set_icon(c, icon) local client = client
if not c then return end
icon = gears.surface(icon) local icons
c.icon = icon and icon._native or nil local dynamic_icons
local dynamic_classes
local set_delay
local function len(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end end
local function new(config) local function contains(T, V)
for _, v in ipairs(T) do
if v == V then
return true
end
end
return false
end
local function set_icon(c, icon)
if c and c.valid and icon then
icon = gears.surface(icon)
c.icon = icon and icon._native or nil
end
end
local function set_dynamic_icon(c)
for regex, icon in pairs(dynamic_icons) do
if string.find(c.name, regex) then
set_icon(c, icon)
return
end
end
end
local function setup(config)
local cfg = config or {} local cfg = config or {}
icons = cfg.icons or {}
dynamic_icons = cfg.dynamic_icons or {}
dynamic_classes = cfg.dynamic_classes or {}
set_delay = cfg.set_delay or 1.0
if type(icons) ~= 'table' then icons = {} end
if type(dynamic_icons) ~= 'table' then dynamic_icons = {} end
if type(dynamic_classes) ~= 'table' then dynamic_classes = {} end
if type(set_delay) ~= 'number' then set_delay = 1.0 end
client.connect_signal("manage", function(c)
-- set icon based on c.class
set_icon(c, icons[c.class])
if len(dynamic_icons) == 0 then
-- user has not defined any dynamic_icons; exit
return
end
-- dynamic classes are only being checked if defined explicitly by the user
if len(dynamic_classes) > 0 then
if not contains(dynamic_classes, c.class) then
-- client is not in dynamic_classes; exit
return
end
end
-- the client name is now being monitored
c:connect_signal("property::name", set_dynamic_icon)
end)
end end
return setmetatable(module, { __call = function(_, ...) return setmetatable(module, { __call = function(_, ...)
new(...) setup(...)
return module -- we have to update all clients icons manually when the user restarts awesomewm
-- since there is no "manage" signal emitted by already running clients.
-- we also have to manually emit a fake "property::name" signal to update dynamic icons.
awful.spawn.easy_async_with_shell("sleep " .. set_delay, function()
for _, c in ipairs(client.get()) do
set_icon(c, icons[c.class])
c:emit_signal("property::name")
end
end)
end }) end })