I'm trying to create my own widgets that inherit from a widget instance constructed using make_widget_declarative
, so I can add more functionality and a state in a more or less OOP fashion. Here's my attempt:
local wibox = require("wibox")
local MyWidget = {
mt = {}
}
function MyWidget.new()
local o = {}
local w = wibox.widget.base.make_widget_declarative {
-- Widget declaration here
}
-- Multiple inheritance
-- Look for a value in the MyWidget "class" and fall back to the widget instance
local pseudo_class = {
__index = function(t, k)
local v = MyWidget[k]
if v then return v end
return w[k]
end
}
setmetatable(o, pseudo_class)
return o
end
function MyWidget:do_something()
-- Self is the local o table and not the widget
local children = self:get_children_by_id("some_child")
end
-- Setup "constructor"
function MyWidget.mt:__call(...)
return MyWidget.new(...)
end
return setmetatable(MyWidget, MyWidget.mt)
It works for the most part, except when calling the do_something
function, where it correctly resolves the self:get_children_by_id
method but still fails with an
attempt to index a nil value
error. I suspect it is because self
is the local o
table and not the actual widget instance and thus it can't resolve other internal stuff, but shouldn't that be covered by the __index
in the pseudo_class
?
What am I missing? Has this been attempted before?