-
Notifications
You must be signed in to change notification settings - Fork 18
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
Implement a macOS listener #30
Implement a macOS listener #30
Conversation
Hi, thanks for having worked on this. I was not aware of Could you provide a usage example? I tried the one reported in the README import darkdetect
import threading
# def listener(callback: typing.Callable[[str], None]) -> None: ...
t = threading.Thread(target=darkdetect.listener, args=(print,))
t.daemon = True
t.start() but it does not work, the thread is not printing when the theme is changed. In addition, because this implementation of the listener depends on an external package ( |
@albertosottile Concerning extras, I could, but I will point out that the way I added it to As for the |
One of the key propositions of That being said, I understand there might be cases in which having an extra dependency could be desirable, e.g. for having a listener, but I would confine these to an extra.
This was also my main struggle when I tried to implement this on my own. Perhaps you will benefit from reading the recap of my findings here: #25 (comment) |
This PR now requires python3, which I've taken is ok given: #28 (comment) Right now it uses |
I'll update the PR description with all the changes. |
I am sorry for all the effort you are investing in this, perhaps I should have been clearer with the requirements. The goal of In addition to this general principle, I would especially avoid using In summary, I would rather not merge the PR as it is. I will adapt the text in #25 to clarify the requirements for this function. |
As further clarification, I would like to emphasize that most GUI bindings actually provide native methods to continuously detect theme changes, see e.g. an implementation for Qt here: #14 (comment) (the same is not true for determining dark vs light, hence the need for |
I can adapt this to use subprocess then that waits on and reads stdout; just like how the linux listener works. That should satisfy every requirement listed: def listener(callback):
with subprocess.Popen(
('gsettings', 'monitor', 'org.gnome.desktop.interface', 'gtk-theme'),
stdout=subprocess.PIPE,
universal_newlines=True,
) as p:
for line in p.stdout:
callback('Dark' if '-dark' in line.strip().removeprefix("gtk-theme: '").removesuffix("'").lower() else 'Light') Except, instead of As for QT, that is actually the reason I've found this problem, since that signal was insufficient for determining if the theme changed when used with custom palletes. Unfortunately we've found no native way to truly detect light vs dark mode in QT. |
The latest commit removes multiprocessing and updates the listener to be basically as the above, just like the linux listener. |
96a5a27
to
e37d0ac
Compare
e37d0ac
to
db5f950
Compare
Rebased onto master because of the |
I am fine with this approach, provided that it fulfills all the requirements listed in #25.
True, but Qt can notify you when there is a change, then you can call
I tried your implementation on my MacBook Air, but it does not work, not even in the main thread: nothing is printed when I switch from Dark to Light and vice versa. Did you test this code? |
I did. What code did you use to test this? I can run it myself (test3) zwimer@Lotus ~/D/W/darkdetect> python
Python 3.10.8 (main, Oct 13 2022, 09:48:40) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import threading
>>> import darkdetect
>>>
>>> # def listener(callback: typing.Callable[[str], None]) -> None: ...
>>>
>>> t = threading.Thread(target=darkdetect.listener, args=(print,))
>>> t.daemon = True
>>> t.start()
>>> Dark
Light
Dark
Light
[1] 21169 quit python3 |
Same code as you, but also |
Hmm, if you send over your alternative I shall try integrating it. |
The last commit should address the requested changes. |
Unfortunately for my application, since the pallete is customizeable it can be manually set so this wouldn't work either. This is a failing on QT's part, other frameworks do implement theme detection notifications. That's why I intend to fall back to darkdetect. |
@albertosottile That should use the listener you provided. I've tested it on my end; let me know if it works for you. |
Works perfectly both from the main and from a secondary thread. Thanks! |
The last commit:
|
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.
LGTM, I tested it with my KeyboardInterrupt
script and it worked perfectly.
Thanks for having worked on this. I will make a release with this feature when I have some spare time. |
Implements: #25
UPDATED: This PR:
pip install darkdetect[mac_listener]
; if the extramac_listener
is not installed, the listener raisesNotImpelementedError
0.8.0
as a feature has been added.