Plank Dock - wrong icons/wmclass

Viewed 474

I have a problem with plank. Unfortunately I could not find any entries here on stackoverflow nor anywhere else that address my problem. Because of this, I decided to open a new question here.

My Problem:

Some appliactions (.desktop files) show the wrong icon on plank dock. And its not only that, its also happen, that its link to the wrong application. For example visual studio code. If I run code (terminal or albert) then a electron icon apears on the plank dock, but if I zoom out (gnome 40) the right icon shows on the vs code window. Here a screenshot: Screenshot of plank and vs code

If I use the middle mousebutton to start a new window, then an electron window apears, not a second vs code.

Question:

How can this be fixed? Is there a workaround?

2 Answers

I had the same problem in arch linux. Unfortunately, the solution I found is only temporary (unless they fix the bug), but it works as long as you re-implement it every time vs code is updated

  1. Delete the electron launcher from plank.
  2. Change the StartupWMClass value in the desktop file to be set to code-oss. This is the step that has to be repeated on every upgrade to vs code.
  3. Run vs code and then persist the launcher in plank with "Keep in Dock".

/usr/share/applications/code-oss.desktop

[Desktop Entry]
Name=Code - OSS
Comment=Code Editing. Redefined.
GenericName=Text Editor
Exec=/usr/bin/code-oss --no-sandbox --unity-launch %F
Icon=com.visualstudio.code.oss
Type=Application
StartupNotify=false
StartupWMClass=code-oss
Categories=Utility;TextEditor;Development;IDE;
MimeType=text/plain;application/x-code-oss-workspace;
Actions=new-empty-window;
Keywords=vscode;

[Desktop Action new-empty-window]
Name=New Empty Window
Exec=/usr/bin/code-oss --no-sandbox --new-window %F
Icon=com.visualstudio.code.oss

Dealing with upgrades

Any time you upgrade vs code, the desktop file will probably have StartupWMClass reset. You have a few options.

  1. Change the desktop file every time vs code upgrades. You can script this and run it automatically after every system upgrade. Here's a basic example:

~/.bashrc

my_upgrade() {
    sudo pacman -Syu
    sudo sed -i 's/^StartupWMClass.*/StartupWMClass=code-oss/g' /usr/share/applications/code-oss.desktop
}

Alternatively, you can add a pacman hook to automate this within pacman itself so you don't need a custom wrapper for pacman (credit tdy):

/etc/pacman.d/hooks/code-oss-desktop.hook

[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = code

[Action]
Depends = sed
Depends = sudo
When = PostTransaction
Exec = /bin/bash -c '/usr/bin/sudo /usr/bin/sed -i "s/^StartupWMClass.*/StartupWMClass=code-oss/g" /usr/share/applications/code-oss.desktop'
  1. Uninstall code and create your own PKGBUILD for vs code that patches this issue and make sure it tracks upgrades. There are several approaches for this, but it can get complicated and is probably not in scope for this answer.
  2. Block upgrades to code so it is not unexpectedly reset, and periodically upgrade the package explicitly when you are ready to fix the issue.

/etc/pacman.conf

...
IgnorePkg = code
...

Expanding on Drew's answer, the post-upgrade modification of code-oss.desktop can be automated natively with pacman as a PostTransaction hook. That way you don't need the my_upgrade bash wrapper -- pacman will just automatically fix the file when you -Syu.

For example this hook will automatically fix code-oss.desktop whenever pacman installs/upgrades the code package:

#
# /etc/pacman.d/hooks/code-oss-desktop.hook
#

[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = code

[Action]
Depends = sed
Depends = sudo
When = PostTransaction
Exec = /bin/bash -c '/usr/bin/sudo /usr/bin/sed -i "s/^StartupWMClass.*/StartupWMClass=code-oss/g" /usr/share/applications/code-oss.desktop'
Related