Open RossComputerGuy opened 4 years ago
I have no idea about this code (CC @TingPing do you happen to have some more clue here?).
Random comment that might or might not be helpful: The example in https://github.com/pavouk/lgi/blob/master/docs/guide.md#382-installing-new-properties also defines a function in a _property_set
table.
By default, the value of the property is mirrored in priv table of the instance. However, it is possible to specify custom getter and setter method in _property_set and _property_get tables. Both approaches are illustrated in the following example with property called my_label
Adding a function to _property_set
is not needed.
@RossComputerGuy I tried to do what the LGI documentation says and it didn't work. However, there is still another way to do this, by doing it just as it is done in C:
local lgi = require('lgi')
local Gtk = lgi.Gtk
local GObject = lgi.GObject
local Expidus = lgi.package('Expidus')
Expidus:class('AppButton', Gtk.FlowBoxChild)
function Expidus.AppButton:_class_init(klass)
function klass:set_property(id, value, pspec)
if id == 1 then
self.priv.label = value:get_string()
elseif id == 2 then
self.priv.icon = value:get_string()
else
GObject.OBJECT_WARN_INVALID_PROPERTY_ID(self, id, pspec)
end
end
function klass:get_property(id, value, pspec)
if id == 1 then
value:set_string(self.priv.label)
elseif id == 2 then
value:set_string(self.priv.icon)
else
GObject.OBJECT_WARN_INVALID_PROPERTY_ID(self, id, pspec)
end
end
klass:install_property(1, GObject.ParamSpecString(
"label", "Label", "Application label", "App",
{ 'READWRITE', 'CONSTRUCT' }
)
)
klass:install_property(2, GObject.ParamSpecString(
"icon", "Icon", "Application icon", "app-icon-unknown",
{ 'READWRITE', 'CONSTRUCT' }
)
)
end
local btn = Expidus.AppButton{
label = "ExpBtn",
icon = "go-home-symbolic"
}
print(btn.label, btn.icon)
-- Default values:
print(Expidus.AppButton().label)
print(Expidus.AppButton().icon)
Note that with this method it is mandatory to assign the functions set_property
and get_property
.
Please, close this if no activity or is solved
@M1que4s but we need it fixed
@RossComputerGuy I left an answer with a solution to the problem above
I tried to do what the LGI documentation says and it didn't work. However, there is still another way to do this, by doing it just as it is done in C
This sounds more like a temporary fix until this issue is fixed.
@RossComputerGuy The way in which properties are created with LGI is specific to the library. On the other hand, the way I have done it is the way it is done according to GObject, that is, more C-style.
I wouldn't say it's a temporary solution
Well the way it is documented should work because its documented as a feature.
@RossComputerGuy Here you can see how subclassing is done with GObject in C: https://developer.gnome.org/SubclassGObject/
Well the way it is documented should work because its documented as a feature.
Yes, you are right. However, I'm honestly not sure why it doesn't work
@RossComputerGuy Anyway, I hope my comment was helpful
My custom class: https://github.com/ExpidusOS/shell/blob/master/share/expidus-shell/expidus/widgets/app-button.lua
Code that calls my class: https://github.com/ExpidusOS/shell/blob/master/share/expidus-shell/expidus/launcher.lua
Result:
Note that I've reduced the calling code down so I don't have the entire file pasted.