moses-palmer / pystray

GNU General Public License v3.0
482 stars 59 forks source link

Taking control of the main loop. #168

Open BMaxV opened 4 months ago

BMaxV commented 4 months ago

Hey there,

I really like the package, it's doing what I want, and behaves as expected.

There is a single thing I would like to have and I would write the PR for it: I want to be in control of the main loop, like this:

import random
import pystray
class MyApp:
    def __init__(self):
        self.pystrayIcon = pystray.icon("foo")

    def whatever(self):
        print("bar")
        r = random.random()
        if r < 0.01:
            return True
        else:
            return False

    def main(self):
        while True:
            r = self.whatever()
            self.pystrayIcon.mainstep() # this would be new
            if r:
                self.pystrayIcon.stop()
                break

if __name__=="__main__":
    myapp_instance = MyApp()
    myapp_instance.main()

From what I can see, all that's required is to not while True: yield the events?

Would you accept such a PR?

moses-palmer commented 1 month ago

Thank you for you suggestion!

I agree that this would be a very helpful addition, and if you managed I would certainly accept such a merge request. I think, however, that it may not be possible to implement for all supported backends, and I am very reluctant to add special functionality for single backends.

BMaxV commented 1 month ago

I agree that the idea is to have a unified solution, so if a specific exception has to be made, that would be bad. Are there tests to find out if a solution would work? Or would it have to be a manual trial and error?

Anyway, now that you've given your ok, I think it's worth the time to investigate. So I will do that.