wibox.widget.base: add __tostring method to widgets

Fixes https://github.com/awesomeWM/awesome/issues/337.
Closes https://github.com/awesomeWM/awesome/pull/341.
This commit is contained in:
Daniel Hahler 2015-07-23 21:42:54 +02:00
parent 705f965915
commit 68ad2529a4
2 changed files with 25 additions and 2 deletions

View File

@ -112,6 +112,21 @@ function object.mt:__call(...)
return new(...)
end
--- Helper function to get the module name out of `debug.getinfo`.
-- @usage
-- local mt = {}
-- mt.__tostring = function(o)
-- return require("gears.object").modulename(2)
-- end
-- return setmetatable(ret, mt)
--
-- @tparam[opt=2] integer level Level for `debug.getinfo(level, "S")`.
-- Typically 2 or 3.
-- @treturn string The module name, e.g. "wibox.widget.background".
function object.modulename(level)
return debug.getinfo(level, "S").source:gsub(".*/lib/", ""):gsub("/", "."):gsub("%.lua", "")
end
return setmetatable(object, object.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -64,7 +64,9 @@ end
-- that the needed signals are added and mouse input handling is set up.
-- @param proxy If this is set, the returned widget will be a proxy for this
-- widget. It will be equivalent to this widget.
function base.make_widget(proxy)
-- @tparam[opt] string widget_name Name of the widget. If not set, it will be
-- set automatically via `gears.object.modulename`.
function base.make_widget(proxy, widget_name)
local ret = object()
-- This signal is used by layouts to find out when they have to update.
@ -101,7 +103,13 @@ function base.make_widget(proxy)
ret._fit_geometry_cache = setmetatable({}, { __mode = 'v' })
end)
return ret
-- Add __tostring method to metatable.
ret.widget_name = widget_name or object.modulename(3)
local mt = {}
mt.__tostring = function(o)
return ret.widget_name
end
return setmetatable(ret, mt)
end
--- Generate an empty widget which takes no space and displays nothing