-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
WIP, ENH: Automatic theme switching on macOS #10655
Conversation
Related: https://github.com/albertosottile/darkdetect/releases/tag/v0.6.0 |
👀 – this is great! But Qt application still don't change to dark (native look) on Windows and Linux unless stylesheets are used, right? |
I'm afraid so |
Too bad it's only for Windows and Linux, it would be nice to use the same approach for all systems if possible. |
Detection/switching on macOS has worked for a long time I thought? And yes, it seems to be GNOME-specific. |
I suppose this PR is about switching without restarting the app. |
This has worked on macOS for years. |
But not with Qt out of the box… |
MWE: from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import sys
class PushButton(QWidget):
def __init__(self):
super(PushButton,self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("PushButton")
self.setGeometry(400,400,300,260)
self.closeButton = QPushButton(self)
self.closeButton.setText("Close")
self.closeButton.clicked.connect(self.close)
self.closeButton.move(100,100)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = PushButton()
ex.show()
sys.exit(app.exec_()) Switching between light and dark only affects the title bar: Screen.Recording.2022-05-24.at.11.47.11.mov |
Works for me. I've used this for years with MNELAB. macOS is the only platform where this works, Windows and Linux require a workaround (e.g. with a listener thread constantly polling for the theme). mwe.mov |
Son of a gun, you're right – seems the only reason it didn't work for me here is because I used an x86 Python install on my M1 Mac. With a native |
Well in your case, you use the default 'dark style' macOS palette but this PR is about applying the one from |
|
There is also the option to use the |
To me, it works as it should only on macOS. It would be great if Qt added support for theme changes on Windows and Linux via the |
I agree, anything consistent across the main OSes/desktops would be great. But it seems that |
Yep! Not only something consistent, but Qt should support native light/dark modes on Windows and Linux too. On Windows and Linux the app needs to use stylesheets, which doesn't look like native apps. |
Yes I would love that, but @larsoner was opposed. I don't think our custom dark theme looks native on macOS. It feels a little strange. But let's address this in a different issue or PR |
Okay, I did not remember all the details of the discussion back then. Does the PR work in its current state by the way, anybody tried? |
I don't remember why I was opposed -- but in any case, currently There are cases where I think we can't get around using As an aside, I looked for quite some time to see if there is a way to tell Qt "even though my system is in light mode, use the native dark mode Qt styling" but I couldn't find a way to do it. They don't just apply an internal stylesheet to do it, it's coded in a class somehow somewhere IIRC using private attributes/methods so I didn't see a way we could use them. |
def event(self, ev): | ||
"""Catch system events.""" | ||
if ev.type() == QEvent.PaletteChange: # detect theme switches | ||
theme = get_config('MNE_3D_OPTION_THEME', 'auto') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this won't quite do what I'd expect -- if I pass theme='light'
when instantiating a brain, I would expect it not to change to dark when the system switches. So the user-selected option (light, dark, auto) needs to be stored on instantiation, and switching done only when it's auto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah good idea!
Could it be the macOS system itself that changes the Qt palette somehow? It would explain why we need to filter |
Could be, with stuff like: https://github.com/qt/qtbase/blob/067b53864112c084587fa9a507eb4bde3d50a6e1/src/widgets/widgets/qtoolbar.cpp#L28-L30 etc. So maybe there is some way for us to dig into the What might be a low effort possible solution would be to post a question like this to the Qt forums somewhere, and hope someone knows how to do it. If the solution is easy, we implement it; if nobody responds, if someone is bored they can look into the Qt internals to figure out if there's a way to tell Cocoa to restyle (in the same way that the user changing their OS mode changes things); and until then we just use |
@larsoner if you post a question in the forum please add a link to the thread here. I'm very interested to see if this is possible. Ideally, this should be added to Qt directly instead of rolling our own workaround/fix. And not just the macOS dark app in system light mode, but also support for native light/dark mode on Windows and Linux. |
I'll let someone else post when they're motivated :) |
I'm motivated but I need to find the time to do it... Let's see. I could imagine that there are tons of threads about light/dark mode switching on Windows/Linux already, but I haven't even searched the forum properly. |
My 2 cts: I don't see any reason to support selecting a different theme than what is currently set system-wide, as long as we allow automated switching upon system-wide theme change. It's so trivially easy to change the theme on macOS – for me, it means right-clicking on an icon in the menu bar. I'm not against having this functionality, but if it causes pain and more complicated code (does it? I'm not sure?), I'd say, just don't support it and tell users about that neat tool that I'm using, NightOwl 🦉 It even allows you to always start certain apps in light or dark mode, independent from the global system setting. |
I agree with @hoechenberger. The priority should be to support automatic switching on all three OSs, preferably using a native styling (as opposed to stylesheets). |
It sounds like we might just wait on this one (?) and even if we don't, will try to avoid having |
This PR comes from #10565 (comment) and adds support for automatic theme switching on macOS. It is a draft until it has been tested on macOS.
How to test?
On a macOS machine, plot any 3d figure (alignment, stc, coreg, ...etc) then switch the system theme.
Expected result
The window, the widgets and the icons should follow the new theme.
Closes #9182