How to read from code if dark/light theme in system was used

I have a problem, because in my code I have hardcoded dark colours and in Linux Mint with light theme it works bad.

Why do you even have colors hardcoded?

I need to use in ListStore a little darker colour for headers ( 2 x 140318 in picture )
This is how it looks in dark theme

Is there any way to get these results without using hardcoded values?

You can use CSS in order to make sure it stays readable if that’s what you’re asking.

That said, I think it’s important to reiterate what the previous response was trying to get across : This is a very BAD idea. You should simply respect the users theme choice.

Also, it probably looks terrible and isn’t functional with a lot of themes not just that one.

But you do you.

I would suggest a tree view with child levels: your dark “headers” could be nodes which you expand/collapse to see/hide their children.

I think that the best solution is to get main color theme like green(#33ff9d) and substract from it e.g. #222222 to get darker color #11dd7b, because this will work with all themes.

But how can I get this color?

I checked, and seems that only 2 states I need to handle - white and dark themes, but still I don’t know how to handle this especially with running app(like native wigdets do)

1 Like

You can’t query the theme for colors, because CSS does not work that way. CSS states can contain solid colors, images, gradients, or even stack of different assets; additionally, CSS states, as the name implies, cascade through the widgets hierarchy, which means that the end result you see on the screen is the sum total of states across the widget and style classes trees. Querying colors on a specific widget is just not possible.

The way widgets work is by using the gtk_render_* API to render the current CSS state, which is taken from the theme; that state is internal to GTK.

Basically: what you want to do is simply not possible programmatically. The only thing you can do is to support a set of themes, and observe the GtkSettings:gtk-theme-name property; whenever that property changes, you can load a specific CSS fragment matching the behaviour of that theme.


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