awful.widget: stop using useless closures in taglist
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
1a7413d257
commit
f58b9e3566
|
@ -31,59 +31,71 @@ taglist.label = {}
|
||||||
tasklist = {}
|
tasklist = {}
|
||||||
tasklist.label = {}
|
tasklist.label = {}
|
||||||
|
|
||||||
--- Create a new taglist widget.
|
local function taglist_update (screen, w, label, buttons, data)
|
||||||
-- @param screen The screen to draw tag list.
|
local tags = capi.screen[screen]:tags()
|
||||||
-- @param label Label function to use.
|
-- Hack: if it has been registered as a widget in a wibox,
|
||||||
-- @param buttons A table with buttons binding to set.
|
-- it's w.len since __len meta does not work on table until Lua 5.2.
|
||||||
function taglist.new(scr, label, buttons)
|
-- Otherwise it's standard #w.
|
||||||
local w = {}
|
local len = w.len or #w
|
||||||
local function taglist_update (screen)
|
-- Add more widgets
|
||||||
-- Return right now if we do not care about this screen
|
if len < #tags then
|
||||||
if scr ~= screen then return end
|
for i = len + 1, #tags do
|
||||||
local tags = capi.screen[screen]:tags()
|
w[i] = capi.widget({ type = "textbox", name = "taglist" .. i })
|
||||||
-- 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
|
|
||||||
-- Add more widgets
|
|
||||||
if len < #tags then
|
|
||||||
for i = len + 1, #tags do
|
|
||||||
w[i] = capi.widget({ type = "textbox", name = "taglist" .. i })
|
|
||||||
end
|
|
||||||
-- Remove widgets
|
|
||||||
elseif len > #tags then
|
|
||||||
for i = #tags + 1, len do
|
|
||||||
w[i] = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
-- Update widgets text
|
-- Remove widgets
|
||||||
for k, tag in ipairs(tags) do
|
elseif len > #tags then
|
||||||
w[k].text = label(tag)
|
for i = #tags + 1, len do
|
||||||
if buttons then
|
w[i] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Update widgets text
|
||||||
|
for k, tag in ipairs(tags) do
|
||||||
|
w[k].text = label(tag)
|
||||||
|
if buttons then
|
||||||
|
if not data[tag] then
|
||||||
-- Replace press function by a new one calling with tags as
|
-- Replace press function by a new one calling with tags as
|
||||||
-- argument.
|
-- argument.
|
||||||
-- This is done here because order of tags can change
|
-- This is done here because order of tags can change
|
||||||
local mbuttons = {}
|
data[tag] = {}
|
||||||
for kb, b in ipairs(buttons) do
|
for kb, b in ipairs(buttons) do
|
||||||
-- Copy object
|
-- Copy object
|
||||||
mbuttons[kb] = capi.button(b)
|
data[tag][kb] = capi.button(b)
|
||||||
mbuttons[kb].press = function () b.press(tag) end
|
data[tag][kb].press = function () b.press(tag) end
|
||||||
end
|
end
|
||||||
w[k]:buttons(mbuttons)
|
|
||||||
end
|
end
|
||||||
|
w[k]:buttons(data[tag])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
hooks.focus.register(function (c) return taglist_update(c.screen) end)
|
end
|
||||||
hooks.unfocus.register(function (c) return taglist_update(c.screen) end)
|
|
||||||
hooks.arrange.register(taglist_update)
|
--- Create a new taglist widget.
|
||||||
hooks.tags.register(taglist_update)
|
-- @param screen The screen to draw tag list for.
|
||||||
hooks.tagged.register(function (c, tag) taglist_update(c.screen) end)
|
-- @param label Label function to use.
|
||||||
|
-- @param buttons A table with buttons binding to set.
|
||||||
|
function taglist.new(screen, label, buttons)
|
||||||
|
local w = {}
|
||||||
|
local data = otable()
|
||||||
|
local u = function (s)
|
||||||
|
if s == screen then
|
||||||
|
taglist_update(s, w, label, buttons, data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local uc = function (c) return u(c.screen) end
|
||||||
|
hooks.focus.register(uc)
|
||||||
|
hooks.unfocus.register(uc)
|
||||||
|
hooks.arrange.register(u)
|
||||||
|
hooks.tags.register(u)
|
||||||
|
hooks.tagged.register(uc)
|
||||||
hooks.property.register(function (c, prop)
|
hooks.property.register(function (c, prop)
|
||||||
if c.screen == scr and prop == "urgent" then
|
if prop == "urgent" then
|
||||||
taglist_update(c.screen)
|
u(c.screen)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
taglist_update(scr)
|
-- Free data on tag removal
|
||||||
|
hooks.tags.register(function (s, tag, action)
|
||||||
|
if action == "remove" then data[tag] = nil end
|
||||||
|
end)
|
||||||
|
u(screen)
|
||||||
return w
|
return w
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue