Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNE-Python application icon broken in Ubuntu 24.04 #12899

Open
larsoner opened this issue Oct 16, 2024 · 19 comments
Open

MNE-Python application icon broken in Ubuntu 24.04 #12899

larsoner opened this issue Oct 16, 2024 · 19 comments
Milestone

Comments

@larsoner
Copy link
Member

... I think due to the use of Gnome3 newer Ubuntu seems to by default now require a .desktop file to use something other than this in the dock:

image

I tried a bunch of Qt stuff and none of it worked. At some point I stumbled upon a post that mentioned .desktop with StartupWMClass could fix it and indeed this seems to work:

~/.local/share/applications/mne-python.desktop
[Desktop Entry]
Type=Application
Version=1.5
Name=MNE-Python
StartupWMClass=MNE-Python
Icon=/home/larsoner/python/mne-python/mne/icons/mne_default_icon.png
SingleMainWindow=true
NoDisplay=true

image

@hoechenberger our installers currently add files like mne-python-180_prompt-mne.desktop but I'm not sure if it's possible to have them create a separate linux-only file, do you?

Either way, we could consider adding a note about this to our install docs somewhere as well.

@larsoner larsoner added this to the 1.9 milestone Oct 16, 2024
@hoechenberger
Copy link
Member

@larsoner The installer creates .desktop files via menuinst; this is why we ship all those icons etc:

https://github.com/conda-forge/mne-feedstock/tree/main/recipe/menu

See the respective config file:
https://github.com/conda-forge/mne-feedstock/blob/ac4cd905e3e5ac5e667fd707dfff498a60ade511/recipe/menu/menu.json

I don't fully understand your issue report – are you saying you didn't end up with a .desktop file after using the installer?

@larsoner
Copy link
Member Author

are you saying you didn't end up with a .desktop file after using the installer?

No, there are several .desktop files as expected.

The issue is that on latest Ubuntu -- regardless of whether or not the installer is used -- when you open Python, init a Qt app, and show a window, you get the generic gear icon that shows up in the taskbar (first image pasted above). No runtime Qt calls could be used to set it. This is different from previous versions of Ubuntu, where our app.setWindowIcon(...) worked to set this icon.

The workaround is to have a .desktop file with the StartupWMClass=MNE-Python and Icon=... fields populated appropriately, creating an association between windows of the MNE-Python class (which ours have when we use _init_qt_app) and a given icon. In contrast to what gets installed by the installer, this .desktop file I pasted above is not meant to be a launcher of a program, but rather a sign to Gnome that "hey, windows of this class should use this icon instead of the generic one". That's why it has fields like NoDisplay=true that prevent it from showing up as a program you can launch (e.g., when pressing the Windows key), but once it's launched from the Python terminal (by creating a Qt app and showing a window) the application shows up in the dock with the correct icon.

So the question is really how do should we suggest to people (in install docs) or add to our installer a custom .desktop file for people with the contents I pasted above? For the installer, I'm guessing menuinst would not be the correct way to do it, since we're actually not trying to install a new menu launcher, just a specific .desktop file having to do with a window-class-to-icon association. So maybe the easiest thing would be to add it in the post-install scripts. I'll try a PR for this. Then we just need to decide where this suggestion should live in our install docs.

@larsoner
Copy link
Member Author

... looking at https://conda.github.io/menuinst/reference/ I don't see an option for StartupWMClass which is what we'd need to set. I'll open in issue over there since maybe we could add a Linux specific shortcut for it if we could set this.

@larsoner
Copy link
Member Author

Okay, opened conda-forge/mne-feedstock#139, conda/menuinst#265, and #12900

@hoechenberger
Copy link
Member

hoechenberger commented Oct 18, 2024

Hello, just to clarify, no veto against this PR from my side! I only think that maybe we can come up with an additional / a better solution that requires less user interaction 👍

@cbrnr
Copy link
Contributor

cbrnr commented Oct 22, 2024

Hi @larsoner! I have the same problem with MNELAB, but adding the .desktop file doesn't work for me.

I've created ~/.local/share/applications/mnelab.desktop with the following content:

[Desktop Entry]
Type=Application
Version=0.9.2
Name=MNELAB
StartupWMClass=MNELAB
Icon=/home/clemens/mnelab.png
SingleMainWindow=true
NoDisplay=true

Do I have to do anything else? Or is it a problem that the "executable" is really running Python with a specific module (or uvx mnelab)?

@larsoner
Copy link
Member Author

You need to know the StartupWMClass so start Looking Glass (e.g., alt-F2 then lg) and inspect the window. For example for import matplotlib.pyplot as plt; plt.figure() (which now also has the same problem as us) in the Windows tab I can see:

