I am trying to port an older (thankfully, this one is GTK 3-based) application to a more declarative UI based on GtkBuilder. Is there a way to take a snapshot of the widget tree (e.g. in Inspector) and export it as ui file so I do not have to rebuild the whole UI manually? I understand that it would be lossy just like copying the HTML elements in web browser’s inspector does not capture all DOM properties but it would still be useful as a starter point.
No, the widget tree is not serialisable.
It might not be serializable but it is still introspectable. Looking at the inspector, GTK 4 handles it using gtk_widget_observe_children
and GTK 3 through gtk_container_forall
. I will try to cobble something up when I switch back to the project. Would a tool like this be accepted in GTK inspector or should I just temporarily add it to application code?
Somewhat. The deserialisation process that goes from XML to a widget tree is not a reversible process because any GtkBuildable
implementation may have custom parsers. Additionally, properties may be construct-only and write-only, so you can’t really serialise them properly.
You can see just about the amount of edge cases are present in the defaultvalue
test. This is just for GTK’s own types: as soon as you throw in code defined in other libraries or applications, you’re going to be hitting a lot more cases.
No: a simple “traverse the widget tree and dump its state” isn’t going to be accepted as is, I’m sorry. It’s going to do more harm than good.
If you want to do it for your own application, you’re free to do so, because you control the environment a lot more than the toolkit possibly can.
Maybe one day we’ll have a proper serialisation/deserialisation API in GObject, and we’ll be able to implement this functionality in GTK.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.