How to make GTK icons dark/light theme-aware

Background
My application uses custom icons (pixbuf) on buttons. When the theme is a bright theme, everything looks fine, but when the theme is a dark theme, the icons are pretty useless.

Question
Is there a possibility to tell my application to use a different set of icons depending of the theme is bright or dark?

Perhaps by proper naming, like myicon.png and myicon-dark.png?

No, have to do that yourself. But you can use symbolic icons instead, which will take the foreground color fo the theme.

Thank you for the information.

have to do that yourself

Are there any proper means (not hacks) to decide if the theme is dark or light?

OT
Is there anything in the making in this direction(theme brightness-sensing or even automatic application)?
I could imagine this could be a really helpful feature given the current rise of night/dark modes and the distribution of users of light and night themes.

No

But because I need 20 characters, let me tell you how nice the weather is today.

2 Likes

Do you have a link for more information how-to?

I tried gtk_image_new_from_gicon (g_file_icon_new ( g_file_new_for_path (PIXMAPDIR"/myicon-symbolic.svg")), 32);, but it does not adapt colors. The svg is a black-white image.

Symbolic icons are recolored only when part of a theme. There is documentation available on the developer docs site.

Hi,

Not sure if it works in gtk but I fixed this color problem on a gnome shell extension manually removing all color information from the svg file. After that it followed the fg/bg colors from the system style.

The svg it’s ‘kind of’ a XML file, you can edit it in gedit (or any other text editor).

@ebassi I tried to use gtk_icon_theme_add_resource_path and gtk_image_new_from_icon_name, but I couldn’t even get the icon to show (see below). If there’s any more information on how to get things done, I’d appreciate links.

 gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (), my_abs_path);
 tmpimage = gtk_image_new_from_icon_name ("aaaa",GTK_ICON_SIZE_DND);
// tmpimage = gtk_image_new_from_icon_name ("aaaa.svg",GTK_ICON_SIZE_DND); // tried both

With my_abs_path/aaaa.svg in place.

@PerryWerneck
Very nice trick! Worked for me as well!
However, as ebassi and baedert didn’t point this out, I guess it’s rather a bug, than a feature and might be removed later? (maybe a comment from the devs here?)

1 Like

Resource paths are paths inside GResources, not file system paths. Every API that uses “resource” in their name refers to GResources.

For using symbolic icons, that change color, …

Further, if you want to manually detect colors of the theme, you can use this:

GdkRGBA foreground_color = {0};
GtkWidget * tmp = gtk_button_new ();
GtkStyleContext * context = gtk_widget_get_style_context (tmp);
gtk_style_context_get_color (context,GTK_STATE_FLAG_NORMAL,&foreground_color);
gtk_widget_destroy (tmp);