awesome/lib/awful/widget/common.lua.in

89 lines
3.1 KiB
Lua
Raw Normal View History

---------------------------------------------------------------------------
-- @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 setmetatable = setmetatable
local capi = { widget = widget }
local abutton = require("awful.button")
--- Common widget code
module("awful.widget.common")
-- Private structures
tagwidgets = setmetatable({}, { __mode = 'k' })
function list_update(w, buttons, label, data, widgets, 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) / 2
-- Add more widgets
if len < #objects then
for i = len * 2 + 1, #objects * 2, 2 do
local ib = capi.widget({ type = "imagebox", align = widgets.imagebox.align })
local tb = capi.widget({ type = "textbox", align = widgets.textbox.align })
w[i] = ib
w[i + 1] = tb
w[i + 1]:margin({ left = widgets.textbox.margin.left, right = widgets.textbox.margin.right })
w[i + 1].bg_resize = widgets.textbox.bg_resize or false
w[i + 1].bg_align = widgets.textbox.bg_align or ""
if type(objects[math.floor(i / 2) + 1]) == "tag" then
tagwidgets[ib] = objects[math.floor(i / 2) + 1]
tagwidgets[tb] = objects[math.floor(i / 2) + 1]
end
end
-- Remove widgets
elseif len > #objects then
for i = #objects * 2 + 1, len * 2, 2 do
w[i] = nil
w[i + 1] = nil
end
end
-- update widgets text
for k = 1, #objects * 2, 2 do
local o = objects[(k + 1) / 2]
if buttons then
if not data[o] then
data[o] = { }
-- Replace press function by a new one calling with object as
-- argument
for kb, b in ipairs(buttons) do
-- Copy object
local bpress, brelease = b.press, b.release
local press, release
if bpress then press = function() bpress(o) end end
if brelease then release = function () brelease(o) end end
data[o][kb] = abutton(b.modifiers, b.button, press, release)
end
end
w[k]:buttons(data[o])
w[k + 1]:buttons(data[o])
end
local text, bg, bg_image, icon = label(o)
w[k + 1].text, w[k + 1].bg, w[k + 1].bg_image = text, bg, bg_image
w[k].bg, w[k].image = bg, icon
if not w[k + 1].text then
w[k+1].visible = false
else
w[k+1].visible = true
end
if not w[k].image then
w[k].visible = false
else
w[k].visible = true
end
end
end
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80