fix #2, format with stylua

This commit is contained in:
BZ 2022-01-02 22:11:36 +01:00
parent 5deb3cb960
commit 56890bacfb
1 changed files with 100 additions and 96 deletions

196
init.lua
View File

@ -11,125 +11,129 @@ local client = client
local icons, dynamic_icons, dynamic_classes, delay local icons, dynamic_icons, dynamic_classes, delay
local function len(T) local function len(T)
local count = 0 local count = 0
for _ in pairs(T) do for _ in pairs(T) do
count = count + 1 count = count + 1
end end
return count return count
end end
local function contains(T, V) local function contains(T, V)
for _, v in ipairs(T) do for _, v in ipairs(T) do
if v == V then if v == V then
return true return true
end end
end end
return false return false
end end
local function icon_copy(icon) local function icon_copy(icon)
if not icon then if not icon then
return nil return nil
end end
local s = gears.surface(icon) local s = gears.surface(icon)
local img = cairo.ImageSurface.create(cairo.Format.ARGB32, s:get_width(), s:get_height()) local img = cairo.ImageSurface.create(cairo.Format.ARGB32, s:get_width(), s:get_height())
local cr = cairo.Context(img) local cr = cairo.Context(img)
cr:set_source_surface(s, 0, 0) cr:set_source_surface(s, 0, 0)
cr:paint() cr:paint()
return img return img
end end
local function set_icon(c, icon) local function set_icon(c, icon)
if c and c.valid then if c and c.valid then
if not c.icon_backup then if not c.icon_backup then
c.icon_backup = icon_copy(icons[c.class]) or icon_copy(c.icon) c.icon_backup = icon_copy(icons[c.class]) or icon_copy(c.icon)
end end
icon = icon_copy(icon) icon = icon_copy(icon)
if icon then if icon then
c.icon = icon._native c.icon = icon._native
end end
end end
end end
local function set_dynamic_icon(c) local function set_dynamic_icon(c)
if c.name then if c.name then
for regex, icon in pairs(dynamic_icons) do for regex, icon in pairs(dynamic_icons) do
if string.find(c.name, regex) then if string.find(c.name, regex) then
set_icon(c, icon) set_icon(c, icon)
return return
end end
end end
end end
if c.icon_backup then if c.icon_backup then
c.icon = c.icon_backup._native or nil c.icon = c.icon_backup._native or nil
end end
end end
local function setup(config) local function setup(config)
local cfg = config or {} local cfg = config or {}
icons = cfg.icons or theme.ic_icons or {} icons = cfg.icons or theme.ic_icons or {}
dynamic_icons = cfg.dynamic_icons or theme.ic_dynamic_icons or {} dynamic_icons = cfg.dynamic_icons or theme.ic_dynamic_icons or {}
dynamic_classes = cfg.dynamic_classes or theme.ic_dynamic_classes or {} dynamic_classes = cfg.dynamic_classes or theme.ic_dynamic_classes or {}
delay = cfg.delay or 0.5 delay = cfg.delay or 0.5
local fallback_icon = cfg.fallback_icon or theme.ic_fallback_icon or nil local fallback_icon = cfg.fallback_icon or theme.ic_fallback_icon or nil
if type(icons) ~= 'table' then if type(icons) ~= "table" then
icons = {} icons = {}
end end
if type(dynamic_icons) ~= 'table' then if type(dynamic_icons) ~= "table" then
dynamic_icons = {} dynamic_icons = {}
end end
if type(dynamic_classes) ~= 'table' then if type(dynamic_classes) ~= "table" then
dynamic_classes = {} dynamic_classes = {}
end end
if type(delay) ~= 'number' then if type(delay) ~= "number" then
delay = 0.5 delay = 0.5
end end
client.connect_signal("manage", function(c) client.connect_signal("manage", function(c)
-- set icon based on c.class -- set icon based on c.class
awful.spawn.easy_async_with_shell("sleep " .. delay, function() awful.spawn.easy_async_with_shell("sleep " .. delay, function()
if c and c.valid then if c and c.valid then
if icons[c.class] then if icons[c.class] then
set_icon(c, icons[c.class]) set_icon(c, icons[c.class])
elseif not c.icon and fallback_icon then elseif not c.icon and fallback_icon then
set_icon(c, fallback_icon) set_icon(c, fallback_icon)
end end
end -- fix #2: Dynamic icons not working without changing the window name
end) if contains(dynamic_classes, c.class) then
set_dynamic_icon(c)
end
end
end)
if len(dynamic_icons) == 0 then if len(dynamic_icons) == 0 then
-- user has not defined any dynamic_icons; exit -- user has not defined any dynamic_icons; exit
return return
end end
-- dynamic classes are only being checked if defined explicitly by the user -- dynamic classes are only being checked if defined explicitly by the user
if len(dynamic_classes) > 0 then if len(dynamic_classes) > 0 then
if not contains(dynamic_classes, c.class) then if not contains(dynamic_classes, c.class) then
-- client is not in dynamic_classes; exit -- client is not in dynamic_classes; exit
return return
end end
end end
-- the client name is now being monitored -- the client name is now being monitored
c:connect_signal("property::name", set_dynamic_icon) c:connect_signal("property::name", set_dynamic_icon)
end) end)
end end
return setmetatable(module, { return setmetatable(module, {
__call = function(_, ...) __call = function(_, ...)
setup(...) setup(...)
-- we have to update all clients icons manually when the user restarts awesomewm -- we have to update all clients icons manually when the user restarts awesomewm
-- since there is no "property::name" signal emitted by already running clients. -- since there is no "property::name" signal emitted by already running clients.
-- the set delay has to be higher than the regular delay, otherwise a race condition between both signals exists. -- the set delay has to be higher than the regular delay, otherwise a race condition between both signals exists.
awful.spawn.easy_async_with_shell("sleep " .. (delay + 0.5), function() awful.spawn.easy_async_with_shell("sleep " .. (delay + 0.5), function()
for _, c in ipairs(client.get()) do for _, c in ipairs(client.get()) do
if c and c.valid then if c and c.valid then
c:emit_signal("property::name") c:emit_signal("property::name")
end end
end end
end) end)
end, end,
}) })