2009-02-27 17:22:03 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008-2009 Julien Danjou
|
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")
|
2017-08-10 06:28:06 +02:00
|
|
|
local gdebug = require("gears.debug")
|
2015-07-13 23:27:24 +02:00
|
|
|
local dpi = require("beautiful").xresources.apply_dpi
|
2017-08-10 06:28:06 +02:00
|
|
|
local base = require("wibox.widget.base")
|
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)
|
2018-12-27 00:30:08 +01:00
|
|
|
local is_formatted = buttons and buttons[1] and (
|
|
|
|
type(buttons[1]) == "button" or buttons[1]._is_capi_button) or false
|
|
|
|
|
2013-01-05 16:12:51 +01:00
|
|
|
if buttons then
|
|
|
|
local btns = {}
|
2018-12-27 00:30:08 +01:00
|
|
|
for _, src in ipairs(buttons) do
|
|
|
|
--TODO v6 Remove this legacy overhead
|
|
|
|
for _, b in ipairs(is_formatted and {src} or src) do
|
|
|
|
-- Create a proxy button object: it will receive the real
|
|
|
|
-- press and release events, and will propagate them to 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", object) end)
|
|
|
|
btn:connect_signal("release", function () b:emit_signal("release", object) end)
|
|
|
|
btns[#btns + 1] = btn
|
|
|
|
end
|
2013-01-05 16:12:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return btns
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-10 06:28:06 +02:00
|
|
|
local function custom_template(args)
|
|
|
|
local l = base.make_widget_from_value(args.widget_template)
|
|
|
|
|
|
|
|
-- The template system requires being able to get children elements by ids.
|
|
|
|
-- This is not optimal, but for now there is no way around it.
|
|
|
|
assert(l.get_children_by_id,"The given widget template did not result in a"..
|
|
|
|
"layout with a 'get_children_by_id' method")
|
|
|
|
|
|
|
|
return {
|
|
|
|
ib = l:get_children_by_id( "icon_role" )[1],
|
|
|
|
tb = l:get_children_by_id( "text_role" )[1],
|
|
|
|
bgb = l:get_children_by_id( "background_role" )[1],
|
|
|
|
tbm = l:get_children_by_id( "text_margin_role" )[1],
|
|
|
|
ibm = l:get_children_by_id( "icon_margin_role" )[1],
|
|
|
|
primary = l,
|
|
|
|
update_callback = l.update_callback,
|
|
|
|
create_callback = l.create_callback,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-28 18:51:13 +01:00
|
|
|
local function default_template()
|
|
|
|
return custom_template {
|
|
|
|
widget_template = {
|
|
|
|
id = 'background_role',
|
2019-03-12 19:06:50 +01:00
|
|
|
border_strategy = 'inner',
|
2019-03-28 18:51:13 +01:00
|
|
|
widget = wibox.container.background,
|
|
|
|
{
|
|
|
|
widget = wibox.layout.fixed.horizontal,
|
|
|
|
fill_space = true,
|
|
|
|
{
|
|
|
|
id = 'icon_margin_role',
|
|
|
|
widget = wibox.container.margin,
|
|
|
|
{
|
|
|
|
id = 'icon_role',
|
|
|
|
widget = wibox.widget.imagebox,
|
|
|
|
left = dpi(4),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id = 'text_margin_role',
|
|
|
|
widget = wibox.container.margin,
|
|
|
|
left = dpi(4),
|
|
|
|
right = dpi(4),
|
|
|
|
{
|
|
|
|
id = 'text_role',
|
|
|
|
widget = wibox.widget.textbox,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-07 04:20:14 +01:00
|
|
|
-- Find all the childrens (without the hierarchy) and set a property.
|
|
|
|
function common._set_common_property(widget, property, value)
|
|
|
|
if widget["set_"..property] then
|
|
|
|
widget["set_"..property](widget, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
if widget.get_children then
|
|
|
|
for _, w in ipairs(widget:get_children()) do
|
|
|
|
common._set_common_property(w, property, value)
|
|
|
|
end
|
|
|
|
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.
|
2017-08-10 06:28:06 +02:00
|
|
|
-- @tparam[opt={}] table args
|
|
|
|
function common.list_update(w, buttons, label, data, objects, args)
|
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]
|
2017-08-10 06:28:06 +02:00
|
|
|
|
|
|
|
if not cache then
|
|
|
|
cache = (args and args.widget_template) and
|
|
|
|
custom_template(args) or default_template()
|
|
|
|
|
|
|
|
cache.primary:buttons(common.create_buttons(buttons, o))
|
|
|
|
|
|
|
|
if cache.create_callback then
|
|
|
|
cache.create_callback(cache.primary, o, i, objects)
|
2019-03-07 00:30:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if args and args.create_callback then
|
|
|
|
args.create_callback(cache.primary, o, i, objects)
|
2017-08-10 06:28:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
data[o] = cache
|
|
|
|
elseif cache.update_callback then
|
|
|
|
cache.update_callback(cache.primary, o, i, objects)
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
|
2017-08-10 06:28:06 +02:00
|
|
|
local text, bg, bg_image, icon, item_args = label(o, cache.tb)
|
|
|
|
item_args = item_args or {}
|
2016-08-05 07:13:39 +02:00
|
|
|
|
2015-02-26 16:05:47 +01:00
|
|
|
-- The text might be invalid, so use pcall.
|
2017-08-10 06:28:06 +02:00
|
|
|
if cache.tbm and (text == nil or text == "") then
|
|
|
|
cache.tbm:set_margins(0)
|
|
|
|
elseif cache.tb then
|
|
|
|
if not cache.tb:set_markup_silently(text) then
|
|
|
|
cache.tb:set_markup("<i><Invalid text></i>")
|
2015-01-26 17:50:15 +01:00
|
|
|
end
|
2011-03-18 17:28:38 +01:00
|
|
|
end
|
2017-08-10 06:28:06 +02:00
|
|
|
|
|
|
|
if cache.bgb then
|
|
|
|
cache.bgb:set_bg(bg)
|
|
|
|
|
|
|
|
--TODO v5 remove this if, it existed only for a removed and
|
|
|
|
-- undocumented API
|
|
|
|
if type(bg_image) ~= "function" then
|
|
|
|
cache.bgb:set_bgimage(bg_image)
|
|
|
|
else
|
|
|
|
gdebug.deprecate("If you read this, you used an undocumented API"..
|
|
|
|
" which has been replaced by the new awful.widget.common "..
|
|
|
|
"templating system, please migrate now. This feature is "..
|
|
|
|
"already staged for removal", {
|
|
|
|
deprecated_in = 4
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-04-05 22:52:01 +02:00
|
|
|
cache.bgb.shape = item_args.shape
|
|
|
|
cache.bgb.border_width = item_args.shape_border_width
|
|
|
|
cache.bgb.border_color = item_args.shape_border_color
|
2017-08-10 06:28:06 +02:00
|
|
|
|
2015-07-13 23:27:24 +02:00
|
|
|
end
|
2016-08-05 07:13:39 +02:00
|
|
|
|
2017-08-10 06:28:06 +02:00
|
|
|
if cache.ib and icon then
|
|
|
|
cache.ib:set_image(icon)
|
|
|
|
elseif cache.ibm then
|
|
|
|
cache.ibm:set_margins(0)
|
|
|
|
end
|
2016-08-05 07:13:39 +02:00
|
|
|
|
2019-03-06 20:06:11 +01:00
|
|
|
if item_args.icon_size and cache.ib then
|
|
|
|
cache.ib.forced_height = item_args.icon_size
|
|
|
|
cache.ib.forced_width = item_args.icon_size
|
|
|
|
elseif cache.ib then
|
|
|
|
cache.ib.forced_height = nil
|
|
|
|
cache.ib.forced_width = nil
|
|
|
|
end
|
|
|
|
|
2018-01-13 03:32:51 +01:00
|
|
|
w:add(cache.primary)
|
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
|