Git Product home page Git Product logo

Comments (6)

albertosottile avatar albertosottile commented on May 9, 2024 1

@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.

albertosottile avatar albertosottile commented on May 9, 2024

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.

zwimer avatar zwimer commented on May 9, 2024

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.

zwimer avatar zwimer commented on May 9, 2024

A PR of the above: #27

from darkdetect.

zwimer avatar zwimer commented on May 9, 2024

@albertosottile I implemented a proper listener here: #30

from darkdetect.

albertosottile avatar albertosottile commented on May 9, 2024

Closed in #30, thanks to @zwimer.

from darkdetect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.