Can we build a reusable UI configuration structure in a worker thread and apply it on the main thread?

Hello,

I’m developing a GTK3 desktop application in C, and I’m working with a multithreaded design.

The idea is to gather and prepare all UI-related properties (such as font, color, size, margins, etc.) in a background (worker) thread, and then apply them to actual GTK widgets on the main (UI) thread

My question is:

Is there a recommended pattern in GTK3 where we can define a reusable structure or configuration (not involving actual GTK widgets) that holds all UI control properties, and then apply that structure to a widget in one go on the main thread?

This would be similar in spirit to how UIKit’s UIButtonConfiguration works — where a complete set of UI properties is constructed separately from the control and applied later.

You can generate a GtkBuilder XML string plus a CSS string in a separate thread (consider using GTask for that). Once the strings are constructed, you can give it to GTK on the main thread.

Edit: however I don’t know any GTK apps doing that, and it doesn’t make much sense to me. What problem are you trying to solve?

Thank you, this helps.

As a widget can have a very big list of properties (100+), I was exploring an option to avoid writing those many setters in main thread but write it in worker thread and consume it in 1 go in main thread.

If you have a huge list of things to display, avoid building a big UI at once, instead try to implement some kind of progressive generation, creating elements only when they have to be shown in the visible area.
(only build the data model in the thread)

In gtk4, the new list widgets GtkListView and GtkGridView work that way, you can look at the code how it’s done.

1 Like