image

So if I then create:

~/.local/share/applications/matplotlib.desktop
[Desktop Entry]
Type=Application
Version=1.5
Name=matplotlib
StartupWMClass=matplotlib
Icon=/home/larsoner/python/matplotlib/lib/matplotlib/mpl-data/images/matplotlib_large.png
SingleMainWindow=true
NoDisplay=true

It updates to use the matplotlib a few seconds later:

image

@cbrnr
Copy link
Contributor

cbrnr commented Oct 23, 2024

Thanks @larsoner, this works! However, the StartupWMClass is python3, so any custom icon I assign would then be used for all Python windows, which is not ideal.

I assume in the case of MNE, the installer sets the class to MNE-Python, so it's not a problem to assign an icon. However, if that's the case, I don't understand why the installer can't add this file in the first place (I thought that you were also discussing non-installer MNE).

@drammock
Copy link
Member

I don't understand why the installer can't add this file in the first place (I thought that you were also discussing non-installer MNE).

It will. See #12900 (comment). The problem is that users who don't use the installer still need it

@cbrnr
Copy link
Contributor

cbrnr commented Oct 23, 2024

It will. See #12900 (comment). The problem is that users who don't use the installer still need it

Right, but users without the installer will not have the MNE-Python class. They will have python3, so this workaround will not really work.

@drammock
Copy link
Member

users without the installer will not have the MNE-Python class. They will have python3, so this workaround will not really work.

IIUC the wmclass is set at runtime by the mne qt browser package, not by the installer

@cbrnr
Copy link
Contributor

cbrnr commented Oct 23, 2024

IIUC the wmclass is set at runtime by the mne qt browser package, not by the installer

I don't think so. It has python3 for me.

@larsoner
Copy link
Member Author

larsoner commented Oct 23, 2024

IIUC the wmclass is set at runtime by the mne qt browser package, not by the installer

Correct that the installer does not set it. Whatever creates the Qt app (or whatever app shows up in the dock) at runtime sets it implicitly or explicitly. So if it's mne-python -- including mne-qt-browser since it uses the helpers in mne.viz -- and you do for example mne browse_raw ... it should be MNE-Python as the wmclass. It does for me at least 🤷

In the example above, by contrast, doing plt.figure() first (which causes matplotlib to spin up a Qt app rather than us), it has matplotlib as the wmclass.

So @cbrnr I think most likely whatever library (ipython?) is spinning up the app that shows up in the dock is implicitly or explicitly setting the wmclass to python3. But I'd expect folks using plain python and doing mne browse_raw ... or otherwise having the Qt app started by MNE-Python the wmclass should in principle be MNE-Python.

@cbrnr
Copy link
Contributor

cbrnr commented Oct 23, 2024

Yeah, this sounds really brittle. I wish there was a way to set this in the app.

But I still don't understand how people who don't use the installer will benefit from the desktop icon. How can I start the Qt app by MNE-Python? What even is "MNE-Python" in this context? Sorry I keep asking questions, but maybe you can give me a simple reproducible way to open some MNE GUI app that has the MNE-Python window class (the CLI example mne browse_raw ... has Matplotlib).

@larsoner
Copy link
Member Author

For me mne browse_raw does it. Are you using the mne-qt-browser backend or the matplotlib backend?

This should also work in plain Python:

>>> import mne
>>> mne.viz.set_browser_backend("qt")
>>> raw = mne.io.read_raw_fif(mne.datasets.sample.data_path() / "MEG" / "sample" / "sample_audvis_raw.fif")
>>> raw.plot()

This should cause mne-qt-browser to be the first thing to spawn the Qt app, which should in turn set the wmclass to MNE-Python.

@larsoner
Copy link
Member Author

How can I start the Qt app by MNE-Python? What even is "MNE-Python" in this context?

For this part it's really this under the hood:

import mne
mne.viz.backends._utils._init_mne_qtapp()

That would be the QApplication created by MNE-Python (the mne module).

@cbrnr
Copy link
Contributor

cbrnr commented Oct 23, 2024

This should cause mne-qt-browser to be the first thing to spawn the Qt app, which should in turn set the wmclass to MNE-Python.

No, it's python3 for me. I ran the commands you listed in a plain old terminal.

@larsoner
Copy link
Member Author

Weird, didn't know what the difference would be! But at least now you know how to change the icon if you want to.

@cbrnr
Copy link
Contributor

cbrnr commented Oct 23, 2024

Weird, didn't know what the difference would be! But at least now you know how to change the icon if you want to.

Well, that's kind of my point: if I change it, then all Python GUI apps have that same icon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants