You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatic light/dark mode switching of open STC plot windows #9182
On macOS, apps that support light and dark themes switch between the two if the system theme is changed, without requiring a restart. It would be good if our STC plotters could do the same.
Screen.Recording.2021-03-22.at.21.18.53.mov
In this screencast you can also see that the color of the title bar is not correct in dark mode, but that might be a Qt issue?
This one to me seems like it will be a lot of work and/or potentially fragile (probably need to hook into some system notification thing) for very small added benefit (since theme changes are generally rare).
This one to me seems like it will be a lot of work and/or potentially fragile (probably need to hook into some system notification thing) for very small added benefit (since theme changes are generally rare).
Yeah, no idea, I thought maybe Qt can trigger a callback upon theme change, no idea.
But the light title bar … would be super nice if we could make this one dark ;)
plus the STC views are typically not long-lived and can simply be recreated, unlike e.g. a text editor or terminal window, which you may want to keep open for days or weeks.
This one to me seems like it will be a lot of work and/or potentially fragile (probably need to hook into some system notification thing) for very small added benefit (since theme changes are generally rare).
Yeah, no idea, I thought maybe Qt can trigger a callback upon theme change, no idea.
But the light title bar … would be super nice if we could make this one dark ;)
Is the titlebar white when you start in darkmode?
To dynamically change with the mode, one could maybe setup a QTimer and check periodically for a change from darkdetect. But it is maybe questionable as you already said, if it was worth it (and I don't know @GuillaumeFavelier, how that would influence the performance of for example the BackgroundPlotter).
I read some forum-entries about the titlebar, it seems that the titlebar-color is not influenced by Qt but by the OS. There seem to be a workaround in cpp for MacOS, but I don't know if that could be translated to (MNE-)Python. And I found none for Windows or Linux.
But there seems to be another possibility by replacing the whole window-surroundings including the titlebar. as qtmodern does it. If we'd be willing to add another dependency, this may be the best solution, which would also unify the look across platforms as a side-effect. What do you say?
This event is triggered whenever the OS theme changes. Therefore, you can use it to change themes on the fly, but as I said not on Windows. However, maybe this isn't that bad, because Windows doesn't have a dynamically changing dark/light theme (it's more like a setting you choose once).
Nice thank you, maybe it would work if we connect the Stylesheet-Change with your PaletteChange-Event?
Could it be, that Qt changes to dark automatically on MacOS and Linux? Or how do you influence the appearance apart from the icons in mnelab?
Coming from Windows (unfortunately I have not access to other devices at the moment) I just thought about stylesheets because they seemed to work with all OS.
Nice thank you, maybe it would work if we connect the Stylesheet-Change with your PaletteChange-Event?
It's worth a try.
Could it be, that Qt changes to dark automatically on MacOS and Linux? Or how do you influence the appearance apart from the icons in mnelab?
Exactly, I only change the icons manually. On Windows this doesn't work unfortunately, mainly because Qt still does not provide a native dark theme for Windows (not even Qt6).
Coming from Windows (unfortunately I have not access to other devices at the moment) I just thought about stylesheets because they seemed to work with all OS.
They do (except for the title bar apparently), but style sheets never look like a native app (although I think it's very close).
This one to me seems like it will be a lot of work and/or potentially fragile
I'm catching a QEvent here
Ahh if it's based on some Qt event then ignore what I said about being much work or fragile, it should be neither of those. And even detection of the style name is two lines of Foundation code which seems okay.
Personally my vote would be to use Qt's builtin dark support/styling as much as possible (OSX via icon change) and resort to the style sheet method where Qt does not support it or we don't know how to do it more natively (Windows and Linux).
Personally my vote would be to use Qt's builtin dark support/styling as much as possible (OSX via icon change) and resort to the style sheet method where Qt does not support it or we don't know how to do it more natively (Windows and Linux).
👍 but native styling works on both macOS and Linux, so style sheets should only be required on Windows.
Although I did manage to get it to use a dark theme with the qt5ct tool (you have to manually select "Adwaita-Dark"). Now I'm pretty sure all this could be done with environment variables so that at least we could use the correct light/dark theme when starting a Qt app. Changing the theme on the fly will probably be still difficult. Update: It even works on the fly 🚀!
Now that pyvista/pyvistaqt#152 is in, we can subclass pyvistaqt.window.MainWindow to have the palette change signal connected or use an eventFilter. I think the signal connection method would be my slight preference. Feel free to give it a shot @cbrnr@GuillaumeFavelier if you're motivated !
Nice! Just to make sure I didn't miss anything, this still works only on macOS, right?
I'd also prefer either processing the event directly (as I'm doing in MNELAB) or emitting a signal and connecting it to a method that does the processing. I don't think it makes a big difference. I'd not write a custom MainWindow class just for that.
Good question, I will try a simple code snippet on my KDE desktop to find out 👍
No idea for Windows though.
I'd not write a custom MainWindow class just for that.
I think it's just a misunderstanding. On mnelab you design your own MainWindow class. In mne-python, we borrow the one created by default by pyvistaqt. What @larsoner means is exactly what you suggest above:
creating a custom MainWindow (which inherits from PyQtVista's MainWindow) seems to be the better solution to me.
To clarify, I think we would end up doing the same as mnelab.
@GuillaumeFavelier OK, now I got it! I also confirmed that your example is working on macOS. I think that's unfortunately still the only OS, it doesn't work on Windows either.
Activity
larsoner commentedon Mar 22, 2021
This one to me seems like it will be a lot of work and/or potentially fragile (probably need to hook into some system notification thing) for very small added benefit (since theme changes are generally rare).
hoechenberger commentedon Mar 22, 2021
Yeah, no idea, I thought maybe Qt can trigger a callback upon theme change, no idea.
But the light title bar … would be super nice if we could make this one dark ;)
hoechenberger commentedon Mar 22, 2021
cc @marsipu
hoechenberger commentedon Mar 22, 2021
plus the STC views are typically not long-lived and can simply be recreated, unlike e.g. a text editor or terminal window, which you may want to keep open for days or weeks.
marsipu commentedon Mar 22, 2021
Is the titlebar white when you start in darkmode?
To dynamically change with the mode, one could maybe setup a QTimer and check periodically for a change from darkdetect. But it is maybe questionable as you already said, if it was worth it (and I don't know @GuillaumeFavelier, how that would influence the performance of for example the BackgroundPlotter).
I read some forum-entries about the titlebar, it seems that the titlebar-color is not influenced by Qt but by the OS. There seem to be a workaround in cpp for MacOS, but I don't know if that could be translated to (MNE-)Python. And I found none for Windows or Linux.
But there seems to be another possibility by replacing the whole window-surroundings including the titlebar. as qtmodern does it. If we'd be willing to add another dependency, this may be the best solution, which would also unify the look across platforms as a side-effect. What do you say?
cbrnr commentedon Mar 22, 2021
Take a look how I do it in MNELAB. It works on macOS and Linux, but unfortunately Qt doesn't support that on Windows (yet).
marsipu commentedon Mar 22, 2021
Nice, what do you do? Is it the part with the NSBundle in main.py?
cbrnr commentedon Mar 23, 2021
No, I'm catching a
QEventhere: https://github.com/cbrnr/mnelab/blob/main/mnelab/mainwindow.py#L1011-L1019This event is triggered whenever the OS theme changes. Therefore, you can use it to change themes on the fly, but as I said not on Windows. However, maybe this isn't that bad, because Windows doesn't have a dynamically changing dark/light theme (it's more like a setting you choose once).
cbrnr commentedon Mar 23, 2021
However, since you're using style sheets this will probably not work. I think the white title bar is also due to style sheets.
marsipu commentedon Mar 23, 2021
Nice thank you, maybe it would work if we connect the Stylesheet-Change with your PaletteChange-Event?
Could it be, that Qt changes to dark automatically on MacOS and Linux? Or how do you influence the appearance apart from the icons in mnelab?
Coming from Windows (unfortunately I have not access to other devices at the moment) I just thought about stylesheets because they seemed to work with all OS.
cbrnr commentedon Mar 23, 2021
It's worth a try.
Exactly, I only change the icons manually. On Windows this doesn't work unfortunately, mainly because Qt still does not provide a native dark theme for Windows (not even Qt6).
They do (except for the title bar apparently), but style sheets never look like a native app (although I think it's very close).
larsoner commentedon Mar 23, 2021
Ahh if it's based on some Qt event then ignore what I said about being much work or fragile, it should be neither of those. And even detection of the style name is two lines of Foundation code which seems okay.
Personally my vote would be to use Qt's builtin dark support/styling as much as possible (OSX via icon change) and resort to the style sheet method where Qt does not support it or we don't know how to do it more natively (Windows and Linux).
cbrnr commentedon Mar 23, 2021
👍 but native styling works on both macOS and Linux, so style sheets should only be required on Windows.
25 remaining items
cbrnr commentedon Mar 30, 2021
@larsoner you were right, Qt doesn't seem to support dark theming on Linux 😞 (only the title bar changes).
cbrnr commentedon Mar 30, 2021
Although I did manage to get it to use a dark theme with the
qt5cttool (you have to manually select "Adwaita-Dark"). Now I'm pretty sure all this could be done with environment variables so that at least we could use the correct light/dark theme when starting a Qt app.Changing the theme on the fly will probably be still difficult.Update: It even works on the fly 🚀!larsoner commentedon Mar 10, 2022
Now that pyvista/pyvistaqt#152 is in, we can subclass
pyvistaqt.window.MainWindowto have the palette change signal connected or use an eventFilter. I think the signal connection method would be my slight preference. Feel free to give it a shot @cbrnr @GuillaumeFavelier if you're motivated !cbrnr commentedon Mar 10, 2022
Nice! Just to make sure I didn't miss anything, this still works only on macOS, right?
I'd also prefer either processing the event directly (as I'm doing in MNELAB) or emitting a signal and connecting it to a method that does the processing. I don't think it makes a big difference. I'd not write a custom
MainWindowclass just for that.GuillaumeFavelier commentedon Mar 10, 2022
Good question, I will try a simple code snippet on my KDE desktop to find out 👍
No idea for Windows though.
I think it's just a misunderstanding. On
mnelabyou design your ownMainWindowclass. Inmne-python, we borrow the one created by default bypyvistaqt. What @larsoner means is exactly what you suggest above:To clarify, I think we would end up doing the same as
mnelab.GuillaumeFavelier commentedon Mar 10, 2022
Nope, it did not work for me unfortunately. I'm really surprised considering everything KDE is basically Qt under the hood.
output.mp4
The rough code snippet I used:
Details
cbrnr commentedon Mar 10, 2022
@GuillaumeFavelier OK, now I got it! I also confirmed that your example is working on macOS. I think that's unfortunately still the only OS, it doesn't work on Windows either.