Merge pull request #2012 from Elv13/doc_tests_and_notif_p1_2

widget: Add a flexible generic constructor
This commit is contained in:
Daniel Hahler 2017-10-21 19:26:49 +02:00 committed by GitHub
commit c362524a5f
1 changed files with 28 additions and 0 deletions

View File

@ -584,6 +584,34 @@ function base.make_widget_declarative(args)
return setmetatable(w, mt) return setmetatable(w, mt)
end end
--- Create a widget from an undetermined value.
--
-- The value can be:
--
-- * A widget (in which case nothing new is created)
-- * A declarative construct
-- * A constructor function
-- * A metaobject
--
-- @param wdg The value.
-- @param[opt=nil] ... Arguments passed to the contructor (if any).
-- @treturn The new widget.
function base.make_widget_from_value(wdg, ...)
local is_table = type(wdg) == "table"
local is_function = ((not is_table) and type(wdg) == "function")
or (is_table and getmetatable(wdg) and getmetatable(wdg).__call)
if is_function then
wdg = wdg(...)
elseif is_table and not wdg.is_widget then
wdg = base.make_widget_declarative(wdg)
else
assert(wdg.is_widget, "The argument is not a function, table, or widget.")
end
return wdg
end
--- Create an empty widget skeleton. --- Create an empty widget skeleton.
-- --
-- See [Creating new widgets](../documentation/04-new-widget.md.html). -- See [Creating new widgets](../documentation/04-new-widget.md.html).