Using class methods like gtk_widget_class_get_css_name from gjs

I’m writing a gjs app in Typescript. At one point I wanted to use gtk_widget_class_get_css_name, but couldn’t find a way how. Although I’ve found another way to achieve the styling I wanted, I’m still curious about this.

In the gir XML file the function is a method of Gtk.WidgetClass which is a record. However, it has the attribute glib:is-gtype-struct-for which seems to mean it shouldn’t be made available to bindings, at least not gjs. ts-for-gjs doesn’t export records with this attribute, and if I try plain Javascript there is no WidgetClass in imports.gi.Gtk. There is no widget_class_get_css_name either. Is there some way I can access the function without writing some sort of shim in C?

I’ve noticed GTK4 has a corresponding instance method, gtk_widget_get_css_name, so I’d be able to use that, but I want to stick to GTK3 for now, so I can easily do most of the development on my favourite laptop which happens to be a Mac.

    const name = Gtk.Widget.get_css_name.call(widget);

That does seem to work, thanks. I should update ts-for-gjs to output this sort of method. But I’m puzzled, because the original function takes a GObject class as its argument, but here we’re passing an instance. How does that work? Does gjs perform the conversion when necessary?

I note I can also call it with a JS prototype of a GObject class: Gtk.Widget.get_css_name.call(Gtk.Box); (but not Gtk.Box.get_css_name();).

These are actually supposed to take a GType object, but we have some code that converts both instances and constructors to their GType: https://gitlab.gnome.org/GNOME/gjs/-/blob/a4d40792f6b39772197137d7737b3f4daf2022f0/gi/gtype.cpp#L152-200
(It’s debatable whether that was a good idea, but here we are.)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.