Hello! I have default application with just one window and an icon for this window (Icon for desktop panel). I have to set this icon to my window. I surfed the Internet and found the code below:
the subpath can be resolved automatically if you rename your folder “hincolor” to “hicolor”.
Last tip: if you embed your icon as GResource, you can then skip adding the icon search path entirely, as long as the resource path matches your application-ID.
Thank you for your response gwillems. I rewrote the code and tried your advice with resource directory. Unfortunately, the application still has no icon. Moreover, use only “rcs” in gtk_icon_theme_add_search_path is not enough even if “hincolor” is renamed “hicolor”.
Maybe PNG format is not supported anymore? Or my picture is broken?
Hm… it’s pretty strange. I tried it one more time and there is no good result. The code is the same. Maybe compile command is wrong? gcc `pkg-config --cflags gtk4` *.c -o app `pkg-config --libs gtk4`
Or maybe I should do something with application in the main function?
Well, as it mentioned in GNOME Developer documentation, /usr/share/icons/hicolor/48x48/apps/ (or 256x256 which is strongly encouraged) should be used for application icon.
I have there an icon for gtk-demo so for example it should be enough. I tried:
Hello! I run it with GTK_DEBUG and it shows the icon_name. In the previous code it writes: “gtk3-demo”. Everything is good here (as I see and understand) but the icon isn’t shown
Which parameters I should show?
ah, wait… are you running the code under Wayland?
If yes, I heard that wayland can’t use the window’s “icon-name” property, but instead reads the application-ID and looks for an icon with the same name in the system icon paths… or something like this, not sure exactly (I’m not using wayland).
There are so many combinations wayland/xwayland use and parts on different systems, on some of them starting WAYLAND_DISPLAY= ./app in wayland session is enough to get that icon for GTK4 app with gtk_window_set_icon(), on some other ones - not.
p.s. I believe that icon from .desktop works almost in all cases
Your desktop file must match your application id (minus the .desktop extension), so that GNOME can associate your windows to the desktop file, and extract the icon.
If you’re using an X11 session, GNOME can also use the _NET_WM_ICON_NAME and _NET_WM_ICON properties on the window, but those are fallback values, and given the nature of X11 properties, do not produce high quality results.
Well, I checked this page and now it’s become much harder (to me) to understand. What was done:
- I renamed icon name (now it’s com.example.Application.png) and moved it into the /usr/share/icons/hicolor/256x256/apps
- I created com.example.Application.desktop file. It’s moved into /usr/share/applications directory.
- Exec is moved into the /usr/bin and /bin directories.
There is a step forward! I found this: icons from /usr/share/icons/hicolor/... are ignored or something else but it doesn’t work. And .desktop files look for an icon in /var/cache/swcatalog/icons/repo-oss/128x128.
Moreover icons cannot be used if application is not installed. How does it work? I mean I write an application that uses this icon that exists but I can’t use it, strange…
I checked it with icon for GTK Demo and it works. Now we have to understand why icons from /usr/... are ignored, gentlemen.
if I’m not mistaken, caches can be updated with gtk-update-icon-cache(1) and update-desktop-database(1) correspondingly, an application launch tested with gtk-launch(1)
But why do I need to update cache in this situation? What do I really need is upload an icon from /usr/share/icons/hicolor/256x256/apps directory or from local project’s directory with other resources (the last one preferred)
I’m not too much into it. That’s usually properly done at installation by some package manager, or postinst script of used build system for a project.
The “display server” has nothing to do with this, so please don’t confuse things even further.
Upload where? You don’t “upload” anything.
Once again: the icon of an application in GNOME is determined by the desktop file’s Icon key; your application IDmust match the name of the desktop file, so that GNOME can associate a window to the desktop file.
Installing an icon inside the theme requires updating the icon cache at installation time. There are literally thousands of free and open source software applications that conform to this, and you can check their build system to see how that works.
You are getting confused by trying to reverse engineer a system that gives you the code and documentation for what you have to do.