Comments (11)
I invested a lot of time in trying to implement a listener for macOS and failed miserably. Since I want to add more information on what I tried (in view of helping further people that might venture through the same path) I am now closing this issue and putting the macOS endeavor in #25.
@TransparentLC thanks again for having implemented the listeners for Windows and Linux!
from darkdetect.
i actually have no plans in this regard. This could be a nice enhancement indeed, however I foresee a lot of work and I am not even sure this is possible in pure Python and for all the platform.
Personally, I could invest some time for macOS, but I am not sure when I will have enough capacity for doing this.
Nevertheless, PRs in this direction are more than welcome.
from darkdetect.
Just FYI, this is something that is easily doable in PySide2 (and I guess pyqt5):
import darkdetect
from PySide2 import QtCore, QtWidgets
class LabelHandler():
def __init__(self):
self.label = QtWidgets.QLabel(f'Mode: {darkdetect.theme()}')
@QtCore.Slot(str)
def mode_change(self):
self.label.setText(f'Mode: {darkdetect.theme()}')
if __name__ == "__main__":
app = QtWidgets.QApplication([])
win = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
lh = LabelHandler()
layout.addWidget(lh.label)
app.paletteChanged.connect(lh.mode_change)
win.setLayout(layout)
win.show()
app.exec_()
Darkdetect was mostly created to overcome the limitations of GUI libraries, and detecting this change on the fly seems to be supported by (at least some) of them. I would encourage you to check whether this listener detection is supported in the library of your choice.
from darkdetect.
On Windows the dark mode setting is stored in registry key HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize DWORD AppsUseLightTheme
. I found a WIN32 API function named RegNotifyChangeKeyValue
which makes notifies about changes to a specified registry key. Perhaps a native pyd extension is required but I'm not familiar with WIN32 APIs.
from darkdetect.
I forked this repo and commited the native extension's code to add a listener function on Windows. Pass a callback function and it will be called with string "Dark" or "Light" when the OS switches the dark mode setting. On Linux and macOS it simply raises a NotImplementedError
.
The listener is an infinite loop so a separated thread is required. However the thread is unstoppable so I think the API design and the implementation can be further improved.
Usage example:
import darkdetect, threading
# def listener(callback: typing.Callable[[str], None]) -> None: ...
t = threading.Thread(target=darkdetect.listener, args=(print,))
t.daemon = True
t.start()
Try it out by cloning it and install with command python setup.py build_ext --inplace
and python setup.py install
.
from darkdetect.
6b14eac9426ebd51311e7c4e848cb898.mp4
I guess we can spawn a subprocess gsettings monitor org.gnome.desktop.interface gtk-theme
and read its output to detect dark mode switch on Linux. I'll try to add it to my fork (and the pull request).
from darkdetect.
@albertosottile I also found a solution for macOS: Golang: Detect dark mode change in OSX
from darkdetect.
@albertosottile I also found a solution for macOS: Golang: Detect dark mode change in OSX
The detection method based on defaults read -g AppleInterfaceStyle
is less reliable than the one implemented in darkdetect
([[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"]
).
In principle, Foundation allows monitoring values from NSUserDefaults
with callbacks, but I was not able to get this working via ctypes
when I tried.
For the moment, I would merge your PR as it is. We can then continue investigating if callback
can be implemented on macOS with the NSUserDefaults
-based detection method.
from darkdetect.
Just Googled for any available solutions and I don't have any device running macOS. So I'm afraid I can't provide further help for implementation for macOS.
from darkdetect.
The listeners for Windows and Linux were released in 0.6.0. I will keep this issue open for macOS.
from darkdetect.
Many thanks to all!
from darkdetect.
Related Issues (20)
- Crash on macOS Big Sur 11.4 HOT 19
- SIGSEGV (Address boundary error) on Apple Silicon M1, Big Sur 11.5.2 HOT 6
- Check compatibility with Windows 11 HOT 7
- distutils.version LooseVersion is deprecated HOT 2
- Doesn't work for Pop!_OS 22.04 LTS x86_64 HOT 5
- Module produces ValueError on Windows 8.1 HOT 2
- Implement `listener` for macOS HOT 6
- correct dark detection for gtk3 HOT 19
- A way to terminate listener thread HOT 5
- [RFC] macOS code choices
- Listener class PR acceptable? HOT 3
- macOS listener support on apps where `sys.executable` is not a python interpreter HOT 8
- False positive HOT 1
- Issue: darkdetect listener cannot work under sudo HOT 2
- WARNING message from PyInstaller about ignoring AppKit.framework/AppKit imported from _mac_detect.py HOT 1
- Two Windows 'Dark' modes: `AppsUseLightTheme` and `SystemUsesLightTheme`
- ModuleNotFoundError: No module named 'darkdetect' in Python 3.8 HOT 2
- Duplicate
- Linux (GNOME) HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from darkdetect.