Search code examples
ciconsgtkgtk4

GTK 4 and applications icons : how to include an application icon in a portable way?


I have difficulties to understand the icon system in GTK 4 (I work in C language). In GTK 2 and 3 that was easy to instruct the OS about the icon to use for displaying the apps.

In GTK 4 the set_icon functions have been removed which let us with the theming system.

I understand that, by default, gtk want us to follow the Freedesktop Icon Theme Specification and so to put the icons during the installation in directories like /usr/local/share/icons/hicolor/ and setting it in the application via function like gtk_window_set_default_icon_name or gtk_window_set_icon_name. But I didn't really manage to make this system work.

Moreover, it remains obscure to me what happens on other systems that are not gnome-based like Windows (or even KDE desktop)...

So, well, I have a few questions that stem from theses previous points :

  • How the system work on other OS or DE that do not follow the Freedesktop Icon Theme Specification ?

  • Is this possible to have a very short working example that illustrates how to use, in GTK4, a new application icons that, for example, was just copied in /usr/local/share/icons/hicolor/

  • And my real question for my use case : is this still possible, by one way or another, to include applications icons in the binary or in the binary directory to have a simple portable application which do not need installations and work on every system ?

(edit : edited to include nielsdg precision)


Solution

  • Ok, I have found a (partial) answer to my second question and third question, sort of.

    In fact, it's pretty simple, but I made a few mistakes.

    You simply have to have a directory with this structure exactly (I made a mistake in the structure) :

        my_ressource_directory/hicolor/apps/48x48/my-icon.png  
        my_ressource_directory/hicolor/apps/256x256/my-icon.png  
        my_ressource_directory/hicolor/apps/512x512/my-icon.png  
    

    Only the 48x48 size is necessary, there are a lot of other sizes which seems to be : 8x8 16x16 22x22 24x24 32x32 36x36 42x42 44x44 48x48 64x64 72x72 96x96 128x128 150x150 192x192 256x256 310x310 512x512 and "scalable".

    I don't know in which context the system use all of them and I don't know if personalised size is possible.

    I equally don't know in which context the svg icons in scalable can be used by the system...

    In the code, then, you simply have to include a thing like that :

        icon_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
        gtk_icon_theme_add_search_path(icon_theme,path_to_my_ressource_directory); 
    
        if(gtk_icon_theme_has_icon(icon_theme,"my-icon")!=1)
           {
            // manage error
           }
        
        gtk_window_set_default_icon_name("my-icon"); // default icon for all windows if nothing is set explicitly if I understand well. 
        
        window = gtk_application_window_new (app);
        gtk_window_set_title (GTK_WINDOW (window), "Your app");
        gtk_window_set_icon_name(GTK_WINDOW (window),"my-icon"); // set explicitly the icon for the min window
    

    And tadaaa; it works :) .

    Please also notice that some post seems to say that the call to gtk-update-icon-cache is important to make the system works. This command creates a "icon-theme.cache" file in the directory that is used by GTK to accelerate the opening/processing of icons. In practice I can't say if this is efficient or really used by GTK but after checking, even without the icon-theme.cache it works. So the call of this command seems not to be mandatory.

    That said, it cost nothing to call the command in the Makefile. The exact command is :

        gtk-update-icon-cache -f -t my_ressource_directory/
    

    EDIT

    The solution is incomplete because the resulting icon is fuzzy in certain context. See this question : Why GTK4 seems to use only 48x48 icons for displaying minimized application in all context?

    And the answer, to have the full solution.