Comments (6)
@zwimer Thanks for your contribution. I apologize if I did not reply sooner, but I think this approach is not truly viable. This thread will constantly call sleep
and the ctypes reading code, which will have a CPU impact on the host machine and an impact on the other threads due to the constant GIL lock/release. I did not test this, but I assume the effect on e.g. laptops battery life would be significant.
Therefore, I would be against integrating this in the darkdetect
codebase, as any programmer would expect that the listener
function would not behave like this. The general, and correct, expectation would be that the spawned thread does not consume any constant resources and only fires the callback when needed. The approach proposed here does not allow that.
As you mentioned, though, as a last resort measure any developer can implement a similar approach in their own codebase, where they have full control on the secondary effects. But I would rather not add this in the codebase, especially hidden under a listener
API that, as mentioned before, carries a different set of expectations..
from darkdetect.
I invested some time into trying to implement this, but so far I had lo luck.
If anyone is willing to pick up this task, further information on the most severe issues can be found here: https://stackoverflow.com/questions/72982210/swift-and-python-get-a-runloop-that-can-run-in-threading and here: https://developer.apple.com/forums/thread/710135?login=true&page=1&r_s_legacy=true#720551022
I pushed the library implementations that I tried in the macos_listener_attempts branch, but none of them is functional from a secondary thread (a fundamental requirement for this package).
from darkdetect.
Is there a reason something akin to this would not work?
def listener(callback):
old = isDark()
while True:
x = isDark()
if x != old:
old = x
callback(x)
time.sleep(.01)
def listen(callback):
threading.Thread(target=listener, args=(callback,)).start()
This seemed to work for me, though maybe you'd want theme()
instead of isDark
?
I realize this might not be an ideal listener, but given the current lack of any, might this be acceptable in the meantime?
from darkdetect.
A PR of the above: #27
from darkdetect.
@albertosottile I implemented a proper listener here: #30
from darkdetect.
Closed in #30, thanks to @zwimer.
from darkdetect.
Related Issues (20)
- [REQUEST] Watch for real-time theme changes HOT 1
- 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
- Listener for dark mode switch? HOT 11
- distutils.version LooseVersion is deprecated HOT 2
- Doesn't work for Pop!_OS 22.04 LTS x86_64 HOT 5
- Detect dark mode on Windows 10 HOT 2
- Module produces ValueError on Windows 8.1 HOT 2
- 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
- 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.