100 lines
3.2 KiB
Lua
100 lines
3.2 KiB
Lua
---------------------------------------------------------------------------
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
-- @copyright 2008-2009 Julien Danjou
|
|
-- @release @AWESOME_VERSION@
|
|
---------------------------------------------------------------------------
|
|
|
|
-- Grab environment we need
|
|
local math = math
|
|
local type = type
|
|
local ipairs = ipairs
|
|
local pairs = pairs
|
|
local setmetatable = setmetatable
|
|
local capi = { button = button }
|
|
local layout = require("awful.widget.layout")
|
|
local util = require("awful.util")
|
|
local imagebox = require("wibox.widget.imagebox")
|
|
local textbox = require("wibox.widget.textbox")
|
|
|
|
local common = {}
|
|
|
|
-- Recursively processes a template, replacing the tables representing the icon and
|
|
-- the title with the widgets ib and tb
|
|
local function replace_in_template(t, ib, tb)
|
|
for i, v in ipairs(t) do
|
|
if type(t[i]) == "table" then
|
|
if v.item == "icon" then
|
|
t[i] = ib
|
|
elseif v.item == "title" then
|
|
t[i] = tb
|
|
else
|
|
replace_in_template(v, ib, tb)
|
|
end
|
|
end
|
|
end
|
|
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, tb, bgb, l
|
|
ib = wibox.widget.imagebox()
|
|
tb = wibox.widget.textbox()
|
|
bgb = wibox.widget.background()
|
|
local m = wibox.layout.margin()
|
|
m:set_left(4)
|
|
m:set_right(4)
|
|
m:set_widget(tb)
|
|
bgb:set_widget(m)
|
|
l = wibox.layout.fixed.horizontal()
|
|
l:fill_space(true)
|
|
l:add(ib)
|
|
l:add(bgb)
|
|
w:add(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
|
|
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
|
|
-- button object the user provided, but with the object as
|
|
-- argument.
|
|
local btn = capi.button { modifiers = b.modifiers, button = b.button }
|
|
btn:connect_signal("press", function () b:emit_signal("press", o) end)
|
|
btn:connect_signal("release", function () b:emit_signal("release", o) end)
|
|
btns[#btns + 1] = btn
|
|
end
|
|
end
|
|
l:buttons(btns)
|
|
end
|
|
|
|
local text, bg, bg_image, icon = label(o)
|
|
tb:set_markup(text)
|
|
bgb:set_bg(bg)
|
|
bgb:set_bgimage(bg_image)
|
|
ib:set_image(icon)
|
|
end
|
|
end
|
|
|
|
return common
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|