From 7a6d49999a881811c4d71ac10b7c150acac66d01 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 4 Oct 2011 13:55:59 +0200 Subject: [PATCH] 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 --- lib/awful/widget/common.lua.in | 60 +++++++++++++++----------------- lib/awful/widget/taglist.lua.in | 2 +- lib/awful/widget/tasklist.lua.in | 2 +- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lib/awful/widget/common.lua.in b/lib/awful/widget/common.lua.in index 21e08938..f7e008ad 100644 --- a/lib/awful/widget/common.lua.in +++ b/lib/awful/widget/common.lua.in @@ -35,43 +35,32 @@ local function replace_in_template(t, ib, tb) end 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 w:reset() for i, o in ipairs(objects) do - local ib = wibox.widget.imagebox() - local tb = wibox.widget.textbox() - local bgb = wibox.widget.background() - local m = wibox.layout.margin(tb, 4, 4) - local l = wibox.layout.fixed.horizontal() + local cache = data[o] + local ib, tb, bgb, m, l + if cache then + ib = cache.ib + 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 - l:fill_space(true) - l:add(ib) - l:add(m) + -- All of this is added in a fixed widget + l:fill_space(true) + l:add(ib) + l:add(m) - -- And all of this gets a background - bgb:set_widget(l) - w:add(bgb) + -- And all of this gets a background + bgb:set_widget(l) - if buttons then - -- Use a local variable so that the garbage collector doesn't strike - -- between now and the :buttons() call. - local btns = data[o] - if not btns then - btns = {} - data[o] = btns + if buttons then + local btns = {} for kb, b in ipairs(buttons) do -- Create a proxy button object: it will receive the real -- press and release events, and will propagate them the the @@ -82,8 +71,14 @@ function common.list_update(w, buttons, label, data, objects) btn:connect_signal("release", function () b:emit_signal("release", o) end) btns[#btns + 1] = btn end + bgb:buttons(btns) end - bgb:buttons(btns) + + data[o] = { + ib = ib, + tb = tb, + bgb = bgb + } end local text, bg, bg_image, icon = label(o) @@ -94,6 +89,7 @@ function common.list_update(w, buttons, label, data, objects) bgb:set_bg(bg) bgb:set_bgimage(bg_image) ib:set_image(icon) + w:add(bgb) end end diff --git a/lib/awful/widget/taglist.lua.in b/lib/awful/widget/taglist.lua.in index 3d7fcdd5..c414c2a5 100644 --- a/lib/awful/widget/taglist.lua.in +++ b/lib/awful/widget/taglist.lua.in @@ -129,7 +129,7 @@ end function new(screen, filter, buttons, style) local w = fixed.horizontal() - local data = setmetatable({}, { __mode = 'vk' }) + local data = setmetatable({}, { __mode = 'k' }) local u = function (s) if s == screen then taglist_update(s, w, buttons, filter, data, style) diff --git a/lib/awful/widget/tasklist.lua.in b/lib/awful/widget/tasklist.lua.in index 19f9fa9d..e0135d44 100644 --- a/lib/awful/widget/tasklist.lua.in +++ b/lib/awful/widget/tasklist.lua.in @@ -96,7 +96,7 @@ end function new(screen, filter, buttons, style) 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 capi.screen[screen]:connect_signal("tag::detach", u) tag.attached_connect_signal(screen, "property::selected", u)