Addressing the state of themed icons in snaps

Snaps currently supports themed icons, but, there is a big problem. You’ll need to put the icons with the name snap.<snap_name>.<icon_name> in the meta/gui folder. Now, it’s very tough/tedious process to manually do this in the snap manifest. But, even if someone does this, if a metadata file is given to parse, snapcraft will parse it and instead of putting the icon themes to the proper folder, it’ll automatically select an icon (mostly sclable ones) and hardcode the path of it in the desktop file. Now, to get rid of this, one need to edit these

diff -Naur a/NickvisionTubeConverter.Shared/Linux/ b/NickvisionTubeConverter.Shared/Linux/
--- a/NickvisionTubeConverter.Shared/Linux/	2023-06-13 00:34:09.996218531 +0530
+++ b/NickvisionTubeConverter.Shared/Linux/	2023-06-13 00:42:47.352870719 +0530
@@ -3,7 +3,7 @@
 Name=Tube Converter
 Comment=Get video and audio from the web
diff -Naur a/NickvisionTubeConverter.Shared/Linux/ b/NickvisionTubeConverter.Shared/Linux/
--- a/NickvisionTubeConverter.Shared/Linux/	2023-06-11 21:08:29.643511112 +0530
+++ b/NickvisionTubeConverter.Shared/Linux/	2023-06-13 00:39:01.039920329 +0530
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <component type="desktop-application">
+  <icon type="stock"></icon>
   <name>Tube Converter</name>

This is an example patch file. Now, even after all of this, what you’ll get is a cog icon on the first time installation of those apps specifically in gnome. Because, for gnome to use icon theme, one needs to update the index.theme file which contains the folder structure of the directory(/var/lib/snapd/desktop/icons for snaps) which contains the icon themes. Updating the index.theme or doing anything in this directory is absolutely impossible from within a snap. So, we have two options, either don’t have symbolic icons/themed icons or scarfice for the first time an user installs the app.

One possible way is to allow the extensions do the job, specifically gnome extension. To update or add the index.theme file during installation, and allow those binaries from gnome extension modify those folders. Another is to let snapd itself handle these.

@jamesh do you have any ideas?

It’s a bit hard to offer advice without knowing how you’ve laid things out in your snap.

This should not be necessary. From the icon theme specification:

Each theme is stored as subdirectories of the base directories. The internal name of the theme is the name of the subdirectory, although the user-visible name as specified by the theme may be different. Hence, theme names are case sensitive, and are limited to ASCII characters. Theme names may also not contain comma or space.

In at least one of the theme directories there must be a file called index.theme that describes the theme. The first index.theme found while searching the base directories in order is used. This file describes the general attributes of the theme.

For a theme named foo, it will look for icons in subdirectories icons/foo of $XDG_DATA_HOME and all of $XDG_DATA_DIRS. Only one of those directories needs to contain an index.theme file, which will describe the layout within all the matching directories. We make sure /var/lib/snapd/desktop is in $XDG_DATA_DIRS, so icons should be found without any need to alter index.theme files.

If you wanted to add a scalable app icon to the hicolor theme (the theme that all other themes derive from), it’s index.theme file says it should go in the scalable/apps directory. So we want the file to end up in /var/lib/snapd/desktop/icons/hicolor/scalable/apps. To achieve this, the icon should be in the $SNAP/meta/gui/icons/hicolor/scalable/apps directory.

If you have other types of icons, you’d place them in other subdirectories of $SNAP/meta/gui/icons/hicolor. If you happen to have created variants that integrate with other themes better (e.g. the HighContrast accessibility theme), you’d place them in other subdirectories of $SNAP/meta/gui/icons and follow that theme’s index.theme file for directory layout specifics.

We have it in this way, but, it doesn’t help on the first installation of the app. That’s why an index.theme is needed in the /var/lib/snapd/desktop/icons/hicolor folder. Thus, we can update the icon cache, and icons will show up then and there right after installation.

A video to show the issue.

Manifest of this snap.

I’m looking at the icon theme code in GTK, and it definitely stops looking for an index.theme file for the theme once it finds one (as I’d expect from a read of the spec):

So adding extra index.theme files does not sound like a real solution.

Does anything change if you log out and log in again after installing the snap? If so, then I think we might be looking at a race condition that we can solve in snapd. At the moment, the desktop file gets installed before themed icons when installing a snap:

This means that there is a short period of time when the desktop file is available but the icon has not yet available. If gnome-shell resolved the icon name at that point, then it could explain the behaviour you’re seeing. Reversing the order we do these two operations should let it work without having to log out and in again.

Yup exactly it does. That’s why we took up the risk. So then this is solvable! Yay! :tada: