2009-02-27 17:22:03 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @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
|
2009-10-24 14:53:01 +02:00
|
|
|
local pairs = pairs
|
2011-03-18 17:28:38 +01:00
|
|
|
local pcall = pcall
|
2009-04-11 14:13:20 +02:00
|
|
|
local setmetatable = setmetatable
|
2010-10-06 14:23:36 +02:00
|
|
|
local capi = { button = button }
|
2009-10-24 14:53:01 +02:00
|
|
|
local util = require("awful.util")
|
2012-06-12 15:29:52 +02:00
|
|
|
local wibox = require("wibox")
|
2010-10-06 14:23:36 +02:00
|
|
|
local imagebox = require("wibox.widget.imagebox")
|
|
|
|
local textbox = require("wibox.widget.textbox")
|
2009-02-27 17:22:03 +01:00
|
|
|
|
2012-11-19 15:35:32 +01:00
|
|
|
--- Common utilities for awful widgets
|
2009-10-24 14:53:01 +02:00
|
|
|
local common = {}
|
2009-02-27 17:22:03 +01:00
|
|
|
|
2009-10-24 14:53:01 +02:00
|
|
|
-- 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
|
|
|
|
|
2010-10-06 14:23:36 +02:00
|
|
|
function common.list_update(w, buttons, label, data, objects)
|
2009-10-24 14:53:01 +02:00
|
|
|
-- update the widgets, creating them if needed
|
2010-10-06 14:23:36 +02:00
|
|
|
w:reset()
|
2009-10-24 14:53:01 +02:00
|
|
|
for i, o in ipairs(objects) do
|
2011-10-04 13:55:59 +02:00
|
|
|
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()
|
2010-10-06 18:45:50 +02:00
|
|
|
|
2011-10-04 13:55:59 +02:00
|
|
|
-- All of this is added in a fixed widget
|
|
|
|
l:fill_space(true)
|
|
|
|
l:add(ib)
|
|
|
|
l:add(m)
|
2010-10-06 18:45:50 +02:00
|
|
|
|
2011-10-04 13:55:59 +02:00
|
|
|
-- And all of this gets a background
|
|
|
|
bgb:set_widget(l)
|
2009-10-24 14:53:01 +02:00
|
|
|
|
2011-10-04 13:55:59 +02:00
|
|
|
if buttons then
|
|
|
|
local btns = {}
|
2009-02-27 17:22:03 +01:00
|
|
|
for kb, b in ipairs(buttons) do
|
2009-07-09 12:46:06 +02:00
|
|
|
-- 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 }
|
2009-10-09 20:39:55 +02:00
|
|
|
btn:connect_signal("press", function () b:emit_signal("press", o) end)
|
|
|
|
btn:connect_signal("release", function () b:emit_signal("release", o) end)
|
2010-09-16 19:02:15 +02:00
|
|
|
btns[#btns + 1] = btn
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
2011-10-04 13:55:59 +02:00
|
|
|
bgb:buttons(btns)
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
2011-10-04 13:55:59 +02:00
|
|
|
|
|
|
|
data[o] = {
|
|
|
|
ib = ib,
|
|
|
|
tb = tb,
|
|
|
|
bgb = bgb
|
|
|
|
}
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local text, bg, bg_image, icon = label(o)
|
2011-03-18 17:28:38 +01:00
|
|
|
-- The text might be invalid, so use pcall
|
|
|
|
if not pcall(tb.set_markup, tb, text) then
|
|
|
|
tb:set_markup("<i><Invalid text></i>")
|
|
|
|
end
|
2010-10-06 14:23:36 +02:00
|
|
|
bgb:set_bg(bg)
|
|
|
|
bgb:set_bgimage(bg_image)
|
|
|
|
ib:set_image(icon)
|
2011-10-04 13:55:59 +02:00
|
|
|
w:add(bgb)
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-24 14:53:01 +02:00
|
|
|
return common
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|