ta{g,sk}list: Cache widgets again

Instead of re-creating all the widgets for the list entries on each update, this
code now caches its widgets again and only creates new widgets when a new
client/tag appears.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2011-10-04 13:55:59 +02:00
parent a9bedf39d8
commit 7a6d49999a
3 changed files with 30 additions and 34 deletions

View File

@ -35,26 +35,21 @@ local function replace_in_template(t, ib, tb)
end end
function common.list_update(w, buttons, label, data, objects) function common.list_update(w, buttons, label, data, objects)
-- 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)
-- Remove excessive widgets
if len > #objects then
for i = #objects, len do
w[i] = nil
end
end
-- update the widgets, creating them if needed -- update the widgets, creating them if needed
w:reset() w:reset()
for i, o in ipairs(objects) do for i, o in ipairs(objects) do
local ib = wibox.widget.imagebox() local cache = data[o]
local tb = wibox.widget.textbox() local ib, tb, bgb, m, l
local bgb = wibox.widget.background() if cache then
local m = wibox.layout.margin(tb, 4, 4) ib = cache.ib
local l = wibox.layout.fixed.horizontal() tb = cache.tb
bgb = cache.bgb
else
ib = wibox.widget.imagebox()
tb = wibox.widget.textbox()
bgb = wibox.widget.background()
m = wibox.layout.margin(tb, 4, 4)
l = wibox.layout.fixed.horizontal()
-- All of this is added in a fixed widget -- All of this is added in a fixed widget
l:fill_space(true) l:fill_space(true)
@ -63,15 +58,9 @@ function common.list_update(w, buttons, label, data, objects)
-- And all of this gets a background -- And all of this gets a background
bgb:set_widget(l) bgb:set_widget(l)
w:add(bgb)
if buttons then if buttons then
-- Use a local variable so that the garbage collector doesn't strike local btns = {}
-- between now and the :buttons() call.
local btns = data[o]
if not btns then
btns = {}
data[o] = btns
for kb, b in ipairs(buttons) do for kb, b in ipairs(buttons) do
-- Create a proxy button object: it will receive the real -- Create a proxy button object: it will receive the real
-- press and release events, and will propagate them the the -- press and release events, and will propagate them the the
@ -82,10 +71,16 @@ function common.list_update(w, buttons, label, data, objects)
btn:connect_signal("release", function () b:emit_signal("release", o) end) btn:connect_signal("release", function () b:emit_signal("release", o) end)
btns[#btns + 1] = btn btns[#btns + 1] = btn
end end
end
bgb:buttons(btns) bgb:buttons(btns)
end end
data[o] = {
ib = ib,
tb = tb,
bgb = bgb
}
end
local text, bg, bg_image, icon = label(o) local text, bg, bg_image, icon = label(o)
-- The text might be invalid, so use pcall -- The text might be invalid, so use pcall
if not pcall(tb.set_markup, tb, text) then if not pcall(tb.set_markup, tb, text) then
@ -94,6 +89,7 @@ function common.list_update(w, buttons, label, data, objects)
bgb:set_bg(bg) bgb:set_bg(bg)
bgb:set_bgimage(bg_image) bgb:set_bgimage(bg_image)
ib:set_image(icon) ib:set_image(icon)
w:add(bgb)
end end
end end

View File

@ -129,7 +129,7 @@ end
function new(screen, filter, buttons, style) function new(screen, filter, buttons, style)
local w = fixed.horizontal() local w = fixed.horizontal()
local data = setmetatable({}, { __mode = 'vk' }) local data = setmetatable({}, { __mode = 'k' })
local u = function (s) local u = function (s)
if s == screen then if s == screen then
taglist_update(s, w, buttons, filter, data, style) taglist_update(s, w, buttons, filter, data, style)

View File

@ -96,7 +96,7 @@ end
function new(screen, filter, buttons, style) function new(screen, filter, buttons, style)
local w = flex.horizontal() local w = flex.horizontal()
local data = setmetatable({}, { __mode = 'kv' }) local data = setmetatable({}, { __mode = 'k' })
local u = function () tasklist_update(screen, w, buttons, filter, data, style) end local u = function () tasklist_update(screen, w, buttons, filter, data, style) end
capi.screen[screen]:connect_signal("tag::detach", u) capi.screen[screen]:connect_signal("tag::detach", u)
tag.attached_connect_signal(screen, "property::selected", u) tag.attached_connect_signal(screen, "property::selected", u)