albertosottile / darkdetect

Detect OS Dark Mode from Python
Other
171 stars 18 forks source link

Issue: darkdetect listener cannot work under sudo #37

Open LorenEteval opened 10 months ago

LorenEteval commented 10 months ago

Hi. Recently I found that darkdetect listener cannot work under sudo mode. This is how I reproduce:

Environment: macOS 13.5.1 Apple Silicon, Python 3.11

Create a new venv, and install darkdetect by pip install "darkdetect[macos-listener]"

Test sample.py as follows:

import threading
import darkdetect

def printCurrentTheme(theme):
    print(f'System theme is {theme}')

if __name__ == '__main__':
    thread = threading.Thread(
        target=darkdetect.listener, args=(printCurrentTheme,), daemon=True
    )

    thread.start()
    thread.join()

When running with python sample.py, the theme is printed correctly when changing system theme in the settings manually.

However, if running with sudo python sample.py, the callback never called when switching system theme.

There are some cases that I want to launch my application as sudo, and found that darkdetect failed to behave as expected.

Is this a bug related to darkdetect, or related to macos-listener implementation under the hood? Will darkdetect fix this in the future? Thanks.

LorenEteval commented 10 months ago

Just tested on Ubuntu 22.04, same steps except for installing darkdetect using pip install darkdetect. It still fails to print under sudo mode.

LorenEteval commented 9 months ago

darkdetect.theme() also fails under sudo mode, but it seems that on Ubuntu 22.04 the result is caused by gsettings and sudo gsettings giving different output.

I took a quick glance on the implemetation of macOS side, not sure why it would fail because it's just interacting with pyobjc.

Also, because of issue #33 , I have to fall back to combination of darkdetect.theme + timer to achieve the same "listener" feature on macOS (non-sudo, packed application).