awful.widget: store buttons in tasklist

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-12-04 20:20:16 +01:00
parent b0090950a0
commit f36cb984e3
1 changed files with 73 additions and 64 deletions

View File

@ -8,6 +8,7 @@
local ipairs = ipairs
local pairs = pairs
local table = table
local otable = otable
local capi =
{
screen = screen,
@ -159,83 +160,91 @@ function taglist.label.noempty(t, args)
end
end
local function tasklist_update(w, buttons, label, data)
local clients = capi.client.get()
local shownclients = {}
for k, c in ipairs(clients) do
if not (c.skip_taskbar or c.hide
or c.type == "splash" or c.type == "dock" or c.type == "desktop") then
table.insert(shownclients, c)
end
end
clients = shownclients
-- Hack: if it has been registered as a widget in a wibox,
-- it's w.len since __len meta does not work on table until Lua 5.2.
-- Otherwise it's standard #w.
local len = (w.len or #w) / 2
-- Add more widgets
if len < #clients then
for i = len * 2 + 1, #clients * 2, 2 do
w[i] = capi.widget({ type = "imagebox", name = "tasklist_icon" .. i, align = "flex" })
w[i + 1] = capi.widget({ type = "textbox", name = "tasklist_text" .. i, align = "flex" })
end
-- Remove widgets
elseif len > #clients then
for i = #clients * 2 + 1, len * 2, 2 do
w[i] = nil
w[i + 1] = nil
end
end
-- Update widgets text
for k = 1, #clients * 2, 2 do
if buttons then
local c = clients[(k + 1) / 2]
if not data[c] then
data[c] = {}
-- Replace press function by a new one calling with tags as
-- argument
for kb, b in ipairs(buttons) do
-- Copy object
data[c][kb] = capi.button(b)
data[c][kb].press = function () b.press(c) end
end
end
w[k]:buttons(data[c])
w[k + 1]:buttons(data[c])
end
w[k + 1].text, w[k].bg = label(clients[(k + 1) / 2])
if w[k + 1].text then
-- Set icon
w[k].image = clients[(k + 1) / 2].icon
if w[k].image then
w[k].visible = true
else
w[k].visible = false
end
w[k + 1].visible = true
else
w[k].visible = false
w[k + 1].visible = false
end
end
end
--- Create a new tasklist widget.
-- @param label Label function to use.
-- @param buttons A table with buttons binding to set.
function tasklist.new(label, buttons)
local w = {}
local function tasklist_update ()
local clients = capi.client.get()
local shownclients = {}
for k, c in ipairs(clients) do
if not (c.skip_taskbar or c.hide
or c.type == "splash" or c.type == "dock" or c.type == "desktop") then
table.insert(shownclients, c)
end
end
clients = shownclients
-- Hack: if it has been registered as a widget in a wibox,
-- it's w.len since __len meta does not work on table until Lua 5.2.
-- Otherwise it's standard #w.
local len = (w.len or #w) / 2
-- Add more widgets
if len < #clients then
for i = len * 2 + 1, #clients * 2, 2 do
w[i] = capi.widget({ type = "imagebox", name = "tasklist_icon" .. i, align = "flex" })
w[i + 1] = capi.widget({ type = "textbox", name = "tasklist_text" .. i, align = "flex" })
end
-- Remove widgets
elseif len > #clients then
for i = #clients * 2 + 1, len * 2, 2 do
w[i] = nil
w[i + 1] = nil
end
end
-- Update widgets text
for k = 1, #clients * 2, 2 do
if buttons then
-- Replace press function by a new one calling with tags as
-- argument
local mbuttons = {}
for kb, b in ipairs(buttons) do
-- Copy object
mbuttons[kb] = capi.button(b)
mbuttons[kb].press = function () b.press(clients[(k + 1) / 2]) end
end
w[k]:buttons(mbuttons)
w[k + 1]:buttons(mbuttons)
end
w[k + 1].text, w[k].bg = label(clients[(k + 1) / 2])
if w[k + 1].text then
-- Set icon
w[k].image = clients[(k + 1) / 2].icon
if w[k].image then
w[k].visible = true
else
w[k].visible = false
end
w[k + 1].visible = true
else
w[k].visible = false
w[k + 1].visible = false
end
end
end
hooks.arrange.register(tasklist_update)
hooks.clients.register(tasklist_update)
hooks.tagged.register(tasklist_update)
hooks.focus.register(tasklist_update)
hooks.unfocus.register(tasklist_update)
local data = otable()
local u = function () tasklist_update(w, buttons, label, data) end
hooks.arrange.register(u)
hooks.clients.register(u)
hooks.tagged.register(u)
hooks.focus.register(u)
hooks.unfocus.register(u)
hooks.property.register(function (c, prop)
if prop == "urgent"
or prop == "floating"
or prop == "icon"
or prop == "name"
or prop == "icon_name" then
tasklist_update()
u()
end
end)
tasklist_update()
u()
-- Free data on unmanage
hooks.unmanage.register(function (c) data[c] = nil end)
return w
end