2009-02-27 17:22:03 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008-2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
2015-02-26 16:05:47 +01:00
|
|
|
-- @classmod awful.widget.common
|
2009-02-27 17:22:03 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local type = type
|
|
|
|
local ipairs = ipairs
|
2010-10-06 14:23:36 +02:00
|
|
|
local capi = { button = button }
|
2012-06-12 15:29:52 +02:00
|
|
|
local wibox = require("wibox")
|
2015-07-13 23:27:24 +02:00
|
|
|
local dpi = require("beautiful").xresources.apply_dpi
|
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
|
|
|
|
2015-02-26 16:05:47 +01:00
|
|
|
--- Common method to create buttons.
|
|
|
|
-- @tab buttons
|
|
|
|
-- @param object
|
|
|
|
-- @treturn table
|
2013-01-05 16:12:51 +01:00
|
|
|
function common.create_buttons(buttons, object)
|
|
|
|
if buttons then
|
|
|
|
local btns = {}
|
2016-02-07 15:12:22 +01:00
|
|
|
for _, b in ipairs(buttons) do
|
2013-01-05 16:12:51 +01:00
|
|
|
-- Create a proxy button object: it will receive the real
|
2016-03-06 14:46:13 +01:00
|
|
|
-- press and release events, and will propagate them to the
|
2013-01-05 16:12:51 +01:00
|
|
|
-- 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", object) end)
|
|
|
|
btn:connect_signal("release", function () b:emit_signal("release", object) end)
|
|
|
|
btns[#btns + 1] = btn
|
|
|
|
end
|
|
|
|
|
|
|
|
return btns
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-26 16:05:47 +01:00
|
|
|
--- Common update method.
|
|
|
|
-- @param w The widget.
|
|
|
|
-- @tab buttons
|
|
|
|
-- @func label Function to generate label parameters from an object.
|
|
|
|
-- The function gets passed an object from `objects`, and
|
|
|
|
-- has to return `text`, `bg`, `bg_image`, `icon`.
|
|
|
|
-- @tab data Current data/cache, indexed by objects.
|
|
|
|
-- @tab objects Objects to be displayed / updated.
|
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]
|
2015-07-13 23:27:24 +02:00
|
|
|
local ib, tb, bgb, tbm, ibm, l
|
2011-10-04 13:55:59 +02:00
|
|
|
if cache then
|
|
|
|
ib = cache.ib
|
|
|
|
tb = cache.tb
|
|
|
|
bgb = cache.bgb
|
2015-07-13 23:27:24 +02:00
|
|
|
tbm = cache.tbm
|
|
|
|
ibm = cache.ibm
|
2011-10-04 13:55:59 +02:00
|
|
|
else
|
|
|
|
ib = wibox.widget.imagebox()
|
|
|
|
tb = wibox.widget.textbox()
|
2016-05-23 05:56:45 +02:00
|
|
|
bgb = wibox.container.background()
|
|
|
|
tbm = wibox.container.margin(tb, dpi(4), dpi(4))
|
|
|
|
ibm = wibox.container.margin(ib, dpi(4))
|
2011-10-04 13:55:59 +02:00
|
|
|
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)
|
2015-07-13 23:27:24 +02:00
|
|
|
l:add(ibm)
|
|
|
|
l:add(tbm)
|
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
|
|
|
|
2013-01-05 16:12:51 +01:00
|
|
|
bgb:buttons(common.create_buttons(buttons, o))
|
2011-10-04 13:55:59 +02:00
|
|
|
|
|
|
|
data[o] = {
|
2015-07-13 23:27:24 +02:00
|
|
|
ib = ib,
|
|
|
|
tb = tb,
|
2013-01-04 02:54:40 +01:00
|
|
|
bgb = bgb,
|
2015-07-13 23:27:24 +02:00
|
|
|
tbm = tbm,
|
|
|
|
ibm = ibm,
|
2011-10-04 13:55:59 +02:00
|
|
|
}
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
|
2015-07-13 23:47:37 +02:00
|
|
|
local text, bg, bg_image, icon = label(o, tb)
|
2015-02-26 16:05:47 +01:00
|
|
|
-- The text might be invalid, so use pcall.
|
2015-01-26 17:50:15 +01:00
|
|
|
if text == nil or text == "" then
|
2015-07-13 23:27:24 +02:00
|
|
|
tbm:set_margins(0)
|
2015-01-26 17:50:15 +01:00
|
|
|
else
|
2016-01-17 16:50:03 +01:00
|
|
|
if not tb:set_markup_silently(text) then
|
2015-01-26 17:50:15 +01:00
|
|
|
tb:set_markup("<i><Invalid text></i>")
|
|
|
|
end
|
2011-03-18 17:28:38 +01:00
|
|
|
end
|
2010-10-06 14:23:36 +02:00
|
|
|
bgb:set_bg(bg)
|
2013-01-04 02:54:40 +01:00
|
|
|
if type(bg_image) == "function" then
|
2016-02-07 15:11:12 +01:00
|
|
|
-- TODO: Why does this pass nil as an argument?
|
|
|
|
bg_image = bg_image(tb,o,nil,objects,i)
|
2013-01-04 02:54:40 +01:00
|
|
|
end
|
2010-10-06 14:23:36 +02:00
|
|
|
bgb:set_bgimage(bg_image)
|
2015-07-13 23:27:24 +02:00
|
|
|
if icon then
|
|
|
|
ib:set_image(icon)
|
|
|
|
else
|
|
|
|
ibm:set_margins(0)
|
|
|
|
end
|
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
|